private static extern ErrorCode clGetKernelWorkGroupInfo(
     KernelSafeHandle kernel,
     ClDeviceID device,
     int paramName,
     UIntPtr paramValueSize,
     IntPtr paramValue,
     out UIntPtr paramValueSizeRet);
 private static extern ErrorCode clGetKernelArgInfo(
     KernelSafeHandle kernel,
     int argumentIndex,
     int paramName,
     UIntPtr paramValueSize,
     IntPtr paramValue,
     out UIntPtr paramValueSizeRet);
 private static extern ErrorCode clEnqueueNDRangeKernel(
     CommandQueueSafeHandle commandQueue,
     KernelSafeHandle kernel,
     uint workDim,
     [In, MarshalAs(UnmanagedType.LPArray)] IntPtr[] globalWorkOffset,
     [In, MarshalAs(UnmanagedType.LPArray)] IntPtr[] globalWorkSize,
     [In, MarshalAs(UnmanagedType.LPArray)] IntPtr[] localWorkSize,
     uint numEventsInWaitList,
     [In, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(SafeHandleArrayMarshaler))] EventSafeHandle[] eventWaitList,
     out EventSafeHandle @event);
        public static T GetKernelArgInfo <T>(KernelSafeHandle kernel, int argumentIndex, KernelArgParameterInfo <T> parameter)
        {
            if (kernel == null)
            {
                throw new ArgumentNullException("kernel");
            }
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }
            if (argumentIndex < 0)
            {
                throw new ArgumentOutOfRangeException("argumentIndex");
            }

            int?fixedSize = parameter.ParameterInfo.FixedSize;

#if DEBUG
            bool verifyFixedSize = true;
#else
            bool verifyFixedSize = false;
#endif

            UIntPtr requiredSize;
            if (fixedSize.HasValue && !verifyFixedSize)
            {
                requiredSize = (UIntPtr)fixedSize;
            }
            else
            {
                ErrorHandler.ThrowOnFailure(clGetKernelArgInfo(kernel, argumentIndex, parameter.ParameterInfo.Name, UIntPtr.Zero, IntPtr.Zero, out requiredSize));
            }

            if (verifyFixedSize && fixedSize.HasValue)
            {
                if (requiredSize.ToUInt64() != (ulong)fixedSize.Value)
                {
                    throw new ArgumentException("The parameter definition includes a fixed size that does not match the required size according to the runtime.");
                }
            }

            IntPtr memory = IntPtr.Zero;
            try
            {
                memory = Marshal.AllocHGlobal((int)requiredSize.ToUInt32());
                UIntPtr actualSize;
                ErrorHandler.ThrowOnFailure(clGetKernelArgInfo(kernel, argumentIndex, parameter.ParameterInfo.Name, requiredSize, memory, out actualSize));
                return(parameter.ParameterInfo.Deserialize(actualSize, memory));
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }
        }
        public static void SetKernelArg(KernelSafeHandle kernel, int argumentIndex, UIntPtr argSize, IntPtr argValue)
        {
            if (kernel == null)
            {
                throw new ArgumentNullException("kernel");
            }
            if (argumentIndex < 0)
            {
                throw new ArgumentOutOfRangeException("argumentIndex");
            }

            ErrorHandler.ThrowOnFailure(clSetKernelArg(kernel, argumentIndex, argSize, argValue));
        }
