Exemple #1
0
        private void InitVendorFeatures()
        {
            // Check major vendor features
            if (CLAPI.GetDeviceInfo(
                    DeviceId,
                    CLDeviceInfoType.CL_DEVICE_WARP_SIZE_NV,
                    out int warpSize) == CLError.CL_SUCCESS)
            {
                // Nvidia platform
                WarpSize = warpSize;
                Vendor   = CLAcceleratorVendor.Nvidia;

                int major = CLAPI.GetDeviceInfo <int>(
                    DeviceId,
                    CLDeviceInfoType.CL_DEVICE_COMPUTE_CAPABILITY_MAJOR_NV);
                int minor = CLAPI.GetDeviceInfo <int>(
                    DeviceId,
                    CLDeviceInfoType.CL_DEVICE_COMPUTE_CAPABILITY_MINOR_NV);
                if (major < 7 || major == 7 && minor < 5)
                {
                    MaxNumThreadsPerMultiprocessor *= 2;
                }
            }
            else if (CLAPI.GetDeviceInfo(
                         DeviceId,
                         CLDeviceInfoType.CL_DEVICE_WAVEFRONT_WIDTH_AMD,
                         out int wavefrontSize) == CLError.CL_SUCCESS)
            {
                // AMD platform
                WarpSize = wavefrontSize;
                Vendor   = CLAcceleratorVendor.AMD;
            }
            else
            {
                Vendor = VendorName.Contains(CLAcceleratorVendor.Intel.ToString()) ?
                         CLAcceleratorVendor.Intel :
                         CLAcceleratorVendor.Other;

                // Compile dummy kernel to resolve additional information
                CLException.ThrowIfFailed(CLKernel.LoadKernel(
                                              this,
                                              DummyKernelSource,
                                              out IntPtr programPtr,
                                              out IntPtr kernelPtr));
                try
                {
                    // Resolve information
                    WarpSize = CLAPI.GetKernelWorkGroupInfo <IntPtr>(
                        kernelPtr,
                        DeviceId,
                        CLKernelWorkGroupInfoType.CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE).ToInt32();
                }
                finally
                {
                    CLException.ThrowIfFailed(
                        CLAPI.ReleaseKernel(kernelPtr) |
                        CLAPI.ReleaseProgram(programPtr));
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Constructs a new OpenCL accelerator reference.
        /// </summary>
        /// <param name="platformId">The OpenCL platform id.</param>
        /// <param name="deviceId">The OpenCL device id.</param>
        public CLAcceleratorId(IntPtr platformId, IntPtr deviceId)
            : base(AcceleratorType.OpenCL)
        {
            if (platformId == IntPtr.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(platformId));
            }
            if (deviceId == IntPtr.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(deviceId));
            }

            PlatformId = platformId;
            DeviceId   = deviceId;

            DeviceType = (CLDeviceType)CLAPI.GetDeviceInfo <long>(
                deviceId,
                CLDeviceInfoType.CL_DEVICE_TYPE);

            // Resolve extensions
            var extensionString = CLAPI.GetDeviceInfo(
                DeviceId,
                CLDeviceInfoType.CL_DEVICE_EXTENSIONS);

            extensionSet = new HashSet <string>(
                extensionString.ToLower().Split(' '));
            Extensions = extensionSet.ToImmutableArray();

            // Resolve extension method
            getKernelSubGroupInfo = CLAPI.GetExtension <clGetKernelSubGroupInfoKHR>(platformId);
        }
Exemple #3
0
        /// <summary>
        /// Constructs a new OpenCL accelerator reference.
        /// </summary>
        /// <param name="platformId">The OpenCL platform id.</param>
        /// <param name="deviceId">The OpenCL device id.</param>
        public CLAcceleratorId(IntPtr platformId, IntPtr deviceId)
            : base(AcceleratorType.OpenCL)
        {
            if (platformId == IntPtr.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(platformId));
            }
            if (deviceId == IntPtr.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(deviceId));
            }

            PlatformId = platformId;
            DeviceId   = deviceId;

            DeviceType = (CLDeviceType)CLAPI.GetDeviceInfo <long>(
                deviceId,
                CLDeviceInfoType.CL_DEVICE_TYPE);

            // Resolve extensions
            var extensionString = CLAPI.GetDeviceInfo(
                DeviceId,
                CLDeviceInfoType.CL_DEVICE_EXTENSIONS);

            extensionSet = new HashSet <string>(
                extensionString.ToLower().Split(' '));
            Extensions = extensionSet.ToImmutableArray();

            // Determine the supported OpenCL C version
            var clVersionString = CLAPI.GetDeviceInfo(
                DeviceId,
                CLDeviceInfoType.CL_DEVICE_OPENCL_C_VERSION);

            if (!CLCVersion.TryParse(clVersionString, out CLCVersion version))
            {
                version = CLCVersion.CL10;
            }
            CVersion = version;

            // Resolve extension method
            getKernelSubGroupInfo = CLAPI.GetExtension <clGetKernelSubGroupInfoKHR>(
                platformId);
        }
