Example #1
0
        private void DoReduce(Volume <double> result, cudnnReduceTensorOp op)
        {
            if (this.Shape.Equals(result.Shape))
            {
                result.Storage.CopyFrom(this.Storage);
                return;
            }

            var aStorage = this._volumeStorage;
            var cStorage = result.Storage as VolumeStorage;

            // Copy to device if not already done
            aStorage.CopyToDevice();
            cStorage.CopyToDevice();

            using (var reduceTensorDesc = new ReduceTensorDescriptor())
                using (var aDesc = new TensorDescriptor())
                    using (var cDesc = new TensorDescriptor())
                    {
                        var an = this.Shape.GetDimension(3);
                        var ac = this.Shape.GetDimension(2);
                        var ah = this.Shape.GetDimension(1);
                        var aw = this.Shape.GetDimension(0);

                        var cn = result.Shape.GetDimension(3);
                        var cc = result.Shape.GetDimension(2);
                        var ch = result.Shape.GetDimension(1);
                        var cw = result.Shape.GetDimension(0);

                        aDesc.SetTensor4dDescriptor(cudnnTensorFormat.NCHW, cudnnDataType.Double, an, ac, ah, aw);
                        cDesc.SetTensor4dDescriptor(cudnnTensorFormat.NCHW, cudnnDataType.Double, cn, cc, ch, cw);

                        reduceTensorDesc.SetReduceTensorDescriptor(op, cudnnDataType.Double, cudnnNanPropagation.NotPropagateNan, cudnnReduceTensorIndices.NoIndices,
                                                                   cudnnIndicesType.Indices32Bit);



                        var workspaceSize = this._context.CudnnContext.GetReductionWorkspaceSize(reduceTensorDesc, aDesc, cDesc);
                        workspaceSize = workspaceSize == 0 ? new SizeT(1) : workspaceSize;

                        if (this._volumeStorage.ReductionStorage == null || this._volumeStorage.ReductionStorage.Size != workspaceSize)
                        {
                            this._volumeStorage.ReductionStorage = new CudaDeviceVariable <byte>(workspaceSize);
                        }

                        this._context.CudnnContext.ReduceTensor(reduceTensorDesc,
                                                                CudaDeviceVariable <uint> .Null,
                                                                this._volumeStorage.ReductionStorage,
                                                                this._volumeStorage.ReductionStorage.SizeInBytes,
                                                                1.0, aDesc, aStorage.DeviceBuffer,
                                                                0.0, cDesc, cStorage.DeviceBuffer);
                    }
        }
Example #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="reduceTensorOp"></param>
 /// <param name="reduceTensorCompType"></param>
 /// <param name="reduceTensorNanOpt"></param>
 /// <param name="reduceTensorIndices"></param>
 /// <param name="reduceTensorIndicesType"></param>
 public void GetReduceTensorDescriptor(
     ref cudnnReduceTensorOp reduceTensorOp,
     ref cudnnDataType reduceTensorCompType,
     ref cudnnNanPropagation reduceTensorNanOpt,
     ref cudnnReduceTensorIndices reduceTensorIndices,
     ref cudnnIndicesType reduceTensorIndicesType)
 {
     res = CudaDNNNativeMethods.cudnnGetReduceTensorDescriptor(_desc, ref reduceTensorOp, ref reduceTensorCompType,
                                                               ref reduceTensorNanOpt, ref reduceTensorIndices, ref reduceTensorIndicesType);
     Debug.Write("");//Line(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cudnnGetReduceTensorDescriptor", res));
     if (res != cudnnStatus.Success)
     {
         throw new CudaDNNException(res);
     }
 }