/// <summary> /// Enqueues a command to copy data between buffers. /// </summary> /// <typeparam name="T"> The type of data in the buffers. </typeparam> /// <param name="source"> The buffer to copy from. </param> /// <param name="destination"> The buffer to copy to. </param> /// <param name="sourceOffset"> The <paramref name="source"/> element position where reading starts. </param> /// <param name="destinationOffset"> The <paramref name="destination"/> element position where writing starts. </param> /// <param name="region"> The region of elements to copy. </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> a new event identifying this command is attached to the end of the collection. </param> public void Copy <T>(ComputeBufferBase <T> source, ComputeBufferBase <T> destination, long sourceOffset, long destinationOffset, long region, 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.EnqueueCopyBuffer(Handle, source.Handle, destination.Handle, new IntPtr(sourceOffset * sizeofT), new IntPtr(destinationOffset * sizeofT), new IntPtr(region * sizeofT), eventWaitListSize, eventHandles, newEventHandle); ComputeException.ThrowOnError(error); if (eventsWritable) { events.Add(new ComputeEvent(newEventHandle[0], this)); } }
/// <summary> /// Enqueues a command to copy data between buffers. /// </summary> /// <typeparam name="T"> The type of data in the buffers. </typeparam> /// <param name="source"> The buffer to copy from. </param> /// <param name="destination"> The buffer to copy to. </param> /// <param name="sourceOffset"> The <paramref name="source"/> element position where reading starts. </param> /// <param name="destinationOffset"> The <paramref name="destination"/> element position where writing starts. </param> /// <param name="region"> The region of elements to copy. </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> a new event identifying this command is attached to the end of the collection. </param> public void Copy(OpenCLBufferBase source, OpenCLBufferBase destination, long sourceOffset, long destinationOffset, long region, 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.EnqueueCopyBuffer(Handle, source.Handle, destination.Handle, new IntPtr(sourceOffset * sizeofT), new IntPtr(destinationOffset * sizeofT), new IntPtr(region * sizeofT), eventWaitListSize, eventHandles, newEventHandle); OpenCLException.ThrowOnError(error); if (newEvents != null) { lock (newEvents) { newEvents.Add(new OpenCLEvent(newEventHandle[0], this)); } } }
public void sortKeysValue(CLMemoryHandle key, CLMemoryHandle value, int numElements) { debugRead = new int[Math.Max(numElements, numCounters)]; ComputeErrorCode error; ComputeEvent eve; /* * error = CL10.EnqueueReadBuffer(cqCommandQueue, input, Bool.True, IntPtr.Zero, (IntPtr)(numElements * 4), * debugRead, 0, null, out eve); * CheckErr(error, "CL10.EnqueueReadBuffer"); */ 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"); CLMemoryHandle outputValue = CL10.CreateBuffer(cxGPUContext, ComputeMemoryFlags.ReadWrite, (IntPtr)(8 * numElements), out error); CheckErr(error, "CL10.CreateBuffer"); CLMemoryHandle outputKey = CL10.CreateBuffer(cxGPUContext, ComputeMemoryFlags.ReadWrite, (IntPtr)(4 * numElements), out error); CheckErr(error, "CL10.CreateBuffer"); 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(key, 4 * i); SumIt(key, 4 * i); ReorderingKeyValue(key, outputKey, value, outputValue, 4 * i); } else { SetupAndCount(outputKey, 4 * i); SumIt(outputKey, 4 * i); ReorderingKeyValue(outputKey, key, outputValue, value, 4 * i); } } if (i % 2 == 0) { error = CL10.EnqueueCopyBuffer(cqCommandQueue, outputKey, key, 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, outputValue, value, 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(outputKey); CheckErr(error, "CL10.ReleaseMemObj"); error = CL10.ReleaseMemObject(outputValue); CheckErr(error, "CL10.ReleaseMemObj"); 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++; }