public static void ThrowIfError(this CLResponse response)
 {
     if (!response.Success)
     {
         throw CLException.FromCode(response.Error.Code, response.Error.Info);
     }
 }
Пример #2
0
        /// <summary>
        /// Resolves platform information as typed structure value of type <typeparamref name="T"/>
        /// </summary>
        /// <typeparam name="T">The target type.</typeparam>
        /// <param name="platform">The platform.</param>
        /// <param name="type">The information type.</param>
        /// <returns>The resolved value.</returns>
        public static T GetPlatformInfo <T>(IntPtr platform, CLPlatformInfoType type)
            where T : struct
        {
            T value = default;

            CLException.ThrowIfFailed(NativeMethods.GetPlatformInfo(
                                          platform,
                                          type,
                                          new IntPtr(Interop.SizeOf <T>()),
                                          Unsafe.AsPointer(ref value),
                                          IntPtr.Zero));
            return(value);
        }
Пример #3
0
 /// <summary>
 /// Resolves device information as array of typed structure values of type
 /// <typeparamref name="T"/>.
 /// </summary>
 /// <typeparam name="T">The target type.</typeparam>
 /// <param name="device">The device.</param>
 /// <param name="type">The information type.</param>
 /// <param name="elements">The elements to fill.</param>
 /// <returns>The resolved value.</returns>
 public static void GetDeviceInfo <T>(
     IntPtr device,
     CLDeviceInfoType type,
     T[] elements)
     where T : unmanaged
 {
     fixed(T *ptr = &elements[0])
     {
         CLException.ThrowIfFailed(NativeMethods.GetDeviceInfo(
                                       device,
                                       type,
                                       new IntPtr(Interop.SizeOf <T>() * elements.Length),
                                       ptr,
                                       IntPtr.Zero));
     }
 }
Пример #4
0
        /// <summary>
        /// Resolves kernel work-group information as typed structure value of type <typeparamref name="T"/>
        /// </summary>
        /// <typeparam name="T">The target type.</typeparam>
        /// <param name="kernel">The kernel.</param>
        /// <param name="device">The device.</param>
        /// <param name="type">The information type.</param>
        /// <returns>The resolved value.</returns>
        public static T GetKernelWorkGroupInfo <T>(
            IntPtr kernel,
            IntPtr device,
            CLKernelWorkGroupInfoType type)
            where T : struct
        {
            T value = default;

            CLException.ThrowIfFailed(NativeMethods.GetKernelWorkGroupInfo(
                                          kernel,
                                          device,
                                          type,
                                          new IntPtr(Interop.SizeOf <T>()),
                                          Unsafe.AsPointer(ref value),
                                          IntPtr.Zero));
            return(value);
        }
Пример #5
0
        /// <summary>
        /// Resolves platform information as string value.
        /// </summary>
        /// <param name="platform">The platform.</param>
        /// <param name="type">The information type.</param>
        /// <returns>The resolved string value.</returns>
        public static string GetPlatformInfo(IntPtr platform, CLPlatformInfoType type)
        {
            const int MaxStringLength = 1024;
            var       stringValue     = stackalloc sbyte[MaxStringLength];
            var       size            = IntPtr.Zero;

            CLException.ThrowIfFailed(NativeMethods.GetPlatformInfo(
                                          platform,
                                          type,
                                          new IntPtr(MaxStringLength),
                                          stringValue,
                                          new IntPtr(&size)));
            int intSize = size.ToInt32();

            return(intSize > 0 && intSize < MaxStringLength
                ? new string(stringValue, 0, intSize - 1)
                : string.Empty);
        }
Пример #6
0
        /// <summary>
        /// Resolves device information as array of typed structure values of type <typeparamref name="T"/>
        /// </summary>
        /// <typeparam name="T">The target type.</typeparam>
        /// <param name="device">The device.</param>
        /// <param name="type">The information type.</param>
        /// <param name="elements">The elements to fill.</param>
        /// <returns>The resolved value.</returns>
        public static void GetDeviceInfo <T>(IntPtr device, CLDeviceInfoType type, T[] elements)
            where T : struct
        {
            var handle = GCHandle.Alloc(elements, GCHandleType.Pinned);

            try
            {
                CLException.ThrowIfFailed(NativeMethods.GetDeviceInfo(
                                              device,
                                              type,
                                              new IntPtr(Interop.SizeOf <T>() * elements.Length),
                                              handle.AddrOfPinnedObject().ToPointer(),
                                              IntPtr.Zero));
            }
            finally
            {
                handle.Free();
            }
        }
Пример #7
0
        /// <summary>
        /// Resolves device information as string value.
        /// </summary>
        /// <param name="device">The device.</param>
        /// <param name="type">The information type.</param>
        /// <returns>The resolved string value.</returns>
        public static string GetDeviceInfo(IntPtr device, CLDeviceInfoType type)
        {
            const int MaxStringLength = 8192;
            var       stringValue     = stackalloc sbyte[MaxStringLength];
            var       size            = IntPtr.Zero;

            CLException.ThrowIfFailed(NativeMethods.GetDeviceInfo(
                                          device,
                                          type,
                                          new IntPtr(MaxStringLength),
                                          stringValue,
                                          new IntPtr(&size)));
            int intSize = size.ToInt32();

            if (intSize > 0 && intSize < MaxStringLength)
            {
                return(new string(stringValue, 0, intSize - 1));
            }
            return(string.Empty);
        }