示例#1
0
        public Volume(double[] array, Shape shape, GpuContext context) : base(new VolumeStorage(array, shape, context))
        {
            this._context       = context;
            this._volumeStorage = this.Storage as VolumeStorage;

            LoadKernels();
        }
示例#2
0
        public Volume(double[] array, Shape shape) : base(new VolumeStorage(array, shape, GpuContext.Default))
        {
            this._context       = GpuContext.Default;
            this._volumeStorage = this.Storage as VolumeStorage;

            this.LoadKernels();
        }
示例#3
0
        public Volume(VolumeStorage storage) : base(storage)
        {
            this._context       = storage.Context;
            this._volumeStorage = this.Storage as VolumeStorage;

            LoadKernels();
        }
        public VolumeStorage(VolumeStorage storage, Shape newShape) : base(newShape)
        {
            if (storage == null)
            {
                throw new ArgumentNullException(nameof(storage));
            }

            if (storage._hostPointer == null)
            {
                throw new ArgumentException();
            }

            this._isOwner           = false;
            this._hostPointer       = storage._hostPointer;
            this.Shape              = newShape;
            this.Context            = storage.Context;
            this._allocatedOnDevice = storage._allocatedOnDevice;

            storage.CopyToDevice();

            this.Location     = DataLocation.Device;
            this.DeviceBuffer = new CudaDeviceVariable <double>(storage.DeviceBuffer.DevicePointer);

            this.ConvolutionBackwardFilterStorage = storage.ConvolutionBackwardFilterStorage;
            this.ConvolutionBackwardStorage       = storage.ConvolutionBackwardStorage;
            this.ConvolutionStorage  = storage.ConvolutionStorage;
            this.ReductionStorage    = storage.ReductionStorage;
            this.DropoutStorage      = storage.DropoutStorage;
            this.DropoutStateStorage = storage.DropoutStateStorage;
        }
示例#5
0
        public override Volume <double> SameAs(VolumeStorage <double> example, double value, Shape shape)
        {
            if (example is VolumeStorage gpuStorage)
            {
                return(new Volume(new VolumeStorage(new double[shape.TotalLength].Populate(value), shape, gpuStorage.Context)));
            }

            throw new NotImplementedException();
        }
示例#6
0
        public override Volume <double> SameAs(VolumeStorage <double> example, Shape shape)
        {
            if (example is VolumeStorage gpuStorage)
            {
                return(new Volume(new VolumeStorage(shape, gpuStorage.Context)));
            }

            throw new NotImplementedException();
        }
示例#7
0
        public override Volume <double> Build(VolumeStorage <double> storage, Shape shape)
        {
            if (storage is VolumeStorage gpuStorage)
            {
                return(new Volume(new VolumeStorage(gpuStorage, shape)));
            }

            throw new NotImplementedException();
        }
        public override Volume <double> SameAs(VolumeStorage <double> example, Shape shape)
        {
            var gpuStorage = example as VolumeStorage;

            if (gpuStorage != null)
            {
                return(new Volume(new VolumeStorage(shape, gpuStorage.Context)));
            }

            throw new NotImplementedException();
        }
        public override Volume <double> Build(VolumeStorage <double> storage, Shape shape)
        {
            var gpuStorage = storage as VolumeStorage;

            if (gpuStorage != null)
            {
                return(new Volume(new VolumeStorage(gpuStorage, shape)));
            }

            throw new NotImplementedException();
        }
示例#10
0
        public VolumeStorage(VolumeStorage storage, Shape shape)
            : this(shape, storage.Context, storage.Shape.TotalLength)
        {
            storage.CopyToHost();

            // Fill host buffer
            for (var i = 0; i < this.Shape.TotalLength; i++)
            {
                this.HostBuffer[i] = storage.HostBuffer[i];
            }
        }
示例#11
0
        public VolumeStorage(VolumeStorage storage, Shape newShape) : base(newShape)
        {
            this._isOwner     = false;
            this._hostPointer = storage._hostPointer;
            this.Shape        = newShape;
            this.HostBuffer   = storage.HostBuffer;
            this.Context      = storage.Context;

            storage.CopyToDevice();

            this.Location     = DataLocation.Device;
            this.DeviceBuffer = new CudaDeviceVariable <double>(storage.DeviceBuffer.DevicePointer);
        }
示例#12
0
        public VolumeStorage(VolumeStorage storage, Shape shape)
            : this(shape, storage.Context, storage.Shape.TotalLength)
        {
            this._isOwner           = false;
            this.Location           = storage.Location;
            this.HostBuffer         = storage.HostBuffer;
            this._hostPointer       = storage._hostPointer;
            this._allocatedOnDevice = storage._allocatedOnDevice;

            storage.CopyToDevice();
            this.DeviceBuffer = new CudaDeviceVariable <double>(storage.DeviceBuffer.DevicePointer);

            this.Location = DataLocation.Device;
        }
        public override void CopyFrom(VolumeStorage <double> source)
        {
            Debug.Assert(!this._disposed);

            var real = source as VolumeStorage;

            if (!ReferenceEquals(this, real))
            {
                if (this.Shape.TotalLength != real.Shape.TotalLength)
                {
                    throw new ArgumentException($"origin and destination volume should have the same number of weight ({this.Shape.TotalLength} != {real.Shape}).");
                }

                real.CopyToDevice();

                if (this.DeviceBuffer == null)
                {
                    this.DeviceBuffer = new CudaDeviceVariable <double>(this.Shape.TotalLength);
                }

                var res = DriverAPINativeMethods.SynchronousMemcpy_v2.cuMemcpy(
                    this.DeviceBuffer.DevicePointer,
                    real.DeviceBuffer.DevicePointer,
                    this.Shape.TotalLength * sizeof(double));

                if (res != CUResult.Success)
                {
                    throw new CudaException(res);
                }

                this.Location = DataLocation.Device;
            }
            else
            {
                CopyToDevice();
            }
        }
