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(); row1Buffer.Dispose(); row2Buffer.Dispose(); kernel.Dispose(); program.Dispose(); queue.Dispose(); context.Dispose(); Console.WriteLine("Finished"); Console.ReadKey(); }
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)); }
static void Main(string[] args) { int[] r1 = new int[] {1, 2, 3, 4}; int[] r2 = new int[] {4, 3, 2, 1}; 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); kernel.SetMemoryArgument(0, row1Buffer); // set the integer array kernel.SetMemoryArgument(1, row2Buffer); // set the integer array kernel.SetValueArgument(2, rowSize); // set the array size // execute kernel queue.ExecuteTask(kernel, null); // wait for completion queue.Finish(); Console.WriteLine("Finished"); Console.ReadKey(); }
// 4719 ms 4096x4096@512 iter with 1 core? static long Method03(float ymin, float xmin, float width, int[] message) { // 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 StreamReader streamReader = new StreamReader("Mandel.cl"); string clSource = streamReader.ReadToEnd(); streamReader.Close(); // create program with opencl source ComputeProgram program = new ComputeProgram(context, clSource); // compile opencl source program.Build(null, null, null, IntPtr.Zero); // load chosen kernel from program ComputeKernel kernel = program.CreateKernel("mandel"); int messageSize = message.Length; // allocate a memory buffer with the message ComputeBuffer <int> messageBuffer = new ComputeBuffer <int>(context, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.UseHostPointer, message); kernel.SetMemoryArgument(0, messageBuffer); kernel.SetValueArgument(1, N); kernel.SetValueArgument(2, ymin); kernel.SetValueArgument(3, xmin); kernel.SetValueArgument(4, width); kernel.SetValueArgument(5, MaxIter); var watch = System.Diagnostics.Stopwatch.StartNew(); // Execute kernel queue.ExecuteTask(kernel, null); // Read data back unsafe { fixed(int *retPtr = message) { queue.Read(messageBuffer, false, 0, messageSize, new IntPtr(retPtr), null); queue.Finish(); } } watch.Stop(); return(watch.ElapsedMilliseconds); }
public void ProccessOld() { // 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 StreamReader streamReader = new StreamReader("1.cl"); string clSource = streamReader.ReadToEnd(); streamReader.Close(); // create program with opencl source ComputeProgram program = new ComputeProgram(context, clSource); // compile opencl source program.Build(null, null, null, IntPtr.Zero); // load chosen kernel from program ComputeKernel kernel = program.CreateKernel("sum"); // create a ten integer array and its length float[] a = new float[] { 1.1f, 2.3f }; float[] b = new float[] { 3.6f, 4.9f }; // allocate a memory buffer with the message (the int array) ComputeBuffer <float> aBuffer = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, a); ComputeBuffer <float> bBuffer = new ComputeBuffer <float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, b); kernel.SetMemoryArgument(0, aBuffer); kernel.SetMemoryArgument(1, bBuffer); // execute kernel queue.ExecuteTask(kernel, null); // wait for completion queue.Finish(); queue.ReadFromBuffer <float>(aBuffer, ref a, true, null); for (int i = 0; i < a.Length; i++) { Console.WriteLine(a[i]); } /* * // create a ten integer array and its length * int[] message = new int[] { 1, 2, 3, 4, 5 }; * int messageSize = message.Length; * * // allocate a memory buffer with the message (the int array) * ComputeBuffer<int> messageBuffer = new ComputeBuffer<int>(context, * ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, message); * * kernel.SetMemoryArgument(0, messageBuffer); // set the integer array * kernel.SetValueArgument(1, messageSize); // set the array size * * // execute kernel * queue.ExecuteTask(kernel, null); * * // wait for completion * queue.Finish();*/ }