public void SetKernelArgumentVal(int index, object value) { // Checks if the index is positive, if not, then an exception is thrown if (index < 0) { throw new OpenClArgumentIndexOutOfRangeException( $"The specified index {index} is invalid. The index of the argument must always be greater or equal to 0." ); } // The set kernel argument method needs a pointer to the pointer, therefore the pointer is pinned, so that the garbage collector can not move it in memory GCHandle garbageCollectorHandle = GCHandle.Alloc(value, GCHandleType.Pinned); try { // Sets the kernel argument and checks if it was successful, if not, then an exception is thrown Result result = KernelsNativeApi.SetKernelArgument( Handle, (uint)index, new UIntPtr((uint)Marshal.SizeOf(value)), garbageCollectorHandle.AddrOfPinnedObject() ); if (result != Result.Success) { throw new OpenClException($"The kernel argument with the index {index} could not be set.", result); } } finally { garbageCollectorHandle.Free(); } }
public void SetKernelArg(string kernelName, uint idx, MemoryAllocation mem) { GCHandle garbageCollectorHandle = GCHandle.Alloc(mem.buffer, GCHandleType.Pinned); var errCode = KernelsNativeApi.SetKernelArgument(kernels[kernelName], idx, new UIntPtr((uint)Marshal.SizeOf(mem.buffer)), garbageCollectorHandle.AddrOfPinnedObject()); garbageCollectorHandle.Free(); ThrowOnError(errCode, String.Format("Failed to set arg #{0} for kernel {1}", idx, kernelName)); }