Beispiel #1
0
 /// <summary>
 /// Constructs a new Cuda buffer.
 /// </summary>
 /// <param name="accelerator">The accelerator.</param>
 /// <param name="extent">The extent.</param>
 internal CudaMemoryBuffer(CudaAccelerator accelerator, TIndex extent)
     : base(accelerator, extent)
 {
     CudaException.ThrowIfFailed(
         CudaAPI.Current.AllocateMemory(
             out IntPtr resultPtr,
             new IntPtr(extent.Size * ElementSize)));
     NativePtr = resultPtr;
 }
Beispiel #2
0
 internal CudaKernel(
     CudaAccelerator accelerator,
     CompiledKernel kernel,
     MethodInfo launcher)
     : base(accelerator, kernel, launcher)
 {
     CudaException.ThrowIfFailed(CudaAPI.Current.LoadModule(out modulePtr, kernel.GetBuffer()));
     CudaException.ThrowIfFailed(CudaAPI.Current.GetModuleFunction(out functionPtr, modulePtr, kernel.EntryName));
 }
Beispiel #3
0
        /// <summary>
        /// Constructs a new cuRand wrapper.
        /// </summary>
        /// <param name="accelerator">The associated Cuda accelerator.</param>
        /// <param name="rngType">The cuRand RNG type.</param>
        /// <param name="apiVersion">The cuRand API version.</param>
        private GPUCuRand(
            CudaAccelerator accelerator,
            CuRandRngType rngType,
            CuRandAPIVersion?apiVersion)
            : base(accelerator)
        {
            API = CuRandAPI.Create(apiVersion);

            CuRandException.ThrowIfFailed(
                API.CreateGenerator(out IntPtr nativePtr, rngType));
            GeneratorPtr = nativePtr;

            CuRandException.ThrowIfFailed(
                API.GetVersion(out int version));
            Version = version;

            Stream = accelerator.DefaultStream as CudaStream;
        }
Beispiel #4
0
        /// <summary>
        /// Constructs a new CuBlas instance to access the Nvidia cublas library.
        /// </summary>
        /// <param name="accelerator">The associated cuda accelerator.</param>
        /// <param name="apiVersion">The cuBlas API version.</param>
        private CuBlas(CudaAccelerator accelerator, CuBlasAPIVersion?apiVersion)
        {
            if (accelerator == null)
            {
                throw new ArgumentNullException(nameof(accelerator));
            }

            API = CuBlasAPI.Create(apiVersion);
            accelerator.Bind();
            CuBlasException.ThrowIfFailed(
                API.Create(out IntPtr handle));
            Handle = handle;

            CuBlasException.ThrowIfFailed(
                API.GetVersion(handle, out int currentVersion));
            Version = currentVersion;

            Stream = accelerator.DefaultStream as CudaStream;
        }
Beispiel #5
0
        /// <summary>
        /// Creates image buffers to hold an image of the specified width, height and
        /// number of components/channels.
        /// </summary>
        /// <param name="accelerator">The accelerator.</param>
        /// <param name="width">The width (in bytes) per channel.</param>
        /// <param name="height">The height (in bytes) per channel.</param>
        /// <param name="numComponents">The number of components/channels.</param>
        /// <returns>The allocated buffers.</returns>
        public static NvJpegImage Create(
            CudaAccelerator accelerator,
            int width,
            int height,
            int numComponents)
        {
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(width));
            }
            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(height));
            }
            if (numComponents <= 0 ||
                numComponents > NvJpegConstants.NVJPEG_MAX_COMPONENT)
            {
                throw new ArgumentOutOfRangeException(nameof(numComponents));
            }

            var size        = width * height;
            var outputImage = new NvJpegImage
            {
                Channel = new MemoryBuffer1D <byte, Stride1D.Dense>[]
                {
                    numComponents >= 1 ? accelerator.Allocate1D <byte>(size) : null,
                    numComponents >= 2 ? accelerator.Allocate1D <byte>(size) : null,
                    numComponents >= 3 ? accelerator.Allocate1D <byte>(size) : null,
                    numComponents >= 4 ? accelerator.Allocate1D <byte>(size) : null
                },
                Pitch = new ulong[]
                {
                    numComponents >= 1 ? (ulong)width : 0,
                    numComponents >= 2 ? (ulong)width : 0,
                    numComponents >= 3 ? (ulong)width : 0,
                    numComponents >= 4 ? (ulong)width : 0,
                }
            };

            return(outputImage);
        }
