Ejemplo n.º 1
0
        public static Device[] GetDevices(ProgramSafeHandle program)
        {
            UIntPtr requiredSize;
            int     val = 0x1163;

            ErrorHandler.ThrowOnFailure(clGetProgramInfo(program, val, UIntPtr.Zero, IntPtr.Zero, out requiredSize));

            uint size = requiredSize.ToUInt32();

            if (size == 0)
            {
                return(new Device[0]);
            }

            IntPtr memory = IntPtr.Zero;

            try
            {
                memory = Marshal.AllocHGlobal((int)requiredSize.ToUInt32());
                UIntPtr actualSize;
                ErrorHandler.ThrowOnFailure(clGetProgramInfo(program, val, requiredSize, memory, out actualSize));
                IntPtr[] array = new IntPtr[(int)((long)actualSize.ToUInt64() / IntPtr.Size)];
                Marshal.Copy(memory, array, 0, array.Length);

                return(Array.ConvertAll(array, d => new Device(d)));
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }
        }
Ejemplo n.º 2
0
 private static extern ErrorCode clBuildProgram(
     ProgramSafeHandle program,
     uint numDevices,
     [In, MarshalAs(UnmanagedType.LPArray)] ClDeviceID[] devices,
     [MarshalAs(UnmanagedType.LPStr)] string options,
     BuildProgramCallback pfnNotify,
     IntPtr userData);
Ejemplo n.º 3
0
        public static Binary[] GetBinaries(ProgramSafeHandle program)
        {
            int[]      sizes     = GetBinarySizes(program);
            GCHandle[] gh        = Array.ConvertAll(sizes, s => GCHandle.Alloc(new byte[s], GCHandleType.Pinned));
            IntPtr[]   binaries2 = Array.ConvertAll(gh, h => h.AddrOfPinnedObject());

            int     progCt = sizes.Length;
            UIntPtr actual = UIntPtr.Zero;

            try
            {
                ErrorHandler.ThrowOnFailure(clGetProgramInfo(program, 0x1166, new UIntPtr((uint)(UIntPtr.Size * progCt)), binaries2, out actual));

                if (IntPtr.Size * progCt != actual.ToUInt32())
                {
                    throw new Exception(string.Format("unexpected clGetProgramInfo size: {0}!={1}", actual.ToUInt32(), IntPtr.Size * progCt));
                }

                return(Array.ConvertAll(gh, g => new Binary((byte[])g.Target)));
            }
            finally
            {
                for (int i = 0; i < progCt; i++)
                {
                    gh[i].Free();
                }
            }
        }
Ejemplo n.º 4
0
 private static extern ErrorCode clGetProgramBuildInfo(
     ProgramSafeHandle program,
     ClDeviceID device,
     int paramName,
     UIntPtr paramValueSize,
     IntPtr paramValue,
     out UIntPtr paramValueSizeRet);
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a program object for a <see cref="Context"/>, and loads specified binary
        /// data into the <see cref="Program"/> object.
        /// </summary>
        /// <remarks>
        /// OpenCL allows applications to create a program object using the program source or binary
        /// and build appropriate program executables. This allows applications to determine whether
        /// they want to use the pre-built offline binary or load and compile the program source
        /// and use the executable compiled/linked online as the program executable. This can be
        /// very useful as it allows applications to load and build program executables online on
        /// its first instance for appropriate OpenCL devices in the system. These executables can
        /// now be queried and cached by the application. Future instances of the application
        /// launching will no longer need to compile and build the program executables. The cached
        /// executables can be read and loaded by the application, which can help significantly
        /// reduce the application initialization time.
        /// </remarks>
        /// <param name="devices">A pointer to a list of devices that are in context. device_list
        /// must be a non-NULL value. The binaries are loaded for devices specified in this list.</param>
        /// <param name="bins">An array of pointers to program binaries to be loaded for devices
        /// specified by device_list. For each device given by device_list[i], the pointer to the
        /// program binary for that device is given by binaries[i] and the length of this
        /// corresponding binary is given by lengths[i]. lengths[i] cannot be zero and binaries[i]
        /// cannot be a NULL pointer.</param>
        /// <returns>
        /// <see cref="Program"/>
        /// </returns>
        public Program CreateProgramWithBinary(byte[][] bins, params Device[] devices)
        {
            UnsafeNativeMethods.ClDeviceID[] deviceIDs = Array.ConvertAll(devices, device => device.ID);
            ProgramSafeHandle handle = UnsafeNativeMethods.CreateProgramWithBinary(_context, deviceIDs, bins);

            return(new Program(this, handle));
        }
