Giter VIP home page Giter VIP logo

Comments (2)

MoFtZ avatar MoFtZ commented on June 2, 2024

hi @RationalFragile.

You can create your own MemoryBuffer class for this purpose.
ILGPU puts a few extra layers on top of MemoryBuffer, so you will need to create your own ArrayView instances, for example. But the functionality should be available.

using ILGPU;
using ILGPU.Runtime;
using ILGPU.Runtime.OpenCL;
using ILGPU.Util;
using System;

namespace CLHostMemory
{
    class Program
    {
        static void MyKernel(
            Index1D index,
            ArrayView<int> dataView,
            int constant)
        {
            dataView[index] = index + constant;
        }

        static void Main()
        {
            // Create main context
            using var context = Context.CreateDefault();
            using var accelerator = context.CreateCLAccelerator(0);

            var kernel = accelerator.LoadAutoGroupedStreamKernel<Index1D, ArrayView<int>, int>(MyKernel);

            using var buffer = accelerator.AllocateHostMemory<int>(1024);
            var bufferView = new ArrayView<int>(buffer, 0, buffer.Length);
            var bufferView1D = new ArrayView1D<int, Stride1D.Dense>(bufferView, bufferView.Extent, new Stride1D.Dense());

            kernel((int)buffer.Length, bufferView1D, 42);

            var data = bufferView1D.GetAsArray1D();
            for (int i = 0, e = data.Length; i < e; ++i)
            {
                if (data[i] != 42 + i)
                    Console.WriteLine($"Error at element location {i}: {data[i]} found");
            }
        }
    }

    class CLHostMemoryBuffer<T> : MemoryBuffer
        where T : unmanaged
    {
        public CLHostMemoryBuffer(
            CLAccelerator accelerator,
            long length,
            int elementSize)
            : base(accelerator, length, elementSize)
        {
            CLException.ThrowIfFailed(
                CLAPI.CurrentAPI.CreateBuffer(
                    accelerator.NativePtr,
                    CLBufferFlags.CL_MEM_ALLOC_HOST_PTR,
                    new IntPtr(LengthInBytes),
                    IntPtr.Zero,
                    out IntPtr resultPtr));
            NativePtr = resultPtr;
        }

        protected override void DisposeAcceleratorObject(bool disposing)
        {
            if (disposing)
                CLException.ThrowIfFailed(CLAPI.CurrentAPI.ReleaseBuffer(NativePtr));
            NativePtr = IntPtr.Zero;
        }

        protected override void CopyFrom(
            AcceleratorStream stream,
            in ArrayView<byte> sourceView,
            in ArrayView<byte> targetView) =>
            CLMemoryBuffer.CLCopy(stream.AsNotNullCast<CLStream>(), sourceView, targetView);

        protected override void CopyTo(
            AcceleratorStream stream,
            in ArrayView<byte> sourceView,
            in ArrayView<byte> targetView) =>
            CLMemoryBuffer.CLCopy(stream.AsNotNullCast<CLStream>(), sourceView, targetView);

        protected override void MemSet(
            AcceleratorStream stream,
            byte value,
            in ArrayView<byte> targetView) =>
            CLMemoryBuffer.CLMemSet(stream.AsNotNullCast<CLStream>(), value, targetView);
    }

    static class CLHostMemoryExtensions
    {
        public static CLHostMemoryBuffer<T> AllocateHostMemory<T>(this CLAccelerator accelerator, long length)
            where T : unmanaged =>
            new CLHostMemoryBuffer<T>(accelerator, length, Interop.SizeOf<T>());
    }
}

Some semi-related posts include #794 and #826.

from ilgpu.

RationalFragile avatar RationalFragile commented on June 2, 2024

Thank you very much 😊

from ilgpu.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.