public OpenCLRunner(string functionName) { var platform = OpenCLPlatform.Platforms.FirstOrDefault(p => p.Vendor.Contains("NVIDIA")); Context = new OpenCLContext( OpenCLDeviceType.Gpu, new OpenCLContextPropertyList(platform), null, IntPtr.Zero); var device = Context.Devices.First(); CommandQueue = new OpenCLCommandQueue(Context, device, OpenCLCommandQueueProperties.None); var resourceName = $"FractalsWpf.OpenCL.{functionName}.cl"; var program = LoadProgram(resourceName); Kernel = program.CreateKernel(functionName); }
private static void RunKernel(OpenCLPlatform platform, OpenCLDevice device) { var context = new OpenCLContext(new List <OpenCLDevice> { device }, new OpenCLContextPropertyList(platform), null, IntPtr.Zero); var program = LoadProgram(context, device, "ReductionUsingFSCLOpenCLManagedWrapper.reduction.cl"); var kernel1 = program.CreateKernel("reductionVector"); var kernel2 = program.CreateKernel("reductionComplete"); const int numValues = 1024 * 1024; const int numValuesPerWorkItem = 4; var globalWorkSize = numValues / numValuesPerWorkItem; const int localWorkSize = 32; var initialNumWorkGroups = globalWorkSize / localWorkSize; const int value = 42; var data = Enumerable.Repeat(value, numValues).Select(n => (float)n).ToArray(); var commandQueue = new OpenCLCommandQueue(context, device, OpenCLCommandQueueProperties.None); var floatType = typeof(float); var floatSize = sizeof(float); var dataBuffer1 = new OpenCLBuffer(context, OpenCLMemoryFlags.ReadWrite | OpenCLMemoryFlags.AllocateHostPointer, floatType, new long[] { numValues }); var dataBuffer2 = new OpenCLBuffer(context, OpenCLMemoryFlags.ReadWrite | OpenCLMemoryFlags.AllocateHostPointer, floatType, new long[] { initialNumWorkGroups *numValuesPerWorkItem }); var sumBuffer = new OpenCLBuffer(context, OpenCLMemoryFlags.WriteOnly | OpenCLMemoryFlags.AllocateHostPointer, floatType, new long[] { 1 }); var resultDataBuffer = dataBuffer2; using (var pinnedData = new PinnedObject(data)) { commandQueue.WriteToBuffer(pinnedData, dataBuffer1, true, 0L, numValues); } foreach (var index in Enumerable.Range(0, int.MaxValue)) { var dataBufferIn = index % 2 == 0 ? dataBuffer1 : dataBuffer2; var dataBufferOut = index % 2 == 0 ? dataBuffer2 : dataBuffer1; resultDataBuffer = dataBufferOut; kernel1.SetMemoryArgument(0, dataBufferIn); kernel1.SetMemoryArgument(1, dataBufferOut); kernel1.SetLocalArgument(2, localWorkSize * numValuesPerWorkItem * floatSize); Console.WriteLine($"Calling commandQueue.Execute(kernel1) with globalWorkSize: {globalWorkSize}; localWorkSize: {localWorkSize}; num work groups: {globalWorkSize / localWorkSize}"); commandQueue.Execute(kernel1, null, new long[] { globalWorkSize }, new long[] { localWorkSize }); globalWorkSize /= localWorkSize; if (globalWorkSize <= localWorkSize) { break; } } kernel2.SetMemoryArgument(0, resultDataBuffer); kernel2.SetLocalArgument(1, globalWorkSize * numValuesPerWorkItem * floatSize); kernel2.SetMemoryArgument(2, sumBuffer); Console.WriteLine($"Calling commandQueue.Execute(kernel2) with globalWorkSize: {globalWorkSize}; localWorkSize: {globalWorkSize}"); commandQueue.Execute(kernel2, null, new long[] { globalWorkSize }, new long[] { globalWorkSize }); commandQueue.Finish(); var sum = new float[1]; using (var pinnedSum = new PinnedObject(sum)) { commandQueue.ReadFromBuffer(sumBuffer, pinnedSum, true, 0L, 1L); } const int correctAnswer = numValues * value; Console.WriteLine($"OpenCL final answer: {Math.Truncate(sum[0]):N0}; Correct answer: {correctAnswer:N0}"); }
public static extern Error clEnqueueCopyImageToBuffer(OpenCLCommandQueue command_queue, OpenCLMem src_image, OpenCLMem dst_buffer, IntPtr[] src_origin, IntPtr[] region, IntPtr dst_offset, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e);
public static extern Error clEnqueueBarrier(OpenCLCommandQueue command_queue);
public static Error clRetainCommandQueue(OpenCLCommandQueue command_queue) { Console.WriteLine("Calling Error clRetainCommandQueue(OpenCLCommandQueue command_queue)"); return default(Error); }
public static Error clEnqueueWriteImage(OpenCLCommandQueue command_queue, OpenCLMem image, Boolean blocking_write, IntPtr[] origin, IntPtr[] region, IntPtr input_row_pitch, IntPtr input_slice_pitch, IntPtr ptr, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e) { Console.WriteLine("Calling Error clEnqueueWriteImage(OpenCLCommandQueue command_queue, OpenCLMem image, Boolean blocking_write, IntPtr[] origin, IntPtr[] region, IntPtr input_row_pitch, IntPtr input_slice_pitch, IntPtr ptr, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e)"); return default(Error); }
public static Error clEnqueueWaitForEvents(OpenCLCommandQueue command_queue, Int32 num_events, [In] OpenCLEvent[] event_list) { Console.WriteLine("Calling Error clEnqueueWaitForEvents(OpenCLCommandQueue command_queue, Int32 num_events, [In] OpenCLEvent[] event_list)"); return default(Error); }
public static Error clEnqueueTask(OpenCLCommandQueue command_queue, OpenCLKernel kernel, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e) { Console.WriteLine("Calling Error clEnqueueTask(OpenCLCommandQueue command_queue, OpenCLKernel kernel, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e)"); return default(Error); }
public static extern Error clRetainCommandQueue(OpenCLCommandQueue command_queue);
public static extern Error clGetCommandQueueInfo(OpenCLCommandQueue command_queue, CommandQueueInfo param_name, IntPtr param_value_size, [Out] OpenCLDevice[] param_value, IntPtr param_value_size_ret);
public static extern Error clFlush(OpenCLCommandQueue command_queue);
public static extern Error clEnqueueWriteImage(OpenCLCommandQueue command_queue, OpenCLMem image, Boolean blocking_write, IntPtr[] origin, IntPtr[] region, IntPtr input_row_pitch, IntPtr input_slice_pitch, IntPtr ptr, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e);
public static extern Error clEnqueueWriteBuffer(OpenCLCommandQueue command_queue, OpenCLMem buffer, Boolean blocking_write, IntPtr offset, IntPtr cb, IntPtr ptr, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e);
public static extern Error clEnqueueWaitForEvents(OpenCLCommandQueue command_queue, Int32 num_events, [In] OpenCLEvent[] event_list);
public static extern Error clEnqueueUnmapMemObject(OpenCLCommandQueue command_queue, OpenCLMem memobj, IntPtr mapped_ptr, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e);
public static extern Error clEnqueueTask(OpenCLCommandQueue command_queue, OpenCLKernel kernel, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e);
public static Error clEnqueueReadBuffer(OpenCLCommandQueue command_queue, OpenCLMem buffer, Boolean blocking_read, IntPtr offset, IntPtr cb, IntPtr ptr, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, out OpenCLEvent e) { Marshal.WriteInt32(ptr, 0, 1); Marshal.WriteInt32(ptr, 1, 2); Marshal.WriteInt32(ptr, 2, 3); e = default(OpenCLEvent); Console.WriteLine("Calling Error clEnqueueReadBuffer(OpenCLCommandQueue command_queue, OpenCLMem buffer, Boolean blocking_read, IntPtr offset, IntPtr cb, IntPtr ptr, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, out OpenCLEvent e)"); return default(Error); }
public static extern Error clSetCommandQueueProperty(OpenCLCommandQueue command_queue, CommandQueueProperties properties, Boolean enable, CommandQueueProperties old_properties);
public static Error clEnqueueUnmapMemObject(OpenCLCommandQueue command_queue, OpenCLMem memobj, IntPtr mapped_ptr, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e) { Console.WriteLine("Calling Error clEnqueueUnmapMemObject(OpenCLCommandQueue command_queue, OpenCLMem memobj, IntPtr mapped_ptr, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e)"); return default(Error); }
public static Error clEnqueueCopyBuffer(OpenCLCommandQueue command_queue, OpenCLMem src_buffer, OpenCLMem dst_buffer, IntPtr src_offset, IntPtr dst_offset, IntPtr cb, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, out OpenCLEvent e) { e = default(OpenCLEvent); Console.WriteLine("Calling Error clEnqueueCopyBuffer(OpenCLCommandQueue command_queue, OpenCLMem src_buffer, OpenCLMem dst_buffer, IntPtr src_offset, IntPtr dst_offset, IntPtr cb, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, out OpenCLEvent e)"); return default(Error); }
public static Error clEnqueueWriteBuffer(OpenCLCommandQueue command_queue, OpenCLMem buffer, Boolean blocking_write, IntPtr offset, IntPtr cb, IntPtr ptr, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e) { Console.WriteLine("Calling Error clEnqueueWriteBuffer(OpenCLCommandQueue command_queue, OpenCLMem buffer, Boolean blocking_write, IntPtr offset, IntPtr cb, IntPtr ptr, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e)"); return default(Error); }
public static Error clEnqueueCopyImageToBuffer(OpenCLCommandQueue command_queue, OpenCLMem src_image, OpenCLMem dst_buffer, IntPtr[] src_origin, IntPtr[] region, IntPtr dst_offset, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e) { Console.WriteLine("Calling Error clEnqueueCopyImageToBuffer(OpenCLCommandQueue command_queue, OpenCLMem src_image, OpenCLMem dst_buffer, IntPtr[] src_origin, IntPtr[] region, IntPtr dst_offset, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e)"); return default(Error); }
public static Error clGetCommandQueueInfo(OpenCLCommandQueue command_queue, CommandQueueInfo param_name, IntPtr param_value_size, [Out] OpenCLDevice[] param_value, IntPtr param_value_size_ret) { Console.WriteLine("Calling Error clGetCommandQueueInfo(OpenCLCommandQueue command_queue, CommandQueueInfo param_name, IntPtr param_value_size, [Out] OpenCLDevice[] param_value, IntPtr param_value_size_ret)"); return default(Error); }
public static IntPtr clEnqueueMapBuffer(OpenCLCommandQueue command_queue, OpenCLMem buffer, Boolean blocking_map, Map map_flags, IntPtr offset, IntPtr cb, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e, Error error) { Console.WriteLine("Calling IntPtr clEnqueueMapBuffer(OpenCLCommandQueue command_queue, OpenCLMem buffer, Boolean blocking_map, Map map_flags, IntPtr offset, IntPtr cb, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e, Error error)"); return default(IntPtr); }
public static Error clSetCommandQueueProperty(OpenCLCommandQueue command_queue, CommandQueueProperties properties, Boolean enable, CommandQueueProperties old_properties) { Console.WriteLine("Calling Error clSetCommandQueueProperty(OpenCLCommandQueue command_queue, CommandQueueProperties properties, Boolean enable, CommandQueueProperties old_properties)"); return default(Error); }
public static IntPtr clEnqueueMapImage(OpenCLCommandQueue command_queue, OpenCLMem image, Boolean blocking_map, Map map_flags, IntPtr[] origin, IntPtr[] region, IntPtr image_row_pitch, IntPtr image_slice_pitch, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e, Error error) { Console.WriteLine("Calling IntPtr clEnqueueMapImage(OpenCLCommandQueue command_queue, OpenCLMem image, Boolean blocking_map, Map map_flags, IntPtr[] origin, IntPtr[] region, IntPtr image_row_pitch, IntPtr image_slice_pitch, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e, Error error)"); return default(IntPtr); }
public static extern Error clEnqueueCopyBuffer(OpenCLCommandQueue command_queue, OpenCLMem src_buffer, OpenCLMem dst_buffer, IntPtr src_offset, IntPtr dst_offset, IntPtr cb, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e);
public static Error clEnqueueMarker(OpenCLCommandQueue command_queue, OpenCLEvent e) { Console.WriteLine("Calling Error clEnqueueMarker(OpenCLCommandQueue command_queue, OpenCLEvent e)"); return default(Error); }
public static extern IntPtr clEnqueueMapBuffer(OpenCLCommandQueue command_queue, OpenCLMem buffer, Boolean blocking_map, Map map_flags, IntPtr offset, IntPtr cb, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e, Error error);
public static Error clEnqueueNativeKernel(OpenCLCommandQueue command_queue, UserFunction user_func, [In] IntPtr[] args, IntPtr cb_args, Int32 num_mem_objects, [In] OpenCLMem[] mem_list, [In] IntPtr[] args_mem_loc, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e) { Console.WriteLine("Calling Error clEnqueueNativeKernel(OpenCLCommandQueue command_queue, UserFunction user_func, [In] IntPtr[] args, IntPtr cb_args, Int32 num_mem_objects, [In] OpenCLMem[] mem_list, [In] IntPtr[] args_mem_loc, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e)"); return default(Error); }
public static Error clEnqueueNDRangeKernel(OpenCLCommandQueue command_queue, OpenCLKernel kernel, Int32 work_dim, [In] IntPtr[] global_work_offset, [In] IntPtr[] global_work_size, [In] IntPtr[] local_work_size, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e) { Console.WriteLine("Calling Error clEnqueueNDRangeKernel(OpenCLCommandQueue command_queue, OpenCLKernel kernel, Int32 work_dim, [In] IntPtr[] global_work_offset, [In] IntPtr[] global_work_size, [In] IntPtr[] local_work_size, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e)"); return default(Error); }
public static extern Error clEnqueueReadImage(OpenCLCommandQueue command_queue, OpenCLMem image, Boolean blocking_read, IntPtr[] origin, IntPtr[] region, IntPtr row_pitch, IntPtr slice_pitch, IntPtr ptr, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, out OpenCLEvent e);