Ejemplo n.º 6
0
 private static extern ErrorCode clCompileProgram(
     ProgramSafeHandle program,
     uint numDevices,
     [In, MarshalAs(UnmanagedType.LPArray)] ClDeviceID[] devices,
     string options,
     uint numInputHeaders,
     [In, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(SafeHandleArrayMarshaler))] ProgramSafeHandle[] inputHeaders,
     [In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] string[] headerIncludeNames,
     [MarshalAs(UnmanagedType.FunctionPtr)] CompileProgramCallback callbackPointer, IntPtr userData);
Ejemplo n.º 7
0
        internal Program(Context context, ProgramSafeHandle handle)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            if (handle == null)
                throw new ArgumentNullException("handle");

            _context = context;
            _handle = handle;
        }
Ejemplo n.º 8
0
        internal Program(Context context, ProgramSafeHandle handle)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (handle == null)
            {
                throw new ArgumentNullException(nameof(handle));
            }

            _context = context;
            _handle  = handle;
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Compiles a program’s source for all the devices or a specific device(s) in the OpenCL context associated with program.
 /// </summary>
 /// <param name="program">The program object that is the compilation target.</param>
 /// <param name="devices">A pointer to a list of devices associated with program. If device_list is
 /// a NULL value, the compile is performed for all devices associated with program. If device_list
 /// is a non-NULL value, the compile is performed for devices specified in this list.</param>
 /// <param name="options">A pointer to a null-terminated string of characters that describes the
 /// compilation options to be used for building the program executable. The list of supported options
 /// is as described below.</param>
 /// <param name="inputHeaders">An array of program embedded headers created with
 /// <see cref="clCreateProgramWithSource"/>.</param>
 /// <param name="headerIncludeNames">An array that has a one to one correspondence with input_headers.
 /// Each entry in header_include_names specifies the include name used by source in program that comes
 /// from an embedded header. The corresponding entry in input_headers identifies the program object
 /// which contains the header source to be used. The embedded headers are first searched before the
 /// headers in the list of directories specified by the –I compile option (as described in section
 /// 5.8.4.1). If multiple entries in header_include_names refer to the same header name, the first one
 /// encountered will be used.</param>
 /// <param name="pfnNotify">A function pointer to a notification routine. The notification routine is
 /// a callback function that an application can register and which will be called when the program
 /// executable has been built (successfully or unsuccessfully). If pfnNotify is not NULL,
 /// clCompileProgram does not need to wait for the compiler to complete and can return immediately
 /// once the compilation can begin. The compilation can begin if the context, program whose sources
 /// are being compiled, list of devices, input headers, programs that describe input headers and
 /// compiler options specified are all valid and appropriate host and device resources needed to
 /// perform the compile are available. If pfn_notify is NULL, clCompileProgram does not return until
 /// the compiler has completed. This callback function may be called asynchronously by the OpenCL
 /// implementation. It is the application’s responsibility to ensure that the callback function is
 /// thread-safe.</param>
 /// <param name="userData">Passed as an argument when pfnNotify is called. user_data can be NULL.</param>
 public static void CompileProgram(ProgramSafeHandle program, ClDeviceID[] devices, string options, ProgramSafeHandle[] inputHeaders,
                                   string[] headerIncludeNames, CompileProgramCallback pfnNotify, IntPtr userData)
 {
     ErrorHandler.ThrowOnFailure(clCompileProgram(
                                     program,
                                     (devices != null && devices.Length > 0) ? (uint)devices.Length : 0,
                                     devices,
                                     options,
                                     (uint)inputHeaders.Length,
                                     inputHeaders,
                                     headerIncludeNames,
                                     pfnNotify,
                                     userData));
 }
