Example #1
0
        /// <summary>
        /// Gets all the available platforms.
        /// </summary>
        /// <exception cref="InvalidOperationException">If the platforms could not be queried, then an <see cref="InvalidOperationException"/> is thrown.</exception>
        /// <returns>Returns a list with all the availabe platforms.</returns>
        public static IEnumerable <Platform> GetPlatforms()
        {
            // Gets the number of available platforms
            uint   numberOfAvailablePlatforms;
            Result result = PlatformsNativeApi.GetPlatformIds(0, null, out numberOfAvailablePlatforms);

            if (result != Result.Success)
            {
                throw new OpenClException("The number of platforms could not be queried.", result);
            }

            // Gets pointers to all the platforms
            IntPtr[] platformPointers = new IntPtr[numberOfAvailablePlatforms];
            result = PlatformsNativeApi.GetPlatformIds(numberOfAvailablePlatforms, platformPointers,
                                                       out numberOfAvailablePlatforms);
            if (result != Result.Success)
            {
                throw new OpenClException("The platforms could not be retrieved.", result);
            }

            // Converts the pointers to platform objects
            foreach (IntPtr platformPointer in platformPointers)
            {
                yield return(new Platform(platformPointer));
            }
        }
Example #2
0
        /// <summary>
        ///     Retrieves the specified information about the OpenCL platform.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of the data that is to be returned.</param>
        ///     <param name="platformInformation">The kind of information that is to be retrieved.</param>
        ///     <exception cref="OpenClException">
        ///         If the information could not be retrieved, then an <see cref="OpenClException" />
        ///         is thrown.
        ///     </exception>
        ///     <returns>Returns the specified information.</returns>
        private T GetPlatformInformation <T>(PlatformInformation platformInformation)
        {
            // Retrieves the size of the return value in bytes, this is used to later get the full information
            UIntPtr returnValueSize;
            Result  result = PlatformsNativeApi.GetPlatformInformation(
                Handle,
                platformInformation,
                UIntPtr.Zero,
                null,
                out returnValueSize
                );

            if (result != Result.Success)
            {
                throw new OpenClException("The platform information could not be retrieved.", result);
            }

            // Allocates enough memory for the return value and retrieves it
            byte[] output = new byte[returnValueSize.ToUInt32()];
            result = PlatformsNativeApi.GetPlatformInformation(
                Handle,
                platformInformation,
                new UIntPtr((uint)output.Length),
                output,
                out returnValueSize
                );
            if (result != Result.Success)
            {
                throw new OpenClException("The platform information could not be retrieved.", result);
            }

            // Returns the output
            return(InteropConverter.To <T>(output));
        }
Example #3
0
        /// <summary>
        /// Provides a list of available OpenCL devices on the system
        /// </summary>
        /// <returns>A list of OpenCL devices</returns>
        public static List <ComputeDeviceDesc> GetDevices()
        {
            List <ComputeDeviceDesc> ret = new List <ComputeDeviceDesc>();

            // Gets the number of available platforms
            uint   numberOfAvailablePlatforms;
            Result result = PlatformsNativeApi.GetPlatformIds(0, null, out numberOfAvailablePlatforms);

            if (result != Result.Success)
            {
                throw new OpenClException("The number of platforms could not be queried.", result);
            }

            // Gets pointers to all the platforms
            IntPtr[] platformPointers = new IntPtr[numberOfAvailablePlatforms];
            result = PlatformsNativeApi.GetPlatformIds(numberOfAvailablePlatforms, platformPointers, out numberOfAvailablePlatforms);
            if (result != Result.Success)
            {
                throw new OpenClException("The platforms could not be retrieved.", result);
            }

            // Converts the pointers to platform objects
            int platformId = 0;

            foreach (IntPtr platformPointer in platformPointers)
            {
                ++platformId;

                // Gets the number of available devices of the specified type
                uint   numberOfAvailableDevices;
                Result result_d = DevicesNativeApi.GetDeviceIds(platformPointer, DeviceType.All, 0, null, out numberOfAvailableDevices);
                if (result_d != Result.Success)
                {
                    throw new OpenClException("The number of available devices could not be queried.", result_d);
                }

                // Gets the pointers to the devices of the specified type
                IntPtr[] devicePointers = new IntPtr[numberOfAvailableDevices];
                result_d = DevicesNativeApi.GetDeviceIds(platformPointer, DeviceType.All, numberOfAvailableDevices, devicePointers, out numberOfAvailableDevices);
                if (result_d != Result.Success)
                {
                    throw new OpenClException("The devices could not be retrieved.", result_d);
                }

                // Converts the pointer to device objects
                int deviceId = 0;
                foreach (IntPtr devicePointer in devicePointers)
                {
                    ++deviceId;

                    OpenCLComputeDeviceDesc devDesc = new OpenCLComputeDeviceDesc();
                    devDesc.platform   = platformPointer;
                    devDesc.device     = devicePointer;
                    devDesc.platformId = platformId;
                    devDesc.deviceId   = deviceId;
                    ret.Add(devDesc);
                }
            }

            return(ret);
        }
Example #4
0
        public static uint GetPlatformCount()
        {
            Result result = PlatformsNativeApi.GetPlatformIds(0, null, out uint numberOfAvailablePlatforms);

            return(numberOfAvailablePlatforms);
        }