示例#14
0
        public override void CopyFrom(VolumeStorage <double> source)
        {
            Debug.Assert(!_disposed);

            var real = source as VolumeStorage;

            if (!object.ReferenceEquals(this, real))
            {
                if (this.Shape.TotalLength != real.Shape.TotalLength)
                {
                    throw new ArgumentException($"{nameof(real)} has different length!");
                }

                real.CopyToDevice();

                if (this.DeviceBuffer == null)
                {
                    this.DeviceBuffer = new CudaDeviceVariable <double>(this.Shape.TotalLength);
                }

                var res = DriverAPINativeMethods.SynchronousMemcpy_v2.cuMemcpy(
                    this.DeviceBuffer.DevicePointer,
                    real.DeviceBuffer.DevicePointer,
                    this.Shape.TotalLength * sizeof(double));

                if (res != CUResult.Success)
                {
                    throw new CudaException(res);
                }

                this.Location = DataLocation.Device;
            }
            else
            {
                this.CopyToDevice();
            }
        }
示例#15
0
 public override bool Equals(VolumeStorage <double> other)
 {
     throw new NotImplementedException();
 }
示例#16
0
        private void Op(Volume <double> right, cudnnOpTensorOp op, Volume <double> result)
        {
            var resultStorage = result.Storage as VolumeStorage;

            if (resultStorage == null)
            {
                throw new ArgumentException($"{nameof(result)} storage should be VolumeStorage", nameof(result));
            }

            VolumeStorage rightStorage = null;

            if (right != null)
            {
                rightStorage = right.Storage as VolumeStorage;
                if (rightStorage == null)
                {
                    throw new ArgumentException($"{nameof(right)} storage should be VolumeStorage", nameof(right));
                }
            }

            // Copy to device if not already done
            this._volumeStorage.CopyToDevice();
            rightStorage?.CopyToDevice();
            resultStorage.CopyToDevice();

            var           aStorage = this._volumeStorage;
            Shape         bShape   = null;
            VolumeStorage bStorage = null;

            if (rightStorage != null)
            {
                bStorage = rightStorage;
                if (bStorage.Shape.TotalLength > aStorage.Shape.TotalLength)
                {
                    aStorage = rightStorage;
                    bStorage = this._volumeStorage;
                }

                bShape = bStorage.Shape;
            }

            var n = aStorage.Shape.Dimensions[3];
            var c = aStorage.Shape.Dimensions[2];
            var h = aStorage.Shape.Dimensions[1];
            var w = aStorage.Shape.Dimensions[0];

            // Add tensors
            using var descA = new TensorDescriptor();
            using var descB = new TensorDescriptor();
            using var descC = new TensorDescriptor();

            descA.SetTensor4dDescriptor(cudnnTensorFormat.NCHW, cudnnDataType.Double, n, c, h, w);
            if (bShape != null)
            {
                descB.SetTensor4dDescriptor(cudnnTensorFormat.NCHW, cudnnDataType.Double, bShape.Dimensions[3], bShape.Dimensions[2], bShape.Dimensions[1],
                                            bShape.Dimensions[0]);
            }

            descC.SetTensor4dDescriptor(cudnnTensorFormat.NCHW, cudnnDataType.Double, n, c, h, w);

            using var opt = new OpTensorDescriptor(this._context.CudnnContext);

            opt.SetOpTensorDescriptor(
                op,
                cudnnDataType.Double,
                cudnnNanPropagation.PropagateNan);

            var one  = 1.0;
            var zero = 0.0;

            var status = CudaDNNNativeMethods.cudnnOpTensor(
                this._context.CudnnContext.Handle,
                opt.Desc,
                ref one, descA.Desc, aStorage.DeviceBuffer.DevicePointer,
                ref one, bStorage != null ? descB.Desc : descA.Desc, bStorage?.DeviceBuffer.DevicePointer ?? aStorage.DeviceBuffer.DevicePointer,
                ref zero, descC.Desc, resultStorage.DeviceBuffer.DevicePointer);

            if (status != cudnnStatus.Success)
            {
                throw new Exception(CudaDNNNativeMethods.cudnnGetErrorString(status));
            }

            resultStorage.Location = DataLocation.Device;
        }
示例#17
0
 public Volume(VolumeStorage storage) : base(new VolumeStorage(storage, storage.Shape))
 {
     this._context       = storage.Context;
     this._volumeStorage = this.Storage as VolumeStorage;
 }