public FIRFilterSolver(Platform platform, Device device, IList<FIRFilter> filters) { FilterCount = filters.Count; var order = filters[0].B.Count; FilterOrder = order; foreach (var x in filters) if (order != x.B.Count) throw new InvalidOperationException("The filters should have the same order"); var table = new float[filters.Count * order]; var f = 0; foreach (var x in filters) { for (int i = 0; i < order; i++) table[f*order + i] = x.B[i]; f++; } Platform = platform; OpenCLContext = Platform.CreateDefaultContext(); OpenCLCommandQueue = OpenCLContext.CreateCommandQueue(device);//, CommandQueueProperties.PROFILING_ENABLE); DiffEqnProgram = OpenCLContext.CreateProgramWithSource(File.OpenText("opencl/FIRKernels.cl").ReadToEnd()); DiffEqnProgram.Build(); initWorkBufferKernel = DiffEqnProgram.CreateKernel("initWorkBuffer"); filterKernel = DiffEqnProgram.CreateKernel("filter"); bufferedFilterKernel = DiffEqnProgram.CreateKernel("bufferedFilter"); Table = OpenCLContext.CreateBuffer(MemFlags.READ_ONLY, table.Length * 4); WorkBuffer = OpenCLContext.CreateBuffer(MemFlags.WRITE_ONLY, table.Length * 4); OutputBuffer = OpenCLContext.CreateBuffer(MemFlags.WRITE_ONLY, filters.Count * 4); fixed (float* array = table) { OpenCLCommandQueue.EnqueueWriteBuffer(Table, false, 0, table.Length * 4, new IntPtr((void*)array)); } initWorkBufferKernel.SetArg(0, WorkBuffer); OpenCLCommandQueue.EnqueueNDRangeKernel(initWorkBufferKernel, 1, null, new int[] { table.Length }, null); OpenCLCommandQueue.EnqueueBarrier(); filterKernel.SetArg(2, FilterOrder); filterKernel.SetArg(3, Table); filterKernel.SetArg(4, WorkBuffer); filterKernel.SetArg(5, OutputBuffer); bufferedFilterKernel.SetArg(2, FilterOrder); bufferedFilterKernel.SetArg(3, Table); bufferedFilterKernel.SetArg(4, WorkBuffer); bufferedFilterKernel.SetArg(5, OutputBuffer); filterKernelGlobalWorkSize = new int[] { table.Length }; //mapToOutputKernel.SetArg(1, FilterOrder); //mapToOutputKernel.SetArg(2, WorkBuffer); //mapToOutputKernel.SetArg(3, OutputBuffer); mapToOutputKernelGlobalWorkSize = new int[] { filters.Count }; filterKernelLocalWorkSize = getLocalSize(filterKernelGlobalWorkSize[0]); mapToOutputLocalWorkSize = getLocalSize(mapToOutputKernelGlobalWorkSize[0]); SetOutputBufferLength(1); }
internal Context( Platform platform, ContextProperties[] properties, Device[] devices ) { IntPtr[] intPtrProperties; IntPtr[] deviceIDs; ErrorCode result; Platform = platform; deviceIDs = InteropTools.ConvertDevicesToDeviceIDs( devices ); intPtrProperties = new IntPtr[properties.Length]; for( int i=0; i<properties.Length; i++ ) intPtrProperties[i] = new IntPtr( (long)properties[i] ); ContextID = (IntPtr)OpenCL.CreateContext( intPtrProperties, (uint)devices.Length, deviceIDs, null, IntPtr.Zero, out result ); if( result!=ErrorCode.SUCCESS ) throw new OpenCLException( "CreateContext failed: "+result , result); Is64BitContext = ContainsA64BitDevice(); }
public Platform( IntPtr platformID ) { PlatformID = platformID; // Create a local representation of all devices DeviceIDs = QueryDeviceIntPtr( DeviceType.ALL ); for( int i=0; i<DeviceIDs.Length; i++ ) _Devices[DeviceIDs[i]] = new Device( this, DeviceIDs[i] ); DeviceList = InteropTools.ConvertDeviceIDsToDevices( this, DeviceIDs ); InitializeExtensionHashSet(); Match m = VersionStringRegex.Match(Version); if (m.Success) { OpenCLMajorVersion = int.Parse(m.Groups["Major"].Value); OpenCLMinorVersion = int.Parse(m.Groups["Minor"].Value); } else { OpenCLMajorVersion = 1; OpenCLMinorVersion = 0; } }
public UpSampler(Platform platform, Device device, int by, int inputBufferLen) { InputBufferLen = inputBufferLen; By = by; Platform = platform; OpenCLContext = Platform.CreateDefaultContext(); OpenCLCommandQueue = OpenCLContext.CreateCommandQueue(device); UpSampleProgram = OpenCLContext.CreateProgramWithSource(File.OpenText("opencl/upSample.cl").ReadToEnd()); UpSampleProgram.Build(); upsampleKernel = UpSampleProgram.CreateKernel("upSample"); upsample2Kernel = UpSampleProgram.CreateKernel("upSample2"); upsampleKernel.SetArg(0, by); upsampleKernel.SetArg(1, inputBufferLen); upsample2Kernel.SetArg(0, by); upsample2Kernel.SetArg(1, inputBufferLen); InputBuffer = OpenCLContext.CreateBuffer(MemFlags.WRITE_ONLY, inputBufferLen * 4); OutputBuffer = OpenCLContext.CreateBuffer(MemFlags.WRITE_ONLY, inputBufferLen * by * 4); upsampleKernel.SetArg(2, InputBuffer); upsampleKernel.SetArg(3, OutputBuffer); upsample2Kernel.SetArg(2, InputBuffer); upsample2Kernel.SetArg(3, OutputBuffer); globalworksize = new int[] { (int)(by * inputBufferLen) }; }
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); }
private void TestDevice( Device d ) { Output(""); Output("Testing device: \"" + d.Name+"\""); // d.ToString() is overloaded to output all properties as a string, so every property will be used that way Output(d.ToString()); }
public void Build(Device[] devices, ProgramNotify notify, IntPtr userData) { Build(devices, null, notify, userData); }
public Program CreateProgramWithBinary( Device[] devices, byte[][] binaries, ErrorCode[] binaryStatus ) { IntPtr programID; ErrorCode result; IntPtr[] lengths; int[] binStatus = new int[binaryStatus.Length]; lengths = new IntPtr[devices.Length]; for (int i = 0; i < lengths.Length; i++) lengths[i] = (IntPtr)binaries[i].Length; programID = OpenCL.CreateProgramWithBinary(ContextID, (uint)devices.Length, InteropTools.ConvertDevicesToDeviceIDs(devices), lengths, binaries, binStatus, out result ); for( int i=0; i<binaryStatus.Length; i++ ) binaryStatus[i] = (ErrorCode)binStatus[i]; if (result != ErrorCode.SUCCESS) throw new OpenCLException("CreateProgramWithBinary failed with error code " + result, result); return new Program(this, programID); }
public CommandQueue CreateCommandQueue( Device device, CommandQueueProperties properties ) { IntPtr commandQueueID; ErrorCode result; commandQueueID = (IntPtr)OpenCL.CreateCommandQueue( ContextID, device.DeviceID, (ulong)properties, out result ); if( result!=ErrorCode.SUCCESS ) throw new OpenCLException( "CreateCommandQueue failed with error code "+result, result); return new CommandQueue( this, device, commandQueueID ); }
public static IntPtr[] ConvertDevicesToDeviceIDs(Device[] devices) { IntPtr[] deviceIDs; if (devices == null) return null; deviceIDs = new IntPtr[devices.Length]; for (int i = 0; i < devices.Length; i++) deviceIDs[i] = devices[i]; return deviceIDs; }
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"); } }
public Context CreateContext(IntPtr[] contextProperties, Device[] devices, ContextNotify notify, IntPtr userData) { IntPtr contextID; ErrorCode result; IntPtr[] deviceIDs = InteropTools.ConvertDevicesToDeviceIDs(devices); contextID = (IntPtr)OpenCL.CreateContext(contextProperties, (uint)deviceIDs.Length, deviceIDs, notify, userData, out result); if (result != ErrorCode.SUCCESS) throw new OpenCLException("CreateContext failed with error code: " + result, result); return new Context(this, contextID); }
public BuildInfo( Program p, Device d ) { Program = p; Device = d; }
public BuildStatus GetBuildStatus( Device device ) { BuildInfo buildInfo; buildInfo = new BuildInfo( this, device ); return (BuildStatus)InteropTools.ReadInt( buildInfo, (uint)ProgramBuildInfo.STATUS ); }
public string GetBuildOptions( Device device ) { BuildInfo buildInfo; buildInfo = new BuildInfo( this, device ); return InteropTools.ReadString( buildInfo, (uint)ProgramBuildInfo.OPTIONS ); }
public string GetBuildLog( Device device ) { BuildInfo buildInfo; buildInfo = new BuildInfo( this, device ); return InteropTools.ReadString( buildInfo, (uint)ProgramBuildInfo.LOG ); }
public void Build(Device[] devices, string options, ProgramNotify notify, IntPtr userData) { ErrorCode result; IntPtr[] deviceIDs; int deviceLength = 0; if (devices != null) deviceLength = devices.Length; deviceIDs = InteropTools.ConvertDevicesToDeviceIDs(devices); result = (ErrorCode)OpenCL.BuildProgram(ProgramID, (uint)deviceLength, deviceIDs, options, notify, userData); if (result != ErrorCode.SUCCESS) throw new OpenCLBuildException(this, result); }
private void comboBoxOpenCLPlatforms_SelectedIndexChanged(object sender, EventArgs e) { try { ReleaseDeviceResources(); oclPlatform = OpenCL.GetPlatform(comboBoxOpenCLPlatforms.SelectedIndex); oclDevices = oclPlatform.QueryDevices(DeviceType.ALL); PopulateOCLDevicesComboBox(oclPlatform, DeviceType.ALL); if (comboBoxOpenCLDevices.Items.Count > 0) { comboBoxOpenCLDevices.SelectedIndex = 0; } else { oclDevice = null; } } catch (OpenCLException ocle) { MessageBox.Show(this, ocle.Message, "OpenCL exception"); } }
/// <summary> /// CreateSubDevicesEXT uses a slightly modified API, /// due to the overall messiness of creating a /// cl_device_partition_property_ext in managed C#. /// /// The object list properties is a linear list of partition properties and arguments /// add the DevicePartition property IDs and ListTerminators as ulongs and the argument lists as ints /// CreateSubDevicesEXT will use that info to construct a binary block /// </summary> /// <param name="properties"></param> public unsafe Device[] CreateSubDevicesEXT(List<object> properties) { ErrorCode result; MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); for (int i = 0; i < properties.Count; i++) { if (properties[i] is ulong) bw.Write((ulong)properties[i]); else if (properties[i] is int) bw.Write((int)properties[i]); else throw new ArgumentException("CreateSubDevicesEXT: property lists only accepts ulongs and ints"); } bw.Flush(); byte[] propertyArray = ms.ToArray(); uint numDevices; result = OpenCL.CreateSubDevicesEXT(DeviceID, propertyArray, 0, null, &numDevices); if (result != ErrorCode.SUCCESS) throw new OpenCLException("CreateSubDevicesEXT failed with error code: "+result, result); IntPtr[] subDeviceIDs = new IntPtr[(int)numDevices]; result = OpenCL.CreateSubDevicesEXT(DeviceID, propertyArray, numDevices, subDeviceIDs, null); if (result != ErrorCode.SUCCESS) throw new OpenCLException("CreateSubDevicesEXT failed with error code: " + result, result); Device[] subDevices = new Device[(int)numDevices]; for (int i = 0; i < (int)numDevices; i++) { Device d = new Device(Platform, subDeviceIDs[i]); d.IsSubDevice = true; subDevices[i] = d; } return subDevices; }
protected void SaveDeviceBinary(Context context, string fileName, byte[][] binaries, string platformDirectoryName, Device device ) { throw new NotImplementedException("SaveDeviceBinary not implemented"); }
public CommandQueue CreateCommandQueue(Device device) { return CreateCommandQueue(device, (CommandQueueProperties)0); }
public static Device[] ConvertDeviceIDsToDevices(Platform platform, IntPtr[] deviceIDs) { Device[] devices; if (deviceIDs == null) return null; devices = new Device[deviceIDs.Length]; for (int i = 0; i < deviceIDs.Length; i++) devices[i] = platform.GetDevice(deviceIDs[i]); return devices; }
internal CommandQueue( Context context, Device device, IntPtr commandQueueID ) { Context = context; Device = device; CommandQueueID = commandQueueID; }