/// <summary> /// Loads the source and initializes the CLProgram /// </summary> private void Initialize(CLAPI instance) { int vnum = GetVectorNum(genType); string source = TextProcessorAPI.PreprocessSource(filePath, new Dictionary <string, bool>()); string[] kernelNames = FindKernelNames(source); ClProgramHandle = CLAPI.CreateClProgramFromSource(instance, source); foreach (string kernelName in kernelNames) { Kernel k = CLAPI.CreateKernelFromName(ClProgramHandle, kernelName); int kernelNameIndex = source.IndexOf(" " + kernelName + " ", StringComparison.InvariantCulture); kernelNameIndex = kernelNameIndex == -1 ? source.IndexOf(" " + kernelName + "(", StringComparison.InvariantCulture) : kernelNameIndex; KernelParameter[] parameter = KernelParameter.CreateKernelParametersFromKernelCode(source, kernelNameIndex, source.Substring(kernelNameIndex, source.Length - kernelNameIndex).IndexOf(')') + 1); if (k == null) { ContainedKernels.Add(kernelName, new CLKernel(instance, null, kernelName, parameter)); } else { ContainedKernels.Add(kernelName, new CLKernel(instance, k, kernelName, parameter)); } } }
/// <summary> /// Runs a kernel with a valid FL kernel signature /// </summary> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="kernel">The CLKernel to be executed</param> /// <param name="image">The image buffer that serves as input</param> /// <param name="dimensions">The dimensions of the input buffer</param> /// <param name="genTypeMaxVal">The max valuee of the generic type that is used.(byte = 255)</param> /// <param name="enabledChannels">The enabled channels for the kernel</param> /// <param name="channelCount">The amount of active channels.</param> public static void Run(CLAPI instance, CLKernel kernel, MemoryBuffer image, int3 dimensions, float genTypeMaxVal, MemoryBuffer enabledChannels, int channelCount) { kernel.Run(instance.commandQueue, image, dimensions, genTypeMaxVal, enabledChannels, channelCount); }
/// <summary> /// Manually adds a Program to the database /// </summary> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="file">Path fo the file</param> public void AddProgram(CLAPI instance, string file) { if (!IOManager.FileExists(file)) { throw new FileNotFoundException("File not found: " + file); } string path = file; Logger.Log(LogType.Log, "Creating CLProgram from file: " + file, 3); CLProgram program = new CLProgram(instance, path); loadedPrograms.Add(program); foreach (KeyValuePair <string, CLKernel> containedKernel in program.ContainedKernels) { if (!loadedKernels.ContainsKey(containedKernel.Key)) { Logger.Log(LogType.Log, "Adding Kernel: " + containedKernel.Key, 4); loadedKernels.Add(containedKernel.Key, containedKernel.Value); } else { Logger.Log(LogType.Log, "Kernel with name: " + containedKernel.Key + " is already loaded. Skipping...", 5); } } }
/// <summary> /// Manually adds a Program to the database /// </summary> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="file">Path fo the file</param> public void AddProgram(CLAPI instance, string file) { if (!CLAPI.FileExists(file)) { throw new Exception(file); } string path = Path.GetFullPath(file); logger.Log(LogType.Log, "Creating CLProgram from file: " + file); CLProgram program = new CLProgram(instance, path, GenDataType); foreach (KeyValuePair <string, CLKernel> containedKernel in program.ContainedKernels) { if (!LoadedKernels.ContainsKey(containedKernel.Key)) { logger.Log(LogType.Log, "Adding Kernel: " + containedKernel.Key); LoadedKernels.Add(containedKernel.Key, containedKernel.Value); } else { logger.Log(LogType.Log, "Kernel with name: " + containedKernel.Key + " is already loaded. Skipping..."); } } }
/// <summary> /// Public Constructor /// </summary> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="filePath">The FilePath where the source is located</param> /// <param name="genType">The Gen Type used</param> public CLProgram(CLAPI instance, string filePath) { this.filePath = filePath; ContainedKernels = new Dictionary <string, CLKernel>(); Initialize(instance); }
public string[] ReadAllLines(string file) { TextReader tr = new StreamReader(CLAPI.GetStream(file)); string[] ret = tr.ReadToEnd().Replace("\r", "").Split('\n'); tr.Close(); return(ret); }
//Optimization private static MemoryBuffer CreateEmptyOptimized <T>(CLAPI instance, int size, MemoryFlag flags, object handleIdentifier) where T : struct { //return CreateBuffer(instance, new byte[size], flags, handleIdentifier); int bufByteSize = Marshal.SizeOf <T>() * size; return(instance.context.CreateBuffer(flags | MemoryFlag.AllocateHostPointer, bufByteSize, handleIdentifier)); }
/// <summary> /// Initializes the Kernel Database /// </summary> private void Initialize(CLAPI instance) { string[] files = CLAPI.GetFiles(folderName, "*.cl"); foreach (string file in files) { AddProgram(instance, file); } }
public static MemoryBuffer CreateBuffer(CLAPI instance, object[] data, Type t, MemoryFlag flags, object handleIdentifier) { MemoryBuffer mb = instance.context.CreateBuffer(flags | MemoryFlag.CopyHostPointer, t, data, handleIdentifier); return(mb); }
public static void UpdateBitmap(CLAPI instance, Bitmap target, byte[] bytes) { BGRAtoARGB(bytes); BitmapData data = target.LockBits(new Rectangle(0, 0, target.Width, target.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); Marshal.Copy(bytes, 0, data.Scan0, bytes.Length); target.UnlockBits(data); }
/// <summary> /// Casts the supplied value to the specified type /// </summary> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="value">the value casted to the required type for the parameter</param> /// <returns></returns> public object CastToType(CLAPI instance, object value) { if (IsArray) { throw new OpenClException("Can not Change types on an array."); } return(CastToType(Converters[(int)DataType], value)); }
internal static Program CreateClProgramFromSource(CLAPI instance, string source) { try { return(instance.context.CreateAndBuildProgramFromString(source)); } catch (Exception e) { throw new OpenClException("Could not compile file", e); } }
/// <summary> /// Public constructor /// </summary> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="folderName">Folder name where the kernels are located</param> /// <param name="genDataType">The DataTypes used to compile the FL Database</param> public KernelDatabase(CLAPI instance, string folderName, TypeEnums.DataTypes genDataType) { GenDataType = KernelParameter.GetDataString(genDataType); if (!CLAPI.DirectoryExists(folderName)) { throw new Exception(folderName); } this.folderName = folderName; LoadedKernels = new Dictionary <string, CLKernel>(); Initialize(instance); }
/// <summary> /// Writes random values to a MemoryBuffer /// </summary> /// <typeparam name="T">Type of the values</typeparam> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="buf">MemoryBuffer containing the values to overwrite</param> /// <param name="rnd">the RandomFunc delegate providing the random numbers.</param> /// <param name="enabledChannels">the channels that are enables(aka. get written with bytes)</param> /// <param name="uniform">Should every channel receive the same value on the same pixel?</param> public static void WriteRandom <T>(CLAPI instance, MemoryBuffer buf, RandomFunc <T> rnd, byte[] enabledChannels, bool uniform) where T : struct { MemoryBuffer buffer = buf; T[] data = instance.commandQueue.EnqueueReadBuffer <T>(buffer, (int)buffer.Size); WriteRandom(data, enabledChannels, rnd, uniform); instance.commandQueue.EnqueueWriteBuffer(buffer, data); }
/// <summary> /// Public constructor /// </summary> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="folderName">Folder name where the kernels are located</param> /// <param name="genDataVectorType">The DataVectorTypes used to compile the FL Database</param> public KernelDatabase(CLAPI instance, string folderName, TypeEnums.DataVectorTypes genDataVectorType) : base( OpenCLDebugConfig.Settings) { GenDataType = KernelParameter.GetDataString(genDataVectorType); if (!IOManager.DirectoryExists(folderName)) { throw new OpenClException("Can not find directory: " + folderName); } this.folderName = folderName; loadedPrograms = new List <CLProgram>(); loadedKernels = new Dictionary <string, CLKernel>(); Initialize(instance); }
/// <summary> /// Casts the supplied value to the specified type /// </summary> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="value">the value casted to the required type for the parameter</param> /// <returns></returns> public object CastToType(CLAPI instance, object value) { if (IsArray) { object[] data = (object[])value; return(CLAPI.CreateBuffer(instance, Array.ConvertAll(data, x => CastToType(Converters[(int)DataType], x)), Converters[(int)DataType], MemoryFlag.CopyHostPointer | MemoryFlag.ReadOnly)); } return(CastToType(Converters[(int)DataType], value)); }
/// <summary> /// Constructor /// </summary> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="k">The Compiled and Linked Kernel</param> /// <param name="name">The name of the kernel</param> /// <param name="parameter">The parsed KernelParameter</param> public CLKernel(CLAPI instance, Kernel k, string name, KernelParameter[] parameter) { this.instance = instance; Kernel = k; Name = name; Parameter = new Dictionary <string, KernelParameter>(); IEnumerable <KeyValuePair <string, KernelParameter> > l = parameter.Select(x => new KeyValuePair <string, KernelParameter>(x.Name, x)); foreach (KeyValuePair <string, KernelParameter> keyValuePair in l) { Parameter.Add(keyValuePair.Key, keyValuePair.Value); } }
/// <summary> /// Creates a buffer with the content of an image and the specified Memory Flags /// </summary> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="bmp">The image that holds the data</param> /// <param name="flags">The memory flags for the buffer creation</param> /// <returns></returns> public static MemoryBuffer CreateFromImage(CLAPI instance, Bitmap bmp, MemoryFlag flags, object handleIdentifier) { BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); byte[] buffer = new byte[bmp.Width * bmp.Height * 4]; Marshal.Copy(data.Scan0, buffer, 0, buffer.Length); bmp.UnlockBits(data); ARGBtoBGRA(buffer); MemoryBuffer mb = CreateBuffer(instance, buffer, flags, handleIdentifier); return(mb); }
public static void DisposeInstance() { Instance.Dispose(); Instance = null; }
internal static Program CreateClProgramFromSource(CLAPI instance, string[] source) { return(instance.context.CreateAndBuildProgramFromString(source)); }
/// <summary> /// Writes values to a MemoryBuffer /// </summary> /// <typeparam name="T">Type of the values</typeparam> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="buf">MemoryBuffer containing the values to overwrite</param> /// <param name="size">The count of structs to be read from the buffer</param> /// <returns>The content of the buffer</returns> public static T[] ReadBuffer <T>(CLAPI instance, MemoryBuffer buf, int size) where T : struct { return(instance.commandQueue.EnqueueReadBuffer <T>(buf, size)); }
public string[] GetFiles(string path, string searchPattern = "*") { return(CLAPI.GetFiles(path, searchPattern)); }
/// <summary> /// Creates an empty buffer of type T with the specified size and MemoryFlags /// </summary> /// <typeparam name="T">The type of the struct</typeparam> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="size">The size of the buffer(Total size in bytes: size*sizeof(T)</param> /// <param name="flags">The memory flags for the buffer creation</param> /// <returns></returns> public static MemoryBuffer CreateEmpty <T>(CLAPI instance, int size, MemoryFlag flags, object handleIdentifier) where T : struct { return(CreateEmptyOptimized <T>(instance, size, flags, handleIdentifier)); }
public bool FileExists(string file) { return(CLAPI.FileExists(file)); }
/// <summary> /// Reinitializes the CL API /// Used by The unit tests when requireing actual CL api calls(e.g. not in NO_CL mode) /// </summary> public static void Reinitialize() { Instance = new CLAPI(); }
public static void Flush(CLAPI instance) { instance.commandQueue.Flush(); }
/// <summary> /// Creates a Buffer with the specified content and Memory Flags /// </summary> /// <typeparam name="T">Type of the struct</typeparam> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="data">The array of T</param> /// <param name="flags">The memory flags for the buffer creation</param> /// <returns></returns> public static MemoryBuffer CreateBuffer <T>(CLAPI instance, T[] data, MemoryFlag flags, object handleIdentifier) where T : struct { object[] arr = Array.ConvertAll(data, x => (object)x); return(CreateBuffer(instance, arr, typeof(T), flags, handleIdentifier)); }
public static void UpdateBitmap(CLAPI instance, Bitmap target, MemoryBuffer buffer) { byte[] bs = ReadBuffer <byte>(instance, buffer, (int)buffer.Size); UpdateBitmap(instance, target, bs); }
/// <summary> /// Returns the Command queue(dont use, its just for debugging if something is wrong) /// </summary> /// <returns>The Internal Command queue</returns> internal static CommandQueue GetQueue(CLAPI instance) { return(instance.commandQueue); }
public static void Run(CLAPI instance, CLKernel kernel, int groupSize) { kernel.Run(instance.commandQueue, 1, groupSize); }