Пример #1
0
        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);
        }
Пример #2
0
        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}");
        }
Пример #3
0
 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);
Пример #4
0
 public static extern Error clEnqueueBarrier(OpenCLCommandQueue command_queue);
Пример #5
0
 public static Error clRetainCommandQueue(OpenCLCommandQueue command_queue)
 {
     Console.WriteLine("Calling Error clRetainCommandQueue(OpenCLCommandQueue command_queue)");
     return default(Error);
 }
Пример #6
0
 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);
 }
Пример #7
0
 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);
 }
Пример #8
0
 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);
 }
Пример #9
0
 public static extern Error clRetainCommandQueue(OpenCLCommandQueue command_queue);
Пример #10
0
 public static extern Error clGetCommandQueueInfo(OpenCLCommandQueue command_queue, CommandQueueInfo param_name, IntPtr param_value_size, [Out] OpenCLDevice[] param_value, IntPtr param_value_size_ret);
Пример #11
0
 public static extern Error clFlush(OpenCLCommandQueue command_queue);
Пример #12
0
 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);
Пример #13
0
 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);
Пример #14
0
 public static extern Error clEnqueueWaitForEvents(OpenCLCommandQueue command_queue, Int32 num_events, [In] OpenCLEvent[] event_list);
Пример #15
0
 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);
Пример #16
0
 public static extern Error clEnqueueTask(OpenCLCommandQueue command_queue, OpenCLKernel kernel, Int32 num_events_in_wait_list, [In] OpenCLEvent[] event_wait_list, OpenCLEvent e);
Пример #17
0
        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);
        }
Пример #18
0
 public static extern Error clSetCommandQueueProperty(OpenCLCommandQueue command_queue, CommandQueueProperties properties, Boolean enable, CommandQueueProperties old_properties);
Пример #19
0
 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);
 }
Пример #20
0
 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);
 }
Пример #21
0
 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);
 }
Пример #22
0
 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);
 }
Пример #23
0
 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);
 }
Пример #24
0
 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);
 }
Пример #25
0
 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);
 }
Пример #26
0
 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);
 }
Пример #27
0
 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);
Пример #28
0
 public static Error clEnqueueMarker(OpenCLCommandQueue command_queue, OpenCLEvent e)
 {
     Console.WriteLine("Calling Error clEnqueueMarker(OpenCLCommandQueue command_queue, OpenCLEvent e)");
     return default(Error);
 }
Пример #29
0
 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);
Пример #30
0
 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);
 }
Пример #31
0
 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);
 }
Пример #32
0
 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);