Exemplo n.º 1
0
 /// <summary>
 /// Init general platform information.
 /// </summary>
 private void InitPlatformInfo()
 {
     PlatformName = CurrentAPI.GetPlatformInfo(
         PlatformId,
         CLPlatformInfoType.CL_PLATFORM_NAME);
     PlatformVersion = CLPlatformVersion.TryParse(
         CurrentAPI.GetPlatformInfo(
             PlatformId,
             CLPlatformInfoType.CL_PLATFORM_VERSION),
         out var platformVersion)
         ? platformVersion
         : CLPlatformVersion.CL10;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Tries to parse the given string expression into an OpenCL C version.
        /// </summary>
        /// <param name="expression">The expression to parse.</param>
        /// <param name="version">The parsed version (if any).</param>
        /// <returns>
        /// True, if the given expression could be parsed into an OpenCL C version.
        /// </returns>
        public static bool TryParse(string expression, out CLPlatformVersion version)
        {
            version = default;
            var match = VersionRegex.Match(expression);

            if (!match.Success)
            {
                return(false);
            }
            version = new CLPlatformVersion(
                int.Parse(match.Groups[1].Value),
                int.Parse(match.Groups[2].Value));
            return(true);
        }
Exemplo n.º 3
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));
            }

            Backends.Backend.EnsureRunningOnNativePlatform();

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

            PlatformName = CurrentAPI.GetPlatformInfo(
                PlatformId,
                CLPlatformInfoType.CL_PLATFORM_NAME);
            PlatformVersion = CLPlatformVersion.TryParse(
                CurrentAPI.GetPlatformInfo(
                    PlatformId,
                    CLPlatformInfoType.CL_PLATFORM_VERSION),
                out var platformVersion)
                ? platformVersion
                : CLPlatformVersion.CL10;

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

            // Create new context
            CLException.ThrowIfFailed(
                CurrentAPI.CreateContext(DeviceId, out var contextPtr));
            NativePtr = contextPtr;

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

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

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

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

            CurrentAPI.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 = CurrentAPI.GetDeviceInfo <IntPtr>(
                DeviceId,
                CLDeviceInfoType.CL_DEVICE_MAX_WORK_GROUP_SIZE).ToInt32();
            MaxGroupSize = new Index3(
                MaxNumThreadsPerGroup,
                MaxNumThreadsPerGroup,
                MaxNumThreadsPerGroup);

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

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

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

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

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

            base.Capabilities = new CLCapabilityContext(acceleratorId);

            Bind();
            InitVendorFeatures();
            InitSubGroupSupport(acceleratorId);
            DefaultStream = CreateStreamInternal();
            Init(new CLBackend(Context, Capabilities, Vendor));
        }