Comments (2)
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.
Thank you very much 😊
from ilgpu.
Related Issues (20)
- XMath.Pow() only work on CPU HOT 3
- Better error messages when kernel program failed to run. HOT 1
- Is it possible to use a stored dataset on GPU again and again with throwing extra data to GPU, and even change the value of the established dataset? HOT 1
- Iteration of value with loops on GPU slows down significantly HOT 5
- Feature request: cudaStreamWaitEvent HOT 7
- Higher precision float (decimal) support? HOT 2
- Passing Int128 as kernel parameter is not working HOT 3
- System.BadImageFormatException in System.Reflection.Metadata.dll HOT 4
- OpenCL.CLException HOT 2
- [QUESTION]: Exception in Accelerator.Synchronize on CUDA HOT 2
- [BUG] Cuda 12 SDK not supported with ILGPU 1.5.X HOT 2
- [POTENTIAL BUG]: CopyToCpu is using refs in unsafe way but there is no indication of that. HOT 3
- [BUG]: Unit tests failing on GitHub runner with MacOS 14 HOT 2
- [QUESTION]: Help/Orientation/Documentation to use ILGPU.Algorithms HOT 3
- [BUG]: `IRContext.Import(Method)` does not correctly duplicate the full IR graph HOT 2
- [BUG]: NET v8.0.5 SDK has broken CI pipeline
- Side by side comparison with CUDA and OpenCL
- XMath.Exp : An internal compiler error has been detected HOT 4
- [BUG]: Variable naming HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ilgpu.