internal OpenCLQueue(OpenCLContext context, ComputeCommandQueue computeCommandQueue) { Contract.Requires(context != null); Contract.Requires(computeCommandQueue != null); Context = context; ComputeCommandQueue = computeCommandQueue; }
protected OpenCLMemory(OpenCLContext context, ComputeMemory computeMemory) { Contract.Requires(context != null); Contract.Requires(computeMemory != null); Context = context; ComputeMemory = computeMemory; }
internal OpenCLProgram(OpenCLContext context, ComputeProgram program) { Contract.Requires(context != null); Contract.Requires(program != null); Context = context; ComputeProgram = program; }
public MagnetSimulation(OpenCLContext context, Bitmap Template) { sourceTemplate = Template.Clone(new Rectangle(0, 0, Template.Width, Template.Height), PixelFormat.Format32bppArgb); oclc = context; randoCal = new Random(); setupCanvas(); generateRandowPositions(1000000); blackKernel = oclc.program.CreateKernel("black"); drawKernel = oclc.program.CreateKernel("draw"); magKernel = oclc.program.CreateKernel("magnitate"); loadDataOntoCard(); }
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 OpenCLProgram LoadProgram(OpenCLContext context, OpenCLDevice device, string resourceName) { var source = GetProgramSourceFromResource(Assembly.GetExecutingAssembly(), resourceName); var program = new OpenCLProgram(context, source); try { program.Build(new List <OpenCLDevice> { device }, string.Empty, null, IntPtr.Zero); } catch (BuildProgramFailureOpenCLException) { var buildLog = program.GetBuildLog(device); throw new ApplicationException($"Error building program \"{resourceName}\":{Environment.NewLine}{buildLog}"); } return(program); }
public Program(MainWindow mw) { mainWindow = mw; mainWindow.WindowState = WindowState.Maximized; testbm = new Bitmap(1843, 1060); Graphics g = Graphics.FromImage(testbm); g.FillRectangle(Brushes.Black, new Rectangle(0, 0, testbm.Width, testbm.Height)); BitmapSource testabm = new AliasedBitmapSource(testbm); back = new WriteableBitmap(testabm); test = new WriteableBitmap(testabm); openCLContext = new OpenCLContext(); Thread T = new Thread(() => runSimulation()) { IsBackground = true }; T.Start(); }
private void CreateOpenCLContext(OpenCLNNInitParameters oclInitPars) { ComputePlatform platform = null; if (String.IsNullOrEmpty(oclInitPars.PlatformName)) { platform = OpenCLContext.DefaultPlatform; } else { platform = OpenCLContext.Platforms.Where(p => string.Compare(oclInitPars.PlatformName, p.Name, true) == 0).FirstOrDefault(); } if (platform == null) throw new OpenCLException("Platform '" + oclInitPars.PlatformName + "' not found."); oclContext = new OpenCLContext(ComputeDeviceTypes.All, platform); try { if (string.IsNullOrEmpty(oclInitPars.DeviceName)) { oclDevice = oclContext.DefaultDevice; //oclDevice = oclContext.Devices.Where(d => d.Type == ComputeDeviceTypes.Cpu).First(); } else { oclDevice = oclContext.Devices.Where(d => string.Compare(oclInitPars.DeviceName, d.Name, true) == 0).FirstOrDefault(); } if (oclDevice == null) throw new OpenCLException("Device '" + oclInitPars.DeviceName + "' not found."); oclQueue = oclContext.CreateQueue(oclDevice, ComputeCommandQueueFlags.OutOfOrderExecution); } catch { oclContext.Dispose(); oclContext = null; throw; } }
public static extern OpenCLProgram clCreateProgramWithSource(OpenCLContext context, Int32 count, String Strings, [In] IntPtr[] lengths, Error error);
public static extern OpenCLProgram clCreateProgramWithSource(OpenCLContext context, Int32 count, [In] Byte[][] sources, [In] IntPtr[] lengths, out Error error);
public static OpenCLMem clCreateBuffer(OpenCLContext context, MemFlags flags, IntPtr size, [In] Byte[] bytes, out Error error) { error = default(Error); Console.WriteLine("Calling OpenCLMem clCreateBuffer(OpenCLContext context, MemFlags flags, IntPtr size, [In] Byte[] bytes, out Error error)"); return default(OpenCLMem); }
public static extern Error clReleaseContext(OpenCLContext context);
private void DisposeUnmanagedResources() { oclValueBuffer.Dispose(); oclValueBuffer = null; oclQueue.Dispose(); oclQueue = null; oclContext.Dispose(); oclContext = null; }
public static extern Error clRetainContext(OpenCLContext context);
public static OpenCLProgram clCreateProgramWithSource(OpenCLContext context, Int32 count, [In] Byte[][] sources, [In] IntPtr[] lengths, out Error error) { error = default(Error); Console.WriteLine("Calling OpenCLProgram clCreateProgramWithSource(OpenCLContext context, Int32 count, [In] Byte[][] sources, [In] IntPtr[] lengths, out Error error)"); return default(OpenCLProgram); }
public static extern OpenCLMem clCreateBuffer(OpenCLContext context, MemFlags flags, IntPtr size, [In] Byte[] bytes, out Error error);
public static Error clRetainContext(OpenCLContext context) { Console.WriteLine("Calling Error clRetainContext(OpenCLContext context)"); return default(Error); }
public static Error clGetSupportedImageFormats(OpenCLContext context, MemFlags flags, MemObjectType image_type, Int32 num_entries, [Out] ImageFormat[] image_formats, Int32 num_image_formats) { Console.WriteLine("Calling Error clGetSupportedImageFormats(OpenCLContext context, MemFlags flags, MemObjectType image_type, Int32 num_entries, [Out] ImageFormat[] image_formats, Int32 num_image_formats)"); return default(Error); }
public static OpenCLCommandQueue clCreateCommandQueue(OpenCLContext context, OpenCLDevice device, CommandQueueProperties properties, Error error) { Console.WriteLine("Calling OpenCLCommandQueue clCreateCommandQueue(OpenCLContext context, OpenCLDevice device, CommandQueueProperties properties, Error error)"); return default(OpenCLCommandQueue); }
public static Error clGetContextInfo(OpenCLContext context, ContextInfo param_name, IntPtr param_value_size, [Out] OpenCLDevice[] param_value, IntPtr param_value_size_ret) { Console.WriteLine("Calling Error clGetContextInfo(OpenCLContext context, ContextInfo param_name, IntPtr param_value_size, [Out] OpenCLDevice[] param_value, IntPtr param_value_size_ret)"); return default(Error); }
public static Error clGetContextInfo(OpenCLContext context, ContextInfo param_name, IntPtr param_value_size, IntPtr param_value, out IntPtr param_value_size_ret) { param_value_size_ret = default(IntPtr); Console.WriteLine("Calling Error clGetContextInfo(OpenCLContext context, ContextInfo param_name, IntPtr param_value_size, IntPtr param_value, out IntPtr param_value_size_ret)"); return default(Error); }
public static OpenCLMem clCreateBuffer(OpenCLContext context, MemFlags flags, IntPtr size, IntPtr host_ptr, Error error) { Console.WriteLine("Calling OpenCLMem clCreateBuffer(OpenCLContext context, MemFlags flags, IntPtr size, IntPtr host_ptr, Error error)"); return default(OpenCLMem); }
public static extern OpenCLSampler clCreateSampler(OpenCLContext context, Boolean normalized_coords, AddressingMode addressing_mode, FilterMode filter_mode, Error error);
private static void ComputeOpenCL(float[] inputs, float[] weights, float[] outputs, float alpha) { string program = File.ReadAllText("code.cl"); using (var ctx = new OpenCLContext()) using (var prog = ctx.CompileProgram(program)) using (var kernel = prog.CreateKernel("ActivationNeuron")) using (var bInputs = ctx.CreateBuffer(inputs, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer)) using (var bWeights = ctx.CreateBuffer(weights, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer)) using (var bAlpha = ctx.CreateBuffer(new[] { alpha }, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer)) using (var bInputCount = ctx.CreateBuffer(new[] { inputs.Length }, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer)) using (var bOutputs = ctx.CreateBuffer(outputs, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer)) using (var queue = ctx.CreateQueue()) { kernel.SetArguments(bInputs, bWeights, bOutputs, bInputCount, bAlpha); queue.Execute(kernel, new long[] { outputs.Length }); queue.ReadAll(bOutputs, outputs); } }
public static OpenCLProgram clCreateProgramWithSource(OpenCLContext context, Int32 count, String Strings, [In] IntPtr[] lengths, Error error) { Console.WriteLine("Calling OpenCLProgram clCreateProgramWithSource(OpenCLContext context, Int32 count, String Strings, [In] IntPtr[] lengths, Error error)"); return default(OpenCLProgram); }
public static OpenCLProgram clCreateProgramWithBinary(OpenCLContext context, Int32 num_devices, [In] OpenCLDevice[] device_list, [In] IntPtr[] lengths, [In] IntPtr[] binaries, [In] Int32[] binary_status, Error error) { Console.WriteLine("Calling OpenCLProgram clCreateProgramWithBinary(OpenCLContext context, Int32 num_devices, [In] OpenCLDevice[] device_list, [In] IntPtr[] lengths, [In] IntPtr[] binaries, [In] Int32[] binary_status, Error error)"); return default(OpenCLProgram); }
public static extern Error clGetSupportedImageFormats(OpenCLContext context, MemFlags flags, MemObjectType image_type, Int32 num_entries, [Out] ImageFormat[] image_formats, Int32 num_image_formats);
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 OpenCLMem clCreateImage3D(OpenCLContext context, MemFlags flags, ref ImageFormat image_format, IntPtr image_width, IntPtr image_height, IntPtr image_depth, IntPtr image_row_pitch, IntPtr image_slice_pitch, IntPtr host_ptr, Error error);
public static OpenCLMem clCreateImage2D(OpenCLContext context, MemFlags flags, ref ImageFormat image_format, IntPtr image_width, IntPtr image_height, IntPtr image_row_pitch, IntPtr host_ptr, Error error) { Console.WriteLine("Calling OpenCLMem clCreateImage2D(OpenCLContext context, MemFlags flags, ref ImageFormat image_format, IntPtr image_width, IntPtr image_height, IntPtr image_row_pitch, IntPtr host_ptr, Error error)"); return default(OpenCLMem); }
public static extern OpenCLProgram clCreateProgramWithBinary(OpenCLContext context, Int32 num_devices, [In] OpenCLDevice[] device_list, [In] IntPtr[] lengths, [In] IntPtr[] binaries, [In] Int32[] binary_status, Error error);
public static extern OpenCLMem clCreateBuffer(OpenCLContext context, MemFlags flags, IntPtr size, IntPtr host_ptr, Error error);
public static OpenCLSampler clCreateSampler(OpenCLContext context, Boolean normalized_coords, AddressingMode addressing_mode, FilterMode filter_mode, Error error) { Console.WriteLine("Calling OpenCLSampler clCreateSampler(OpenCLContext context, Boolean normalized_coords, AddressingMode addressing_mode, FilterMode filter_mode, Error error)"); return default(OpenCLSampler); }
public static extern OpenCLCommandQueue clCreateCommandQueue(OpenCLContext context, OpenCLDevice device, CommandQueueProperties properties, Error error);
public static extern Error clGetContextInfo(OpenCLContext context, ContextInfo param_name, IntPtr param_value_size, [Out] OpenCLDevice[] param_value, IntPtr param_value_size_ret);