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;
                }
            }
        }
示例#2
0
文件: Context.cs 项目: ByteChkR/Byt3
        /// <summary>
        /// Creates a new context for the specified device.
        /// </summary>
        /// <param name="devices">The devices for which the context is to be created.</param>
        /// <exception cref="OpenClException">If the context could not be created, then an <see cref="OpenClException"/> exception is thrown.</exception>
        /// <returns>Returns the created context.</returns>
        public static Context CreateContext(IEnumerable <Device> devices)
        {
            // Creates the new context for the specified devices
            IntPtr contextPointer = ContextsNativeApi.CreateContext(IntPtr.Zero, (uint)devices.Count(),
                                                                    devices.Select(device => device.Handle).ToArray(), IntPtr.Zero, IntPtr.Zero, out Result result);

            // Checks if the device creation was successful, if not, then an exception is thrown
            if (result != Result.Success)
            {
                throw new OpenClException("The context could not be created.", result);
            }

            // Creates the new context object from the pointer and returns it
            return(new Context(contextPointer, devices));
        }