Пример #1
0
 public unsafe static extern bool DeviceIoControl(SafeFileHandle hDevice, int controlCode,
                                                  ref KernelFunctions functions, int inputSize,
                                                  IntPtr output, int outputSize,
                                                  out int returned, NativeOverlapped *overlapped = null);
Пример #2
0
        /// <summary>
        /// Executes the specified kernel function name.
        /// </summary>
        /// <typeparam name="TSource">The type of the source.</typeparam>
        /// <param name="functionName">Name of the function.</param>
        /// <param name="args"></param>
        /// <exception cref="ExecutionException">
        /// </exception>
        public override void Execute(string functionName, params object[] args)
        {
            ValidateArgs(functionName, args);

            ComputeKernel       kernel   = _compiledKernels.FirstOrDefault(x => (x.FunctionName == functionName));
            ComputeCommandQueue commands = new ComputeCommandQueue(_context, _defaultDevice, ComputeCommandQueueFlags.None);

            if (kernel == null)
            {
                throw new ExecutionException(string.Format("Kernal function {0} not found", functionName));
            }

            try
            {
                Array       ndobject    = (Array)args.FirstOrDefault(x => (x.GetType().IsArray));
                List <long> length      = new List <long>();
                long        totalLength = 0;
                if (ndobject == null)
                {
                    var xarrayList = args.Where(x => (x.GetType().Name == "XArray" || x.GetType().BaseType.Name == "XArray")).ToList();
                    foreach (var item in xarrayList)
                    {
                        var xarrayobj = (XArray)item;
                        if (xarrayobj.Direction == Direction.Output)
                        {
                            totalLength = xarrayobj.Count;
                            if (!xarrayobj.IsElementWise)
                            {
                                length = xarrayobj.Sizes.ToList();
                            }
                            else
                            {
                                length.Add(totalLength);
                            }
                        }
                    }

                    if (totalLength == 0)
                    {
                        var xarrayobj = (XArray)xarrayList[0];
                        totalLength = xarrayobj.Count;
                        if (!xarrayobj.IsElementWise)
                        {
                            length = xarrayobj.Sizes.ToList();
                        }
                        else
                        {
                            length.Add(totalLength);
                        }
                    }
                }
                else
                {
                    totalLength = ndobject.Length;
                    for (int i = 0; i < ndobject.Rank; i++)
                    {
                        length.Add(ndobject.GetLength(i));
                    }
                }

                var method = KernelFunctions.FirstOrDefault(x => (x.Name == functionName));
                Dictionary <int, GenericArrayMemory> buffers = null;
                if (method != null)
                {
                    buffers = BuildKernelArguments(method, args, kernel, totalLength);
                }
                else
                {
                    buffers = BuildKernelArguments(args, kernel, totalLength);
                }

                if (OpenCLVars.Enabled)
                {
                    commands.Execute(kernel, OpenCLVars.GlobalWorkOffset, OpenCLVars.GlobalWorkSize, OpenCLVars.LocalWorkSize, null);
                }
                else
                {
                    commands.Execute(kernel, null, length.ToArray(), null, null);
                }

                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i].GetType().IsArray)
                    {
                        var ioMode = method != null?method.Parameters.ElementAt(i).Value.IOMode : IOMode.InOut;

                        if (ioMode == IOMode.InOut || ioMode == IOMode.Out)
                        {
                            Array r = (Array)args[i];
                            commands.ReadFromMemory(buffers[i], ref r, true, 0, null);
                        }

                        buffers[i].Dispose();
                    }
                    else if (args[i].GetType().Name == "XArray" || args[i].GetType().BaseType.Name == "XArray")
                    {
                        var ioMode = method != null?method.Parameters.ElementAt(i).Value.IOMode : IOMode.InOut;

                        if (ioMode == IOMode.InOut || ioMode == IOMode.Out)
                        {
                            XArray r = (XArray)args[i];
                            commands.ReadFromMemory(buffers[i], ref r, true, 0, null);
                        }

                        buffers[i].Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ExecutionException(ex.Message);
            }
            finally
            {
                commands.Finish();
                commands.Dispose();
            }
        }
Пример #3
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)
                {
                    var isInput  = p.GetAttributes().Any(x => x.AttributeType.Name == "InputAttribute");
                    var isOutput = p.GetAttributes().Any(x => x.AttributeType.Name == "OutputAttribute");
                    var mode     = IOMode.InOut;
                    if (isInput)
                    {
                        mode = IOMode.In;
                    }
                    if (isOutput)
                    {
                        mode = IOMode.Out;
                    }
                    if (isInput && isOutput)
                    {
                        mode = IOMode.InOut;
                    }
                    k.Parameters.Add(p.Name, new FunctionParameter
                    {
                        TypeName = p.Type.FullName,
                        IOMode   = mode
                    });
                }

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