Exemplo n.º 1
0
        /// <summary>
        /// Builds the kernel arguments.
        /// </summary>
        /// <typeparam name="TSource">The type of the source.</typeparam>
        /// <param name="method">The method.</param>
        /// <param name="inputs">The inputs.</param>
        /// <param name="kernel">The kernel.</param>
        /// <param name="length">The length.</param>
        /// <param name="returnInputVariable">The return result.</param>
        /// <returns></returns>
        private Dictionary <int, GenericArrayMemory> BuildKernelArguments(KernelFunction method, object[] inputs, ComputeKernel kernel, long length, int?returnInputVariable = null)
        {
            int i = 0;
            Dictionary <int, GenericArrayMemory> result = new Dictionary <int, GenericArrayMemory>();

            foreach (var item in inputs)
            {
                int size = 0;
                if (item.GetType().IsArray)
                {
                    var mode = method.Parameters.ElementAt(i).Value.IOMode;
                    var flag = ComputeMemoryFlags.ReadWrite;
                    if (mode == IOMode.Out)
                    {
                        flag |= ComputeMemoryFlags.AllocateHostPointer;
                    }
                    else
                    {
                        flag |= ComputeMemoryFlags.CopyHostPointer;
                    }
                    GenericArrayMemory mem = new GenericArrayMemory(_context, flag, (Array)item);
                    kernel.SetMemoryArgument(i, mem);
                    result.Add(i, mem);
                }
                else if (item.GetType().Name == "XArray" || item.GetType().BaseType.Name == "XArray")
                {
                    var mode = method.Parameters.ElementAt(i).Value.IOMode;
                    var flag = ComputeMemoryFlags.ReadWrite;
                    if (mode == IOMode.Out)
                    {
                        flag |= ComputeMemoryFlags.AllocateHostPointer;
                    }
                    else
                    {
                        flag |= ComputeMemoryFlags.CopyHostPointer;
                    }
                    GenericArrayMemory mem = new GenericArrayMemory(_context, flag, (XArray)item);
                    kernel.SetMemoryArgument(i, mem);
                    result.Add(i, mem);
                }
                else
                {
                    size = Marshal.SizeOf(item);
                    var datagch = GCHandle.Alloc(item, GCHandleType.Pinned);
                    kernel.SetArgument(i, new IntPtr(size), datagch.AddrOfPinnedObject());
                }

                i++;
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the kernel code.
        /// </summary>
        /// <param name="kernalClass">The kernal class.</param>
        /// <returns></returns>
        private string GetKernelCode(Type kernalClass)
        {
            string           assemblyPath = kernalClass.Assembly.Location;
            CSharpDecompiler cSharpDecompiler
                = new CSharpDecompiler(assemblyPath, new Amplifier.Decompiler.DecompilerSettings()
            {
                ThrowOnAssemblyResolveErrors = false, ForEachStatement = false
            });
            StringBuilder   result   = new StringBuilder();
            ITypeDefinition typeInfo = cSharpDecompiler.TypeSystem.MainModule.Compilation.FindType(new FullTypeName(kernalClass.FullName)).GetDefinition();

            List <IMethod> kernelMethods    = new List <IMethod>();
            List <IMethod> nonKernelMethods = new List <IMethod>();

            foreach (var item in typeInfo.Methods)
            {
                if (item.IsConstructor)
                {
                    continue;
                }

                if (item.GetAttributes().FirstOrDefault(x => (x.AttributeType.Name == "OpenCLKernelAttribute")) == null)
                {
                    nonKernelMethods.Add(item);
                    continue;
                }

                kernelMethods.Add(item);

                var k = new KernelFunction()
                {
                    Name = item.Name
                };
                foreach (var p in item.Parameters)
                {
                    k.Parameters.Add(p.Name, p.Type.FullName);
                }

                KernelFunctions.Add(k);
            }

            var kernelHandles    = kernelMethods.ToList().Select(x => (x.MetadataToken)).ToList();
            var nonKernelHandles = nonKernelMethods.ToList().Select(x => (x.MetadataToken)).ToList();

            result.AppendLine(cSharpDecompiler.DecompileAsString(nonKernelHandles));
            result.AppendLine(cSharpDecompiler.DecompileAsString(kernelHandles));

            return(CodeTranslator.Translate(result.ToString()));
        }