/// <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 { var ndobject = (Array)args.FirstOrDefault(x => (x.GetType().IsArray)); long length = ndobject != null ? ndobject.Length : 1; var method = KernelFunctions.FirstOrDefault(x => (x.Name == functionName)); var buffers = BuildKernelArguments(method, args, kernel, length); commands.Execute(kernel, null, new long[] { length }, null, null); for (int i = 0; i < args.Length; i++) { if (!args[i].GetType().IsArray) { continue; } var ioMode = method.Parameters.ElementAt(i).Value.IOMode; if (ioMode == IOMode.InOut || ioMode == IOMode.Out) { Array r = (Array)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(); } }
/// <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)); var buffers = BuildKernelArguments(method, args, kernel, totalLength); commands.Execute(kernel, null, length.ToArray(), null, null); for (int i = 0; i < args.Length; i++) { if (args[i].GetType().IsArray) { var ioMode = method.Parameters.ElementAt(i).Value.IOMode; 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.Parameters.ElementAt(i).Value.IOMode; 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(); } }