Ejemplo n.º 10
0
        public static T GetProgramBuildInfo <T>(ProgramSafeHandle program, ClDeviceID device, ProgramBuildParameterInfo <T> parameter)
        {
            if (program == null)
            {
                throw new ArgumentNullException("program");
            }
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            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(clGetProgramBuildInfo(program, device, 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(clGetProgramBuildInfo(program, device, parameter.ParameterInfo.Name, requiredSize, memory, out actualSize));
                return(parameter.ParameterInfo.Deserialize(actualSize, memory));
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }
        }
Ejemplo n.º 11
0
        public static ProgramSafeHandle CreateProgramWithSource(ContextSafeHandle context, string[] strings)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (strings == null)
            {
                throw new ArgumentNullException("strings");
            }

            ErrorCode         errorCode;
            ProgramSafeHandle handle = clCreateProgramWithSource(context, (uint)strings.Length, strings, null, out errorCode);

            ErrorHandler.ThrowOnFailure(errorCode);
            return(handle);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Returns an array that contains the size in bytes of the program binary for each device associated with program. The size of the array is the number of devices associated with program. If a binary is not available for a device(s), a size of zero is returned.
        /// </summary>
        /// <remarks>
        /// If program is created using clCreateProgramWithBuiltInKernels, the implementation may return zero in any entries of the returned array.
        /// </remarks>
        public static int[] GetBinarySizes(ProgramSafeHandle program)
        {
            IntPtr  memory = IntPtr.Zero;
            UIntPtr actualSize;

            try
            {
                memory = Marshal.AllocHGlobal(IntPtr.Size);
                ErrorHandler.ThrowOnFailure(clGetProgramInfo(program, 0x1165, (UIntPtr)UIntPtr.Size, memory, out actualSize));
                IntPtr[] array = new IntPtr[1];
                Marshal.Copy(memory, array, 0, array.Length);
                return(Array.ConvertAll(array, p => (int)p));
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Returns an array that contains the size in bytes of the program binary for each device associated with program. The size of the array is the number of devices associated with program. If a binary is not available for a device(s), a size of zero is returned.
        /// </summary>
        /// <remarks>
        /// If program is created using clCreateProgramWithBuiltInKernels, the implementation may return zero in any entries of the returned array.
        /// </remarks>
        public static int[] GetBinarySizes(ProgramSafeHandle program)
        {
            IntPtr  memory = IntPtr.Zero;
            UIntPtr actualSize;

            try
            {
                // Allicate space for up to 8 GPUs - I cannot see going over that in one system.
                memory = Marshal.AllocHGlobal(IntPtr.Size * 8);
                ErrorHandler.ThrowOnFailure(clGetProgramInfo(program, 0x1165 /*BinarySizes*/, (UIntPtr)(UIntPtr.Size * 8), memory, out actualSize));
                IntPtr[] array = new IntPtr[(int)actualSize / IntPtr.Size];
                Marshal.Copy(memory, array, 0, array.Length);
                return(Array.ConvertAll(array, p => (int)p));
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }
        }
Ejemplo n.º 14
0
        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);
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Compiles a program’s source for all the devices.
 /// </summary>
 public void Compile()
 {
     ProgramSafeHandle[] inputHeaders = new ProgramSafeHandle[0];
     string[] headerNames = new string[0];
     UnsafeNativeMethods.CompileProgram(_handle, null, "", inputHeaders, headerNames, null, IntPtr.Zero);
 }
Ejemplo n.º 16
0
        public Program CreateProgramWithSource(params string[] sources)
        {
            ProgramSafeHandle handle = UnsafeNativeMethods.CreateProgramWithSource(Handle, sources);

            return(new Program(this, handle));
        }
Ejemplo n.º 17
0
 public static void BuildProgram(ProgramSafeHandle program, ClDeviceID[] devices, string options, BuildProgramCallback notify, IntPtr userData)
 {
     ErrorHandler.ThrowOnFailure(clBuildProgram(program, devices != null && devices.Length > 0 ? (uint)devices.Length : 0, devices, options, notify, userData));
 }
Ejemplo n.º 18
0
 private static extern ErrorCode clRetainProgram(ProgramSafeHandle program);
Ejemplo n.º 19
0
 private static extern ErrorCode clCreateKernelsInProgram(
     ProgramSafeHandle program,
     uint numKernels,
     [Out, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(SafeHandleArrayMarshaler))] KernelSafeHandle[] kernels,
     out uint numKernelsRet);
Ejemplo n.º 20
0
 private static extern ErrorCode clGetProgramInfo(
     ProgramSafeHandle program,
     int paramName,
     UIntPtr paramValueSize,
     IntPtr[] paramValue,
     out UIntPtr paramValueSizeRet);
Ejemplo n.º 21
0
 private static extern KernelSafeHandle clCreateKernel(
     ProgramSafeHandle program,
     [MarshalAs(UnmanagedType.LPStr)] string kernelName,
     out ErrorCode errorCode);