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; } } }
public CudaFloat32Handler(int deviceId = 0) : base(new CudaFloat32BackendHandle(deviceId, backendTag: -1)) { _cudaBackendHandle = (CudaFloat32BackendHandle)DiffsharpBackendHandle; DeviceId = _cudaBackendHandle.CudaContext.DeviceId; CudaDeviceProperties deviceProperties = CudaContext.GetDeviceInfo(deviceId); _logger.Info($"Using CUDA device {deviceProperties.DeviceName} with device id {deviceId} (compute capability {deviceProperties.ComputeCapability}, " + $"memory {deviceProperties.TotalGlobalMemory / (1024 * 1024)}MB)."); RegisterContext(_cudaBackendHandle.CudaContext, _cudaBackendHandle.CudaStream); }
internal void CopyFromHostToDevice() { if (!_initialisedInContext) { InitialiseCudaBuffer(copyHostToDevice: false); } CudaFloat32BackendHandle backendHandle = (CudaFloat32BackendHandle)SigmaDiffSharpBackendProvider.Instance.GetBackend <T>(BackendTag).BackendHandle; backendHandle.MarkDeviceBufferModified((float[])(object)_data); _cudaBuffer.CopyToDevice(_data, _cudaOffsetBytes, _cudaZero, _cudaLengthBytes); }