Exemple #4
0
        /// <summary>
        /// Constructs a new OpenCL accelerator.
        /// </summary>
        /// <param name="context">The ILGPU context.</param>
        /// <param name="acceleratorId">The accelerator id.</param>
        public CLAccelerator(Context context, CLAcceleratorId acceleratorId)
            : base(context, AcceleratorType.OpenCL)
        {
            if (acceleratorId == null)
            {
                throw new ArgumentNullException(nameof(acceleratorId));
            }

            PlatformId = acceleratorId.PlatformId;
            DeviceId   = acceleratorId.DeviceId;

            PlatformName = CLAPI.GetPlatformInfo(
                PlatformId,
                CLPlatformInfoType.CL_PLATFORM_NAME);

            VendorName = CLAPI.GetPlatformInfo(
                PlatformId,
                CLPlatformInfoType.CL_PLATFORM_VENDOR);

            // Create new context
            CLException.ThrowIfFailed(
                CLAPI.CreateContext(DeviceId, out contextPtr));

            // Resolve device info
            Name = CLAPI.GetDeviceInfo(
                DeviceId,
                CLDeviceInfoType.CL_DEVICE_NAME);

            MemorySize = CLAPI.GetDeviceInfo <long>(
                DeviceId,
                CLDeviceInfoType.CL_DEVICE_GLOBAL_MEM_SIZE);

            DeviceType = (CLDeviceType)CLAPI.GetDeviceInfo <long>(
                DeviceId,
                CLDeviceInfoType.CL_DEVICE_TYPE);

            // Max grid size
            int workItemDimensions = IntrinsicMath.Max(CLAPI.GetDeviceInfo <int>(
                                                           DeviceId,
                                                           CLDeviceInfoType.CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS), 3);
            var workItemSizes = new IntPtr[workItemDimensions];

            CLAPI.GetDeviceInfo(
                DeviceId,
                CLDeviceInfoType.CL_DEVICE_MAX_WORK_ITEM_SIZES,
                workItemSizes);
            MaxGridSize = new Index3(
                workItemSizes[0].ToInt32(),
                workItemSizes[1].ToInt32(),
                workItemSizes[2].ToInt32());

            // Resolve max threads per group
            MaxNumThreadsPerGroup = CLAPI.GetDeviceInfo <IntPtr>(
                DeviceId,
                CLDeviceInfoType.CL_DEVICE_MAX_WORK_GROUP_SIZE).ToInt32();

            // Resolve max shared memory per block
            MaxSharedMemoryPerGroup = (int)IntrinsicMath.Min(
                CLAPI.GetDeviceInfo <long>(
                    DeviceId,
                    CLDeviceInfoType.CL_DEVICE_LOCAL_MEM_SIZE),
                int.MaxValue);

            // Resolve total constant memory
            MaxConstantMemory = (int)CLAPI.GetDeviceInfo <long>(
                DeviceId,
                CLDeviceInfoType.CL_DEVICE_MAX_PARAMETER_SIZE);

            // Resolve clock rate
            ClockRate = CLAPI.GetDeviceInfo <int>(
                DeviceId,
                CLDeviceInfoType.CL_DEVICE_MAX_CLOCK_FREQUENCY);

            // Resolve number of multiprocessors
            NumMultiprocessors = CLAPI.GetDeviceInfo <int>(
                DeviceId,
                CLDeviceInfoType.CL_DEVICE_MAX_COMPUTE_UNITS);

            // Result max number of threads per multiprocessor
            MaxNumThreadsPerMultiprocessor = MaxNumThreadsPerGroup;

            InitVendorFeatures();
            InitSubGroupSupport(acceleratorId);

            Bind();
            DefaultStream = CreateStreamInternal();
            base.Backend  = new CLBackend(Context, Backends.Backend.OSPlatform, Vendor);
        }
