//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Creates the kernel. </summary> /// /// <seealso cref="M:KelpNet.Common.Functions.IParallelizable.CreateKernel()"/> //////////////////////////////////////////////////////////////////////////////////////////////////// public void CreateKernel() { if (GpuEnable) { string kernelSource = ActivateFunctionString + ActivateKernelString; ComputeProgram program = Weaver.CreateProgram(kernelSource); ForwardKernel = program?.CreateKernel(ForwardKernelName); BackwardKernel = program?.CreateKernel(BackwardKernelName); } }
public BlockedMatMulThread(ComputePlatform platform, ComputeDevice device, int inN, float[] inA, float[] inB) { ComputeDevice[] devices = { device }; context = new ComputeContext(devices, new ComputeContextPropertyList(platform), null, IntPtr.Zero); program = new ComputeProgram(context, clProgramSource); try { program.Build(null, null, null, IntPtr.Zero); } catch (BuildProgramFailureComputeException e) { string buildLog = program.GetBuildLog(device); Console.WriteLine(buildLog); throw; } kernel = program.CreateKernel("mmul"); queue = new ComputeCommandQueue(context, device, ComputeCommandQueueFlags.None); n = inN; a = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, inA); b = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, inB); int resultCount = (int)(device.MaxMemoryAllocationSize / n / n / sizeof(float)); results = new ComputeBuffer <float> [resultCount]; for (int i = 0; i < results.Length; ++i) { results[i] = new ComputeBuffer <float>(context, ComputeMemoryFlags.WriteOnly, n * n); } HaltRequested = false; operationCounter = 0; completionCounter = 0; startTime = 0; }
ComputeKernel CreateKernel(string Method, object[] args) { if (args == null) { throw new ArgumentException("You have to pass an argument to a kernel"); } ComputeKernel kernel; if (LastMethod == Method && LastKernel != null) //Kernel caching, do not compile twice { kernel = LastKernel; } else { kernel = program.CreateKernel(Method); LastKernel = kernel; } LastMethod = Method; for (int i = 0; i < args.Length; i++) { Setargument(kernel, i, args[i]); } return(kernel); }
/// <summary> /// 栅格化 /// </summary> /// <param name="pointlist"></param> /// <param name="W"></param> /// <param name="H"></param> /// <param name="T"></param> /// <param name="program"></param> /// <returns></returns> public int[] Insert(int[] test1, int[] test2, ComputeProgram program) { int c1 = test1.GetLength(0); int c2 = test2.GetLength(0); ComputeBuffer <int> buffer1 = new ComputeBuffer <int>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, test1); ComputeBuffer <int> buffer2 = new ComputeBuffer <int>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, test2); ComputeBuffer <int> result = new ComputeBuffer <int>(context, ComputeMemoryFlags.ReadWrite, c1); ComputeKernel kernel = program.CreateKernel("Insert"); kernel.SetMemoryArgument(0, buffer1); kernel.SetMemoryArgument(1, buffer2); kernel.SetMemoryArgument(2, result); commands.Execute(kernel, null, new long[] { c1 }, null, events); int[] resultnum = new int[c1]; GCHandle arrCHandle = GCHandle.Alloc(resultnum, GCHandleType.Pinned); commands.Read(result, true, 0, c1, arrCHandle.AddrOfPinnedObject(), events); arrCHandle.Free(); kernel.Dispose(); buffer1.Dispose(); buffer2.Dispose(); result.Dispose(); return(resultnum); }
public TFP Hello(float num) { ComputeBuffer <TFP> result = new ComputeBuffer <TFP>(context, ComputeMemoryFlags.WriteOnly, 1); string source = Encoding.ASCII.GetString(FZYK.Nest.Properties.Resources.nest); if (fpType == FPType.FP64AMD) { source = "#define AMDFP64\n" + source; } else if (fpType == FPType.FP64) { source = "#define FP64\n" + source; } ComputeProgram program = new ComputeProgram(context, source); try { program.Build(null, null, null, IntPtr.Zero); } catch (Exception) { var log = program.GetBuildLog(context.Devices[0]); Debugger.Break(); } ComputeKernel kernel = program.CreateKernel("hello"); TFP[] myresult = RunKernalTest(num, result, kernel); return(myresult[0]); }
public override void Init(WaveFormat AFormat) { base.Init(AFormat); try { FDevice = App.Settings.Preferences.OpenCL.GetComputeDevice(); FContext = new ComputeContext(new ComputeDevice[] { FDevice }, new ComputeContextPropertyList(FDevice.Platform), null, IntPtr.Zero); program = new ComputeProgram(FContext, FProgramSource); program.Build(new[] { FDevice }, null, null, IntPtr.Zero); kernel = program.CreateKernel("Wave"); commands = new ComputeCommandQueue(FContext, FContext.Devices[0], ComputeCommandQueueFlags.None); } catch (BuildProgramFailureComputeException bex) { Debug.WriteLine(bex.Message); Debug.WriteLine(program.GetBuildLog(FDevice)); throw; } catch (Exception ex) { Debug.WriteLine(ex.Message); throw; } }
public string vectorSum() { string vecSum = @" __kernel void vectorSum(__global float *v1, __global float *v2, __global float *v3) { int i = get_global_id(0); v3[i] = v1[i] + v2[i]; } "; int size = 100000; float[] v1_ = new float[size]; float[] v2_ = new float[size]; float[] v3_ = new float[size]; for (var i = 0; i < size; i++) { v1_[i] = (float)i; v2_[i] = (float).5f; } var platform_ = ComputePlatform.Platforms[0]; ComputeContextPropertyList properties = new ComputeContextPropertyList(platform_); ComputeContext ctx = new ComputeContext(ComputeDeviceTypes.Gpu, properties, null, IntPtr.Zero); ComputeCommandQueue commands = new ComputeCommandQueue(ctx, ctx.Devices[0], ComputeCommandQueueFlags.None); ComputeProgram program = new ComputeProgram(ctx, vecSum); try { program.Build(null, null, null, IntPtr.Zero); Console.WriteLine("program build completed"); } catch { string log = program.GetBuildLog(ctx.Devices[0]); } ComputeBuffer <float> v1, v2, v3; v1 = new ComputeBuffer <float>(ctx, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, v1_); v2 = new ComputeBuffer <float>(ctx, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, v2_); v3 = new ComputeBuffer <float>(ctx, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.CopyHostPointer, v3_); long[] worker = { size }; commands.WriteToBuffer(v1_, v1, false, null); commands.WriteToBuffer(v2_, v2, false, null); ComputeKernel sumKernal = program.CreateKernel("vectorSum"); Console.WriteLine("kernal created"); sumKernal.SetMemoryArgument(0, v1); sumKernal.SetMemoryArgument(1, v2); sumKernal.SetMemoryArgument(2, v3); commands.Execute(sumKernal, null, worker, null, null); Console.WriteLine("Executed"); commands.ReadFromBuffer <float>(v3, ref v3_, false, null); StringBuilder sb = new StringBuilder(); for (int i = 0; i < size; i++) { sb.AppendFormat("{0} + {1} = {2}<br>", v1_[i].ToString(), v2_[i].ToString(), v3_[i].ToString()); } var sum_expression_result = sb.ToString(); return(sum_expression_result); }
private void Initialize() { // get the intel integrated GPU _integratedIntelGPUPlatform = ComputePlatform.Platforms.Where(n => n.Name.Contains("Intel")).First(); // create the compute context. _context = new ComputeContext( ComputeDeviceTypes.Gpu, // use the gpu new ComputeContextPropertyList(_integratedIntelGPUPlatform), // use the intel openCL platform null, IntPtr.Zero); // the command queue is the, well, queue of commands sent to the "device" (GPU) _commandQueue = new ComputeCommandQueue( _context, // the compute context _context.Devices[0], // first device matching the context specifications ComputeCommandQueueFlags.None); // no special flags string kernelSource = null; using (StreamReader sr = new StreamReader("kernel.cl")) { kernelSource = sr.ReadToEnd(); } // create the "program" _program = new ComputeProgram(_context, new string[] { kernelSource }); // compile. _program.Build(null, null, null, IntPtr.Zero); _kernel = _program.CreateKernel("ComputeMatrix"); }
public void SetDevice(int deviceIndx) { if ((deviceIndx < 0) || (deviceIndx >= oclDevices.Count)) { throw new IndexOutOfRangeException("Invalid OpenCL device index."); } if (oclContext != null) { oclContext.Dispose(); oclContext = null; } if (oclCommandQueue != null) { oclCommandQueue.Dispose(); oclCommandQueue = null; } if (oclKernel != null) { oclKernel.Dispose(); oclKernel = null; } ComputeProgram oclProgram = null; try { oclContext = new ComputeContext(new ComputeDevice[] { oclDevices[deviceIndx] }, new ComputeContextPropertyList(oclDevices[deviceIndx].Platform), null, IntPtr.Zero); oclCommandQueue = new ComputeCommandQueue(oclContext, oclDevices[deviceIndx], ComputeCommandQueueFlags.None); oclProgram = new ComputeProgram(oclContext, Encoding.Default.GetString(Properties.Resources.Test)); oclProgram.Build(new ComputeDevice[] { oclDevices[deviceIndx] }, "", null, IntPtr.Zero); oclKernel = oclProgram.CreateKernel("Test"); } catch (BuildProgramFailureComputeException ex) { string buildLog = oclProgram.GetBuildLog(oclDevices[deviceIndx]); throw new Exception(buildLog, ex); } catch (Exception) { throw; } finally { if (oclProgram != null) { oclProgram.Dispose(); oclProgram = null; } } }
// initialiseer OpenCL static void InitCL() { // kies platform 0 (op sommige machines moet dit 1 of 2 zijn) var platform = ComputePlatform.Platforms[CLPlatform]; Console.Write( "initializing OpenCL... " + platform.Name + " (" + platform.Profile + ").\n" ); Console.Write( platform.Devices.First().Name + " (" + platform.Devices.First().Type + ")\n"); Console.Write( (platform.Devices.First().GlobalMemorySize / 1024 / 1024) ); Console.WriteLine( " MiB global memory / " + (platform.Devices.First().LocalMemorySize / 1024) + " KiB local memory"); // maak een compute context context = new ComputeContext( ComputeDeviceTypes.Gpu, new ComputeContextPropertyList( platform ), null, IntPtr.Zero ); // laad opencl programma var streamReader = new StreamReader( "../../program.cl" ); string clSource = streamReader.ReadToEnd(); streamReader.Close(); // compileer opencl source code program = new ComputeProgram( context, clSource ); try { program.Build( null, null, null, IntPtr.Zero ); } catch { // fout in OpenCL code; check console window voor details. Console.Write( "error in kernel code:\n" ); Console.Write( program.GetBuildLog( context.Devices[0] ) + "\n" ); } // maak een commandorij queue = new ComputeCommandQueue( context, context.Devices[0], 0 ); // lokaliseer de gewenste kernel in het programma kernel = program.CreateKernel( "device_function" ); // alloceer data in RAM }
/// <summary> /// 栅格化范围数组 /// </summary> /// <param name="pm"></param> /// <param name="T"></param> /// <param name="program"></param> /// <returns></returns> public int[,] GetGridArray(PlateModel pm, float T, ComputeProgram program) { List <PointF> pointlist = pm.OutModel.ExpandPoint; float W = pm.Rect.Width; float H = pm.Rect.Height; int WI = Convert.ToInt32(Math.Ceiling(W / T)); int HI = Convert.ToInt32(Math.Ceiling(H / T)); int pc = pointlist.Count; MyPoint[] plist = new MyPoint[pc]; for (int i = 0; i < pointlist.Count; i++) { plist[i].X = pointlist[i].X; plist[i].Y = pointlist[i].Y; } ComputeBuffer <MyPoint> pbuffer = new ComputeBuffer <MyPoint>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, plist); int[,] myresult = new int[HI, WI]; ComputeBuffer <int> outdata = new ComputeBuffer <int>(context, ComputeMemoryFlags.WriteOnly, WI * HI); ComputeKernel kernelArray = program.CreateKernel("InitArray"); kernelArray.SetMemoryArgument(0, outdata); commands.Execute(kernelArray, null, new long[] { WI *HI }, null, events); ComputeKernel kernel = program.CreateKernel("GetGridValue"); kernel.SetMemoryArgument(0, pbuffer); kernel.SetValueArgument(1, pc); kernel.SetValueArgument(2, W); kernel.SetValueArgument(3, H); kernel.SetValueArgument(4, T); kernel.SetValueArgument(5, WI); kernel.SetValueArgument(6, HI); kernel.SetMemoryArgument(7, outdata); commands.Execute(kernel, null, new long[] { WI + HI }, null, events); GCHandle arrCHandle = GCHandle.Alloc(myresult, GCHandleType.Pinned); commands.Read(outdata, true, 0, WI * HI, arrCHandle.AddrOfPinnedObject(), events); arrCHandle.Free(); kernel.Dispose(); pbuffer.Dispose(); outdata.Dispose(); return(myresult); }
public FindPointOpenCl() { cpl = new ComputeContextPropertyList(ComputePlatform.Platforms[0]); context = new ComputeContext(ComputeDeviceTypes.Gpu, cpl, null, IntPtr.Zero); program = new ComputeProgram(context, new string[] { FindPointCalculationGpu }); program.Build(null, null, null, IntPtr.Zero); kernel = program.CreateKernel("FindPointCalculationGpu"); }
public bool SetParallel(bool enable) { KernelSource = OpenCL.GetKernelSource(Resources.Dropout); this.IsParallel = enable & OpenCL.Enable; if (IsParallel) { ComputeProgram program = OpenCL.CreateProgram <T>(this.KernelSource); ForwardKernel = program.CreateKernel("DropoutForward"); BackwardKernel = program.CreateKernel("DropoutBackward"); } this.InitFunc(new StreamingContext()); return(IsParallel); }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Creates the kernel. </summary> /// /// <seealso cref="M:KelpNet.Common.Functions.IParallelizable.CreateKernel()"/> //////////////////////////////////////////////////////////////////////////////////////////////////// public void CreateKernel() { if (GpuEnable) { string kernelSource = KernelString; if (Activator != null) { kernelSource = Activator.ActivateFunctionString + KernelString; } ComputeProgram program = Weaver.CreateProgram(kernelSource); ForwardKernel = program?.CreateKernel(ForwardKernelName); BackwardgWKernel = program?.CreateKernel(BackwardgWKernelName); BackwardgXKernel = program?.CreateKernel(BackwardgXKernelName); } }
public void CreateKernel() { if (this.GpuEnable) { string kernelSource = this.KernelString; if (this.Activator != null) { kernelSource = this.Activator.ActivateFunctionString + this.KernelString; } ComputeProgram program = Weaver.CreateProgram(kernelSource); this.ForwardKernel = program.CreateKernel(this.ForwardKernelName); this.BackwardgWKernel = program.CreateKernel(this.BackwardgWKernelName); this.BackwardgXKernel = program.CreateKernel(this.BackwardgXKernelName); } }
public static Tuple <List <List <int> >, TimeSpan> MultiplyParallel(List <List <int> > matrixOne, List <List <int> > matrixTwo) { if (!isRegularMatrix(matrixOne) || !isRegularMatrix(matrixTwo)) { throw new ArgumentException("Non regular matrix detected. Rows size mismatch detected."); } if (matrixOne[0].Count != matrixTwo.Count) { throw new ArgumentException("Matrixes is not compatible. Columns count of first matrix is not equal to rows count of second matrix."); } List <List <int> > result = new List <List <int> >(); ComputePlatform platform = GetGPU(); if (platform is null) { throw new PlatformNotSupportedException("Platform doesn't have a dedicated GPU. Run is impossible."); } ComputeContext context = new ComputeContext(ComputeDeviceTypes.Gpu, new ComputeContextPropertyList(platform), null, IntPtr.Zero); ComputeCommandQueue queue = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); ComputeProgram program = new ComputeProgram(context, CalculateKernel); program.Build(null, null, null, IntPtr.Zero); ComputeKernel kernel = program.CreateKernel("Multiply"); List <ComputeBuffer <int> > rowsMatrixOne = matrixOne.TransformMatrixToComputerBuffersOfRows(context); List <ComputeBuffer <int> > columnsMatrixTwo = matrixTwo.TransformMatrixToComputerBuffersOfColumns(context); List <ComputeBuffer <int> > resultRowsMatrix = TwoDToOneDResult(matrixOne.Count, matrixTwo[0].Count, context); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < resultRowsMatrix.Count; ++i) { for (int j = 0; j < resultRowsMatrix[i].Count; ++j) { kernel.SetMemoryArgument(0, rowsMatrixOne[i]); kernel.SetMemoryArgument(1, columnsMatrixTwo[j]); kernel.SetMemoryArgument(2, resultRowsMatrix[i]); kernel.SetValueArgument(3, matrixTwo.Count); kernel.SetValueArgument(4, j); queue.ExecuteTask(kernel, null); } } queue.Finish(); stopwatch.Stop(); for (int i = 0; i < resultRowsMatrix.Count; ++i) { int[] res = new int[resultRowsMatrix[i].Count]; GCHandle gCHandle = GCHandle.Alloc(res, GCHandleType.Pinned); queue.Read <int>(resultRowsMatrix[i], true, 0, res.Length, gCHandle.AddrOfPinnedObject(), null); result.Add(new List <int>(res)); } return(new Tuple <List <List <int> >, TimeSpan>(result, stopwatch.Elapsed)); }
public void CreateKernel() { if (this.GpuEnable) { string kernelSource = this.ActivateFunctionString; foreach (var parameter in this.ActivationParameters) { kernelSource = this.ActivateFunctionString.Replace(parameter.Key, parameter.Value); } kernelSource += this.ActivateKernelString; ComputeProgram program = Weaver.CreateProgram(kernelSource); this.ForwardKernel = program.CreateKernel(this.ForwardKernelName); this.BackwardKernel = program.CreateKernel(this.BackwardKernelName); } }
public async Task Can_multiply_using_pinned_host_pointer() { // This doesn't seem to work on NVidia platforms 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 }; var sourceHandle = GCHandle.Alloc(source, GCHandleType.Pinned); var sourcePtr = sourceHandle.AddrOfPinnedObject(); using var sourceBuffer = new ComputeBuffer <byte>( context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, source.LongLength, sourcePtr); var target = new byte[5]; var targetHandle = GCHandle.Alloc(target, GCHandleType.Pinned); var targetPtr = targetHandle.AddrOfPinnedObject(); using var targetBuffer = new ComputeBuffer <byte>( context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, target.LongLength, targetPtr); try { kernel.SetMemoryArgument(0, sourceBuffer); kernel.SetMemoryArgument(1, targetBuffer); var events = new List <ComputeEventBase>(); queue.Execute(kernel, null, new long[] { source.Length }, null, events); //await events.WaitForEvents(); for (var i = 0; i < target.Length; i++) { Console.WriteLine($"{source[i]} * 2 = {target[i]}"); } } finally { sourceHandle.Free(); targetHandle.Free(); } }
public async Task Can_use_struct() { 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, Struct_kernel); try { program.Build(new[] { device }, "-cl-std=CL1.2", null, IntPtr.Zero); } catch (Exception ex) { OnProgramBuilt(program, device); return; } using var queue = new ComputeCommandQueue(context, device, ComputeCommandQueueFlags.None); using var kernel = program.CreateKernel("fill"); var target = new Target[10]; var gcHandle = GCHandle.Alloc(target, GCHandleType.Pinned); var ptr = gcHandle.AddrOfPinnedObject(); var buffer = new ComputeBuffer <Target>( context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, 10, ptr); kernel.SetMemoryArgument(0, buffer); var events = new List <ComputeEventBase>(); queue.Execute(kernel, null, new long[] { 10 }, null, events); //await events.WaitForEvents(); unsafe { target[0].a.Should().Be(0); target[0].b.Should().BeApproximately(0.5f, 0.01f); target[0].c.Should().Be(1); target[0].d[0].Should().Be(1); target[0].d[1].Should().Be(2); target[1].a.Should().Be(2); target[1].b.Should().BeApproximately(1.5f, 0.01f); target[1].c.Should().Be(2); target[1].d[0].Should().Be(1); target[1].d[1].Should().Be(2); target[9].a.Should().Be(18); target[9].b.Should().BeApproximately(9.5f, 0.01f); target[9].c.Should().Be(10); target[9].d[0].Should().Be(1); target[9].d[1].Should().Be(2); } }
public static void CLBuilder(ComputeContext context) { UIConsole.Log("Loading kernel.cl"); var clProgramSource = File.ReadAllText("kernel.cl"); UIConsole.Log("Compiling kernel"); program = new ComputeProgram(context, clProgramSource); try { program.Build(null, null, null, IntPtr.Zero); } catch (Exception e) { UIConsole.Error("Build Log: \n" + program.GetBuildLog(context.Devices[0])); throw e; } reprojectKernel = program.CreateKernel("reproject"); apply2DLUTKernel = program.CreateKernel("apply2DLUT"); applyCurveKernel = program.CreateKernel("applyCurve"); UIConsole.Log("Building Curve LUT"); curveLut = new byte[256]; for (int i = 0; i < 256; i++) { float v = 255 * OpenSatelliteProject.Presets.NEW_VIS_FALSE_CURVE[i]; curveLut[i] = (byte)(((int)Math.Floor(v)) & 0xFF); } curveLutBuffer = new ComputeBuffer <byte>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, curveLut); UIConsole.Log("Loading LUT2D"); byte[] buffer = ReadFileFromOSPAssembly("falsecolor.png"); Bitmap lutBmp; using (MemoryStream stream = new MemoryStream(buffer)) { lutBmp = new Bitmap(stream); } lut2D = new uint[256 * 256]; for (int i = 0; i < 256; i++) { for (int j = 0; j < 256; j++) { lut2D[(i * 256) + j] = (uint)(lutBmp.GetPixel(j, i).ToArgb() & 0xFFFFFFFF); } } lut2DBuffer = new ComputeBuffer <uint>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, lut2D); }
public GPU() { context = new ComputeContext(ComputeDeviceTypes.Gpu, new ComputeContextPropertyList(ComputePlatform.Platforms[0]), null, IntPtr.Zero); queue = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); var program = new ComputeProgram(context, ArrayCopy); program.Build(null, null, null, IntPtr.Zero); kernel = program.CreateKernel("ArrayCopy"); }
private void InitClooApi() { try { CvInvoke.UseOpenCL = true; // pick first platform ComputePlatform platform = ComputePlatform.Platforms[1]; // create context with all gpu devices clooCtx = new ComputeContext(ComputeDeviceTypes.Gpu, new ComputeContextPropertyList(platform), null, IntPtr.Zero); // load opencl source StreamReader streamReader = new StreamReader(LASER_CL_PATH); string clSource = streamReader.ReadToEnd(); streamReader.Close(); // build program. ctxLaserCL = new ComputeProgram(clooCtx, clSource); // compile opencl source ctxLaserCL.Build(null, null, null, IntPtr.Zero); // load chosen kernel from program ctxMinMaxKernel = ctxLaserCL.CreateKernel("minMaxValues"); ctxMaskImageKernel = ctxLaserCL.CreateKernel("maskImage"); ctxCenterMassKernel = ctxLaserCL.CreateKernel("centerMass"); ctxTransform3DKernel = ctxLaserCL.CreateKernel("transformPixelsTo3D"); // create a command queue with first gpu found queue = new ComputeCommandQueue(clooCtx, clooCtx.Devices[0], ComputeCommandQueueFlags.None); // execute kernel events = new ComputeEventList(); } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show(ctxLaserCL.GetBuildLog(clooCtx.Devices[0])); } }
static void Main(string[] args) { int w = 11, h = 11, sx = 5, sy = 5, iters = 2; var properties = new ComputeContextPropertyList(ComputePlatform.Platforms[0]); var context = new ComputeContext(ComputeDeviceTypes.All, properties, null, IntPtr.Zero); var quene = new ComputeCommandQueue(context, ComputePlatform.Platforms[0].Devices[0], ComputeCommandQueueFlags.None); //Компиляция программы var prog = new ComputeProgram(context, Source); try { prog.Build(context.Devices, "", null, IntPtr.Zero); } catch { Console.WriteLine(prog.GetBuildLog(context.Devices[0])); } //Создание ядра var kernel = prog.CreateKernel("Test"); var mat = new float[w * h]; for (int i = 0; i < w * h; i++) { mat[i] = (i == w * sy + sx) ? 1f : 0f; } var mat1 = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, mat); var mat2 = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadWrite, w * h); kernel.SetMemoryArgument(0, mat1); kernel.SetMemoryArgument(1, mat2); kernel.SetValueArgument(2, iters); kernel.SetValueArgument(3, w); kernel.SetValueArgument(4, h); quene.Execute(kernel, null, new long[] { (long)h, (long)w }, null, null); quene.ReadFromBuffer(mat1, ref mat, true, null); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { Console.Write($"{mat[i*w+j]:.00} "); } Console.WriteLine(); } Console.ReadKey(); }
private void InitializeCellGrid() { ComputeProgram prog = new ComputeProgram(Ctx, InitilizeGridKernelString); prog.Build(Ctx.Devices, "", null, IntPtr.Zero); InitilizeGridKernel = prog.CreateKernel("InitializeGrid"); InitilizeGridKernel.SetMemoryArgument(0, CurrImage); InitilizeGridKernel.SetMemoryArgument(1, NextImage); CQ = new ComputeCommandQueue(Ctx, Ctx.Devices[0], ComputeCommandQueueFlags.None); CQ.Execute(InitilizeGridKernel, null, new long[] { Width, Height }, null, null); }
static void Main(string[] args) { int[] r1 = new int[] { 8, 2, 3, 4 }; int[] r2 = new int[] { 4, 3, 2, 5 }; int[] r3 = new int[4]; int rowSize = r1.Length; // pick first platform ComputePlatform platform = ComputePlatform.Platforms[0]; // create context with all gpu devices ComputeContext context = new ComputeContext(ComputeDeviceTypes.Gpu, new ComputeContextPropertyList(platform), null, IntPtr.Zero); // create a command queue with first gpu found ComputeCommandQueue queue = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); // load opencl source and // create program with opencl source ComputeProgram program = new ComputeProgram(context, CalculateKernel); // compile opencl source program.Build(null, null, null, IntPtr.Zero); // load chosen kernel from program ComputeKernel kernel = program.CreateKernel("Calc"); // allocate a memory buffer with the message (the int array) ComputeBuffer <int> row1Buffer = new ComputeBuffer <int>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, r1); // allocate a memory buffer with the message (the int array) ComputeBuffer <int> row2Buffer = new ComputeBuffer <int>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, r2); // allocate a memory buffer with the message (the int array) ComputeBuffer <int> resultBuffer = new ComputeBuffer <int>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, new int[4]); kernel.SetMemoryArgument(0, row1Buffer); // set the integer array kernel.SetMemoryArgument(1, row2Buffer); // set the integer array kernel.SetValueArgument(2, rowSize); // set the array size kernel.SetMemoryArgument(3, resultBuffer); // set the integer array // execute kernel queue.ExecuteTask(kernel, null); // wait for completion queue.Finish(); GCHandle arrCHandle = GCHandle.Alloc(r3, GCHandleType.Pinned); queue.Read <int>(resultBuffer, true, 0, r3.Length, arrCHandle.AddrOfPinnedObject(), null); Console.WriteLine("display result from gpu buffer:"); for (int i = 0; i < r3.Length; i++) { Console.WriteLine(r3[i]); } arrCHandle.Free(); Console.WriteLine("Finished"); Console.ReadKey(); }
public void Can_copy() { 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, Copy_kernel); try { program.Build(new[] { device }, "-cl-std=CL1.2", null, IntPtr.Zero); } catch (Exception ex) { OnProgramBuilt(program, device); return; } using var queue = new ComputeCommandQueue(context, device, ComputeCommandQueueFlags.None); using var kernel = program.CreateKernel("copy"); var sourcePath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, "sample00.png")); using var sourceImage = (Bitmap)Image.FromFile(sourcePath); var w = sourceImage.Width; var h = sourceImage.Height; var source = sourceImage.ToBytes(); source.SaveFormatted("source.txt", w, h, channels: 4); using var sourceBuffer = context.CreateImage2D(source, w, h); var target = new byte[h * w * 4]; using var targetBuffer = context.CreateBuffer(target); kernel.SetMemoryArgument(0, sourceBuffer); kernel.SetMemoryArgument(1, targetBuffer); queue.Execute(kernel, null, new long[] { w, h }, null, null); queue.Finish(); target.SaveFormatted("target.txt", w, h, channels: 4); var result = target.ToBitmap(w, h, 4); var resultPath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, "copy.png")); result.Save(resultPath); Run("source.txt"); Run("target.txt"); Run(resultPath); }
public int ArrayTest(int[,] a1, int[,] b) { Point f; int[,] a = new int[2, 2]; MyPoint[] plist = new MyPoint[100000]; MyPoint p1 = new MyPoint() { X = 1, Y = 1 }; MyPoint p2 = new MyPoint() { X = 2, Y = 2 }; plist[0] = p1; plist[1] = p2; IntPtr dataaddr_a = Marshal.UnsafeAddrOfPinnedArrayElement(plist, 0); ComputeBuffer <MyPoint> abuffer = new ComputeBuffer <MyPoint>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, plist); string source = Encoding.ASCII.GetString(FZYK.Nest.Properties.Resources.nest); ComputeProgram program = new ComputeProgram(context, source); try { program.Build(null, null, null, IntPtr.Zero); } catch (Exception) { var log = program.GetBuildLog(context.Devices[0]); Debugger.Break(); } ComputeKernel kernel = program.CreateKernel("Sum"); ComputeBuffer <MyPoint> outdata = new ComputeBuffer <MyPoint>(context, ComputeMemoryFlags.ReadWrite, plist.Length); kernel.SetMemoryArgument(0, abuffer); kernel.SetMemoryArgument(1, outdata); commands.Execute(kernel, null, new long[] { 2 }, null, events); MyPoint[] myresult = new MyPoint[2]; GCHandle arrCHandle = GCHandle.Alloc(myresult, GCHandleType.Pinned); commands.Read(outdata, true, 0, 2, arrCHandle.AddrOfPinnedObject(), events); MyPoint[] test = myresult; arrCHandle.Free(); return(0); }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Creates the kernel. </summary> /// /// <seealso cref="M:KelpNet.Common.Functions.IParallelizable.CreateKernel()"/> //////////////////////////////////////////////////////////////////////////////////////////////////// public void CreateKernel() { if (GpuEnable) { string kernelSource = KernelString; if (Activator != null) { kernelSource = Activator.ActivateFunctionString + KernelString; } if (Verbose) { RILogManager.Default?.SendDebug("CreateKernel -> KernelSource is " + kernelSource); } ComputeProgram program = Weaver.CreateProgram(kernelSource); ForwardKernel = program?.CreateKernel(ForwardKernelName); BackwardgWKernel = program?.CreateKernel(BackwardgWKernelName); BackwardgXKernel = program?.CreateKernel(BackwardgXKernelName); } }
private static ComputeKernel GetKernel(ComputeProgram program) { try { return(program.CreateKernel("place")); } catch { string log = program.GetBuildLog(program.Context.Platform.Devices[0]); Console.WriteLine(log); throw; } }
protected T[] InternalExecuteOpencl <T>( String source, String function, int bufferSize, ParallelTaskParams loaderParams, params Object[] kernelParams) where T : struct { TriggerCheckpoint(ParallelExecutionCheckpointType.CheckpointStart); ComputeCommandQueue queue = QueueWithDevice(loaderParams.OpenCLDevice); TriggerCheckpoint(ParallelExecutionCheckpointType.CheckpointPlatformInit); String updatedSource = "#define OpenCL\r\n" + source; ComputeProgram program = new ComputeProgram(queue.Context, updatedSource); program.Build(new ComputeDevice[] { queue.Device }, null, null, IntPtr.Zero); TriggerCheckpoint(ParallelExecutionCheckpointType.CheckpointKernelBuild); T[] resultBuffer = new T[bufferSize]; ComputeBuffer <T> resultBufferVar = new ComputeBuffer <T>(queue.Context, ComputeMemoryFlags.WriteOnly, bufferSize); List <ComputeMemory> vars = new List <ComputeMemory>(); vars.Add(resultBufferVar); vars.AddRange(WrapDeviceVariables(kernelParams, queue.Context)); ComputeKernel kernel = program.CreateKernel(function); for (int i = 0; i < vars.Count; i++) { kernel.SetMemoryArgument(i, vars[i]); } TriggerCheckpoint(ParallelExecutionCheckpointType.CheckpointDeviceWrite); long[] workersGlobal = new long[2] { loaderParams.GlobalWorkers.Width, loaderParams.GlobalWorkers.Height }; queue.Execute(kernel, null, workersGlobal, null, null); TriggerCheckpoint(ParallelExecutionCheckpointType.CheckpointKernelExecute); queue.ReadFromBuffer <T>(resultBufferVar, ref resultBuffer, false, null); TriggerCheckpoint(ParallelExecutionCheckpointType.CheckpointDeviceRead); queue.Finish(); TriggerCheckpoint(ParallelExecutionCheckpointType.CheckpointPlatformDeinit); return(resultBuffer); }