public void initialize(ComputeContext computeContext, int kernelPositionsLength, Misc.Vector2<int> inputMapSize) { ErrorCode errorCode; OpenCL.Net.Event eventWriteBufferCompletedKernel; this.kernelPositionsLength = kernelPositionsLength; kernelResults = new float[kernelPositionsLength]; bufferForPositions = Cl.CreateBuffer<int>(computeContext.context, MemFlags.AllocHostPtr, 2 * kernelPositionsLength, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); bufferForInputMap = Cl.CreateBuffer<float>(computeContext.context, MemFlags.AllocHostPtr, (int)(inputMapSize.x * inputMapSize.y), out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); OpenCL.Net.IMem<float> bufferForKernel = Cl.CreateBuffer<float>(computeContext.context, MemFlags.AllocHostPtr, (kernelWidth * kernelWidth), out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); bufferForKernelResults = Cl.CreateBuffer<float>(computeContext.context, MemFlags.AllocHostPtr, kernelPositionsLength, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.EnqueueWriteBuffer<float>(computeContext.commandQueue, bufferForKernel, OpenCL.Net.Bool.True, this.kernelArray, 0, null, out eventWriteBufferCompletedKernel); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); program = Cl.CreateProgramWithSource(computeContext.context, 1, new[] { getProgramSource(inputMapSize.x, kernelPositionsLength) }, null, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.BuildProgram(program, 1, new[] { computeContext.chosenDevice }, "", null, IntPtr.Zero); if (errorCode != ErrorCode.Success) { OpenCL.Net.InfoBuffer logInfoBuffer = Cl.GetProgramBuildInfo(program, computeContext.chosenDevice, ProgramBuildInfo.Log, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); throw new ComputeContext.OpenClError(); } kernel = Cl.CreateKernel(program, "kernel0", out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg<float>(kernel, 0, bufferForInputMap); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg<float>(kernel, 1, bufferForKernel); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg<int>(kernel, 2, bufferForPositions); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg<float>(kernel, 3, bufferForKernelResults); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); Cl.ReleaseEvent(eventWriteBufferCompletedKernel); }
public void calculate(ComputeContext computeContext) { ErrorCode errorCode; int lengthOfPositionsForOpenCl; int numberOfPositionsForOpenCl; OpenCL.Net.Event eventWriteBufferInputPositions; OpenCL.Net.Event eventWriteBufferForInputMap; OpenCL.Net.Event eventExecutedKernelNearestPoint; OpenCL.Net.Event eventReadBufferForResultPositions; OpenCL.Net.Event eventReadBufferForFoundNewPosition; numberOfPositionsForOpenCl = inputPositions.Length; numberOfPositionsForOpenCl = numberOfPositionsForOpenCl + 32 + (32 - (numberOfPositionsForOpenCl % 32)); lengthOfPositionsForOpenCl = numberOfPositionsForOpenCl * 2; if (lengthOfPositionsForOpenCl > numberOfAllocatedInputAndOutputPositions*2) { // we need to reallocate the buffer to the right size numberOfAllocatedInputAndOutputPositions = lengthOfPositionsForOpenCl; Cl.ReleaseMemObject(bufferForInputPositions); Cl.ReleaseMemObject(bufferForOutputPositions); Cl.ReleaseMemObject(bufferForFoundNewPosition); bufferForInputPositions = Cl.CreateBuffer<int>(computeContext.context, MemFlags.AllocHostPtr, numberOfAllocatedInputAndOutputPositions * 2, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); bufferForOutputPositions = Cl.CreateBuffer<int>(computeContext.context, MemFlags.AllocHostPtr, numberOfAllocatedInputAndOutputPositions * 2, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); bufferForFoundNewPosition = Cl.CreateBuffer<int>(computeContext.context, MemFlags.AllocHostPtr, numberOfAllocatedInputAndOutputPositions, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); } // copy the positions int[] inputPositionsForOpenCl; inputPositionsForOpenCl = new int[lengthOfPositionsForOpenCl]; int i; for (i = 0; i < inputPositions.Length; i++ ) { inputPositionsForOpenCl[i * 2 + 0] = inputPositions[i].x; inputPositionsForOpenCl[i * 2 + 1] = inputPositions[i].y; } errorCode = Cl.EnqueueWriteBuffer<int>(computeContext.commandQueue, bufferForInputPositions, OpenCL.Net.Bool.False, inputPositionsForOpenCl, 0, new Event[] { }, out eventWriteBufferInputPositions); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); // copy input map int[] translatedInputMap; translatedInputMap = new int[inputMap.getWidth() * inputMap.getLength()]; for (i = 0; i < inputMap.getWidth() * inputMap.getLength(); i++ ) { if( inputMap.unsafeGetValues()[i] ) { translatedInputMap[i] = 1; } } errorCode = Cl.EnqueueWriteBuffer<int>(computeContext.commandQueue, bufferForInputMap, OpenCL.Net.Bool.False, translatedInputMap, 0, new Event[] { }, out eventWriteBufferForInputMap); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); //errorCode = Cl.SetKernelArg(kernelNearestPoint, 5, (IntPtr)4, /*lengthOfPositionsForOpenCl*/inputPositions.Length); // number of relative positions //ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); // call kernel IntPtr[] globalWorkSize = new IntPtr[] { (IntPtr)32, (IntPtr)(numberOfPositionsForOpenCl/32) }; IntPtr[] localWorkSize = new IntPtr[] { (IntPtr)32, (IntPtr)1 }; errorCode = Cl.EnqueueNDRangeKernel(computeContext.commandQueue, kernelNearestPoint, 2, null, globalWorkSize, localWorkSize, 2, new Event[] { eventWriteBufferInputPositions, eventWriteBufferForInputMap }, out eventExecutedKernelNearestPoint); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); int[] resultPositionsFromOpenCl; int[] resultfoundNewPositionsFromOpenCl; resultPositionsFromOpenCl = new int[lengthOfPositionsForOpenCl]; resultfoundNewPositionsFromOpenCl = new int[lengthOfPositionsForOpenCl / 2]; // read results back and copy into arrays errorCode = Cl.EnqueueReadBuffer(computeContext.commandQueue, bufferForOutputPositions, OpenCL.Net.Bool.False, resultPositionsFromOpenCl, 1, new[] { eventExecutedKernelNearestPoint }, out eventReadBufferForResultPositions); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.EnqueueReadBuffer(computeContext.commandQueue, bufferForFoundNewPosition, OpenCL.Net.Bool.False, resultfoundNewPositionsFromOpenCl, 1, new[] { eventExecutedKernelNearestPoint }, out eventReadBufferForFoundNewPosition); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.Flush(computeContext.commandQueue); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.WaitForEvents(2, new Event[] { eventReadBufferForResultPositions, eventReadBufferForFoundNewPosition }); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); // copy back outputPositions = new Vector2<int>[inputPositions.Length]; foundNewPositions = new bool[inputPositions.Length]; for( i = 0; i < inputPositions.Length; i++ ) { // check output position // NOTE< works only if kernel is prepared > { Vector2<int> inputPosition = inputPositions[i]; Vector2<int> outputPosition2; outputPosition2 = new Vector2<int>(); outputPosition2.x = resultPositionsFromOpenCl[i * 2 + 0]; outputPosition2.y = resultPositionsFromOpenCl[i * 2 + 1]; if (System.Math.Abs(outputPosition2.x - inputPosition.x) > 20 || System.Math.Abs(outputPosition2.y - inputPosition.y) > 20) { int here0 = 0; } } Vector2<int> outputPosition; outputPosition = new Vector2<int>(); outputPosition.x = resultPositionsFromOpenCl[i * 2 + 0]; outputPosition.y = resultPositionsFromOpenCl[i * 2 + 1]; outputPositions[i] = outputPosition; if( resultfoundNewPositionsFromOpenCl[i] == 1 ) { foundNewPositions[i] = true; } } Cl.ReleaseEvent(eventWriteBufferInputPositions); Cl.ReleaseEvent(eventWriteBufferForInputMap); Cl.ReleaseEvent(eventExecutedKernelNearestPoint); Cl.ReleaseEvent(eventReadBufferForResultPositions); Cl.ReleaseEvent(eventReadBufferForFoundNewPosition); }
public void initialize(ComputeContext computeContext, int kernelRadius, Misc.Vector2<int> inputMapSize) { ErrorCode errorCode; float[] kernelArray; OpenCL.Net.Event eventWriteBufferCompletedKernel; bufferForInputMap = Cl.CreateBuffer<float>(computeContext.context, MemFlags.AllocHostPtr, (int)(inputMapSize.x * inputMapSize.y), out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); bufferForTemporary = Cl.CreateBuffer<float>(computeContext.context, MemFlags.AllocHostPtr, (int)(inputMapSize.x * inputMapSize.y), out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); bufferForOutputMap = Cl.CreateBuffer<float>(computeContext.context, MemFlags.AllocHostPtr, (int)(inputMapSize.x * inputMapSize.y), out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); kernelArray = calculateKernel(kernelRadius); bufferForKernelArray = Cl.CreateBuffer<float>(computeContext.context, MemFlags.AllocHostPtr, kernelArray.Length, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); // copy kernel into buffer errorCode = Cl.EnqueueWriteBuffer<float>(computeContext.commandQueue, bufferForKernelArray, OpenCL.Net.Bool.True, kernelArray, 0, null, out eventWriteBufferCompletedKernel); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); string programLocation = Assembly.GetEntryAssembly().Location; string pathToLoad = Path.Combine(Path.GetDirectoryName(programLocation), "..\\..\\", "ComputationBackend\\OpenCl\\src\\Blur.cl"); string openClSource = File.ReadAllText(pathToLoad); program = Cl.CreateProgramWithSource(computeContext.context, 1, new[] { openClSource }, null, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.BuildProgram(program, 1, new[] { computeContext.chosenDevice }, "", null, IntPtr.Zero); if (errorCode != ErrorCode.Success) { OpenCL.Net.InfoBuffer logInfoBuffer = Cl.GetProgramBuildInfo(program, computeContext.chosenDevice, ProgramBuildInfo.Log, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); throw new ComputeContext.OpenClError(); } kernelBlurX = Cl.CreateKernel(program, "blurX", out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); //errorCode = Cl.SetKernelArg<float>(kernelBlurX, 0, bufferForInputMap); //ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg<float>(kernelBlurX, 1, bufferForKernelArray); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); //errorCode = Cl.SetKernelArg<float>(kernelBlurX, 2, bufferForTemporary); //ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg(kernelBlurX, 3, (IntPtr)4, kernelRadius); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg(kernelBlurX, 4, (IntPtr)4, inputMapSize.x); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); kernelBlurY = Cl.CreateKernel(program, "blurY", out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); //errorCode = Cl.SetKernelArg<float>(kernelBlurY, 0, bufferForTemporary); //ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg<float>(kernelBlurY, 1, bufferForKernelArray); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); //errorCode = Cl.SetKernelArg<float>(kernelBlurY, 2, bufferForOutputMap); //ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg(kernelBlurY, 3, (IntPtr)4, kernelRadius); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg(kernelBlurY, 4, (IntPtr)4, inputMapSize.x); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg(kernelBlurY, 5, (IntPtr)4, inputMapSize.y); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); Cl.ReleaseEvent(eventWriteBufferCompletedKernel); }
public void initialize(ComputeContext computeContext, int searchRadius, Misc.Vector2<int> inputMapSize) { List<Vector2<int>> relativePositions; ErrorCode errorCode; int[] relativePositionsArray; OpenCL.Net.Event eventWriteBufferForRelativePositions; relativePositions = calculateRelativePositionsForRadius(searchRadius); numberOfAllocatedInputAndOutputPositions = 50000; bufferForInputMap = Cl.CreateBuffer<int>(computeContext.context, MemFlags.AllocHostPtr, (int)(inputMapSize.x * inputMapSize.y), out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); bufferForRelativePositions = Cl.CreateBuffer<int>(computeContext.context, MemFlags.AllocHostPtr, relativePositions.Count * 2, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); relativePositionsArray = convertRelativePositionsToArray(relativePositions); // copy relative positions into buffer errorCode = Cl.EnqueueWriteBuffer<int>(computeContext.commandQueue, bufferForRelativePositions, OpenCL.Net.Bool.True, relativePositionsArray, 0, new Event[] { }, out eventWriteBufferForRelativePositions); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); bufferForInputPositions = Cl.CreateBuffer<int>(computeContext.context, MemFlags.AllocHostPtr, numberOfAllocatedInputAndOutputPositions * 2, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); bufferForOutputPositions = Cl.CreateBuffer<int>(computeContext.context, MemFlags.AllocHostPtr, numberOfAllocatedInputAndOutputPositions * 2, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); bufferForFoundNewPosition = Cl.CreateBuffer<int>(computeContext.context, MemFlags.AllocHostPtr, numberOfAllocatedInputAndOutputPositions, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); program = Cl.CreateProgramWithSource(computeContext.context, 1, new[] { getOpenClSource() }, null, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.BuildProgram(program, 1, new[] { computeContext.chosenDevice }, "", null, IntPtr.Zero); if (errorCode != ErrorCode.Success) { OpenCL.Net.InfoBuffer logInfoBuffer = Cl.GetProgramBuildInfo(program, computeContext.chosenDevice, ProgramBuildInfo.Log, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); throw new ComputeContext.OpenClError(); } kernelNearestPoint = Cl.CreateKernel(program, "findNearestPoint", out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg<int>(kernelNearestPoint, 0, bufferForInputMap); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg<int>(kernelNearestPoint, 1, bufferForRelativePositions); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg<int>(kernelNearestPoint, 2, bufferForInputPositions); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg<int>(kernelNearestPoint, 3, bufferForOutputPositions); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg<int>(kernelNearestPoint, 4, bufferForFoundNewPosition); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg(kernelNearestPoint, 5, (IntPtr)4, relativePositions.Count); // number of relative positions ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg(kernelNearestPoint, 6, (IntPtr)4, inputMapSize.x); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg(kernelNearestPoint, 7, (IntPtr)4, inputMapSize.y); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); Cl.ReleaseEvent(eventWriteBufferForRelativePositions); }
public void initialize(ComputeContext computeContext, Misc.Vector2<int> inputMapSize) { ErrorCode errorCode; bufferForCounterMap = Cl.CreateBuffer<int>(computeContext.context, MemFlags.AllocHostPtr, (int)(inputMapSize.x * inputMapSize.y), out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); bufferForCounterOutputMap = Cl.CreateBuffer<int>(computeContext.context, MemFlags.AllocHostPtr, (int)(inputMapSize.x * inputMapSize.y), out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); bufferForChangeMade = Cl.CreateBuffer<int>(computeContext.context, MemFlags.AllocHostPtr, 1, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); program = Cl.CreateProgramWithSource(computeContext.context, 1, new[] { getOpenClSource() }, null, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.BuildProgram(program, 1, new[] { computeContext.chosenDevice }, "", null, IntPtr.Zero); if (errorCode != ErrorCode.Success) { OpenCL.Net.InfoBuffer logInfoBuffer = Cl.GetProgramBuildInfo(program, computeContext.chosenDevice, ProgramBuildInfo.Log, out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); throw new ComputeContext.OpenClError(); } kernelNarrow = Cl.CreateKernel(program, "narrow", out errorCode); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg(kernelNarrow, 4, (IntPtr)4, inputMapSize.x); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); errorCode = Cl.SetKernelArg(kernelNarrow, 5, (IntPtr)4, inputMapSize.y); ComputeContext.throwErrorIfNotSuccessfull(errorCode, ""); }