Exemple #5
0
        static CLAccelerator()
        {
            var accelerators = ImmutableArray.CreateBuilder <CLAcceleratorId>();

            try
            {
                // Resolve all platforms
                if (CLAPI.GetNumPlatforms(out int numPlatforms) != CLError.CL_SUCCESS ||
                    numPlatforms < 1)
                {
                    return;
                }

                var platforms = new IntPtr[numPlatforms];
                if (CLAPI.GetPlatforms(platforms, out numPlatforms) != CLError.CL_SUCCESS)
                {
                    return;
                }

                foreach (var platform in platforms)
                {
                    // Resolve all devices
                    if (CLAPI.GetNumDevices(
                            platform,
                            CLDeviceType.CL_DEVICE_TYPE_ALL,
                            out int numDevices) != CLError.CL_SUCCESS)
                    {
                        continue;
                    }

                    var devices = new IntPtr[numDevices];
                    if (CLAPI.GetDevices(
                            platform,
                            CLDeviceType.CL_DEVICE_TYPE_ALL,
                            devices,
                            out numDevices) != CLError.CL_SUCCESS)
                    {
                        continue;
                    }

                    foreach (var device in devices)
                    {
                        // Check for available device
                        if (CLAPI.GetDeviceInfo <int>(
                                device,
                                CLDeviceInfoType.CL_DEVICE_AVAILABLE) == 0)
                        {
                            continue;
                        }

                        accelerators.Add(new CLAcceleratorId(
                                             platform,
                                             device));
                    }
                }
            }
            catch (Exception)
            {
                // Ignore API-specific exceptions at this point
            }
            finally
            {
                CLAccelerators = accelerators.ToImmutable();
            }
        }
Exemple #6
0
        static CLAccelerator()
        {
            var accelerators    = ImmutableArray.CreateBuilder <CLAcceleratorId>();
            var allAccelerators = ImmutableArray.CreateBuilder <CLAcceleratorId>();
            var devices         = new IntPtr[MaxNumDevicesPerPlatform];

            try
            {
                // Resolve all platforms
                if (CLAPI.GetNumPlatforms(out int numPlatforms) != CLError.CL_SUCCESS ||
                    numPlatforms < 1)
                {
                    return;
                }

                var platforms = new IntPtr[numPlatforms];
                if (CLAPI.GetPlatforms(platforms, out numPlatforms) !=
                    CLError.CL_SUCCESS)
                {
                    return;
                }

                foreach (var platform in platforms)
                {
                    // Resolve all devices
                    int numDevices = devices.Length;
                    Array.Clear(devices, 0, numDevices);

                    if (CLAPI.GetDevices(
                            platform,
                            CLDeviceType.CL_DEVICE_TYPE_ALL,
                            devices,
                            out numDevices) != CLError.CL_SUCCESS)
                    {
                        continue;
                    }

                    for (int i = 0; i < numDevices; ++i)
                    {
                        // Resolve device and ignore invalid devices
                        var device = devices[i];
                        if (device == IntPtr.Zero)
                        {
                            continue;
                        }

                        // Check for available device
                        if (CLAPI.GetDeviceInfo <int>(
                                device,
                                CLDeviceInfoType.CL_DEVICE_AVAILABLE) == 0)
                        {
                            continue;
                        }

                        var acceleratorId = new CLAcceleratorId(platform, device);
                        allAccelerators.Add(acceleratorId);
                        if (acceleratorId.CVersion >= CLBackend.MinimumVersion)
                        {
                            accelerators.Add(acceleratorId);
                        }
                    }
                }
            }
            catch (Exception)
            {
                // Ignore API-specific exceptions at this point
            }
            finally
            {
                CLAccelerators    = accelerators.ToImmutable();
                AllCLAccelerators = allAccelerators.ToImmutable();
            }
        }
Exemple #7
0
 /// <summary>
 /// Resolves device information as typed structure value of type
 /// <typeparamref name="T"/>.
 /// </summary>
 /// <typeparam name="T">The target type.</typeparam>
 /// <param name="type">The information type.</param>
 /// <returns>The resolved value.</returns>
 public T GetDeviceInfo <T>(CLDeviceInfoType type)
     where T : unmanaged => CLAPI.GetDeviceInfo <T>(DeviceId, type);
Exemple #8
0
 /// <summary>
 /// Resolves device information as typed structure value of type
 /// <typeparamref name="T"/>.
 /// </summary>
 /// <typeparam name="T">The target type.</typeparam>
 /// <param name="type">The information type.</param>
 /// <param name="value">The resolved value.</param>
 /// <returns>The error code.</returns>
 public CLError GetDeviceInfo <T>(CLDeviceInfoType type, out T value)
     where T : unmanaged => CLAPI.GetDeviceInfo(DeviceId, type, out value);