private void SetupAndCount(CLMemoryHandle input, int bitOffset) { ComputeErrorCode error; IntPtr agentPtrSize = (IntPtr)0; agentPtrSize = (IntPtr)Marshal.SizeOf(typeof(IntPtr)); var ptrSize = (IntPtr)Marshal.SizeOf(typeof(Mem)); int globalWorkSize = gpuConstants.numThreadsPerBlock * gpuConstants.numBlocks; int localWorkSize = gpuConstants.numThreadsPerBlock; IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)globalWorkSize }; IntPtr[] localWorkGroupSizePtr = new IntPtr[] { (IntPtr)localWorkSize }; ComputeEvent clevent; error = CL10.SetKernelArg(ckSetupAndCount, 0, ptrSize, input); CheckErr(error, "CL10.SetKernelArg"); error = CL10.SetKernelArg(ckSetupAndCount, 1, ptrSize, mCounters); CheckErr(error, "CL10.SetKernelArg"); //if(DEBUG_CONSOLE_OUTPUT) Console.WriteLine((Marshal.SizeOf(typeof(GPUConstants)))); error = CL10.SetKernelArg(ckSetupAndCount, 2, (IntPtr)(Marshal.SizeOf(typeof(GPUConstants))), gpuConstants); CheckErr(error, "CL10.SetKernelArg"); error = CL10.SetKernelArg(ckSetupAndCount, 3, (IntPtr)4, bitOffset); CheckErr(error, "CL10.SetKernelArg"); error = CL10.EnqueueNDRangeKernel(cqCommandQueue, ckSetupAndCount, 1, null, workGroupSizePtr, localWorkGroupSizePtr, 0, null, out clevent); CheckErr(error, "CL10.EnqueueNDRangeKernel"); error = CL10.Finish(cqCommandQueue); CheckErr(error, "CL10.Finish"); if (DEBUG) { ComputeEvent eve; CL10.EnqueueReadBuffer(cqCommandQueue, input, Bool.True, IntPtr.Zero, (IntPtr)(gpuConstants.numTotalElements * 4), debugRead, 0, null, out eve); CheckErr(error, "CL10.EnqueueReadBuffer"); PrintElementBuffer(debugRead, gpuConstants.numTotalElements, "Setup and Count -> Input -> bitoffset = " + bitOffset); CL10.EnqueueReadBuffer(cqCommandQueue, mCounters, Bool.True, IntPtr.Zero, (IntPtr)(numCounters * sizeof(int)), debugRead, 0, null, out eve); CheckErr(error, "CL10.EnqueueReadBuffer"); PrintCounterBuffer(debugRead, "Setup and Count -> bitoffset = " + bitOffset); if (DEBUG_CONSOLE_OUTPUT) { Console.WriteLine("Setup and Count -> bitoffset = " + bitOffset); } if (DEBUG_CONSOLE_OUTPUT) { Console.WriteLine(); } } }
/// <summary> /// Enqueues a command to read data from a buffer. /// </summary> /// <param name="source"> The buffer to read from. </param> /// <param name="blocking"> The mode of operation of this command. If <c>true</c> this call will not return until the command has finished execution. </param> /// <param name="offset"> The <paramref name="source"/> element position where reading starts. </param> /// <param name="region"> The region of elements to read. </param> /// <param name="destination"> A pointer to a preallocated memory area to read the data into. </param> /// <param name="events"> A collection of events that need to complete before this particular command can be executed. If <paramref name="events"/> is not <c>null</c> or read-only a new <see cref="ComputeEvent"/> identifying this command is created and attached to the end of the collection. </param> /// <remarks> If <paramref name="blocking"/> is <c>true</c> this method will not return until the command completes. If <paramref name="blocking"/> is <c>false</c> this method will return immediately after the command is enqueued. </remarks> public void Read <T>(ComputeBufferBase <T> source, bool blocking, long offset, long region, IntPtr destination, ICollection <ComputeEventBase> events) where T : struct { int sizeofT = HDSPUtils.SizeOf(typeof(T)); int eventWaitListSize; CLEventHandle[] eventHandles = ComputeTools.ExtractHandles(events, out eventWaitListSize); bool eventsWritable = (events != null && !events.IsReadOnly); CLEventHandle[] newEventHandle = (eventsWritable) ? new CLEventHandle[1] : null; ComputeErrorCode error = CL10.EnqueueReadBuffer(Handle, source.Handle, blocking, new IntPtr(offset * sizeofT), new IntPtr(region * sizeofT), destination, eventWaitListSize, eventHandles, newEventHandle); ComputeException.ThrowOnError(error); if (eventsWritable) { events.Add(new ComputeEvent(newEventHandle[0], this)); } }
/// <summary> /// Enqueues a command to read data from a buffer. /// </summary> /// <param name="source"> The buffer to read from. </param> /// <param name="blocking"> The mode of operation of this command. If <c>true</c> this call will not return until the command has finished execution. </param> /// <param name="offset"> The <paramref name="source"/> element position where reading starts. </param> /// <param name="region"> The region of elements to read. </param> /// <param name="destination"> A pointer to a preallocated memory area to read the data into. </param> /// <param name="events"> A collection of events that need to complete before this particular command can be executed. If <paramref name="events"/> is not <c>null</c> or read-only a new <see cref="OpenCLEvent"/> identifying this command is created and attached to the end of the collection. </param> /// <remarks> If <paramref name="blocking"/> is <c>true</c> this method will not return until the command completes. If <paramref name="blocking"/> is <c>false</c> this method will return immediately after the command is enqueued. </remarks> public void Read(OpenCLBufferBase source, bool blocking, long offset, long region, IntPtr destination, IReadOnlyList <OpenCLEventBase> events = null, IList <OpenCLEventBase> newEvents = null) { int sizeofT = Marshal.SizeOf(source.ElementType); int eventWaitListSize; CLEventHandle[] eventHandles = OpenCLTools.ExtractHandles(events, out eventWaitListSize); CLEventHandle[] newEventHandle = (newEvents != null) ? new CLEventHandle[1] : null; OpenCLErrorCode error = CL10.EnqueueReadBuffer(Handle, source.Handle, blocking, new IntPtr(offset * sizeofT), new IntPtr(region * sizeofT), destination, eventWaitListSize, eventHandles, newEventHandle); OpenCLException.ThrowOnError(error); if (newEvents != null) { lock (newEvents) { newEvents.Add(new OpenCLEvent(newEventHandle[0], this)); } } }
private void ReorderingKeyValue(CLMemoryHandle inputKey, CLMemoryHandle outputKey, CLMemoryHandle inputValue, CLMemoryHandle outputValue, int bitOffset) { ComputeErrorCode error; IntPtr agentPtrSize = (IntPtr)0; agentPtrSize = (IntPtr)Marshal.SizeOf(typeof(IntPtr)); var ptrSize = (IntPtr)Marshal.SizeOf(typeof(Mem)); int globalWorkSize = gpuConstants.numThreadsPerBlock * gpuConstants.numBlocks; int localWorkSize = gpuConstants.numThreadsPerBlock; IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)globalWorkSize }; IntPtr[] localWorkGroupSizePtr = new IntPtr[] { (IntPtr)localWorkSize }; ComputeEvent clevent; error = CL10.SetKernelArg(ckReorderingKeyValue, 0, ptrSize, inputKey); CheckErr(error, "CL10.SetKernelArg"); error = CL10.SetKernelArg(ckReorderingKeyValue, 1, ptrSize, outputKey); CheckErr(error, "CL10.SetKernelArg"); error = CL10.SetKernelArg(ckReorderingKeyValue, 2, ptrSize, inputValue); CheckErr(error, "CL10.SetKernelArg"); error = CL10.SetKernelArg(ckReorderingKeyValue, 3, ptrSize, outputValue); CheckErr(error, "CL10.SetKernelArg"); error = CL10.SetKernelArg(ckReorderingKeyValue, 4, ptrSize, mCounters); CheckErr(error, "CL10.SetKernelArg"); error = CL10.SetKernelArg(ckReorderingKeyValue, 5, ptrSize, mRadixPrefixes); CheckErr(error, "CL10.SetKernelArg"); error = CL10.SetKernelArg(ckReorderingKeyValue, 6, (IntPtr)(gpuConstants.numGroupsPerBlock * gpuConstants.numBlocks * gpuConstants.numRadicesPerBlock * 4), null); CheckErr(error, "CL10.SetKernelArg"); error = CL10.SetKernelArg(ckReorderingKeyValue, 7, (IntPtr)(gpuConstants.numRadices * 4), null); CheckErr(error, "CL10.SetKernelArg"); error = CL10.SetKernelArg(ckReorderingKeyValue, 8, (IntPtr)(Marshal.SizeOf(typeof(GPUConstants))), gpuConstants); CheckErr(error, "CL10.SetKernelArg"); error = CL10.SetKernelArg(ckReorderingKeyValue, 9, (IntPtr)4, bitOffset); CheckErr(error, "CL10.SetKernelArg"); error = CL10.EnqueueNDRangeKernel(cqCommandQueue, ckReorderingKeyValue, 1, null, workGroupSizePtr, localWorkGroupSizePtr, 0, null, out clevent); CheckErr(error, "CL10.EnqueueNDRangeKernel"); error = CL10.Finish(cqCommandQueue); CheckErr(error, "CL10.Finish"); if (DEBUG) { if (DEBUG_CONSOLE_OUTPUT) { Console.WriteLine("-------------------------------Reordering-------------------------------------------------"); } ComputeEvent eve; if (DEBUG_CONSOLE_OUTPUT) { Console.WriteLine(" Input "); } CL10.EnqueueReadBuffer(cqCommandQueue, inputKey, Bool.True, IntPtr.Zero, (IntPtr)(gpuConstants.numTotalElements * 4), debugRead, 0, null, out eve); CheckErr(error, "CL10.EnqueueReadBuffer"); PrintElementBuffer(debugRead, gpuConstants.numTotalElements, "Reordering -> Input -> bitoffset = " + bitOffset); CL10.EnqueueReadBuffer(cqCommandQueue, inputValue, Bool.True, IntPtr.Zero, (IntPtr)(gpuConstants.numTotalElements * 4), debugRead, 0, null, out eve); CheckErr(error, "CL10.EnqueueReadBuffer"); PrintElementBuffer(debugRead, gpuConstants.numTotalElements, "Reordering -> InputValues -> bitoffset = " + bitOffset); if (DEBUG_CONSOLE_OUTPUT) { Console.WriteLine(" Counters "); } CL10.EnqueueReadBuffer(cqCommandQueue, mCounters, Bool.True, IntPtr.Zero, (IntPtr)(numCounters * sizeof(int)), debugRead, 0, null, out eve); CheckErr(error, "CL10.EnqueueReadBuffer"); PrintCounterBuffer(debugRead, "Reordering -> bitoffset = " + bitOffset); if (DEBUG_CONSOLE_OUTPUT) { Console.WriteLine(" Counters "); } CL10.EnqueueReadBuffer(cqCommandQueue, mRadixPrefixes, Bool.True, IntPtr.Zero, (IntPtr)(gpuConstants.numRadices * sizeof(int)), debugRead, 0, null, out eve); CheckErr(error, "CL10.EnqueueReadBuffer"); PrintElementBuffer(debugRead, gpuConstants.numRadices, "Reordering -> RadixPrefixe -> bitoffset = " + bitOffset); if (DEBUG_CONSOLE_OUTPUT) { Console.WriteLine(" Output "); } CL10.EnqueueReadBuffer(cqCommandQueue, outputKey, Bool.True, IntPtr.Zero, (IntPtr)(gpuConstants.numTotalElements * 4), debugRead, 0, null, out eve); CheckErr(error, "CL10.EnqueueReadBuffer"); PrintElementBuffer(debugRead, gpuConstants.numTotalElements, "Reordering -> Output -> bitoffset = " + bitOffset); CL10.EnqueueReadBuffer(cqCommandQueue, outputValue, Bool.True, IntPtr.Zero, (IntPtr)(gpuConstants.numTotalElements * 4), debugRead, 0, null, out eve); CheckErr(error, "CL10.EnqueueReadBuffer"); PrintElementBuffer(debugRead, gpuConstants.numTotalElements, "Reordering -> OutputValue -> bitoffset = " + bitOffset); if (DEBUG_CONSOLE_OUTPUT) { Console.WriteLine("Reordering -> bitoffset = " + bitOffset); } if (DEBUG_CONSOLE_OUTPUT) { Console.WriteLine(); } } ; }
public void sortKeysValue(CLMemoryHandle inputKey, CLMemoryHandle outputKey, CLMemoryHandle inputValue, CLMemoryHandle outputValue, int numElements) { debugRead = new int[Math.Max(numElements, numCounters)]; ComputeErrorCode error; ComputeEvent eve; mCounters = CL10.CreateBuffer(cxGPUContext, ComputeMemoryFlags.ReadWrite, (IntPtr)(gpuConstants.numGroupsPerBlock * gpuConstants.numRadices * gpuConstants.numBlocks * sizeof(int)), debugRead, out error); CheckErr(error, "CL10.CreateBuffer"); mRadixPrefixes = CL10.CreateBuffer(cxGPUContext, ComputeMemoryFlags.ReadWrite, gpuConstants.numRadices * sizeof(int), out error); CheckErr(error, "CL10.CreateBuffer"); /* * error = CL10.EnqueueReadBuffer(cqCommandQueue, input, Bool.True, IntPtr.Zero, (IntPtr)(numElements * 4), * debugRead, 0, null, out eve); * CheckErr(error, "CL10.EnqueueReadBuffer"); */ if (DEBUG) { CL10.EnqueueReadBuffer(cqCommandQueue, inputKey, Bool.True, IntPtr.Zero, (IntPtr)(gpuConstants.numTotalElements * 4), debugRead, 0, null, out eve); CheckErr(error, "CL10.EnqueueReadBuffer"); PrintAsArray(debugRead, gpuConstants.numTotalElements); } gpuConstants.numElementsPerGroup = (numElements / (gpuConstants.numBlocks * gpuConstants.numGroupsPerBlock)) + 1; gpuConstants.numTotalElements = numElements; int i; for (i = 0; i < 8; i++) { error = CL10.EnqueueWriteBuffer(cqCommandQueue, mCounters, Bool.True, IntPtr.Zero, (IntPtr)(numCounters * 4), counters, 0, null, out eve); CheckErr(error, "CL10.EnqueueWriteBuffer Counter initialize"); if (i % 2 == 0) { SetupAndCount(inputKey, 4 * i); SumIt(inputKey, 4 * i); ReorderingKeyValue(inputKey, outputKey, inputValue, outputValue, 4 * i); } else { SetupAndCount(outputKey, 4 * i); SumIt(outputKey, 4 * i); ReorderingKeyValue(outputKey, inputKey, outputValue, inputValue, 4 * i); } } if (i % 2 != 0) { error = CL10.EnqueueCopyBuffer(cqCommandQueue, inputKey, outputKey, IntPtr.Zero, IntPtr.Zero, (IntPtr)(numElements * 4), 0, null, out eve); CheckErr(error, "CL10.EnqueueCopyBuffer"); error = CL10.Finish(cqCommandQueue); CheckErr(error, "CL10.Finish Copybuffer"); error = CL10.EnqueueCopyBuffer(cqCommandQueue, inputValue, outputValue, IntPtr.Zero, IntPtr.Zero, (IntPtr)(numElements * 8), 0, null, out eve); CheckErr(error, "CL10.EnqueueCopyBuffer"); error = CL10.Finish(cqCommandQueue); CheckErr(error, "CL10.Finish Copybuffer"); } error = CL10.ReleaseMemObject(mRadixPrefixes); CheckErr(error, "CL10.ReleaseMemObj"); error = CL10.ReleaseMemObject(mCounters); CheckErr(error, "CL10.ReleaseMemObj"); Log_Idx++; }
public void sortKeysOnly(CLMemoryHandle input, CLMemoryHandle output, int numElements) { debugRead = new int[Math.Max(numElements, numCounters)]; ComputeErrorCode error; Compute ComputeEvent eve; mCounters = CL10.CreateBuffer(cxGPUContext, ComputeMemoryFlags.ReadWrite, gpuConstants.numGroupsPerBlock * gpuConstants.numRadices * gpuConstants.numBlocks * sizeof(int), out error); CheckErr(error, "CL10.CreateBuffer"); mRadixPrefixes = CL10.CreateBuffer(cxGPUContext, ComputeMemoryFlags.ReadWrite, gpuConstants.numRadices * sizeof(int), out error); CheckErr(error, "CL10.CreateBuffer"); gpuConstants.numElementsPerGroup = (numElements / (gpuConstants.numBlocks * gpuConstants.numGroupsPerBlock)) + 1; gpuConstants.numTotalElements = numElements; if (DEBUG) { CL10.EnqueueReadBuffer(cqCommandQueue, input, Bool.True, IntPtr.Zero, (IntPtr)(gpuConstants.numTotalElements * 4), debugRead, 0, null, out eve); CheckErr(error, "CL10.EnqueueReadBuffer"); PrintAsArray(debugRead, gpuConstants.numTotalElements); } int i; for (i = 0; i < 8; i++) { error = CL10.EnqueueWriteBuffer(cqCommandQueue, mCounters, true, IntPtr.Zero, (IntPtr)(numCounters * 4), counters, 0, null, out eve); CheckErr(error, "CL10.EnqueueWriteBuffer Counter initialize"); if (i % 2 == 0) { DateTime before = DateTime.Now; SetupAndCount(input, 4 * i); if (DEBUG_CONSOLE_OUTPUT) { Console.WriteLine("Setup and Count =" + (DateTime.Now - before).TotalMilliseconds); } before = DateTime.Now; SumIt(input, 4 * i); if (DEBUG_CONSOLE_OUTPUT) { Console.WriteLine("SumIt =" + (DateTime.Now - before).TotalMilliseconds); } before = DateTime.Now; ReorderingKeysOnly(input, output, 4 * i); if (DEBUG_CONSOLE_OUTPUT) { Console.WriteLine("Reorder =" + (DateTime.Now - before).TotalMilliseconds); } } else { SetupAndCount(output, 4 * i); SumIt(output, 4 * i); ReorderingKeysOnly(output, input, 4 * i); } } if (i % 2 != 0) { error = CL10.EnqueueCopyBuffer(cqCommandQueue, input, output, IntPtr.Zero, IntPtr.Zero, (IntPtr)(numElements * 4), 0, null, out eve); CheckErr(error, "CL10.EnqueueCopyBuffer"); error = CL10.Finish(cqCommandQueue); CheckErr(error, "CL10.Finish Copybuffer"); } error = CL10.ReleaseMemObject(mRadixPrefixes); CheckErr(error, "CL10.ReleaseMemObj"); error = CL10.ReleaseMemObject(mCounters); CheckErr(error, "CL10.ReleaseMemObj"); Log_Idx++; }