Beispiel #6
0
        /// <summary>
        /// Init architecture information.
        /// </summary>
        private void InitArchitectureInfo()
        {
            // Determine the driver version
            ThrowIfFailed(
                CurrentAPI.GetDriverVersion(out var driverVersion));
            DriverVersion = driverVersion;

            // Setup architecture and instruction set
            ThrowIfFailed(
                CurrentAPI.GetDeviceComputeCapability(
                    out int major,
                    out int minor,
                    DeviceId));
            Architecture = GetArchitecture(major, minor);

            if (Architecture.HasValue && CudaAccelerator.TryGetInstructionSet(
                    Architecture.Value,
                    driverVersion,
                    out var _,
                    out var instructionSet))
            {
                InstructionSet = instructionSet;
            }
        }
Beispiel #7
0
        internal CudaKernel(
            CudaAccelerator accelerator,
            PTXCompiledKernel kernel,
            MethodInfo launcher)
            : base(accelerator, kernel, launcher)
        {
#if DEBUG
            var kernelLoaded = CudaAPI.Current.LoadModule(
                out modulePtr,
                kernel.PTXAssembly,
                out string errorLog);
            if (kernelLoaded != CudaError.CUDA_SUCCESS)
            {
                Debug.WriteLine("Kernel loading failed:");
                if (string.IsNullOrWhiteSpace(errorLog))
                {
                    Debug.WriteLine(">> No error information available");
                }
                else
                {
                    Debug.WriteLine(errorLog);
                }
            }
            CudaException.ThrowIfFailed(kernelLoaded);
#else
            CudaException.ThrowIfFailed(
                CudaAPI.Current.LoadModule(
                    out modulePtr,
                    kernel.PTXAssembly));
#endif
            CudaException.ThrowIfFailed(
                CudaAPI.Current.GetModuleFunction(
                    out functionPtr,
                    modulePtr,
                    PTXCompiledKernel.EntryName));
        }
Beispiel #8
0
 /// <summary>
 /// Constructs a new cuRand wrapper on the GPU.
 /// </summary>
 /// <param name="accelerator">The associated Cuda accelerator.</param>
 /// <param name="rngType">The cuRand RNG type.</param>
 /// <param name="apiVersion">The cuRand API version.</param>
 public static GPUCuRand CreateGPU(
     CudaAccelerator accelerator,
     CuRandRngType rngType,
     CuRandAPIVersion apiVersion) =>
 new GPUCuRand(accelerator, rngType, apiVersion);
Beispiel #9
0
 /// <summary>
 /// Constructs a new cuRand wrapper on the GPU using the V10 API.
 /// </summary>
 /// <param name="accelerator">The associated Cuda accelerator.</param>
 /// <param name="rngType">The cuRand RNG type.</param>
 public static GPUCuRand CreateGPU(
     CudaAccelerator accelerator,
     CuRandRngType rngType) =>
 new GPUCuRand(accelerator, rngType);
Beispiel #10
0
 /// <summary>
 /// Constructs a new cuRand wrapper.
 /// </summary>
 /// <param name="accelerator">The associated Cuda accelerator.</param>
 /// <param name="rngType">The cuRand RNG type.</param>
 public GPUCuRand(
     CudaAccelerator accelerator,
     CuRandRngType rngType)
     : this(accelerator, rngType, new CuRandAPIVersion?())
 {
 }
Beispiel #11
0
 /// <summary>
 /// Constructs a new CuBlas instance to access the Nvidia cublas library.
 /// </summary>
 /// <param name="accelerator">The associated cuda accelerator.</param>
 /// <param name="apiVersion">The cuBlas API version.</param>
 public CuBlas(CudaAccelerator accelerator, CuBlasAPIVersion apiVersion)
     : this(accelerator, new CuBlasAPIVersion?(apiVersion))
 {
 }
Beispiel #12
0
 /// <summary>
 /// Constructs a new CuBlas instance to access the Nvidia cublas library.
 /// </summary>
 /// <param name="accelerator">The associated cuda accelerator.</param>
 public CuBlas(CudaAccelerator accelerator)
     : this(accelerator, new CuBlasAPIVersion?())
 {
 }
Beispiel #13
0
 /// <summary>
 /// Constructs a new cuRand wrapper on the GPU using the V10 API.
 /// </summary>
 /// <param name="accelerator">The associated Cuda accelerator.</param>
 /// <param name="rngType">The cuRand RNG type.</param>
 public static GPUCuRand CreateGPU(
     CudaAccelerator accelerator,
     CuRandRngType rngType) =>
 CreateGPU(accelerator, rngType, CuRandAPIVersion.V10);
Beispiel #14
0
 /// <summary>
 /// Constructs a new CuBlas instance to access the Nvidia cublas library.
 /// </summary>
 /// <param name="accelerator">The associated cuda accelerator.</param>
 public CuBlas(CudaAccelerator accelerator)
     : this(accelerator, CuBlasAPIVersion.V10)
 {
 }