Exemple #1
0
        public static MemoryBuffer Copy <T>(CLAPI instance, MemoryBuffer input) where T : struct
        {
            CLKernel k = null;

            if (CopyKernels.ContainsKey(instance))
            {
                k = CopyKernels[instance];
            }
            else
            {
                string clt = KernelParameter.GetDataString(KernelParameter.GetEnumFromType(typeof(T)));
                string src = COPY_KERNEL_SOURCE.Replace("__TYPE__", clt);
                CLProgram.TryBuildProgram(instance, src, "internal_copy_kernel.cl", out CLProgram prog);
                k = prog.ContainedKernels["copy"];
                CopyKernels[instance] = k;
            }

            MemoryBuffer mb = CreateEmpty <T>(
                instance,
                ( int )input.Size,
                input.Flags,
                "CopyOf:" + input,
                true
                );

            k.SetBuffer(0, mb);
            k.SetBuffer(1, input);
            Run(instance, k, ( int )mb.Size);

            return(mb);
        }
Exemple #2
0
        public CLProgram AddProgram(CLAPI instance, string file, bool throwEx, out CLProgramBuildResult ex)
        {
            ex = new CLProgramBuildResult(file, "", new List <CLProgramBuildError>());

            if (!File.Exists(file))
            {
                Exception e = new FileNotFoundException("File not found: " + file);

                if (throwEx)
                {
                    throw e;
                }

                ex.BuildErrors.Add(new CLProgramBuildError(ErrorType.ProgramBuild, e));
            }

            string path = file;

            Logger.Log(LogType.Log, "Creating CLProgram from file: " + file, 3);
            CLProgramBuildResult br = CLProgram.TryBuildProgram(instance, path, out CLProgram program);

            ex.Source = br.Source;

            if (!br)
            {
                if (throwEx)
                {
                    throw br.GetAggregateException();
                }

                ex.BuildErrors.AddRange(br.BuildErrors);

                return(null);
            }

            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
                        );
                }
            }

            return(program);
        }
Exemple #3
0
        public CLProgram AddProgram(
            CLAPI instance,
            string source,
            string filePath,
            bool throwEx,
            out CLProgramBuildResult ex)
        {
            ex = new CLProgramBuildResult(filePath, "", new List <CLProgramBuildError>());
            CLProgramBuildResult br = CLProgram.TryBuildProgram(instance, source, filePath, out CLProgram program);

            ex.Source = br.Source;

            if (!br)
            {
                if (throwEx)
                {
                    throw br.GetAggregateException();
                }

                ex.BuildErrors.AddRange(br.BuildErrors);

                return(null);
            }

            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
                        );
                }
            }

            return(program);
        }
Exemple #4
0
        public static CLProgramBuildResult TryBuildProgram(
            CLAPI instance,
            string source,
            string filePath,
            out CLProgram program)
        {
            CLProgramBuildResult result = new CLProgramBuildResult(
                filePath,
                source,
                new List <CLProgramBuildError>()
                );

            string[] kernelNames = FindKernelNames(source);

            if (kernelNames.Length == 0)
            {
                program = new CLProgram(filePath, new Dictionary <string, CLKernel>(), source);

                return(result);
            }

            Program prgHandle;

            try
            {
                prgHandle = CLAPI.CreateClProgramFromSource(instance, source);
            }
            catch (Exception e)
            {
                result.BuildErrors.Add(new CLProgramBuildError(ErrorType.ProgramBuild, e));
                program = null;

                return(result); //We can not progress
            }

            Dictionary <string, CLKernel> kernels = new Dictionary <string, CLKernel>();

            foreach (string kernelName in kernelNames)
            {
                try
                {
                    Kernel k = CLAPI.CreateKernelFromName(prgHandle, 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)
                    {
                        result.BuildErrors.Add(
                            new CLProgramBuildError(
                                ErrorType.KernelBuild,
                                new OpenClException(
                                    $"Header parser completed on {kernelName} but the kernel could not be loaded."
                                    )
                                )
                            );

                        kernels.Add(kernelName, new CLKernel(instance, null, kernelName, parameter));
                    }
                    else
                    {
                        kernels.Add(kernelName, new CLKernel(instance, k, kernelName, parameter));
                    }
                }
                catch (Exception e)
                {
                    result.BuildErrors.Add(new CLProgramBuildError(ErrorType.KernelBuild, e));

                    //We can try other kernels
                }
            }

            program = new CLProgram(filePath, kernels, source);
            program.ClProgramHandle = prgHandle;

            return(result);
        }
Exemple #5
0
        public static CLProgramBuildResult TryBuildProgram(CLAPI instance, string filePath, out CLProgram program)
        {
            //string source = TextProcessorAPI.PreprocessSource(IOManager.ReadAllLines(filePath),
            //    Path.GetDirectoryName(filePath), Path.GetExtension(filePath), new Dictionary<string, bool>());
            string source = TextProcessorAPI.PreprocessSource(filePath, new Dictionary <string, bool>());

            //            program = null;

            return(TryBuildProgram(instance, source, filePath, out program));
        }