public static void WriterValues(this Context oclContext, CommandQueue oclCQ, CL.Mem oclBuff, int[] values) { oclCQ.EnqueueWriteBuffer(oclBuff, true, 0, values.Length, values.ToIntPtr()); oclCQ.EnqueueBarrier(); oclCQ.Finish(); return; }
/* возвращает буфер */ public unsafe Mem CreateBuffer() { if (buffer == null) { /* указатели в C# используются с fixed (противодействие для GC) */ fixed(short *dataptr = data) { buffer = manager_this.Context.CreateBuffer(MemFlags.COPY_HOST_PTR, data.Count() * 2, new IntPtr(dataptr)); } } return(buffer); }
public static int[] ToInts(this Context oclContext, CommandQueue oclCQ, CL.Mem oclBuff, int Len) { int[] Ints = new int[Len]; Mem buffer = oclContext.CreateBuffer((MemFlags.WRITE_ONLY | MemFlags.USE_HOST_PTR), Len * 4, Ints.ToIntPtr()); oclCQ.EnqueueCopyBuffer(oclBuff, buffer, 0, 0, Len); //oclCQ.EnqueueReadBuffer(oclBuff, true, 0, Len, Ints.ToIntPtr()); oclCQ.EnqueueBarrier(); IntPtr p = oclCQ.EnqueueMapBuffer(buffer, true, MapFlags.READ, 0, Len); oclCQ.EnqueueUnmapMemObject(buffer, p); oclCQ.Finish(); buffer.Dispose(); return(Ints); }
/* private unsafe Mem GetCounter() { fixed (ulong* dataptr = &voxelctr) { counter = manager.Context.CreateBuffer(MemFlags.READ_WRITE, 8, new IntPtr(dataptr)); } return counter; }*/ private unsafe void DoRayCasting(BitmapData output) { try { int deviceIndex = 0; outputBuffer = manager.Context.CreateBuffer(MemFlags.USE_HOST_PTR, output.Stride * output.Height, output.Scan0); if (first || changeDistance) { // модель камеры UVN camPos = new Float4() { S0 = vol.GetSize() / 2 - (float)Math.Cos(camAngle * Math.PI / 180) * camDist, S1 = vol.GetSize() / 2, S2 = vol.GetSize() / 2 - (float)Math.Sin(camAngle * Math.PI / 180) * camDist, S3 = 0 }; first = false; changeDistance = false; camPosOld = camPos; } else { // поворот вокруг оси куба визуализации if (angleChange && leftChange) { camPosOld.S0 -= camLookAt.S0; camPosOld.S2 -= camLookAt.S2; camPos.S0 = (float)Math.Cos(camAngle * Math.PI / 180) * camPosOld.S0 + (float)Math.Sin(camAngle * Math.PI / 180) * camPosOld.S2; camPos.S1 = vol.GetSize() / 2; camPos.S2 = -(float)Math.Sin(camAngle * Math.PI / 180) * camPosOld.S0 + (float)Math.Cos(camAngle * Math.PI / 180) * camPosOld.S2; camPos.S3 = 0; camPos.S0 += camLookAt.S0; camPos.S2 += camLookAt.S2; camPosOld = camPos; angleChange = false; leftChange = false; } if (angleChange && rightChange) { camPosOld.S0 -= camLookAt.S0; camPosOld.S2 -= camLookAt.S2; camPos.S0 = (float)Math.Cos(camAngle * Math.PI / 180) * camPosOld.S0 - (float)Math.Sin(camAngle * Math.PI / 180) * camPosOld.S2; camPos.S1 = vol.GetSize() / 2; camPos.S2 = (float)Math.Sin(camAngle * Math.PI / 180) * camPosOld.S0 + (float)Math.Cos(camAngle * Math.PI / 180) * camPosOld.S2; camPos.S3 = 0; camPos.S0 += camLookAt.S0; camPos.S2 += camLookAt.S2; camPosOld = camPos; angleChange = false; leftChange = false; } } camLookAt = new Float4() { S0 = vol.GetSize() / camfactorX, S1 = vol.GetSize() / camfactorX, S2 = vol.GetSize() / camfactorZ, S3 = 0 }; //light = camPos; // направление камеры, UVN модель camForward = camLookAt.Sub(camPos).Normalize(); // направление просмотра var up = new Float4(0.0f, 1.0f, 0.0f, 0.0f); var right = MathClass.Cross(up, camForward).Normalize().Times(1.5f); up = MathClass.Cross(camForward, right).Normalize().Times(-1.5f); /* обработка выходного изображения BitmapData в OpenCl устройстве */ for (var x = 0; x < output.Width; x += blocksize) { for (var y = 0; y < output.Height; y += blocksize) { var rayTracingGlobalWorkSize = new IntPtr[2]; // work_dim = 2 rayTracingGlobalWorkSize[0] = (IntPtr)(output.Width - x > blocksize ? blocksize : output.Width - x); rayTracingGlobalWorkSize[1] = (IntPtr)(output.Height - y > blocksize ? blocksize : output.Height - y); var rayTracingGlobalOffset = new IntPtr[2]; rayTracingGlobalOffset[0] = (IntPtr)x; rayTracingGlobalOffset[1] = (IntPtr)y; float ka = (float)(Convert.ToDouble(kamb.Text)); float kd = (float)(Convert.ToDouble(kdiff.Text)); float ks = (float)(Convert.ToDouble(kspec.Text)); float exp = (float)(Convert.ToDouble(specexp.Text)); float kkc = (float)(Convert.ToDouble(this.kc.Text)); float kkl = (float)(Convert.ToDouble(this.kl.Text)); float kkq = (float)(Convert.ToDouble(this.kq.Text)); /* передали аргументы в kernel функцию */ kernel.SetArg(0, output.Width); kernel.SetArg(1, output.Height); kernel.SetArg(2, outputBuffer); // в ядре с global, поскольку для выполнения требуется доступ к output kernel.SetArg(3, output.Stride); kernel.SetArg(4, camPos); kernel.SetArg(5, camForward); kernel.SetArg(6, right); kernel.SetArg(7, up); kernel.SetArg(8, vol.CreateBuffer()); kernel.SetArg(9, vol.GetSize()); kernel.SetArg(10, light); kernel.SetArg(11, boxMinCon); kernel.SetArg(12, boxMaxCon); kernel.SetArg(13, Convert.ToInt16(colorMi.Text)); kernel.SetArg(14, Convert.ToInt16(colorMa.Text)); kernel.SetArg(15, _cutArrea.Checked ? (short)1 : (short)0); kernel.SetArg(16, _trilinear.Checked ? (short)1 : (short)0); kernel.SetArg(17, tf.Checked ? (short)1: (short)0 ); kernel.SetArg(18, GetColors()); kernel.SetArg(19, winWidth_vox); kernel.SetArg(20, winCentre_vox); kernel.SetArg(21, form_this.knots_counter); kernel.SetArg(22, Convert.ToInt16(colorMi2.Text)); kernel.SetArg(23, Convert.ToInt16(colorMa2.Text)); kernel.SetArg(24, GetOpacity()); kernel.SetArg(25, ka); kernel.SetArg(26, kd); kernel.SetArg(27, ks); kernel.SetArg(28, exp); kernel.SetArg(29, kkc); kernel.SetArg(30, kkl); kernel.SetArg(31, kkq); //kernel.SetArg(32, GetCounter()); /* Ставит в очередь команду для исполнения kernel на устройстве */ /* rayTracingGlobalOffset - * globalWorkOffset: может использоваться для указания массива значений * размерности work_dim unsigned который описывает смещение используемое для расчета global ID work-item * вместо того чтобы global IDs всегда начинался со смещение (0, 0,... 0). * rayTracingGlobalWorkSize - * globalWorkSize: общее число global work-items вычисляется как global_work_size[0] *...* global_work_size[work_dim - 1]. * */ manager.CQ[deviceIndex].EnqueueNDRangeKernel(kernel, 2, rayTracingGlobalOffset, rayTracingGlobalWorkSize, null); } } /* подождали пока все work-items выполнятся */ manager.CQ[deviceIndex].EnqueueBarrier(); /* для того чтобы получить доступ к памяти и записать в выходное изображение мы просим у OpenCL *наложить* данные в хост-устройство */ IntPtr p = manager.CQ[deviceIndex].EnqueueMapBuffer(outputBuffer, true, MapFlags.READ, IntPtr.Zero, (IntPtr)(output.Stride * output.Height)); //IntPtr z = manager.CQ[deviceIndex].EnqueueMapBuffer(counter, true, MapFlags.READ_WRITE, IntPtr.Zero, (IntPtr)(sizeof(ulong))); /* когда мы заканчиваем работать с буфером надо вызвать эту функцию */ manager.CQ[deviceIndex].EnqueueUnmapMemObject(outputBuffer, p); //manager.CQ[deviceIndex].EnqueueUnmapMemObject(counter, z); manager.CQ[deviceIndex].Finish(); realctr += voxelctr; voxelCounter.Text = Convert.ToString(realctr); } catch (Exception ex) { MessageBox.Show("Ray casting exception:" + ex.Message, "Exception"); //Environment.Exit(-1); } finally { if (outputBuffer != null) { outputBuffer.Dispose(); } } }
public void SetMemArg(int argIndex, Mem mem) { SetIntPtrArg(argIndex, mem.MemID); }
unsafe public FLACCLTask(Program _openCLProgram, int channelsCount, int channels, uint bits_per_sample, int max_frame_size, FLACCLWriter writer, int groupSize, bool gpuOnly, bool gpuRice) { this.UseGPUOnly = gpuOnly; this.UseGPURice = gpuOnly && gpuRice; this.UseMappedMemory = writer._settings.MappedMemory || writer._settings.DeviceType == OpenCLDeviceType.CPU; this.groupSize = groupSize; this.channels = channels; this.channelsCount = channelsCount; this.writer = writer; openCLProgram = _openCLProgram; #if DEBUG var prop = CommandQueueProperties.PROFILING_ENABLE; #else var prop = CommandQueueProperties.NONE; #endif openCLCQ = openCLProgram.Context.CreateCommandQueue(openCLProgram.Context.Devices[0], prop); int MAX_ORDER = this.writer.eparams.max_prediction_order; int MAX_FRAMES = this.writer.framesPerTask; int MAX_CHANNELSIZE = MAX_FRAMES * ((writer.eparams.block_size + 3) & ~3); residualTasksLen = sizeof(FLACCLSubframeTask) * 32 * channelsCount * MAX_FRAMES; bestResidualTasksLen = sizeof(FLACCLSubframeTask) * channels * MAX_FRAMES; int samplesBufferLen = writer.PCM.BlockAlign * MAX_CHANNELSIZE * channelsCount; int residualBufferLen = sizeof(int) * MAX_CHANNELSIZE * channels; // need to adjust residualOffset? int partitionsLen = sizeof(int) * ((writer.PCM.BitsPerSample > 16 ? 31 : 15) * 2 << 8) * channels * MAX_FRAMES; int riceParamsLen = sizeof(int) * (4 << 8) * channels * MAX_FRAMES; int autocorLen = sizeof(float) * (MAX_ORDER + 1) * lpc.MAX_LPC_WINDOWS * channelsCount * MAX_FRAMES; int lpcDataLen = autocorLen * 32; int resOutLen = sizeof(int) * channelsCount * (lpc.MAX_LPC_WINDOWS * lpc.MAX_LPC_ORDER + 8) * MAX_FRAMES; int wndLen = sizeof(float) * MAX_CHANNELSIZE /** 2*/ * lpc.MAX_LPC_WINDOWS; int selectedLen = sizeof(int) * 32 * channelsCount * MAX_FRAMES; int riceLen = sizeof(int) * channels * MAX_CHANNELSIZE; if (!this.UseMappedMemory) { clSamplesBytes = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, samplesBufferLen / 2); clResidual = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, residualBufferLen); clBestRiceParams = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, riceParamsLen / 4); clResidualTasks = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, residualTasksLen); clBestResidualTasks = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, bestResidualTasksLen); clWindowFunctions = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, wndLen); clSelectedTasks = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, selectedLen); clRiceOutput = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, riceLen); clSamplesBytesPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, samplesBufferLen / 2); clResidualPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, residualBufferLen); clBestRiceParamsPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, riceParamsLen / 4); clResidualTasksPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, residualTasksLen); clBestResidualTasksPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, bestResidualTasksLen); clWindowFunctionsPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, wndLen); clSelectedTasksPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, selectedLen); clRiceOutputPinned = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, riceLen); clSamplesBytesPtr = openCLCQ.EnqueueMapBuffer(clSamplesBytesPinned, true, MapFlags.READ_WRITE, 0, samplesBufferLen / 2); clResidualPtr = openCLCQ.EnqueueMapBuffer(clResidualPinned, true, MapFlags.READ_WRITE, 0, residualBufferLen); clBestRiceParamsPtr = openCLCQ.EnqueueMapBuffer(clBestRiceParamsPinned, true, MapFlags.READ_WRITE, 0, riceParamsLen / 4); clResidualTasksPtr = openCLCQ.EnqueueMapBuffer(clResidualTasksPinned, true, MapFlags.READ_WRITE, 0, residualTasksLen); clBestResidualTasksPtr = openCLCQ.EnqueueMapBuffer(clBestResidualTasksPinned, true, MapFlags.READ_WRITE, 0, bestResidualTasksLen); clWindowFunctionsPtr = openCLCQ.EnqueueMapBuffer(clWindowFunctionsPinned, true, MapFlags.READ_WRITE, 0, wndLen); clSelectedTasksPtr = openCLCQ.EnqueueMapBuffer(clSelectedTasksPinned, true, MapFlags.READ_WRITE, 0, selectedLen); clRiceOutputPtr = openCLCQ.EnqueueMapBuffer(clRiceOutputPinned, true, MapFlags.READ_WRITE, 0, riceLen); } else { clSamplesBytes = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, (uint)samplesBufferLen / 2); clResidual = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, residualBufferLen); clBestRiceParams = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, riceParamsLen / 4); clResidualTasks = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, residualTasksLen); clBestResidualTasks = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, bestResidualTasksLen); clWindowFunctions = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, wndLen); clSelectedTasks = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, selectedLen); clRiceOutput = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE | MemFlags.ALLOC_HOST_PTR, riceLen); clSamplesBytesPtr = openCLCQ.EnqueueMapBuffer(clSamplesBytes, true, MapFlags.READ_WRITE, 0, samplesBufferLen / 2); clResidualPtr = openCLCQ.EnqueueMapBuffer(clResidual, true, MapFlags.READ_WRITE, 0, residualBufferLen); clBestRiceParamsPtr = openCLCQ.EnqueueMapBuffer(clBestRiceParams, true, MapFlags.READ_WRITE, 0, riceParamsLen / 4); clResidualTasksPtr = openCLCQ.EnqueueMapBuffer(clResidualTasks, true, MapFlags.READ_WRITE, 0, residualTasksLen); clBestResidualTasksPtr = openCLCQ.EnqueueMapBuffer(clBestResidualTasks, true, MapFlags.READ_WRITE, 0, bestResidualTasksLen); clWindowFunctionsPtr = openCLCQ.EnqueueMapBuffer(clWindowFunctions, true, MapFlags.READ_WRITE, 0, wndLen); clSelectedTasksPtr = openCLCQ.EnqueueMapBuffer(clSelectedTasks, true, MapFlags.READ_WRITE, 0, selectedLen); clRiceOutputPtr = openCLCQ.EnqueueMapBuffer(clRiceOutput, true, MapFlags.READ_WRITE, 0, riceLen); //clSamplesBytesPtr = clSamplesBytes.HostPtr; //clResidualPtr = clResidual.HostPtr; //clBestRiceParamsPtr = clBestRiceParams.HostPtr; //clResidualTasksPtr = clResidualTasks.HostPtr; //clBestResidualTasksPtr = clBestResidualTasks.HostPtr; //clWindowFunctionsPtr = clWindowFunctions.HostPtr; } clSamples = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, samplesBufferLen); clLPCData = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, lpcDataLen); clAutocorOutput = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, autocorLen); clSelectedTasksSecondEstimate = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, selectedLen); clSelectedTasksBestMethod = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, selectedLen); if (UseGPUOnly) { clPartitions = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, partitionsLen); clRiceParams = openCLProgram.Context.CreateBuffer(MemFlags.READ_WRITE, riceParamsLen); } //openCLCQ.EnqueueMapBuffer(clSamplesBytes, true, MapFlags.WRITE, 0, samplesBufferLen / 2); clComputeAutocor = openCLProgram.CreateKernel("clComputeAutocor"); clStereoDecorr = openCLProgram.CreateKernel("clStereoDecorr"); //cudaChannelDecorr = openCLProgram.CreateKernel("clChannelDecorr"); clChannelDecorr2 = openCLProgram.CreateKernel("clChannelDecorr2"); clChannelDecorrX = openCLProgram.CreateKernel("clChannelDecorrX"); clFindWastedBits = openCLProgram.CreateKernel("clFindWastedBits"); clComputeLPC = openCLProgram.CreateKernel("clComputeLPC"); clQuantizeLPC = openCLProgram.CreateKernel("clQuantizeLPC"); //cudaComputeLPCLattice = openCLProgram.CreateKernel("clComputeLPCLattice"); clSelectStereoTasks = openCLProgram.CreateKernel("clSelectStereoTasks"); clEstimateResidual = openCLProgram.CreateKernel("clEstimateResidual"); clChooseBestMethod = openCLProgram.CreateKernel("clChooseBestMethod"); if (UseGPUOnly) { clEncodeResidual = openCLProgram.CreateKernel("clEncodeResidual"); if (openCLCQ.Device.DeviceType != DeviceType.CPU) { clCalcPartition = openCLProgram.CreateKernel("clCalcPartition"); clCalcPartition16 = openCLProgram.CreateKernel("clCalcPartition16"); } clSumPartition = openCLProgram.CreateKernel("clSumPartition"); clFindRiceParameter = openCLProgram.CreateKernel("clFindRiceParameter"); clFindPartitionOrder = openCLProgram.CreateKernel("clFindPartitionOrder"); if (UseGPURice) { clCalcOutputOffsets = openCLProgram.CreateKernel("clCalcOutputOffsets"); clRiceEncoding = openCLProgram.CreateKernel("clRiceEncoding"); } } samplesBuffer = new int[MAX_CHANNELSIZE * channelsCount]; outputBuffer = new byte[max_frame_size * MAX_FRAMES + 1]; frame = new FlacFrame(channelsCount); frame.writer = new BitWriter(outputBuffer, 0, outputBuffer.Length); if (writer._settings.DoVerify) { verify = new FlakeReader(new AudioPCMConfig((int)bits_per_sample, channels, 44100)); verify.DoCRC = false; } }
public Mandelbrot( Platform platform, int width, int height ) { openCLPlatform = platform; openCLDevices = openCLPlatform.QueryDevices(DeviceType.ALL); openCLContext = openCLPlatform.CreateDefaultContext(); openCLCQ = openCLContext.CreateCommandQueue(openCLDevices[0], CommandQueueProperties.PROFILING_ENABLE); mandelBrotProgram = openCLContext.CreateProgramWithSource(File.ReadAllText("Mandelbrot.cl")); try { mandelBrotProgram.Build(); } catch (OpenCLException) { string buildLog = mandelBrotProgram.GetBuildLog(openCLDevices[0]); MessageBox.Show(buildLog,"Build error(64 bit debug sessions in vs2008 always fail like this - debug in 32 bit or use vs2010)"); Application.Exit(); } mandelbrotKernel = mandelBrotProgram.CreateKernel("Mandelbrot"); Left = -2.0f; Top = 2.0f; Right = 2.0f; Bottom = -2.0f; BitmapWidth = width; BitmapHeight = height; mandelbrotMemBuffer = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.WRITE_ONLY), width*height*4, IntPtr.Zero); }
public Core(int Nxp,int Nyp, int Nzp, int Ntm, double Bbeta, double Flux) { Nx = Nxp; Ny = Nyp; Nz = Nzp; Nt = Ntm; betagauge = (floattype)Bbeta; flux = (floattype)Flux; N = Nx * Ny * Nz * Nt; Nspace = Nx * Ny * Nz; string strforcompiler = "-D Nt=" + Nt.ToString() + " -D Nxyz=" + (Nx * Ny * Nz).ToString() + " -D Nxy=" + (Nx*Ny).ToString() + " -D Nx="+(Nx).ToString()+" -D Ny="+(Ny).ToString()+" -D Nz="+(Nz).ToString(); strforcompiler += typeof(floattype) == typeof(double) ? " -D floattype=double -D floattype2=double2 -D floattype4=double4" : " -D floattype=float -D floattype2=float2 -D floattype4=float4"; strforcompiler += " -D phi=" + flux.ToString().Replace(',', '.') + " -D KAPPA=" + kappa.ToString().Replace(',', '.'); string fp64support = "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n"; Plocalsize = AdjustLocalSize(Nspace); Slocalsize = AdjustLocalSize(N / 2); XhermYlocalsize = AdjustLocalSize(4 * N); // Plocalsize = 16; Slocalsize = 16; PNumGroups = Nx * Ny * Nz / Plocalsize; SNumGroups = N/2 / Slocalsize; XhermYNumGroups = 4*4*N / XhermYlocalsize; BufferLength = N * 4 * 9 * 2 * sizeof(floattype); SeedBufLen = N * sizeof(Int32)/2 * 4; AllocBuffers(); openCLPlatform = OpenCL.GetPlatform(0); openCLDevices = openCLPlatform.QueryDevices(DeviceType.ALL); openCLContext = openCLPlatform.CreateDefaultContext(); openCLCQ = openCLContext.CreateCommandQueue(openCLDevices[0], CommandQueueProperties.PROFILING_ENABLE); MyKernelProgram = openCLContext.CreateProgramWithSource( (typeof(floattype)==typeof(double)?fp64support:"") + File.ReadAllText("MyKernel.cl")+File.ReadAllText("dirak_mul.cl")); try { MyKernelProgram.Build(openCLDevices, strforcompiler, null, IntPtr.Zero); } catch (OpenCLException) { string buildLog = MyKernelProgram.GetBuildLog(openCLDevices[0]); MessageBox.Show(buildLog, "Build error(64 bit debug sessions in vs2008 always fail like this - debug in 32 bit or use vs2010)"); // Application.Exit(); } MyKernelKernel = MyKernelProgram.CreateKernel("MyKernel"); PReductionKernel = MyKernelProgram.CreateKernel("PLoop"); SReductionKernel = MyKernelProgram.CreateKernel("CalcS"); DiralMulKernel = MyKernelProgram.CreateKernel("dirakMatrMul"); FillWithKernel = MyKernelProgram.CreateKernel("FillWith"); FillLinkWithKernel = MyKernelProgram.CreateKernel("FillLinkWith"); FillWithRandomKernel = MyKernelProgram.CreateKernel("FillWithRandom"); AXPYKernel = MyKernelProgram.CreateKernel("AXPY"); XhermYKernel = MyKernelProgram.CreateKernel("XhermY"); BackupLinkKernel = MyKernelProgram.CreateKernel("BackupLink"); RestoreLinkKernel = MyKernelProgram.CreateKernel("RestoreLink"); SeedMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), SeedBufLen, IntPtr.Zero); LinkMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), BufferLength, IntPtr.Zero); PGroupMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), floatsize * PNumGroups, IntPtr.Zero); PResMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), floatsize, IntPtr.Zero); SGroupMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), floatsize * SNumGroups, IntPtr.Zero); SResMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), floatsize, IntPtr.Zero); XhermYGroupMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), floatsize * 2*XhermYNumGroups, IntPtr.Zero); XhermYresMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), floatsize * 2, IntPtr.Zero); XhermYrespointer = System.Runtime.InteropServices.Marshal.AllocHGlobal(floatsize * 2); SeedVectorMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), SeedVectorBuf.Length * sizeof(int), IntPtr.Zero); StorageMem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), linksize, IntPtr.Zero); dSmem = openCLContext.CreateBuffer((MemFlags)((long)MemFlags.READ_WRITE), floatsize, IntPtr.Zero); dSpointer = System.Runtime.InteropServices.Marshal.AllocHGlobal(floatsize); MyKernelKernel.SetArg(0, (byte)EvenOdd); MyKernelKernel.SetArg(1, (floattype)betagauge); MyKernelKernel.SetArg(2, (floattype)flux); MyKernelKernel.SetArg(3, SeedMem); MyKernelKernel.SetArg(4, LinkMem); PReductionKernel.SetArg(0, LinkMem); PReductionKernel.SetArg(1, PGroupMem); PReductionKernel.SetArg(2, PResMem); IntPtr ptr = new IntPtr(Plocalsize * floatsize); PReductionKernel.SetArg(3, ptr, IntPtr.Zero); SReductionKernel.SetArg(0, LinkMem); SReductionKernel.SetArg(1, SGroupMem); SReductionKernel.SetArg(2, SResMem); IntPtr ptr1 = new IntPtr(Slocalsize * floatsize); SReductionKernel.SetArg(3, ptr1, IntPtr.Zero); XhermYKernel.SetArg(2, XhermYresMem); XhermYKernel.SetArg(3, XhermYGroupMem); XhermYKernel.SetArg(4, new IntPtr(XhermYlocalsize*floatsize*2),IntPtr.Zero); openCLCQ.EnqueueWriteBuffer(SeedMem, true, 0, SeedBufLen, ipseed); openCLCQ.EnqueueWriteBuffer(LinkMem, true, 0, BufferLength, ip); openCLCQ.EnqueueWriteBuffer(SeedVectorMem, true, 0, SeedVectorBuf.Length*sizeof(int), ipseedvector); rhat0 = new Vector(); //init BICGStab vectors phi = new Vector(); r0 = new Vector(); //rprev = new Vector(); pi = new Vector(); vi = new Vector(); t = new Vector(); s = new Vector(); // xprev = new Vector(); // vprev = new Vector(); // pprev = new Vector(); temp = new Vector(); ri = new Vector(); x = new Vector(); //for fermion update chi = new Vector(); CalculateS(); double s1 = S[0]; BackupLink(0, 0,1, 0, 1); CalculateS(); double s2 = S[0]; RestoreLink(0, 0, 1, 0, 1); CalculateS(); double s3 = S[0]; //MessageBox.Show(s1.ToString() + s2.ToString() + s3.ToString()); }
public unsafe void CopyOCLBitmapToBitmap(Mem oclBitmap, Bitmap bitmap) { IntPtr[] origin = new IntPtr[3]; IntPtr[] region = new IntPtr[3]; Mem buffer; BitmapData bd = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); buffer = oclContext.CreateBuffer((MemFlags)((long)MemFlags.WRITE_ONLY | (long)MemFlags.USE_HOST_PTR), bd.Height * bd.Stride, bd.Scan0); origin[0] = (IntPtr)0; origin[1] = (IntPtr)0; origin[2] = (IntPtr)0; region[0] = (IntPtr)bd.Width; region[1] = (IntPtr)bd.Height; region[2] = (IntPtr)1; oclCQ.EnqueueCopyImageToBuffer(oclBitmap, buffer, origin, region, IntPtr.Zero); oclCQ.EnqueueBarrier(); IntPtr p = oclCQ.EnqueueMapBuffer(buffer, true, MapFlags.READ, IntPtr.Zero, (IntPtr)(bd.Height * bd.Stride)); oclCQ.EnqueueUnmapMemObject(buffer, p); oclCQ.Finish(); buffer.Dispose(); bitmap.UnlockBits(bd); }
public void EnqueueCopyBufferRect(Mem src_buffer, Mem dst_buffer, IntPtr[] src_origin, IntPtr[] dst_origin, IntPtr[] region, IntPtr src_row_pitch, IntPtr src_slice_pitch, IntPtr dst_row_pitch, IntPtr dst_slice_pitch) { ErrorCode result; result = OpenCL.EnqueueCopyBufferRect(CommandQueueID, src_buffer.MemID, dst_buffer.MemID, src_origin, dst_origin, region, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch, 0, null, null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyBufferRect failed with error code " + result, result); }
public void EnqueueCopyBufferRect(Mem src_buffer, Mem dst_buffer, long[] src_origin, long[] dst_origin, long[] region, long src_row_pitch, long src_slice_pitch, long dst_row_pitch, long dst_slice_pitch, int num_events_in_wait_list, Event[] event_wait_list) { ErrorCode result; IntPtr* pSrcOrigin = stackalloc IntPtr[3]; IntPtr* pDstOrigin = stackalloc IntPtr[3]; IntPtr* pRegion = stackalloc IntPtr[3]; IntPtr* pRepackedEvents = stackalloc IntPtr[num_events_in_wait_list]; if (num_events_in_wait_list == 0) pRepackedEvents = null; InteropTools.A3ToIntPtr3(src_origin, pSrcOrigin); InteropTools.A3ToIntPtr3(dst_origin, pDstOrigin); InteropTools.A3ToIntPtr3(region, pRegion); InteropTools.ConvertEventsToEventIDs(num_events_in_wait_list, event_wait_list, pRepackedEvents); result = OpenCL.EnqueueCopyBufferRect(CommandQueueID, src_buffer.MemID, dst_buffer.MemID, pSrcOrigin, pDstOrigin, pRegion, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch, num_events_in_wait_list, pRepackedEvents, null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyBufferRect failed with error code " + result, result); }
public void EnqueueCopyBufferRect(Mem src_buffer, Mem dst_buffer, IntPtr[] src_origin, IntPtr[] dst_origin, IntPtr[] region, IntPtr src_row_pitch, IntPtr src_slice_pitch, IntPtr dst_row_pitch, IntPtr dst_slice_pitch, uint num_events_in_wait_list, Event[] event_wait_list) { ErrorCode result; result = OpenCL.EnqueueCopyBufferRect(CommandQueueID, src_buffer.MemID, dst_buffer.MemID, src_origin, dst_origin, region, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch, num_events_in_wait_list, InteropTools.ConvertEventsToEventIDs(event_wait_list), null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyBufferRect failed with error code " + result, result); }
internal NativeKernelCallbackData(NativeKernel nk,CommandQueue cq, object o, Mem[] buffers) { NativeKernel = nk; CQ = cq; O = o; Buffers = buffers; }
private static int AddNativeKernelParams( NativeKernel nk, CommandQueue cq, object o, Mem[] buffers) { int callbackId; NativeKernelCallbackData callbackData = new NativeKernelCallbackData(nk, cq, o, buffers); bool gotMutex = false; try { gotMutex = NativeKernelParamsMutex.WaitOne(); do { callbackId = NativeKernelParamsId++; } while (NativeKernelDispatch.ContainsKey(callbackId)); NativeKernelDispatch.Add(callbackId, callbackData); } finally { if (gotMutex) NativeKernelParamsMutex.ReleaseMutex(); } return callbackId; }
public static float[] ReadFloatValues(this Context oclContext, CommandQueue oclCQ, CL.Mem oclBuff, int Len) { float[] floats = new float[Len]; oclCQ.EnqueueReadBuffer(oclBuff, true, 0, Len, floats.ToIntPtr()); oclCQ.EnqueueBarrier(); oclCQ.Finish(); return(floats); }
private unsafe Mem GetOpacity() { fixed (float* dataptr = form_this.opacity) { opacity = manager.Context.CreateBuffer(MemFlags.COPY_HOST_PTR, form_this.opacity.Count(), new IntPtr(dataptr)); } return opacity; }
/// <summary> /// OpenCL 1.1 /// </summary> /// <param name="buffer"></param> /// <param name="flags"></param> /// <param name="buffer_create_info"></param> /// <param name="errcode_ret"></param> /// <returns></returns> public Mem CreateSubBuffer(Mem buffer, MemFlags flags, BufferRegion buffer_create_info, out ErrorCode errcode_ret) { IntPtr memID = OpenCL.CreateSubBuffer(buffer.MemID, flags, buffer_create_info, out errcode_ret); return new Mem(buffer.Context, memID); }
public void EnqueueCopyBufferRect(Mem src_buffer, Mem dst_buffer, long[] src_origin, long[] dst_origin, long[] region, long src_row_pitch, long src_slice_pitch, long dst_row_pitch, long dst_slice_pitch) { ErrorCode result; IntPtr* pSrcOrigin = stackalloc IntPtr[3]; IntPtr* pDstOrigin = stackalloc IntPtr[3]; IntPtr* pRegion = stackalloc IntPtr[3]; InteropTools.A3ToIntPtr3(src_origin, pSrcOrigin); InteropTools.A3ToIntPtr3(dst_origin, pDstOrigin); InteropTools.A3ToIntPtr3(region, pRegion); result = OpenCL.EnqueueCopyBufferRect(CommandQueueID, src_buffer.MemID, dst_buffer.MemID, pSrcOrigin, pDstOrigin, pRegion, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch, 0, null, null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyBufferRect failed with error code " + result, result); }
private static void Main() { OpenCL.GetPlatformIDs(32, new IntPtr[32], out uint num_platforms); var devices = new List <Device>(); for (int i = 0; i < num_platforms; i++) { devices.AddRange(OpenCL.GetPlatform(i).QueryDevices(DeviceType.ALL)); } int device = SelectForm.Show((from Device d in devices select d.Name).ToArray()); if (device == -1) { return; } platform = devices[device].Platform; //平台 oclDevice = devices[device]; //选中运算设备 oclContext = platform.CreateContext(new[] { (IntPtr)ContextProperties.PLATFORM, platform.PlatformID, IntPtr.Zero, IntPtr.Zero }, new[] { oclDevice }, new ContextNotify(OpenCLContextNotifyCallBack), IntPtr.Zero); //根据配置建立上下文 oclCQ = oclContext.CreateCommandQueue(oclDevice, CommandQueueProperties.PROFILING_ENABLE); //创建请求队列 if (!oclDevice.ImageSupport) { return; //如果失败返回 } if (!oclContext.SupportsImageFormat(MemFlags.READ_WRITE, MemObjectType.IMAGE2D, ChannelOrder.RGBA, ChannelType.UNSIGNED_INT8)) { return; } sampler = oclContext.CreateSampler(true, AddressingMode.NONE, FilterMode.NEAREST); FilterKernel = oclContext.MakeCode("FilterImage", CLCode1); Kernel K2 = oclContext.MakeCode("vector_add_gpu", CLCode2); int aaa = K2.PECount(oclDevice); aaa = aaa; #region 试一下用GPU做运算 int[] a = new[] { 1, 2, 3, 1722 }; int[] b = new[] { 456, 2, 1, 56 }; int[] c = new[] { 0, 0, 0, 0 }; CL.Mem n1 = oclContext.CreateBuffer(MemFlags.READ_WRITE | MemFlags.COPY_HOST_PTR, a.Length * sizeof(int), a.ToIntPtr()); CL.Mem n2 = oclContext.CreateBuffer(MemFlags.READ_WRITE | MemFlags.COPY_HOST_PTR, b.Length * sizeof(int), b.ToIntPtr()); CL.Mem n3 = null; unchecked { n3 = oclContext.CreateBuffer(MemFlags.READ_WRITE, b.Length * sizeof(int), IntPtr.Zero); } K2.SetArg(0, n1); K2.SetArg(1, n2); K2.SetArg(2, n3); K2.SetArg(3, (int)c.Length); oclCQ.EnqueueNDRangeKernel(K2, 1, null, new[] { c.Length, 0 }, null); oclCQ.EnqueueBarrier(); oclCQ.Finish(); // */ //oclContext.WriterValues(oclCQ, n3, B); c = oclContext.ReadIntValues(oclCQ, n3, c.Length); c = c; // */ #endregion var sdi = new ShowDeviceInfo(); var lb = sdi.listBox1.Items; lb.Add($"Name:{oclDevice.Name}"); lb.Add($"DeviceType:{oclDevice.DeviceType.ToString()}"); lb.Add($"MaxComputeUnits(最大计算单元):{oclDevice.MaxComputeUnits}"); lb.Add($"ImageSupport:{oclDevice.ImageSupport}"); lb.Add($"AddressBits:{oclDevice.AddressBits}"); lb.Add($"DriverVersion:{oclDevice.DriverVersion}"); lb.Add($"MaxClockFrequency(最大时钟频率):{oclDevice.MaxClockFrequency}MHz"); lb.Add($"MaxMemAllocSize(最大内存):{oclDevice.MaxMemAllocSize / 1024 / 1024 / 1024}GB"); lb.Add($"MaxWorkItemDimensions(最大工作维度):{oclDevice.MaxWorkItemDimensions}"); lb.Add($"MaxWorkGroupSize(最大工作组数量):{oclDevice.MaxWorkGroupSize }"); lb.Add($"Version(OpenCL版本):{oclDevice.Version}"); lb.Add($"GlobalMemSize(显存):{oclDevice.GlobalMemSize / 1024 / 1024 / 1024}GB"); lb.Add($"GlobalMemCacheSize(显存缓存):{oclDevice.GlobalMemCacheSize / 1024}KB"); lb.Add($"GlobalMemCacheLineSize:{oclDevice.GlobalMemCacheLineSize}"); lb.Add($"Vendor(厂商):{oclDevice.Vendor}"); lb.Add($"HostUnifiedMemory(是否和Host共用内存):{oclDevice.HostUnifiedMemory}"); sdi.ShowDialog(); #region 调用编译好的生命游戏程序 //if (oclDevice.DeviceType == DeviceType.GPU) { OutImage1 = oclContext.CreateImage2D(MemFlags.READ_WRITE, CL.ImageFormat.RGBA8U, TestImage.Width, TestImage.Height, 0, IntPtr.Zero); OutImage2 = oclContext.CreateImage2D(MemFlags.READ_WRITE, CL.ImageFormat.RGBA8U, TestImage.Width, TestImage.Height, 0, IntPtr.Zero); FilterKernel.SetArg(0, 1.0f); FilterKernel.SetArg(1, 1.0f); FilterKernel.SetArg(2, oclContext.ToCLImage(TestImage)); FilterKernel.SetArg(3, OutImage1); FilterKernel.SetArg(4, sampler); oclCQ.EnqueueNDRangeKernel(FilterKernel, 2, null, new IntPtr[] { OutImage1.Width, OutImage1.Height, IntPtr.Zero }, null); oclCQ.EnqueueBarrier(); oclCQ.Finish(); new MainForm().ShowDialog(); } // */ #endregion Application.Exit(); }
public void EnqueueCopyImage(Mem src_image, Mem dst_image, IntPtr[] src_origin, IntPtr[] dst_origin, IntPtr[] region, int num_events_in_wait_list, Event[] event_wait_list, out Event _event) { ErrorCode result; IntPtr tmpEvent; result = OpenCL.EnqueueCopyImage(CommandQueueID, src_image.MemID, dst_image.MemID, src_origin, dst_origin, region, (uint)num_events_in_wait_list, InteropTools.ConvertEventsToEventIDs(event_wait_list), &tmpEvent); _event = new Event(Context, this, tmpEvent); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyImage failed with error code " + result, result); }
private unsafe Mem GetCounter() { fixed (ulong* dataptr = &voxelctr) { counter = manager.Context.CreateBuffer(MemFlags.READ_WRITE, 8, new IntPtr(dataptr)); } return counter; }
public void EnqueueCopyImage(Mem src_image, Mem dst_image, long[] src_origin, long[] dst_origin, long[] region, int num_events_in_wait_list, Event[] event_wait_list, out Event _event) { ErrorCode result; IntPtr tmpEvent; IntPtr* repackedSrcOrigin = stackalloc IntPtr[3]; IntPtr* repackedDstOrigin = stackalloc IntPtr[3]; IntPtr* repackedRegion = stackalloc IntPtr[3]; IntPtr* repackedEvents = stackalloc IntPtr[num_events_in_wait_list]; if (num_events_in_wait_list == 0) repackedEvents = null; InteropTools.A3ToIntPtr3(src_origin, repackedSrcOrigin); InteropTools.A3ToIntPtr3(dst_origin, repackedDstOrigin); InteropTools.A3ToIntPtr3(region, repackedRegion); InteropTools.ConvertEventsToEventIDs(num_events_in_wait_list, event_wait_list, repackedEvents); result = OpenCL.EnqueueCopyImage(CommandQueueID, src_image, dst_image, repackedSrcOrigin, repackedDstOrigin, repackedRegion, num_events_in_wait_list, repackedEvents, &tmpEvent); _event = new Event(Context, this, tmpEvent); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyImage failed with error code " + result, result); }
/// <summary> /// Set argument argIndex to mem /// </summary> /// <param name="argIndex"></param> /// <param name="mem"></param> public void SetArg(Int32 argIndex, Mem mem) { this.SetArg(argIndex, mem.MemID); }
public void EnqueueCopyImage(Mem src_image, Mem dst_image, IntPtr[] src_origin, IntPtr[] dst_origin, IntPtr[] region) { ErrorCode result; result = OpenCL.EnqueueCopyImage(CommandQueueID, src_image.MemID, dst_image.MemID, src_origin, dst_origin, region, (uint)0, null, null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyImage failed with error code " + result, result); }
/* возвращает буфер */ public unsafe Mem CreateBuffer() { if (buffer == null) { /* указатели в C# используются с fixed (противодействие для GC) */ fixed (short* dataptr = data) { buffer = manager_this.Context.CreateBuffer(MemFlags.COPY_HOST_PTR, data.Count() * 2, new IntPtr(dataptr)); } } return buffer; }
public void EnqueueCopyImage(Mem src_image, Mem dst_image, long[] src_origin, long[] dst_origin, long[] region) { ErrorCode result; IntPtr* repackedSrcOrigin = stackalloc IntPtr[3]; IntPtr* repackedDstOrigin = stackalloc IntPtr[3]; IntPtr* repackedRegion = stackalloc IntPtr[3]; InteropTools.A3ToIntPtr3(src_origin, repackedSrcOrigin); InteropTools.A3ToIntPtr3(dst_origin, repackedDstOrigin); InteropTools.A3ToIntPtr3(region, repackedRegion); result = OpenCL.EnqueueCopyImage(CommandQueueID, src_image, dst_image, repackedSrcOrigin, repackedDstOrigin, repackedRegion, 0, null, null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyImage failed with error code " + result, result); }
/// <summary> /// Set argument argIndex to mem /// </summary> /// <param name="argIndex"></param> /// <param name="mem"></param> public void SetArg(int argIndex, Mem mem) { SetArg(argIndex, mem.MemID); }
public void EnqueueCopyImageToBuffer(Mem src_image, Mem dst_buffer, IntPtr[] src_origin, IntPtr[] region, IntPtr dst_offset, int num_events_in_wait_list, Event[] event_wait_list) { ErrorCode result; result = OpenCL.EnqueueCopyImageToBuffer(CommandQueueID, src_image.MemID, dst_buffer.MemID, src_origin, region, dst_offset, (uint)num_events_in_wait_list, InteropTools.ConvertEventsToEventIDs(event_wait_list), null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyImageToBuffer failed with error code " + result, result); }
public void EnqueueCopyImageToBuffer(Mem src_image, Mem dst_buffer, long[] src_origin, long[] region, long dst_offset, int num_events_in_wait_list, Event[] event_wait_list) { ErrorCode result; IntPtr* repackedSrcOrigin = stackalloc IntPtr[3]; IntPtr* repackedRegion = stackalloc IntPtr[3]; IntPtr* repackedEvents = stackalloc IntPtr[num_events_in_wait_list]; if (num_events_in_wait_list == 0) repackedEvents = null; InteropTools.A3ToIntPtr3(src_origin, repackedSrcOrigin); InteropTools.A3ToIntPtr3(region, repackedRegion); InteropTools.ConvertEventsToEventIDs(num_events_in_wait_list, event_wait_list, repackedEvents); result = OpenCL.EnqueueCopyImageToBuffer(CommandQueueID, src_image, dst_buffer, repackedSrcOrigin, repackedRegion, dst_offset, num_events_in_wait_list, repackedEvents, null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("EnqueueCopyImageToBuffer failed with error code " + result, result); }
/// <summary> /// Loads two bitmaps and locks them for the duration of the program. /// Also creates two OpenCL buffers that map to the locked images /// </summary> private void SetupBitmaps() { InputBitmap0 = (Bitmap)Bitmap.FromFile(@"Input0.png"); InputBitmap1 = (Bitmap)Bitmap.FromFile(@"Input1.png"); if (InputBitmap1.Size != InputBitmap0.Size) InputBitmap1 = new Bitmap(InputBitmap1, InputBitmap0.Size); OutputBitmap = new Bitmap(InputBitmap0); InputBitmapData0 = InputBitmap0.LockBits(new Rectangle(0, 0, InputBitmap0.Width, InputBitmap0.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); InputBitmapData1 = InputBitmap1.LockBits(new Rectangle(0, 0, InputBitmap1.Width, InputBitmap1.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); InputBuffer0 = OCLMan.Context.CreateBuffer(MemFlags.USE_HOST_PTR, InputBitmapData0.Stride * InputBitmapData0.Height, InputBitmapData0.Scan0); InputBuffer1 = OCLMan.Context.CreateBuffer(MemFlags.USE_HOST_PTR, InputBitmapData1.Stride * InputBitmapData1.Height, InputBitmapData1.Scan0); }
private unsafe Mem GetColors() { fixed (uint* dataptr = form_this.colors) { color_buffer = manager.Context.CreateBuffer(MemFlags.COPY_HOST_PTR, form_this.colors.Count() * 4, new IntPtr(dataptr)); } return color_buffer; }
/// <summary> /// Launch the CrossFadeKernel /// /// First we set its arguments, /// then we enqueue the kernel using EnqueueNDRangeKernel, /// and finally, map the buffer for reading to make sure /// there aren't any cache issues when OpenCL completes. /// </summary> /// <param name="ratio"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="input0"></param> /// <param name="inputStride0"></param> /// <param name="input1"></param> /// <param name="inputStride1"></param> /// <param name="output"></param> private void DoCrossFade( float ratio, int width, int height, Mem input0, int inputStride0, Mem input1, int inputStride1, BitmapData output ) { Mem outputBuffer; int deviceIndex = comboBoxDeviceSelector.SelectedIndex; outputBuffer = OCLMan.Context.CreateBuffer(MemFlags.USE_HOST_PTR, output.Stride * output.Height, output.Scan0); CrossFadeGlobalWorkSize[0] = (IntPtr)width; CrossFadeGlobalWorkSize[1] = (IntPtr)height; CrossFadeKernel.SetArg(0, ratio); CrossFadeKernel.SetArg(1, width); CrossFadeKernel.SetArg(2, height); CrossFadeKernel.SetArg(3, input0); CrossFadeKernel.SetArg(4, inputStride0); CrossFadeKernel.SetArg(5, input1); CrossFadeKernel.SetArg(6, inputStride1); CrossFadeKernel.SetArg(7, outputBuffer); CrossFadeKernel.SetArg(8, output.Stride); OCLMan.CQ[deviceIndex].EnqueueNDRangeKernel(CrossFadeKernel, 2, null, CrossFadeGlobalWorkSize, null); OCLMan.CQ[deviceIndex].EnqueueBarrier(); IntPtr p = OCLMan.CQ[deviceIndex].EnqueueMapBuffer(outputBuffer, true, MapFlags.READ, IntPtr.Zero, (IntPtr)(output.Stride * output.Height)); OCLMan.CQ[deviceIndex].EnqueueUnmapMemObject(outputBuffer, p); OCLMan.CQ[deviceIndex].Finish(); outputBuffer.Dispose(); }
public VoxelImage() { InitializeComponent(); first = true; angleChange = false; changeDistance = false; leftChange = false; rightChange = false; voxelctr = 0; realctr = 0; this.lx.Text = Convert.ToString(light.S0); this.ly.Text = Convert.ToString(light.S1); this.lz.Text = Convert.ToString(light.S2); this.trackminX.SetRange(0, 512); this.trackminY.SetRange(0, 512); this.trackminZ.SetRange(0, 512); this.trackmaxX.SetRange(0, 512); this.trackmaxY.SetRange(0, 512); this.trackmaxZ.SetRange(0, 512); this.trackminX.Value = (int)(boxMinCon.S0); this.trackminY.Value = (int)(boxMinCon.S1); this.trackminZ.Value = (int)(boxMinCon.S2); this.trackmaxX.Value = (int)(boxMaxCon.S0); this.trackmaxY.Value = (int)(boxMaxCon.S1); this.trackmaxZ.Value = (int)(boxMaxCon.S2); this.minX.Text = Convert.ToString(boxMinCon.S0); this.minY.Text = Convert.ToString(boxMinCon.S1); this.minZ.Text = Convert.ToString(boxMinCon.S2); this.maxX.Text = Convert.ToString(boxMaxCon.S0); this.maxY.Text = Convert.ToString(boxMaxCon.S1); this.maxZ.Text = Convert.ToString(boxMaxCon.S2); this.kamb.Text = Convert.ToString(0.1); this.kdiff.Text = Convert.ToString(0.5); this.kspec.Text = Convert.ToString(0.4); this.tracklx.SetRange(-1000,1000); this.trackly.SetRange(-1000, 1000); this.tracklz.SetRange(-1000, 1000); this.tracklx.Value = (int)light.S0; this.trackly.Value = (int)light.S1; this.tracklz.Value = (int)light.S2; this.kc.Text = "1,0"; this.kl.Text = "0,0"; this.kq.Text = "0,0"; this.specexp.Text = "1,0"; outputBuffer = null; color_buffer =null; opacity = null; clock = new Stopwatch(); }
public TextureInfo(Mem mem) { Mem = mem; }
private static void Main() { OpenCL.GetPlatformIDs(32, new IntPtr[32], out uint num_platforms); List <Device> pt = new List <Device>(); for (int i = 0; i < num_platforms; pt.AddRange(OpenCL.GetPlatform(i++).QueryDevices(DeviceType.ALL))) { ; } int PT = 0; // SelectForm.Show((from Device d in pt select d.Name).ToArray()); if (PT == -1) { return; } platform = pt[PT].Platform; //平台 oclDevice = pt[PT]; //选中运算设备 oclContext = platform.CreateContext(new[] { (IntPtr)ContextProperties.PLATFORM, platform.PlatformID, IntPtr.Zero, IntPtr.Zero }, new[] { oclDevice }, new ContextNotify(OpenCLContextNotifyCallBack), IntPtr.Zero); //根据配置建立上下文 oclCQ = oclContext.CreateCommandQueue(oclDevice, CommandQueueProperties.PROFILING_ENABLE); //创建请求队列 if (!oclDevice.ImageSupport) { return; //如果失败返回 } if (!oclContext.SupportsImageFormat(MemFlags.READ_WRITE, MemObjectType.IMAGE2D, ChannelOrder.RGBA, ChannelType.UNSIGNED_INT8)) { return; } sampler = oclContext.CreateSampler(true, AddressingMode.NONE, FilterMode.NEAREST); FilterKernel = oclContext.MakeCode("FilterImage", CLCode1); Kernel K2 = oclContext.MakeCode("vector_add_gpu", CLCode2); #region int[] A = new[] { 1, 2, 3, 1722 }; int[] B = new[] { 456, 2, 1, 56 }; int[] C = new[] { 0, 0, 0, 0 }; CL.Mem n1 = oclContext.CreateBuffer(MemFlags.READ_WRITE | MemFlags.COPY_HOST_PTR, A.Length * sizeof(int), A.ToIntPtr()); CL.Mem n2 = oclContext.CreateBuffer(MemFlags.READ_WRITE | MemFlags.COPY_HOST_PTR, B.Length * sizeof(int), B.ToIntPtr()); CL.Mem n3 = oclContext.CreateBuffer(MemFlags.READ_WRITE, B.Length * sizeof(int), IntPtr.Zero); K2.SetArg(0, n1); K2.SetArg(1, n2); K2.SetArg(2, n3); K2.SetArg(3, (int)C.Length); oclCQ.EnqueueNDRangeKernel(K2, 1, null, new[] { C.Length, 0 }, null); oclCQ.EnqueueBarrier(); oclCQ.Finish(); // */ //oclContext.WriterValues(oclCQ, n3, B); C = oclContext.ReadIntValues(oclCQ, n3, C.Length); C = C; // */ #endregion #region 调用编译好的程序 OutImage1 = oclContext.CreateImage2D(MemFlags.READ_WRITE, CL.ImageFormat.RGBA8U, TestImage.Width, TestImage.Height, 0, IntPtr.Zero); OutImage2 = oclContext.CreateImage2D(MemFlags.READ_WRITE, CL.ImageFormat.RGBA8U, TestImage.Width, TestImage.Height, 0, IntPtr.Zero); FilterKernel.SetArg(0, 1.0f); FilterKernel.SetArg(1, 1.0f); FilterKernel.SetArg(2, oclContext.ToCLImage(TestImage)); FilterKernel.SetArg(3, OutImage1); FilterKernel.SetArg(4, sampler); oclCQ.EnqueueNDRangeKernel(FilterKernel, 2, null, new IntPtr[] { OutImage1.Width, OutImage1.Height, IntPtr.Zero }, null); oclCQ.EnqueueBarrier(); oclCQ.Finish(); // */ #endregion new MainForm().ShowDialog(); Application.Exit(); }
private unsafe void DoRayCasting(BitmapData output) { try{ int deviceIndex = 0; outputBuffer = manager.Context.CreateBuffer(MemFlags.USE_HOST_PTR, output.Stride * output.Height, output.Scan0); if (first || changeDistance) { // camera model UVN camPos = new Float4() { S0 = vol.GetSize() / 2 - (float)Math.Cos(camAngle * Math.PI / 180) * camDist, S1 = vol.GetSize() / 2, S2 = vol.GetSize() / 2 - (float)Math.Sin(camAngle * Math.PI / 180) * camDist, S3 = 0 }; first = false; changeDistance = false; camPosOld = camPos; } else{ // rotation around the axis of the cube visualization if (angleChange && leftChange){ camPosOld.S0 -= camLookAt.S0; camPosOld.S2 -= camLookAt.S2; camPos.S0 = (float)Math.Cos(camAngle * Math.PI / 180) * camPosOld.S0 + (float)Math.Sin(camAngle * Math.PI / 180) * camPosOld.S2; camPos.S1 = vol.GetSize() / 2; camPos.S2 = -(float)Math.Sin(camAngle * Math.PI / 180) * camPosOld.S0 + (float)Math.Cos(camAngle * Math.PI / 180) * camPosOld.S2; camPos.S3 = 0; camPos.S0 += camLookAt.S0; camPos.S2 += camLookAt.S2; camPosOld = camPos; angleChange = false; leftChange = false; } if (angleChange && rightChange){ camPosOld.S0 -= camLookAt.S0; camPosOld.S2 -= camLookAt.S2; camPos.S0 = (float)Math.Cos(camAngle * Math.PI / 180) * camPosOld.S0 - (float)Math.Sin(camAngle * Math.PI / 180) * camPosOld.S2; camPos.S1 = vol.GetSize() / 2; camPos.S2 = (float)Math.Sin(camAngle * Math.PI / 180) * camPosOld.S0 + (float)Math.Cos(camAngle * Math.PI / 180) * camPosOld.S2; camPos.S3 = 0; camPos.S0 += camLookAt.S0; camPos.S2 += camLookAt.S2; camPosOld = camPos; angleChange = false; leftChange = false; } } camLookAt = new Float4(){ S0 = vol.GetSize() / camfactorX, S1 = vol.GetSize() / camfactorX, S2 = vol.GetSize() / camfactorZ, S3 = 0 }; // direction of the camera, UVN model camForward = camLookAt.Sub(camPos).Normalize(); // viewing direction var up = new Float4(0.0f, 1.0f, 0.0f, 0.0f); var right = MathClass.Cross(up, camForward).Normalize().Times(1.5f); up = MathClass.Cross(camForward, right).Normalize().Times(-1.5f); /* processing of the output image in BitmapData OpenCl device */ for (var x = 0; x < output.Width; x += blocksize){ for (var y = 0; y < output.Height; y += blocksize){ var rayTracingGlobalWorkSize = new IntPtr[2]; // work_dim = 2 rayTracingGlobalWorkSize[0] = (IntPtr)(output.Width - x > blocksize ? blocksize : output.Width - x); rayTracingGlobalWorkSize[1] = (IntPtr)(output.Height - y > blocksize ? blocksize : output.Height - y); var rayTracingGlobalOffset = new IntPtr[2]; rayTracingGlobalOffset[0] = (IntPtr)x; rayTracingGlobalOffset[1] = (IntPtr)y; float ka = (float)(Convert.ToDouble(kamb.Text)); float kd = (float)(Convert.ToDouble(kdiff.Text)); float ks = (float)(Convert.ToDouble(kspec.Text)); float exp = (float)(Convert.ToDouble(specexp.Text)); float kkc = (float)(Convert.ToDouble(this.kc.Text)); float kkl = (float)(Convert.ToDouble(this.kl.Text)); float kkq = (float)(Convert.ToDouble(this.kq.Text)); /* arguments passed to kernel function */ kernel.SetArg(0, output.Width); kernel.SetArg(1, output.Height); kernel.SetArg(2, outputBuffer); // in the nucleus with global, as is required for access to the output kernel.SetArg(3, output.Stride); kernel.SetArg(4, camPos); kernel.SetArg(5, camForward); kernel.SetArg(6, right); kernel.SetArg(7, up); kernel.SetArg(8, vol.CreateBuffer()); kernel.SetArg(9, vol.GetSize()); kernel.SetArg(10, light); kernel.SetArg(11, boxMinCon); kernel.SetArg(12, boxMaxCon); kernel.SetArg(13, Convert.ToInt16(colorMi.Text)); kernel.SetArg(14, Convert.ToInt16(colorMa.Text)); kernel.SetArg(15, _cutArrea.Checked ? (short)1 : (short)0); kernel.SetArg(16, _trilinear.Checked ? (short)1 : (short)0); kernel.SetArg(17, tf.Checked ? (short)1: (short)0 ); kernel.SetArg(18, GetColors()); kernel.SetArg(19, winWidth_vox); kernel.SetArg(20, winCentre_vox); kernel.SetArg(21, form_this.knots_counter); kernel.SetArg(22, Convert.ToInt16(colorMi2.Text)); kernel.SetArg(23, Convert.ToInt16(colorMa2.Text)); kernel.SetArg(24, GetOpacity()); kernel.SetArg(25, ka); kernel.SetArg(26, kd); kernel.SetArg(27, ks); kernel.SetArg(28, exp); kernel.SetArg(29, kkc); kernel.SetArg(30, kkl); kernel.SetArg(31, kkq); kernel.SetArg(32, GetCounter()); /* Queues a command for kernel execution on the device */ /* rayTracingGlobalOffset - * globalWorkOffset: can be used to specify an array of values * work_dim unsigned dimension that describes the offset used to calculate global ID work-item * instead of global IDs always start at offset (0, 0,... 0). * rayTracingGlobalWorkSize - * globalWorkSize: общее число global work-items вычисляется как global_work_size[0] *...* global_work_size[work_dim - 1].*/ manager.CQ[deviceIndex].EnqueueNDRangeKernel(kernel, 2, rayTracingGlobalOffset, rayTracingGlobalWorkSize, null); } } /*wait until all the work-items executed */ manager.CQ[deviceIndex].EnqueueBarrier(); /* to get access to memory and written to the output image we ask OpenCL * impose * data to the host device */ IntPtr p = manager.CQ[deviceIndex].EnqueueMapBuffer(outputBuffer, true, MapFlags.READ, IntPtr.Zero, (IntPtr)(output.Stride * output.Height)); IntPtr z = manager.CQ[deviceIndex].EnqueueMapBuffer(counter, true, MapFlags.READ_WRITE, IntPtr.Zero, (IntPtr)(sizeof(ulong))); /* when we finish working with the buffer should call this function */ manager.CQ[deviceIndex].EnqueueUnmapMemObject(outputBuffer, p); manager.CQ[deviceIndex].EnqueueUnmapMemObject(counter, z); manager.CQ[deviceIndex].Finish(); realctr += voxelctr; voxelCounter.Text = Convert.ToString(realctr); } catch (Exception ex){ MessageBox.Show("Ray casting exception:" + ex.Message, "Exception"); Environment.Exit(-1); } finally{ if (outputBuffer != null){ outputBuffer.Dispose(); } } }
public static IntPtr[] ConvertMemToMemIDs(Mem[] mems) { IntPtr[] memIDs; if (mems == null) return null; memIDs = new IntPtr[mems.Length]; for (int i = 0; i < mems.Length; i++) memIDs[i] = mems[i].MemID; return memIDs; }
public static int[] ReadIntValues(this Context oclContext, CommandQueue oclCQ, CL.Mem oclBuff, int Len) { int[] values = new int[Len]; oclCQ.EnqueueReadBuffer(oclBuff, true, 0, Len * sizeof(int), values.ToIntPtr()); oclCQ.EnqueueBarrier(); oclCQ.Finish(); return(values); }