/// <summary> /// Creates a kernel with the specified name from the program. /// </summary> /// <param name="kernelName">The name of the kernel that is defined in the program.</param> /// <exception cref="OpenClException">If the kernel could not be created, then an <see cref="OpenClException" /> is thrown.</exception> /// <returns>Returns the created kernel.</returns> public Kernel CreateKernel(string kernelName) { // Allocates enough memory for the return value and retrieves it Result result; IntPtr kernelPointer = KernelsNativeApi.CreateKernel(Handle, kernelName, out result); if (result != Result.Success) { throw new OpenClException("The kernel could not be created.", result); } // Creates a new kernel object from the kernel pointer and returns it return(new Kernel(kernelPointer)); }
private void InitCL(String[] kernelSource, String[] kernelNames, string compileArguments) { lock (lockObj) { if (!hasClInitialized && clDevice != null) { Result err; var devicesArray = new IntPtr[] { clDevice }; clContext = ContextsNativeApi.CreateContext(IntPtr.Zero, 1, devicesArray, IntPtr.Zero, IntPtr.Zero, out err); if (err != Result.Success) { throw new OpenClException("Failed to create context!", err); } commandQueue = CommandQueuesNativeApi.CreateCommandQueue(clContext, clDevice, CommandQueueProperty.None, out err); if (err != Result.Success) { throw new OpenClException("Failed to create command queue!", err); } IntPtr[] sourceList = kernelSource.Select(source => Marshal.StringToHGlobalAnsi(source)).ToArray(); clProgram = ProgramsNativeApi.CreateProgramWithSource(clContext, 1, sourceList, null, out err); if (err != Result.Success) { throw new OpenClException("Failed to create program!", err); } err = ProgramsNativeApi.BuildProgram(clProgram, 1, new IntPtr[] { clDevice }, compileArguments, IntPtr.Zero, IntPtr.Zero); if (err != Result.Success) { var infoBuffer = GetProgramBuildInformation <string>(clProgram, clDevice, ProgramBuildInformation.Log); if (err != Result.Success) { throw new OpenClException("Failed to build program! " + (infoBuffer == null ? "?" : infoBuffer.ToString()), err); } } foreach (var item in kernelNames) { kernels[item] = KernelsNativeApi.CreateKernel(clProgram, item, out err); if (err != Result.Success) { throw new OpenClException("Failed to create kernel: " + item, err); } } hasClInitialized = true; } } }