protected override void RunInternal() { ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.Profiling); Console.WriteLine("Original content:"); Random rand = new Random(); int count = 6; long[] bufferContent = new long[count]; for (int i = 0; i < count; i++) { bufferContent[i] = (long)(rand.NextDouble() * long.MaxValue); Console.WriteLine("\t" + bufferContent[i]); } ComputeBuffer<long> buffer = new ComputeBuffer<long>(context, ComputeMemoryFlags.CopyHostPointer, bufferContent); IntPtr mappedPtr = commands.Map(buffer, false, ComputeMemoryMappingFlags.Read, 0, bufferContent.Length, null); commands.Finish(); Console.WriteLine("Mapped content:"); for (int i = 0; i < bufferContent.Length; i++) { IntPtr ptr = new IntPtr(mappedPtr.ToInt64() + i * sizeof(long)); Console.WriteLine("\t" + Marshal.ReadInt64(ptr)); } commands.Unmap(buffer, ref mappedPtr, null); }
public float[] MultiplyMatricesZeroCopy(float[] matrix1, float[] matrix2, int matrix1Height, int matrix1WidthMatrix2Height, int matrix2Width) { if (!_initialized) { Initialize(); _initialized = true; } ComputeBuffer <float> matrix1Buffer = new ComputeBuffer <float>(_context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, matrix1); _kernel.SetMemoryArgument(0, matrix1Buffer); ComputeBuffer <float> matrix2Buffer = new ComputeBuffer <float>(_context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, matrix2); _kernel.SetMemoryArgument(1, matrix2Buffer); float[] ret = new float[matrix1Height * matrix2Width]; ComputeBuffer <float> retBuffer = new ComputeBuffer <float>(_context, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.UseHostPointer, ret); _kernel.SetMemoryArgument(2, retBuffer); _kernel.SetValueArgument <int>(3, matrix1WidthMatrix2Height); _kernel.SetValueArgument <int>(4, matrix2Width); _commandQueue.Execute(_kernel, new long[] { 0 }, new long[] { matrix2Width, matrix1Height }, null, null); IntPtr retPtr = _commandQueue.Map( retBuffer, true, ComputeMemoryMappingFlags.Read, 0, ret.Length, null); _commandQueue.Unmap(retBuffer, ref retPtr, null); //_commandQueue.Finish(); matrix1Buffer.Dispose(); matrix2Buffer.Dispose(); retBuffer.Dispose(); return(ret); }
public async Task Can_multiply_with_device_allocated_memory() { var platform = ComputePlatform.Platforms.First(); var device = platform.Devices.First(); var context = new ComputeContext(new[] { device }, new ComputeContextPropertyList(platform), null, IntPtr.Zero); using var program = new ComputeProgram(context, Sum_kernel); program.Build(new[] { device }, "-cl-std=CL1.2", (handle, ptr) => OnProgramBuilt(program, device), IntPtr.Zero); using var queue = new ComputeCommandQueue(context, device, ComputeCommandQueueFlags.None); using var kernel = program.CreateKernel("sum"); var source = new byte[] { 1, 2, 3, 4, 5 }; using var sourceBuffer = new ComputeBuffer <byte>( context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, source); using var targetBuffer = new ComputeBuffer <byte>( context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.AllocateHostPointer, 5); kernel.SetMemoryArgument(0, sourceBuffer); kernel.SetMemoryArgument(1, targetBuffer); var events = new List <ComputeEventBase>(); queue.Execute(kernel, null, new long[] { source.Length }, null, events); var targetPtr = queue.Map(targetBuffer, true, ComputeMemoryMappingFlags.Read, 0, source.Length, events); //await events.WaitForEvents(); unsafe { var result = (byte *)targetPtr.ToPointer(); for (var i = 0; i < source.Length; i++) { Console.WriteLine($"{source[i]} * 2 = {result[i]}"); } } queue.Unmap(targetBuffer, ref targetPtr, events); //await events.WaitForEvents(); }
public void Run(ComputeContext context, TextWriter log) { try { ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); log.WriteLine("Original content:"); Random rand = new Random(); int count = 6; long[] bufferContent = new long[count]; for (int i = 0; i < count; i++) { bufferContent[i] = (long)(rand.NextDouble() * long.MaxValue); log.WriteLine("\t" + bufferContent[i]); } ComputeBuffer <long> buffer = new ComputeBuffer <long>(context, ComputeMemoryFlags.CopyHostPointer, bufferContent); IntPtr mappedPtr = commands.Map(buffer, true, ComputeMemoryMappingFlags.Read, 0, bufferContent.Length, null); log.WriteLine("Mapped content:"); for (int i = 0; i < bufferContent.Length; i++) { IntPtr ptr = new IntPtr(mappedPtr.ToInt64() + i * sizeof(long)); log.WriteLine("\t" + Marshal.ReadInt64(ptr)); } commands.Unmap(buffer, ref mappedPtr, null); // wait for the unmap to happen commands.Finish(); // cleanup buffer buffer.Dispose(); // cleanup commands commands.Dispose(); } catch (Exception e) { log.WriteLine(e.ToString()); } }
public void Run(ComputeContext context, TextWriter log) { try { ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); log.WriteLine("Original content:"); Random rand = new Random(); int count = 6; long[] bufferContent = new long[count]; for (int i = 0; i < count; i++) { bufferContent[i] = (long)(rand.NextDouble() * long.MaxValue); log.WriteLine("\t" + bufferContent[i]); } ComputeBuffer<long> buffer = new ComputeBuffer<long>(context, ComputeMemoryFlags.CopyHostPointer, bufferContent); IntPtr mappedPtr = commands.Map(buffer, true, ComputeMemoryMappingFlags.Read, 0, bufferContent.Length, null); log.WriteLine("Mapped content:"); for (int i = 0; i < bufferContent.Length; i++) { IntPtr ptr = new IntPtr(mappedPtr.ToInt64() + i * sizeof(long)); log.WriteLine("\t" + Marshal.ReadInt64(ptr)); } commands.Unmap(buffer, ref mappedPtr, null); // wait for the unmap to happen commands.Finish(); // cleanup buffer buffer.Dispose(); // cleanup commands commands.Dispose(); } catch (Exception e) { log.WriteLine(e.ToString()); } }
public static void Run(TextWriter log, ComputeContext context) { StartTest(log, "Dummy test"); try { ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); log.WriteLine("Original content:"); Random rand = new Random(); int count = 6; long[] bufferContent = new long[count]; for (int i = 0; i < count; i++) { bufferContent[i] = (long)(rand.NextDouble() * long.MaxValue); log.WriteLine("\t" + bufferContent[i]); } ComputeBuffer<long> buffer = new ComputeBuffer<long>(context, ComputeMemoryFlags.CopyHostPointer, bufferContent); IntPtr mappedPtr = commands.Map(buffer, false, ComputeMemoryMappingFlags.Read, 0, bufferContent.Length, null); commands.Finish(); log.WriteLine("Mapped content:"); for (int i = 0; i < bufferContent.Length; i++) { IntPtr ptr = new IntPtr(mappedPtr.ToInt64() + i * sizeof(long)); log.WriteLine("\t" + Marshal.ReadInt64(ptr)); } commands.Unmap(buffer, ref mappedPtr, null); } catch (Exception e) { log.WriteLine(e.ToString()); } EndTest(log, "Dummy test"); }