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;
        }
        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);
        }
Exemple #3
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;
        }
Exemple #4
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;
        }