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 void CreateContext( Platform platform, Device device ) { IntPtr[] contextProperties = new IntPtr[] { (IntPtr)ContextProperties.PLATFORM, platform.PlatformID, IntPtr.Zero, IntPtr.Zero }; Device[] devices = new Device[] { device }; oclContext = platform.CreateContext(contextProperties, devices, oclContextNotify, IntPtr.Zero); oclCQ = oclContext.CreateCommandQueue(device, CommandQueueProperties.PROFILING_ENABLE); }
internal Mem( Context context, IntPtr memID ) { Context = context; MemID = memID; TxInfo = new TextureInfo(this); }
internal Image(Context context, IntPtr memID) : base(context,memID) { }
protected void SaveDeviceBinary(Context context, string fileName, byte[][] binaries, string platformDirectoryName, Device device ) { throw new NotImplementedException("SaveDeviceBinary not implemented"); }
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()); }
// Dispose(bool disposing) executes in two distinct scenarios. // If disposing equals true, the method has been called directly // or indirectly by a user's code. Managed and unmanaged resources // can be disposed. // If disposing equals false, the method has been called by the // runtime from inside the finalizer and you should not reference // other objects. Only unmanaged resources can be disposed. private void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this.disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. Bitmap.Dispose(); Bitmap = null; Image.Dispose(); Image = null; CQ = null; Context = null; } // Call the appropriate methods to clean up // unmanaged resources here. // If disposing is false, // only the following code is executed. // Note disposing has been done. disposed = true; } }
private void comboBoxOpenCLDevices_SelectedIndexChanged(object sender, EventArgs e) { bool supportsImages; bool supportsImageFormat; try { ReleaseDeviceResources(); panelScaled.Refresh(); oclDevice = oclDevices[comboBoxOpenCLDevices.SelectedIndex]; CreateContext(oclPlatform, oclDevice); supportsImages = oclDevice.ImageSupport; supportsImageFormat = oclContext.SupportsImageFormat(MemFlags.READ_WRITE, MemObjectType.IMAGE2D, ChannelOrder.RGBA, ChannelType.UNSIGNED_INT8); if (oclDevice.ImageSupport && supportsImageFormat) { buttonScaleImage.Enabled = true; labelImageSupportIndicator.Text = "Yes"; OpenCLSource = File.ReadAllText(@"OpenCLFunctions.cl"); BuildOCLSource(OpenCLSource); CreateOCLImages(oclContext); oclFullyInitialized = true; } else { buttonScaleImage.Enabled = false; labelImageSupportIndicator.Text = "No " + (supportsImageFormat ? "(No Image support at all)" : "(Images supported, but no support for RGBA8888)"); oclContext = null; } } catch (OpenCLBuildException oclbe) { MessageBox.Show(this, oclbe.BuildLogs[0], "OpenCL build error"); } catch (OpenCLException ocle) { MessageBox.Show(this, ocle.Message, "OpenCL exception"); } }
// Dispose(bool disposing) executes in two distinct scenarios. // If disposing equals true, the method has been called directly // or indirectly by a user's code. Managed and unmanaged resources // can be disposed. // If disposing equals false, the method has been called by the // runtime from inside the finalizer and you should not reference // other objects. Only unmanaged resources can be disposed. private void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this.disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { // Dispose managed resources. } // Call the appropriate methods to clean up // unmanaged resources here. // If disposing is false, // only the following code is executed. if (CQ != null) { foreach (CommandQueue cq in CQ) cq.Dispose(); CQ = null; } if (Context != null) { Context.Dispose(); Context = null; } // Note disposing has been done. disposed = true; } }
internal Event( Context context, CommandQueue cq, IntPtr eventID ) { Context = context; CommandQueue = cq; EventID = eventID; }
internal Kernel( Context context, Program program, IntPtr kernelID ) { Context = context; Program = program; KernelID = kernelID; }
internal Sampler( Context context, IntPtr samplerID ) { Context = context; SamplerID = samplerID; }
protected virtual void Initialize(DeviceType deviceType, string source) { Devices = Platform.QueryDevices(deviceType); if (Devices.Length == 0) throw new OpenCLException("No devices of type "+deviceType+" present"); Context = Platform.CreateContext(null,Devices,null, IntPtr.Zero); CQs = new CommandQueue[Devices.Length]; for( int i=0; i<CQs.Length; i++ ) CQs[i] = Context.CreateCommandQueue(Devices[i], CommandQueueProperties.PROFILING_ENABLE); CQ = CQs[0]; Program = Context.CreateProgramWithSource(source); Program.Build(); Kernels = Program.CreateKernelDictionary(); }
internal Program( Context context, IntPtr programID ) { Context = context; ProgramID = programID; }
public void CreateOCLImages(Context context) { OCLInputImage = CreateOCLBitmapFromBitmap(TestImage); OCLOutputImage = oclContext.CreateImage2D(MemFlags.WRITE_ONLY, CL.ImageFormat.RGBA8U, panelScaled.Width, panelScaled.Height, 0, IntPtr.Zero); OCLSampler = oclContext.CreateSampler(true, AddressingMode.CLAMP_TO_EDGE, FilterMode.LINEAR); }
/// <summary> /// Create a context and initialize default command queues in the CQ property /// </summary> /// <param name="platform"></param> /// <param name="devices"></param> public void CreateContext( Platform platform, IntPtr[] contextProperties, IEnumerable<Device> devices, ContextNotify notify, IntPtr userData ) { if (!OpenCLIsAvailable) throw new OpenCLNotAvailableException(); Platform = platform; Context = platform.CreateContext( contextProperties, devices.ToArray<Device>(), notify, userData ); CQ = new CommandQueue[Context.Devices.Length]; for (int i = 0; i < Context.Devices.Length; i++) CQ[i] = Context.CreateCommandQueue(Context.Devices[0]); }
public void ReleaseDeviceResources() { oclFullyInitialized = false; if (OCLSampler != null) { OCLSampler.Dispose(); OCLSampler = null; } if (OCLInputImage != null) { OCLInputImage.Dispose(); OCLInputImage = null; } if (OCLOutputImage != null) { OCLOutputImage.Dispose(); OCLOutputImage = null; } if (FilterKernel != null) { FilterKernel.Dispose(); FilterKernel = null; } if (oclProgram != null) { oclProgram.Dispose(); oclProgram = null; } if (oclCQ != null) { oclCQ.Dispose(); oclCQ = null; } if (oclContext != null) { oclContext.Dispose(); oclContext = null; } }
protected byte[][] LoadAllBinaries(Context context, string source, string fileName) { string sourcePath = SourcePath + FileSystem.GetDirectorySeparator() + fileName; DateTime sourceDateTime = FileSystem.GetLastWriteTime(sourcePath); byte[][] binaries = new byte[context.Devices.Length][]; if (!Directory.Exists(BinaryPath)) throw new DirectoryNotFoundException(BinaryPath); using (BinaryMetaInfo bmi = BinaryMetaInfo.FromPath(BinaryPath, FileAccess.Read, FileShare.Read)) { Device[] devices; devices = context.Devices; for (int i = 0; i < devices.Length; i++) { if (binaries[i] != null) continue; Device device = devices[i]; string binaryFilePath; MetaFile mf = bmi.FindMetaFile("", fileName, Context.Platform.Name, device.Name, device.DriverVersion, Defines, BuildOptions); if (mf == null) throw new FileNotFoundException("No compiled binary file present in MetaFile"); binaryFilePath = BinaryPath + FileSystem.GetDirectorySeparator() + mf.BinaryName; if (AttemptUseSource) { // This exception will be caught inside the manager and cause recompilation if (FileSystem.GetLastWriteTime(binaryFilePath) < sourceDateTime) throw new Exception("Binary older than source"); } binaries[i] = FileSystem.ReadAllBytes(binaryFilePath); // Check of there are other identical devices that can use the binary we just loaded // If there are, patch it in in the proper slots in the list of binaries for (int j = i+1; j < devices.Length; j++) { if (devices[i].Name == devices[j].Name && devices[i].Vendor == devices[j].Vendor && devices[i].Version == devices[j].Version && devices[i].AddressBits == devices[j].AddressBits && devices[i].DriverVersion == devices[j].DriverVersion && devices[i].EndianLittle == devices[j].EndianLittle) { binaries[j] = binaries[i]; } } } } return binaries; }
protected virtual void Initialize(Context context, CommandQueue cq, int width, int height) { BitmapData bd; Bitmap = new Bitmap(width,height,PixelFormat.Format32bppArgb); bd = LockEntireBitmap( Bitmap, ImageLockMode.ReadOnly ); Image = context.CreateImage2D(MemFlags.COPY_HOST_PTR, OpenCLNet.ImageFormat.ARGB8U, width, height, bd.Stride, bd.Scan0); Context = context; CQ = cq; }
protected void SaveAllBinaries(Context context, string source, string fileName, byte[][] binaries) { XmlSerializer xml = new XmlSerializer(typeof(BinaryMetaInfo)); TestAndCreateDirectory(BinaryPath); using (BinaryMetaInfo bmi = BinaryMetaInfo.FromPath(BinaryPath, FileAccess.ReadWrite, FileShare.None)) { for (int i = 0; i < context.Devices.Length; i++) { Device device = context.Devices[i]; string binaryFileName; MetaFile mf = bmi.FindMetaFile(source, fileName, context.Platform.Name, device.Name, device.DriverVersion, Defines, BuildOptions); if( mf==null ) mf = bmi.CreateMetaFile(source, fileName, context.Platform.Name, device.Name, device.DriverVersion, Defines, BuildOptions); binaryFileName = BinaryPath + FileSystem.GetDirectorySeparator() + mf.BinaryName; FileSystem.WriteAllBytes(binaryFileName, binaries[i]); } bmi.TrimBinaryCache(FileSystem, MaxCachedBinaries); bmi.Save(); } }
internal OpenCLBackedBitmap(Context context, CommandQueue cq, int width, int height) { Initialize(context, cq, width, height); }
internal CommandQueue( Context context, Device device, IntPtr commandQueueID ) { Context = context; Device = device; CommandQueueID = commandQueueID; }