Example #1
0
        public KernelDelegate CompileDelegate(MethodInfo method, out string code)
        {
            code = "";

            if (Programs.TryGetValue(method, out var value))
            {
                return(value);
            }

            if (!method.IsStatic)
            {
                throw new InvalidOperationException("Kernels need to be static methods!");
            }

            var attribute = method.GetCustomAttribute <KernelAttribute>();

            if (attribute == null)
            {
                throw new InvalidOperationException("Kernels must be marked with the Kernel attribute!");
            }

            var source = Compile(method);

            code = CompleteSource(source);

            try
            {
                var program = DeviceProgram.FromSource(Context, code);

                program.Build();

                var kernel = program.BuildKernel(method.Name);

                value = (workers, parameters) => KernelInvoker(source, parameters, kernel, workers);

                Programs[method] = value;

                return(value);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);

                return(null);
            }
        }