Пример #1
0
        /// <summary>
        /// Helper method to read an array of values from the NVML Interop API.
        /// </summary>
        /// <param name="interopFunc">The interop function.</param>
        /// <param name="nvmlArray">Filled in with the result array.</param>
        /// <returns>The interop status code.</returns>
        internal unsafe static NvmlReturn GetNvmlArray <T>(
            GetNvmlArrayInterop <T> interopFunc,
            out T[] nvmlArray)
            where T : unmanaged
        {
            // Query the length of data available.
            // If the result is success, that means an empty array.
            uint       length = 0;
            NvmlReturn result = interopFunc(ref length, null);

            if (result == NvmlReturn.NVML_SUCCESS)
            {
                nvmlArray = Array.Empty <T>();
                return(result);
            }
            else if (result == NvmlReturn.NVML_ERROR_INSUFFICIENT_SIZE)
            {
                // Allocate the correct size, and call the interop again.
                T[] buffer = new T[length];
                fixed(T *ptr = buffer)
                {
                    result    = interopFunc(ref length, ptr);
                    nvmlArray = result == NvmlReturn.NVML_SUCCESS
                        ? buffer
                        : default;
                    return(result);
                }
            }
            else
            {
                nvmlArray = default;
                return(result);
            }
        }
Пример #2
0
        /// <summary>
        /// Helper method to read an array of values from the NVML Interop API.
        /// </summary>
        /// <param name="interopFunc">The interop function.</param>
        /// <param name="nvmlArray1">Filled in with the result array.</param>
        /// <param name="nvmlArray2">Filled in with the result array.</param>
        /// <returns>The interop status code.</returns>
        internal unsafe static NvmlReturn GetNvmlArray <T1, T2>(
            GetNvmlArrayInterop <T1, T2> interopFunc,
            out T1[] nvmlArray1,
            out T2[] nvmlArray2)
            where T1 : unmanaged
            where T2 : unmanaged
        {
            // Query the length of data available.
            // If the result is success, that means an empty array.
            uint       length = 0;
            NvmlReturn result = interopFunc(ref length, null, null);

            if (result == NvmlReturn.NVML_SUCCESS)
            {
                nvmlArray1 = Array.Empty <T1>();
                nvmlArray2 = Array.Empty <T2>();
                return(result);
            }
            else if (result == NvmlReturn.NVML_ERROR_INSUFFICIENT_SIZE)
            {
                // Allocate the correct size, and call the interop again.
                T1[] buffer1 = new T1[length];
                T2[] buffer2 = new T2[length];
                fixed(T1 *ptr1 = buffer1)
                fixed(T2 * ptr2 = buffer2)
                {
                    result = interopFunc(ref length, ptr1, ptr2);
                    if (result == NvmlReturn.NVML_SUCCESS)
                    {
                        nvmlArray1 = buffer1;
                        nvmlArray2 = buffer2;
                    }
                    else
                    {
                        nvmlArray1 = default;
                        nvmlArray2 = default;
                    }
                    return(result);
                }
            }
            else
            {
                nvmlArray1 = default;
                nvmlArray2 = default;
                return(result);
            }
        }