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); }
public static void RunKernels(int nofKernels) { try { // Create the event wait list. An event list is not really needed for this example but it is important to see how it works. // Note that events (like everything else) consume OpenCL resources and creating a lot of them may slow down execution. // For this reason their use should be avoided if possible. ComputeEventList eventList = new ComputeEventList(); commandQueue1.WriteToBuffer(input, CB_input, false, eventList); commandQueue1.WriteToBuffer(weightIDs, CB_networkIndex, false, eventList); // Execute the kernel "count" times. After this call returns, "eventList" will contain an event associated with this command. // If eventList == null or typeof(eventList) == ReadOnlyCollection<ComputeEventBase>, a new event will not be created. commandQueue1.Execute(kernel, null, new long[] { nofKernels }, null, eventList); // Read back the results. If the command-queue has out-of-order execution enabled (default is off), ReadFromBuffer // will not execute until any previous events in eventList (in our case only eventList[0]) are marked as complete // by OpenCL. By default the command-queue will execute the commands in the same order as they are issued from the host. // eventList will contain two events after this method returns. commandQueue1.ReadFromBuffer(CB_output, ref output, false, eventList); // , eventList // A blocking "ReadFromBuffer" (if 3rd argument is true) will wait for itself and any previous commands // in the command queue or eventList to finish execution. Otherwise an explicit wait for all the opencl commands // to finish has to be issued before "arrC" can be used. // This explicit synchronization can be achieved in two ways: // 1) Wait for the events in the list to finish, eventList.Wait(); // 2) Or simply use commandQueue1.Finish(); } catch (Exception e) { Console.WriteLine(e.ToString()); } }
private static void ConductSearch(ComputeContext context, ComputeKernel kernel) { var todos = GetQueenTaskPartition(NumQueens, 4); var done = new List <QueenTask>(); ComputeEventList eventList = new ComputeEventList(); var commands = new ComputeCommandQueue(context, context.Devices[1], ComputeCommandQueueFlags.None); Console.WriteLine("Starting {0} tasks, and working {1} at a time.", todos.Count, Spread); QueenTask[] inProgress = GetNextAssignment(new QueenTask[] {}, todos, done); var sw = new Stopwatch(); sw.Start(); while (inProgress.Any()) { var taskBuffer = new ComputeBuffer <QueenTask>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, inProgress); kernel.SetMemoryArgument(0, taskBuffer); commands.WriteToBuffer(inProgress, taskBuffer, false, null); for (int i = 0; i < 12; i++) { commands.Execute(kernel, null, new long[] { inProgress.Length }, null, eventList); } commands.ReadFromBuffer(taskBuffer, ref inProgress, false, eventList); commands.Finish(); inProgress = GetNextAssignment(inProgress, todos, done); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds / 1000.0); ulong sum = done.Select(state => state.solutions) .Aggregate((total, next) => total + next); Console.WriteLine("Q({0})={1}", NumQueens, sum); }
private static void ConductSearch(ComputeContext context, ComputeKernel kernel) { var todos = GetQueenTaskPartition(NumQueens, 4); var done = new List<QueenTask>(); ComputeEventList eventList = new ComputeEventList(); var commands = new ComputeCommandQueue(context, context.Devices[1], ComputeCommandQueueFlags.None); Console.WriteLine("Starting {0} tasks, and working {1} at a time.", todos.Count, Spread); QueenTask[] inProgress = GetNextAssignment(new QueenTask[] {}, todos, done); var sw = new Stopwatch(); sw.Start(); while (inProgress.Any()) { var taskBuffer = new ComputeBuffer<QueenTask>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, inProgress); kernel.SetMemoryArgument(0, taskBuffer); commands.WriteToBuffer(inProgress, taskBuffer, false, null); for (int i = 0; i < 12; i++) commands.Execute(kernel, null, new long[] { inProgress.Length }, null, eventList); commands.ReadFromBuffer(taskBuffer, ref inProgress, false, eventList); commands.Finish(); inProgress = GetNextAssignment(inProgress, todos, done); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds / 1000.0); ulong sum = done.Select(state => state.solutions) .Aggregate((total, next) => total + next); Console.WriteLine("Q({0})={1}", NumQueens, sum); }
public TerrainGen() { #if CPU_DEBUG var platform = ComputePlatform.Platforms[1]; #else var platform = ComputePlatform.Platforms[0]; #endif _devices = new List<ComputeDevice>(); _devices.Add(platform.Devices[0]); _properties = new ComputeContextPropertyList(platform); _context = new ComputeContext(_devices, _properties, null, IntPtr.Zero); _cmdQueue = new ComputeCommandQueue(_context, _devices[0], ComputeCommandQueueFlags.None); #region setup generator kernel bool loadFromSource = Gbl.HasRawHashChanged[Gbl.RawDir.Scripts]; loadFromSource = true; _chunkWidthInBlocks = Gbl.LoadContent<int>("TGen_ChunkWidthInBlocks"); _chunkWidthInVerts = _chunkWidthInBlocks + 1; _blockWidth = Gbl.LoadContent<int>("TGen_BlockWidthInMeters"); float lacunarity = Gbl.LoadContent<float>("TGen_Lacunarity"); float gain = Gbl.LoadContent<float>("TGen_Gain"); int octaves = Gbl.LoadContent<int>("TGen_Octaves"); float offset = Gbl.LoadContent<float>("TGen_Offset"); float hScale = Gbl.LoadContent<float>("TGen_HScale"); float vScale = Gbl.LoadContent<float>("TGen_VScale"); _genConstants = new ComputeBuffer<float>(_context, ComputeMemoryFlags.ReadOnly, 8); var genArr = new[]{ lacunarity, gain, offset, octaves, hScale, vScale, _blockWidth, _chunkWidthInBlocks }; _cmdQueue.WriteToBuffer(genArr, _genConstants, false, null); if (loadFromSource){ _generationPrgm = new ComputeProgram(_context, Gbl.LoadScript("TGen_Generator")); #if CPU_DEBUG _generationPrgm.Build(null, @"-g -s D:\Projects\Gondola\Scripts\GenTerrain.cl", null, IntPtr.Zero); //use option -I + scriptDir for header search #else _generationPrgm.Build(null, "", null, IntPtr.Zero);//use option -I + scriptDir for header search #endif Gbl.SaveBinary(_generationPrgm.Binaries, "TGen_Generator"); } else{ var binary = Gbl.LoadBinary("TGen_Generator"); _generationPrgm = new ComputeProgram(_context, binary, _devices); _generationPrgm.Build(null, "", null, IntPtr.Zero); } //loadFromSource = false; _terrainGenKernel = _generationPrgm.CreateKernel("GenTerrain"); _normalGenKernel = _generationPrgm.CreateKernel("GenNormals"); //despite the script using float3 for these fields, we need to consider it to be float4 because the //implementation is basically a float4 wrapper that uses zero for the last variable _geometry = new ComputeBuffer<float>(_context, ComputeMemoryFlags.None, _chunkWidthInVerts*_chunkWidthInVerts*4); _normals = new ComputeBuffer<ushort>(_context, ComputeMemoryFlags.None, _chunkWidthInVerts * _chunkWidthInVerts * 4); _binormals = new ComputeBuffer<byte>(_context, ComputeMemoryFlags.None, _chunkWidthInVerts*_chunkWidthInVerts*4); _tangents = new ComputeBuffer<byte>(_context, ComputeMemoryFlags.None, _chunkWidthInVerts*_chunkWidthInVerts*4); _uvCoords = new ComputeBuffer<float>(_context, ComputeMemoryFlags.None, _chunkWidthInVerts*_chunkWidthInVerts*2); _terrainGenKernel.SetMemoryArgument(0, _genConstants); _terrainGenKernel.SetMemoryArgument(3, _geometry); _terrainGenKernel.SetMemoryArgument(4, _uvCoords); _normalGenKernel.SetMemoryArgument(0, _genConstants); _normalGenKernel.SetMemoryArgument(3, _geometry); _normalGenKernel.SetMemoryArgument(4, _normals); _normalGenKernel.SetMemoryArgument(5, _binormals); _normalGenKernel.SetMemoryArgument(6, _tangents); #endregion #region setup quadtree kernel if (loadFromSource){ _qTreePrgm = new ComputeProgram(_context, Gbl.LoadScript("TGen_QTree")); #if CPU_DEBUG _qTreePrgm.Build(null, @"-g -s D:\Projects\Gondola\Scripts\Quadtree.cl", null, IntPtr.Zero); #else _qTreePrgm.Build(null, "", null, IntPtr.Zero); #endif Gbl.SaveBinary(_qTreePrgm.Binaries, "TGen_QTree"); } else{ var binary = Gbl.LoadBinary("TGen_QTree"); _qTreePrgm = new ComputeProgram(_context, binary, _devices); _qTreePrgm.Build(null, "", null, IntPtr.Zero); } _qTreeKernel = _qTreePrgm.CreateKernel("QuadTree"); _crossCullKernel = _qTreePrgm.CreateKernel("CrossCull"); _activeVerts = new ComputeBuffer<byte>(_context, ComputeMemoryFlags.None, _chunkWidthInVerts*_chunkWidthInVerts); _dummy = new ComputeBuffer<int>(_context, ComputeMemoryFlags.None, 50); var rawNormals = new ushort[_chunkWidthInVerts * _chunkWidthInVerts * 4]; _emptyVerts = new byte[_chunkWidthInVerts*_chunkWidthInVerts]; for (int i = 0; i < _emptyVerts.Length; i++){ _emptyVerts[i] = 1; } _cmdQueue.WriteToBuffer(rawNormals, _normals, true, null); _cmdQueue.WriteToBuffer(_emptyVerts, _activeVerts, true, null); _qTreeKernel.SetValueArgument(1, _chunkWidthInBlocks); _qTreeKernel.SetMemoryArgument(2, _normals); _qTreeKernel.SetMemoryArgument(3, _activeVerts); _qTreeKernel.SetMemoryArgument(4, _dummy); _crossCullKernel.SetValueArgument(1, _chunkWidthInBlocks); _crossCullKernel.SetMemoryArgument(2, _normals); _crossCullKernel.SetMemoryArgument(3, _activeVerts); _crossCullKernel.SetMemoryArgument(4, _dummy); #endregion #region setup winding kernel if (loadFromSource){ _winderPrgm = new ComputeProgram(_context, Gbl.LoadScript("TGen_VertexWinder")); #if CPU_DEBUG _winderPrgm.Build(null, @"-g -s D:\Projects\Gondola\Scripts\VertexWinder.cl", null, IntPtr.Zero); #else _winderPrgm.Build(null, "", null, IntPtr.Zero); #endif Gbl.SaveBinary(_winderPrgm.Binaries, "TGen_VertexWinder"); } else{ var binary = Gbl.LoadBinary("TGen_VertexWinder"); _winderPrgm = new ComputeProgram(_context, binary, _devices); _winderPrgm.Build(null, "", null, IntPtr.Zero); } _winderKernel = _winderPrgm.CreateKernel("VertexWinder"); _indicies = new ComputeBuffer<int>(_context, ComputeMemoryFlags.None, (_chunkWidthInBlocks)*(_chunkWidthInBlocks)*8); _winderKernel.SetMemoryArgument(0, _activeVerts); _winderKernel.SetMemoryArgument(1, _indicies); _emptyIndices = new int[(_chunkWidthInBlocks)*(_chunkWidthInBlocks)*8]; for (int i = 0; i < (_chunkWidthInBlocks)*(_chunkWidthInBlocks)*8; i++){ _emptyIndices[i] = 0; } _cmdQueue.WriteToBuffer(_emptyIndices, _indicies, true, null); #endregion if (loadFromSource){ Gbl.AllowMD5Refresh[Gbl.RawDir.Scripts] = true; } _cmdQueue.Finish(); }
public void CopyToDevice() { _queue.WriteToBuffer(_cpubuffer, _gpubuffer, true, null); }
public void Run(IComputeContext context, TextWriter log) { try { log.Write("Creating command queue... "); var commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); log.WriteLine("done."); log.Write("Generating data... "); int linearSize = 24; SysIntX2 rectSize = new SysIntX2(4, 6); SysIntX3 cubicSize = new SysIntX3(2, 3, 4); float[] linearIn = new float[linearSize]; float[] linearOut = new float[linearSize]; float[,] rectIn = new float[(int)rectSize.Y, (int)rectSize.X]; float[,] rectOut = new float[(int)rectSize.Y, (int)rectSize.X]; float[,,] cubicIn = new float[(int)cubicSize.Z, (int)cubicSize.Y, (int)cubicSize.X]; float[,,] cubicOut = new float[(int)cubicSize.Z, (int)cubicSize.Y, (int)cubicSize.X]; for (int i = 0; i < linearSize; i++) { linearIn[i] = i; } for (int i = 0; i < (int)rectSize.X; i++) { for (int j = 0; j < (int)rectSize.Y; j++) { rectIn[j, i] = (float)(rectSize.X.ToInt32() * j + i); } } for (int i = 0; i < (int)cubicSize.X; i++) { for (int j = 0; j < (int)cubicSize.Y; j++) { for (int k = 0; k < (int)cubicSize.Z; k++) { cubicIn[k, j, i] = (float)(k * cubicSize.Y.ToInt32() * cubicSize.X.ToInt32() + cubicSize.X.ToInt32() * j + i); } } } log.WriteLine("done."); log.Write("Creating buffer... "); var buffer = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadWrite, linearSize); log.WriteLine("done."); GC.Collect(); log.Write("Writing to buffer (linear)... "); commands.WriteToBuffer(linearIn, buffer, false, null); log.WriteLine("done."); log.Write("Reading from buffer (linear)... "); commands.ReadFromBuffer(buffer, ref linearOut, false, null); log.WriteLine("done."); GC.Collect(); commands.Finish(); log.Write("Comparing data... "); Compare(linearIn, linearOut); log.WriteLine("passed."); GC.Collect(); log.Write("Writing to buffer (rectangular)... "); commands.WriteToBuffer(rectIn, buffer, false, new SysIntX2(), new SysIntX2(), rectSize, null); log.WriteLine("done."); GC.Collect(); log.Write("Reading from buffer (rectangular)... "); commands.ReadFromBuffer(buffer, ref rectOut, false, new SysIntX2(), new SysIntX2(), rectSize, null); log.WriteLine("done."); GC.Collect(); commands.Finish(); log.Write("Comparing data... "); Compare(rectIn, rectOut); log.WriteLine("passed."); GC.Collect(); log.Write("Writing to buffer (cubic)... "); commands.WriteToBuffer(cubicIn, buffer, false, new SysIntX3(), new SysIntX3(), cubicSize, null); log.WriteLine("done."); GC.Collect(); log.Write("Reading from buffer (cubic)... "); commands.ReadFromBuffer(buffer, ref cubicOut, false, new SysIntX3(), new SysIntX3(), cubicSize, null); log.WriteLine("done."); GC.Collect(); commands.Finish(); log.Write("Comparing data... "); Compare(cubicIn, cubicOut); log.WriteLine("passed."); } catch (Exception e) { log.WriteLine(e.ToString()); } }
public unsafe void EndSend() { for (int i = 0; i < points.Count; i++) { inx[i].x = (float)points[i].Item3.Real; inx[i].y = (float)points[i].Item3.Imaginary; inc[i].x = (float)points[i].Item4.Real; inc[i].y = (float)points[i].Item4.Imaginary; } _krnl.SetMemoryArgument(0, x); _krnl.SetMemoryArgument(1, c); for (int i = 0; i < _ld.Count; i++) { _krnl.SetMemoryArgument(2 + i, outp[i]); } ComputeCommandQueue command = new ComputeCommandQueue(_context, _context.Devices[0], ComputeCommandQueueFlags.None); command.WriteToBuffer(inx, x, false, null); command.WriteToBuffer(inc, c, false, null); command.Execute(_krnl, null, new long[] { points.Count }, null, null); for (int i = 0; i < _ld.Count; i++) command.ReadFromBuffer(outp[i], ref opl[i], false, null); command.Finish(); output = new Queue<Tuple<int, int, List<ProcessLayer>>>(); for (int i = 0; i < points.Count; i++) { List<ProcessLayer> pl = new List<ProcessLayer>(); for (int ii = 0; ii < _ld.Count; ii++) { ProcessLayer p = _ld[ii].Clone(); p.c_active = opl[ii][i].c_active != 0; p.c_calc = opl[ii][i].c_calc; p.c_cmean = opl[ii][i].c_cmean; p.c_cvariance = opl[ii][i].c_cvariance; p.c_cvarsx = opl[ii][i].c_cvarsx; p.c_isin = opl[ii][i].c_isin != 0; p.c_n = opl[ii][i].c_n; p.c_old2x = new Complex(opl[ii][i].c_old2x.x,opl[ii][i].c_old2x.y); p.c_oldx = new Complex(opl[ii][i].c_oldx.x,opl[ii][i].c_oldx.y); p.c_resn = opl[ii][i].c_resn; p.c_resx = new Complex(opl[ii][i].c_resx.x,opl[ii][i].c_resx.y); p.c_x = new Complex(opl[ii][i].c_x.x,opl[ii][i].c_x.y); pl.Add(p); } output.Enqueue(Tuple.Create(points[i].Item1, points[i].Item2, pl)); } }
public void CopyToDevice() { queue.WriteToBuffer(cpuBuffer, gpuBuffer, true, null); }
private void CalculateConvolution(ComputeContext computeContext) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); float dx; bool shiftXParse = float.TryParse(textBoxShiftX.Text, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out dx); if (!shiftXParse) { throw new SyntaxErrorException(", needs to be ."); } float dy; bool shiftYParse = float.TryParse(textBoxShiftX.Text, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out dy); if (!shiftYParse) { throw new SyntaxErrorException(", needs to be ."); } float dz; bool shiftZParse = float.TryParse(textBoxShiftX.Text, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out dz); if (!shiftZParse) { throw new SyntaxErrorException(", needs to be ."); } int pixelCount = _imageDimensionX * _imageDimensionY * _imageDimensionZ; Console.WriteLine("Computing..."); Console.WriteLine("Reading kernel..."); String kernelPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName; String kernelString; using (var sr = new StreamReader(kernelPath + "\\convolution.cl")) kernelString = sr.ReadToEnd(); Console.WriteLine("Reading kernel... done"); float[] selectedTransformation = Transformations.GetTransformation((TransformationType)comboBoxTransform.SelectedItem, 1.0f / float.Parse(textBoxPixelSize.Text), 1.0f / float.Parse(textBoxPixelSize.Text), 1.0f / float.Parse(textBoxPixelSize.Text), dx, dy, dz); //create openCL program ComputeProgram computeProgram = new ComputeProgram(computeContext, kernelString); computeProgram.Build(computeContext.Devices, null, null, IntPtr.Zero); ComputeProgramBuildStatus computeProgramBuildStatus = computeProgram.GetBuildStatus(_selectedComputeDevice); Console.WriteLine("computeProgramBuildStatus\n\t" + computeProgramBuildStatus); String buildLog = computeProgram.GetBuildLog(_selectedComputeDevice); Console.WriteLine("buildLog"); if (buildLog.Equals("\n")) { Console.WriteLine("\tbuildLog is empty..."); } else { Console.WriteLine("\t" + buildLog); } float[] fluorophores = CsvData.ReadFluorophores(_sourceFilename); ///////////////////////////////////////////// // Create a Command Queue & Event List ///////////////////////////////////////////// ComputeCommandQueue computeCommandQueue = new ComputeCommandQueue(computeContext, _selectedComputeDevice, ComputeCommandQueueFlags.None); //////////////////////////////////////////////////////////////// // Create Buffers Transform //////////////////////////////////////////////////////////////// ComputeBuffer <float> fluorophoresCoords = new ComputeBuffer <float>(computeContext, ComputeMemoryFlags.ReadWrite, fluorophores.LongLength); ComputeBuffer <float> transformationMatrix = new ComputeBuffer <float>(computeContext, ComputeMemoryFlags.ReadOnly, selectedTransformation.LongLength); ///////////////////////////////////////////// // Create the transformFluorophoresKernel /////////////////////////////////////////////////////////// ComputeKernel transformFluorophoresKernel = computeProgram.CreateKernel("transform_fluorophores"); ///////////////////////////////////////////// // Set the transformFluorophoresKernel arguments ///////////////////////////////////////////// transformFluorophoresKernel.SetMemoryArgument(0, fluorophoresCoords); transformFluorophoresKernel.SetMemoryArgument(1, transformationMatrix); ///////////////////////////////////////////// // Configure the work-item structure ///////////////////////////////////////////// long[] globalWorkOffsetTransformFluorophoresKernel = null; long[] globalWorkSizeTransformFluorophoresKernel = new long[] { fluorophores.Length / 4 }; long[] localWorkSizeTransformFluorophoresKernel = null; //////////////////////////////////////////////////////// // Enqueue the transformFluorophoresKernel for execution //////////////////////////////////////////////////////// computeCommandQueue.WriteToBuffer(fluorophores, fluorophoresCoords, true, null); computeCommandQueue.WriteToBuffer(selectedTransformation, transformationMatrix, true, null); computeCommandQueue.Execute(transformFluorophoresKernel, globalWorkOffsetTransformFluorophoresKernel, globalWorkSizeTransformFluorophoresKernel, localWorkSizeTransformFluorophoresKernel, null); // computeCommandQueue.ExecuteTask(transformFluorophoresKernel, transformFluorophoresEvents); float[] transformedFluorophores = new float[fluorophores.Length]; computeCommandQueue.ReadFromBuffer(fluorophoresCoords, ref transformedFluorophores, true, null); computeCommandQueue.Finish(); //TODO remove, only for testing // for (int i = 0; i < transformedFluorophores.Length; i++) // { // Console.WriteLine(transformedFluorophores[i]); // } // /TODO remove, only for testing stopwatch.Stop(); Console.WriteLine("Transform fluophores duration:\n\t" + stopwatch.Elapsed); stopwatch.Reset(); stopwatch.Start(); // fluorophoresCoords are now transformed (done in place) //////////////////////////////////////////////////////////////// // Create Buffers Convolve Fluorophores //////////////////////////////////////////////////////////////// const int convolve_kernel_lwgs = 16; int totalBuffer = (int)Math.Ceiling(pixelCount / (float)convolve_kernel_lwgs) * convolve_kernel_lwgs; ComputeBuffer <float> resultImage = new ComputeBuffer <float>(computeContext, ComputeMemoryFlags.WriteOnly, totalBuffer); ///////////////////////////////////////////// // Create the transformFluorophoresKernel ///////////////////////////////////////////// ComputeKernel convolveFluorophoresKernel = computeProgram.CreateKernel("convolve_fluorophores"); ///////////////////////////////////////////// // Set the convolveFluorophoresKernel arguments ///////////////////////////////////////////// convolveFluorophoresKernel.SetMemoryArgument(0, resultImage); convolveFluorophoresKernel.SetValueArgument(1, _imageDimensionX); convolveFluorophoresKernel.SetValueArgument(2, _imageDimensionY); convolveFluorophoresKernel.SetMemoryArgument(3, fluorophoresCoords); convolveFluorophoresKernel.SetLocalArgument(4, convolve_kernel_lwgs); convolveFluorophoresKernel.SetValueArgument(5, fluorophores.Length / 4); ///////////////////////////////////////////// // Configure the work-item structure ///////////////////////////////////////////// long[] globalWorkOffsetTransformConvolveFluorophoresKernel = null; long[] globalWorkSizeTransformConvolveFluorophoresKernel = new long[] { pixelCount }; long[] localWorkSizeTransformConvolveFluorophoresKernel = new long[] { convolve_kernel_lwgs }; //////////////////////////////////////////////////////// // Enqueue the convolveFluorophoresKernel for execution //////////////////////////////////////////////////////// computeCommandQueue.Execute(convolveFluorophoresKernel, globalWorkOffsetTransformConvolveFluorophoresKernel, globalWorkSizeTransformConvolveFluorophoresKernel, localWorkSizeTransformConvolveFluorophoresKernel, null); float[] resultImageData = new float[totalBuffer]; computeCommandQueue.ReadFromBuffer(resultImage, ref resultImageData, true, null); computeCommandQueue.Finish(); for (int i = 0; i < pixelCount; i++) { Console.WriteLine(resultImageData[i]); } Console.WriteLine("Writing data to file..."); // CsvData.WriteToDisk("..\\..\\..\\output.csv", resultImageData); TiffData.WriteToDisk(resultImageData, _saveFilename, _imageDimensionX, _imageDimensionY); Bitmap bitmap = new Bitmap(_imageDimensionX, _imageDimensionY); float max = resultImageData.Max(); float scale = 255 / (float)max; // for (int r = 0; r < _imageDimensionY; r++) // { // for (int c = 0; c < _imageDimensionX; c++) // { // float value = resultImageData[c*(r + 1)]; // Color newColor = Color.FromArgb((int)(value * scale), (int)(value * scale), (int)(value * scale)); // bitmap.SetPixel(c,r, newColor); // } // } ushort[] ushortdata = new ushort[resultImageData.Length]; for (int i = 0; i < resultImageData.Length; i++) { ushortdata[i] = (ushort)resultImageData[i]; } uint[] convertGray16ToRgb = ConvertGray16ToRGB(ushortdata, 16); byte[] bytes = new byte[convertGray16ToRgb.Length * 4]; // // int[] resultImageData2 = new int[resultImageData.Length]; // for (int index = 0; index < convertGray16ToRgb.Length; index++) { // resultImageData2[index] = (int)(scale*resultImageData[index]); byte[] bytes1 = BitConverter.GetBytes(convertGray16ToRgb[index]); bytes[index] = bytes1[0]; bytes[4 * index + 1] = bytes1[1]; bytes[4 * index + 2] = bytes1[2]; bytes[4 * index + 3] = bytes1[3]; } // // for (int r = 0; r < _imageDimensionY; r++) // { // for (int c = 0; c < _imageDimensionX; c++) // { // float value = resultImageData2[c*(r + 1)]; // Color newColor = Color.FromArgb((int)(value), (int)(value), (int)(value)); // bitmap.SetPixel(c,r, newColor); // } // } // bitmap.Save("c:\\temp.bmp"); using (MemoryStream ms = new MemoryStream(bytes)) { Image image = Bitmap.FromStream(ms); image.Save("c:\\temp.bmp"); } Console.WriteLine("Writing data to file... done"); stopwatch.Stop(); Console.WriteLine("Convolve fluophores duration:\n\t" + stopwatch.Elapsed); Console.WriteLine("Computing... done"); }
private void CalculateConvolution(ComputeContext computeContext) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); float dx; bool shiftXParse = float.TryParse(textBoxShiftX.Text, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out dx); if (!shiftXParse) throw new SyntaxErrorException(", needs to be ."); float dy; bool shiftYParse = float.TryParse(textBoxShiftX.Text, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out dy); if (!shiftYParse) throw new SyntaxErrorException(", needs to be ."); float dz; bool shiftZParse = float.TryParse(textBoxShiftX.Text, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat, out dz); if (!shiftZParse) throw new SyntaxErrorException(", needs to be ."); int pixelCount = _imageDimensionX*_imageDimensionY*_imageDimensionZ; Console.WriteLine("Computing..."); Console.WriteLine("Reading kernel..."); String kernelPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName; String kernelString; using (var sr = new StreamReader(kernelPath + "\\convolution.cl")) kernelString = sr.ReadToEnd(); Console.WriteLine("Reading kernel... done"); float[] selectedTransformation = Transformations.GetTransformation((TransformationType)comboBoxTransform.SelectedItem, 1.0f / float.Parse(textBoxPixelSize.Text), 1.0f / float.Parse(textBoxPixelSize.Text), 1.0f / float.Parse(textBoxPixelSize.Text), dx, dy, dz); //create openCL program ComputeProgram computeProgram = new ComputeProgram(computeContext, kernelString); computeProgram.Build(computeContext.Devices, null, null, IntPtr.Zero); ComputeProgramBuildStatus computeProgramBuildStatus = computeProgram.GetBuildStatus(_selectedComputeDevice); Console.WriteLine("computeProgramBuildStatus\n\t"+computeProgramBuildStatus); String buildLog = computeProgram.GetBuildLog(_selectedComputeDevice); Console.WriteLine("buildLog"); if (buildLog.Equals("\n")) Console.WriteLine("\tbuildLog is empty..."); else Console.WriteLine("\t" + buildLog); float[] fluorophores = CsvData.ReadFluorophores(_sourceFilename); ///////////////////////////////////////////// // Create a Command Queue & Event List ///////////////////////////////////////////// ComputeCommandQueue computeCommandQueue = new ComputeCommandQueue(computeContext, _selectedComputeDevice, ComputeCommandQueueFlags.None); //////////////////////////////////////////////////////////////// // Create Buffers Transform //////////////////////////////////////////////////////////////// ComputeBuffer<float> fluorophoresCoords = new ComputeBuffer<float>(computeContext, ComputeMemoryFlags.ReadWrite, fluorophores.LongLength); ComputeBuffer<float> transformationMatrix = new ComputeBuffer<float>(computeContext, ComputeMemoryFlags.ReadOnly, selectedTransformation.LongLength); ///////////////////////////////////////////// // Create the transformFluorophoresKernel /////////////////////////////////////////////////////////// ComputeKernel transformFluorophoresKernel = computeProgram.CreateKernel("transform_fluorophores"); ///////////////////////////////////////////// // Set the transformFluorophoresKernel arguments ///////////////////////////////////////////// transformFluorophoresKernel.SetMemoryArgument(0, fluorophoresCoords); transformFluorophoresKernel.SetMemoryArgument(1, transformationMatrix); ///////////////////////////////////////////// // Configure the work-item structure ///////////////////////////////////////////// long[] globalWorkOffsetTransformFluorophoresKernel = null; long[] globalWorkSizeTransformFluorophoresKernel = new long[] { fluorophores.Length / 4 }; long[] localWorkSizeTransformFluorophoresKernel = null; //////////////////////////////////////////////////////// // Enqueue the transformFluorophoresKernel for execution //////////////////////////////////////////////////////// computeCommandQueue.WriteToBuffer(fluorophores, fluorophoresCoords, true, null); computeCommandQueue.WriteToBuffer(selectedTransformation, transformationMatrix, true, null); computeCommandQueue.Execute(transformFluorophoresKernel, globalWorkOffsetTransformFluorophoresKernel, globalWorkSizeTransformFluorophoresKernel, localWorkSizeTransformFluorophoresKernel, null); // computeCommandQueue.ExecuteTask(transformFluorophoresKernel, transformFluorophoresEvents); float[] transformedFluorophores = new float[fluorophores.Length]; computeCommandQueue.ReadFromBuffer(fluorophoresCoords, ref transformedFluorophores, true, null); computeCommandQueue.Finish(); //TODO remove, only for testing // for (int i = 0; i < transformedFluorophores.Length; i++) // { // Console.WriteLine(transformedFluorophores[i]); // } // /TODO remove, only for testing stopwatch.Stop(); Console.WriteLine("Transform fluophores duration:\n\t" + stopwatch.Elapsed); stopwatch.Reset(); stopwatch.Start(); // fluorophoresCoords are now transformed (done in place) //////////////////////////////////////////////////////////////// // Create Buffers Convolve Fluorophores //////////////////////////////////////////////////////////////// const int convolve_kernel_lwgs = 16; int totalBuffer = (int) Math.Ceiling(pixelCount / (float)convolve_kernel_lwgs) * convolve_kernel_lwgs; ComputeBuffer<float> resultImage = new ComputeBuffer<float>(computeContext, ComputeMemoryFlags.WriteOnly, totalBuffer); ///////////////////////////////////////////// // Create the transformFluorophoresKernel ///////////////////////////////////////////// ComputeKernel convolveFluorophoresKernel = computeProgram.CreateKernel("convolve_fluorophores"); ///////////////////////////////////////////// // Set the convolveFluorophoresKernel arguments ///////////////////////////////////////////// convolveFluorophoresKernel.SetMemoryArgument(0, resultImage); convolveFluorophoresKernel.SetValueArgument(1, _imageDimensionX); convolveFluorophoresKernel.SetValueArgument(2, _imageDimensionY); convolveFluorophoresKernel.SetMemoryArgument(3, fluorophoresCoords); convolveFluorophoresKernel.SetLocalArgument(4, convolve_kernel_lwgs); convolveFluorophoresKernel.SetValueArgument(5, fluorophores.Length / 4); ///////////////////////////////////////////// // Configure the work-item structure ///////////////////////////////////////////// long[] globalWorkOffsetTransformConvolveFluorophoresKernel = null; long[] globalWorkSizeTransformConvolveFluorophoresKernel = new long[] { pixelCount }; long[] localWorkSizeTransformConvolveFluorophoresKernel = new long[] {convolve_kernel_lwgs}; //////////////////////////////////////////////////////// // Enqueue the convolveFluorophoresKernel for execution //////////////////////////////////////////////////////// computeCommandQueue.Execute(convolveFluorophoresKernel, globalWorkOffsetTransformConvolveFluorophoresKernel, globalWorkSizeTransformConvolveFluorophoresKernel, localWorkSizeTransformConvolveFluorophoresKernel, null); float[] resultImageData = new float[totalBuffer]; computeCommandQueue.ReadFromBuffer(resultImage, ref resultImageData, true, null); computeCommandQueue.Finish(); for (int i = 0; i < pixelCount; i++) { Console.WriteLine(resultImageData[i]); } Console.WriteLine("Writing data to file..."); // CsvData.WriteToDisk("..\\..\\..\\output.csv", resultImageData); TiffData.WriteToDisk(resultImageData, _saveFilename, _imageDimensionX, _imageDimensionY); Bitmap bitmap = new Bitmap(_imageDimensionX, _imageDimensionY); float max = resultImageData.Max(); float scale = 255/(float)max; // for (int r = 0; r < _imageDimensionY; r++) // { // for (int c = 0; c < _imageDimensionX; c++) // { // float value = resultImageData[c*(r + 1)]; // Color newColor = Color.FromArgb((int)(value * scale), (int)(value * scale), (int)(value * scale)); // bitmap.SetPixel(c,r, newColor); // } // } ushort[] ushortdata = new ushort[resultImageData.Length]; for (int i = 0; i < resultImageData.Length; i++) { ushortdata[i] = (ushort)resultImageData[i]; } uint[] convertGray16ToRgb = ConvertGray16ToRGB(ushortdata, 16); byte[] bytes = new byte[convertGray16ToRgb.Length * 4]; // // int[] resultImageData2 = new int[resultImageData.Length]; // for (int index = 0; index < convertGray16ToRgb.Length; index++) { // resultImageData2[index] = (int)(scale*resultImageData[index]); byte[] bytes1 = BitConverter.GetBytes(convertGray16ToRgb[index]); bytes[index] = bytes1[0]; bytes[4 * index + 1] = bytes1[1]; bytes[4 * index + 2] = bytes1[2]; bytes[4 * index + 3] = bytes1[3]; } // // for (int r = 0; r < _imageDimensionY; r++) // { // for (int c = 0; c < _imageDimensionX; c++) // { // float value = resultImageData2[c*(r + 1)]; // Color newColor = Color.FromArgb((int)(value), (int)(value), (int)(value)); // bitmap.SetPixel(c,r, newColor); // } // } // bitmap.Save("c:\\temp.bmp"); using (MemoryStream ms = new MemoryStream(bytes)) { Image image = Bitmap.FromStream(ms); image.Save("c:\\temp.bmp"); } Console.WriteLine("Writing data to file... done"); stopwatch.Stop(); Console.WriteLine("Convolve fluophores duration:\n\t" + stopwatch.Elapsed); Console.WriteLine("Computing... done"); }
private static bool prepTris() { int width = frame.GetLength(0); int height = frame.GetLength(1); List <tris> tris = new List <tris>(); List <vec> poss = new List <vec>(); List <vec> rots = new List <vec>(); int[] options = new int[4]; vec[] cam = new vec[3]; cam[0] = eye.toVec(); Vector look = new Vector(0, 0, 1, 0); Vector up = new Vector(0, 1, 0, 0); look *= Matrix4x4.rotationXMatrix(ConsoleSettings.cameraRot.x); look *= Matrix4x4.rotationYMatrix(ConsoleSettings.cameraRot.y); cam[1] = look.toVec(); cam[2] = up.toVec(); foreach (Mesh m in ConsoleRenderer.Meshes) { foreach (Triangle t in m.tris) { tris.Add(t.toTris()); poss.Add(m.position.toVec()); rots.Add(m.rotation.toVec()); } } options[0] = tris.Count; options[1] = width; options[2] = height; if (tris.Count <= 0) { return(false); } if (lastTriCount != tris.Count) { gTris = new ComputeBuffer <tris>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, tris.ToArray()); gOptions = new ComputeBuffer <int>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, options); gCam = new ComputeBuffer <vec>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, cam); gPos = new ComputeBuffer <vec>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, poss.ToArray()); gRot = new ComputeBuffer <vec>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, rots.ToArray()); gTexture = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, texture); } else { commands.WriteToBuffer(tris.ToArray(), gTris, false, eventList); commands.WriteToBuffer(options, gOptions, false, eventList); commands.WriteToBuffer(cam, gCam, false, eventList); commands.WriteToBuffer(rots.ToArray(), gRot, false, eventList); commands.WriteToBuffer(poss.ToArray(), gPos, false, eventList); commands.WriteToBuffer(texture, gTexture, false, eventList); } prepTrisKernel.SetMemoryArgument(0, gTris); prepTrisKernel.SetMemoryArgument(1, gOptions); prepTrisKernel.SetMemoryArgument(2, gRot); prepTrisKernel.SetMemoryArgument(3, gPos); prepTrisKernel.SetMemoryArgument(4, gCam); commands.Execute(prepTrisKernel, null, new long[] { tris.Count }, null, eventList); return(true); }
/// <summary> /// Copy the material data to the GPU. /// </summary> /// <param name="commandQueue"></param> public void syncBuffer(ComputeCommandQueue commandQueue) { // TODO: try non-blocking sends. commandQueue.WriteToBuffer<Material>(_materialArray, Buffer, true, null); }
public static void VectorSumHandler(HttpListenerContext context) { HttpListenerRequest request = context.Request; HttpListenerResponse response = context.Response; 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(); string responseString = string.Format("<html><body>{0}</body></html>", sum_expression_result); byte[] buffer = Encoding.UTF8.GetBytes(responseString); response.ContentLength64 = buffer.Length; Stream output = response.OutputStream; output.Write(buffer, 0, buffer.Length); output.Close(); }
public static void CustomHandler(HttpListenerContext context) { HttpListenerRequest request = context.Request; HttpListenerResponse response = context.Response; string responseString; Stream body = request.InputStream; Encoding encoding = request.ContentEncoding; StreamReader reader = new StreamReader(body, encoding); var data = reader.ReadToEnd(); var props = JsonSerializer.Deserialize <OpenCLPOCO>(data); string vecSum = props.code; int size = props.v1.Length; float[] v1_ = props.v1; float[] v2_ = props.v2; float[] v3_ = new float[size]; 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); try { 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(); responseString = string.Format("{0}", sum_expression_result); } catch (Exception e) { responseString = e.Message; } byte[] buffer = Encoding.UTF8.GetBytes(responseString); response.ContentLength64 = buffer.Length; Stream output = response.OutputStream; output.Write(buffer, 0, buffer.Length); output.Close(); }