Example #6
0
        internal Kernel(KernelSafeHandle handle, Program program)
        {
            if (handle == null)
            {
                throw new ArgumentNullException("handle");
            }
            if (program == null)
            {
                throw new ArgumentNullException("program");
            }

            _handle  = handle;
            _program = program;
        }
        public static EventSafeHandle EnqueueTask(
            CommandQueueSafeHandle commandQueue,
            KernelSafeHandle kernel,
            EventSafeHandle[] eventWaitList)
        {
            if (commandQueue == null)
            {
                throw new ArgumentNullException("commandQueue");
            }
            if (kernel == null)
            {
                throw new ArgumentNullException("kernel");
            }

            EventSafeHandle result;

            ErrorHandler.ThrowOnFailure(clEnqueueTask(commandQueue, kernel, GetNumItems(eventWaitList), GetItems(eventWaitList), out result));
            return(result);
        }
        public static EventSafeHandle EnqueueNDRangeKernel(
            CommandQueueSafeHandle commandQueue,
            KernelSafeHandle kernel,
            IntPtr[] globalWorkOffset,
            IntPtr[] globalWorkSize,
            IntPtr[] localWorkSize,
            EventSafeHandle[] eventWaitList)
        {
            if (commandQueue == null)
            {
                throw new ArgumentNullException("commandQueue");
            }
            if (kernel == null)
            {
                throw new ArgumentNullException("kernel");
            }
            if (globalWorkSize == null)
            {
                throw new ArgumentNullException("globalWorkSize");
            }
            if (localWorkSize == null)
            {
                throw new ArgumentNullException("localWorkSize");
            }

            uint workDim = (uint)globalWorkSize.Length;

            if (globalWorkOffset != null && globalWorkOffset.Length != workDim)
            {
                throw new ArgumentException();
            }
            if (localWorkSize != null && localWorkSize.Length != workDim)
            {
                throw new ArgumentException();
            }

            EventSafeHandle result;

            ErrorHandler.ThrowOnFailure(clEnqueueNDRangeKernel(commandQueue, kernel, (uint)workDim, globalWorkOffset, globalWorkSize, localWorkSize, GetNumItems(eventWaitList), GetItems(eventWaitList), out result));
            return(result);
        }
        public static KernelSafeHandle CreateKernel(ProgramSafeHandle program, string kernelName)
        {
            if (program == null)
            {
                throw new ArgumentNullException("program");
            }
            if (kernelName == null)
            {
                throw new ArgumentNullException("kernelName");
            }
            if (string.IsNullOrEmpty(kernelName))
            {
                throw new ArgumentException();
            }

            ErrorCode        errorCode;
            KernelSafeHandle kernel = clCreateKernel(program, kernelName, out errorCode);

            ErrorHandler.ThrowOnFailure(errorCode);
            return(kernel);
        }
 private static extern ErrorCode clEnqueueTask(
     CommandQueueSafeHandle commandQueue,
     KernelSafeHandle kernel,
     uint numEventsInWaitList,
     [In, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(SafeHandleArrayMarshaler))] EventSafeHandle[] eventWaitList,
     out EventSafeHandle @event);
Example #11
0
        public Kernel CreateKernel(string name)
        {
            KernelSafeHandle kernel = UnsafeNativeMethods.CreateKernel(Handle, name);

            return(new Kernel(kernel, this));
        }
 private static extern ErrorCode clSetKernelArg(
     KernelSafeHandle kernel,
     int argumentIndex,
     UIntPtr argSize,
     IntPtr argValue);
 private static extern ErrorCode clRetainKernel(KernelSafeHandle kernel);
        public static ulong[] GetKernelWorkGroupInfo(KernelSafeHandle kernel, ClDeviceID device, KernelWorkGroupInfo kernelWorkGroupInfo)
        {
            if (kernel == null)
            {
                throw new ArgumentNullException("kernel");
            }

            int size    = 0;
            int arrayCt = 0;

            switch (kernelWorkGroupInfo)
            {
            case KernelWorkGroupInfo.GlobalWorkSize:
                size    = UIntPtr.Size;
                arrayCt = 3;
                break;

            case KernelWorkGroupInfo.WorkGroupSize:
                size    = UIntPtr.Size;
                arrayCt = 1;
                break;

            case KernelWorkGroupInfo.CompileWorkGroupSize:
                size    = UIntPtr.Size;
                arrayCt = 3;
                break;

            case KernelWorkGroupInfo.LocalMemorySize:
                size    = sizeof(ulong);
                arrayCt = 1;
                break;

            case KernelWorkGroupInfo.PreferredWorkGroupSizeMultiple:
                size    = UIntPtr.Size;
                arrayCt = 1;
                break;

            case KernelWorkGroupInfo.PrivateMemorySize:
                size    = sizeof(ulong);
                arrayCt = 1;
                break;
            }
            int fixedSize = size * arrayCt;

#if DEBUG
            UIntPtr requiredSize;
            ErrorHandler.ThrowOnFailure(clGetKernelWorkGroupInfo(kernel, device, (int)kernelWorkGroupInfo, UIntPtr.Zero, IntPtr.Zero, out requiredSize));
            if (requiredSize.ToUInt64() != (ulong)fixedSize)
            {
                throw new ArgumentException("The parameter definition includes a fixed size that does not match the required size according to the runtime.");
            }
#endif

            IntPtr memory = IntPtr.Zero;
            try
            {
                memory = Marshal.AllocHGlobal(fixedSize);
                UIntPtr actualSize;
                ErrorHandler.ThrowOnFailure(clGetKernelWorkGroupInfo(kernel, device, (int)kernelWorkGroupInfo, (UIntPtr)fixedSize, memory, out actualSize));
                IntPtr[] array = new IntPtr[(int)((long)actualSize.ToUInt64() / IntPtr.Size)];
                Marshal.Copy(memory, array, 0, array.Length);

                ulong[] outArray = new ulong[arrayCt];
                for (int i = 0; i < arrayCt; i++)
                {
                    outArray[i] = (ulong)array[i];
                }

                return(outArray);
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }
        }