Exemple #1
0
        /// <inheritdoc />
        protected override Util.ISigmaDiffDataBuffer <T> _InternalDeepCopy()
        {
            T[] copyData = SigmaDiffSharpBackendProvider.Instance.GetBackend <T>(BackendTag).BackendHandle.CreateUninitialisedArray((int)Length);
            CudaSigmaDiffDataBuffer <T> copy = new CudaSigmaDiffDataBuffer <T>(copyData, 0L, Length, BackendTag, CudaContext, Type);

            if (_initialisedInContext && !_flagHostModified)
            {
                copy.InitialiseCudaBuffer(copyHostToDevice: false);

                copy._cudaBuffer.AsyncCopyToDevice(_cudaBuffer, CudaFloat32Handler.GetStreamForContext(CudaContext));

                copy._flagHostModified   = false;
                copy._flagDeviceModified = true;
            }
            else
            {
                OnReadAccess();

                Buffer.BlockCopy(_data, (int)(Offset * Type.SizeBytes), copyData, 0, (int)(Length * Type.SizeBytes));

                copy._flagHostModified = true;
            }

            return(copy);
        }
Exemple #2
0
        internal void InitialiseCudaBuffer(bool copyHostToDevice = true)
        {
            if (CudaContext == null)
            {
                throw new InvalidOperationException($"Cannot initialise cuda buffer, cuda context is invalid (null).");
            }

            CudaFloat32BackendHandle backendHandle = (CudaFloat32BackendHandle)SigmaDiffSharpBackendProvider.Instance.GetBackend <T>(BackendTag).BackendHandle;

            if (_underlyingCudaBuffer != null)
            {
                if (!_underlyingCudaBuffer._initialisedInContext)
                {
                    _underlyingCudaBuffer.InitialiseCudaBuffer(copyHostToDevice);
                }

                _cudaBuffer = new CudaDeviceVariable <T>(_underlyingCudaBuffer._cudaBuffer.DevicePointer + _cudaOffsetBytes, _cudaLengthBytes);
                backendHandle.IncreaseReferenceCount(_data);

                _initialisedInContext = true;
            }
            else
            {
                bool initialisedToValue;
                _cudaBuffer           = backendHandle.AllocateDeviceBuffer(_data, Offset, _cudaLengthBytes, out initialisedToValue);
                _initialisedInContext = true;

                if (copyHostToDevice && !initialisedToValue)
                {
                    CopyFromHostToDevice();

                    _flagHostModified = false;
                }
            }
        }
Exemple #3
0
        /// <inheritdoc />
        public override IDataBuffer <T> GetValues(long startIndex, long length)
        {
            if (_initialisedInContext && !_flagHostModified)
            {
                CudaSigmaDiffDataBuffer <T> vData = new CudaSigmaDiffDataBuffer <T>(_data, startIndex, length, BackendTag, CudaContext);

                vData.InitialiseCudaBuffer(copyHostToDevice: false);

                vData._cudaBuffer.AsyncCopyToDevice(_cudaBuffer.DevicePointer, new SizeT(startIndex * sizeof(float)), vData._cudaZero, vData._cudaLengthBytes,
                                                    CudaFloat32Handler.GetStreamForContext(CudaContext));

                vData._flagHostModified   = false;
                vData._flagDeviceModified = false;

                return(vData);
            }
            else
            {
                OnReadAccess();

                return(new CudaSigmaDiffDataBuffer <T>(this, startIndex, length, BackendTag, CudaContext));
            }
        }