Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[src] Use cuda streams in matrix library #2821

Merged
merged 2 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/cudamatrix/cu-array-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ void CuArray<T>::CopyFromArray(const CuArrayBase<T> &src) {
#if HAVE_CUDA == 1
if (CuDevice::Instantiate().Enabled()) {
CuTimer tim;
CU_SAFE_CALL(cudaMemcpy(this->data_, src.data_, this->dim_ * sizeof(T),
cudaMemcpyDeviceToDevice));
CU_SAFE_CALL(cudaMemcpyAsync(this->data_, src.data_, this->dim_ * sizeof(T),
cudaMemcpyDeviceToDevice,
cudaStreamPerThread));
CuDevice::Instantiate().AccuProfile(__func__, tim);
} else
#endif
Expand All @@ -158,8 +159,8 @@ void CuArrayBase<T>::CopyFromArray(const CuArrayBase<T> &src) {
if (CuDevice::Instantiate().Enabled()) {
CuTimer tim;
CU_SAFE_CALL(
cudaMemcpy(this->data_, src.data_, dim_ * sizeof(T),
cudaMemcpyDeviceToDevice));
cudaMemcpyAsync(this->data_, src.data_, dim_ * sizeof(T),
cudaMemcpyDeviceToDevice, cudaStreamPerThread));
CuDevice::Instantiate().AccuProfile(__func__, tim);
} else
#endif
Expand Down
21 changes: 12 additions & 9 deletions src/cudamatrix/cu-matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,10 @@ void CuMatrixBase<Real>::CopyFromMat(const CuMatrixBase<OtherReal> &M,
MatrixIndexT dst_pitch = stride_ * sizeof(Real);
MatrixIndexT src_pitch = M.Stride() * sizeof(Real);
MatrixIndexT width = M.NumCols() * sizeof(Real);
CU_SAFE_CALL(cudaMemcpy2D(data_, dst_pitch, M.data_, src_pitch,
width, M.num_rows_, cudaMemcpyDeviceToDevice));
CU_SAFE_CALL(
cudaMemcpy2DAsync(data_, dst_pitch, M.data_, src_pitch,
width, M.num_rows_, cudaMemcpyDeviceToDevice,
cudaStreamPerThread));
} else {
if (trans == kNoTrans) {
dim3 dimGrid, dimBlock;
Expand Down Expand Up @@ -2286,14 +2288,15 @@ void CuMatrixBase<Real>::CopyRowsFromVec(const CuVectorBase<Real> &v) {
if (v.Dim() == num_rows_*num_cols_) {
if (stride_ == num_cols_) {
const Real* v_data = v.Data();
CU_SAFE_CALL(cudaMemcpy(data_, v_data,
sizeof(Real)*num_rows_*num_cols_,
cudaMemcpyDeviceToDevice));
CU_SAFE_CALL(
cudaMemcpyAsync(data_, v_data, sizeof(Real)*num_rows_*num_cols_,
cudaMemcpyDeviceToDevice, cudaStreamPerThread));
} else {
CU_SAFE_CALL(cudaMemcpy2D(data_, stride_ * sizeof(Real), v.Data(),
num_cols_*sizeof(Real), num_cols_*sizeof(Real),
num_rows_,
cudaMemcpyDeviceToDevice));
CU_SAFE_CALL(
cudaMemcpy2DAsync(data_, stride_ * sizeof(Real), v.Data(),
num_cols_*sizeof(Real), num_cols_*sizeof(Real),
num_rows_, cudaMemcpyDeviceToDevice,
cudaStreamPerThread));
}
} else if (v.Dim() == num_cols_) {
dim3 dimGrid, dimBlock;
Expand Down
5 changes: 3 additions & 2 deletions src/cudamatrix/cu-packed-matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ void CuPackedMatrix<Real>::CopyFromPacked(const CuPackedMatrix<Real> &src) {
size_t nr = static_cast<size_t>(num_rows_),
num_bytes = ((nr * (nr+1)) / 2) * sizeof(Real);

CU_SAFE_CALL(cudaMemcpy(data_, src.data_, num_bytes,
cudaMemcpyDeviceToDevice));
CU_SAFE_CALL(
cudaMemcpyAsync(data_, src.data_, num_bytes, cudaMemcpyDeviceToDevice,
cudaStreamPerThread));
CuDevice::Instantiate().AccuProfile("CuPackedMatrix::CopyFromPacked1",
tim);
} else
Expand Down
4 changes: 3 additions & 1 deletion src/cudamatrix/cu-value.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class CuValue {
inline CuValue operator = (const CuValue<Real> &other) {
#if HAVE_CUDA == 1
if (CuDevice::Instantiate().Enabled()) {
CU_SAFE_CALL(cudaMemcpy(data_, other.data_, sizeof(Real), cudaMemcpyDeviceToDevice));
CU_SAFE_CALL(
cudaMemcpyAsync(data_, other.data_, sizeof(Real),
cudaMemcpyDeviceToDevice, cudaStreamPerThread));
return *this;
} else
#endif
Expand Down
16 changes: 10 additions & 6 deletions src/cudamatrix/cu-vector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,16 @@ void CuVectorBase<Real>::CopyRowsFromMat(const CuMatrixBase<Real> &mat) {
if (dim_ == 0) return;
CuTimer tim;
if (mat.Stride() == mat.NumCols() && mat.NumRows() != 0) {
CU_SAFE_CALL(cudaMemcpy(data_, mat.Data(), sizeof(Real)*dim_,
cudaMemcpyDeviceToDevice));
CU_SAFE_CALL(
cudaMemcpyAsync(data_, mat.Data(), sizeof(Real)*dim_,
cudaMemcpyDeviceToDevice, cudaStreamPerThread));
} else {
Real* vec_data = data_;
for (MatrixIndexT r = 0; r < mat.NumRows(); r++) {
CU_SAFE_CALL(cudaMemcpy(vec_data, mat.RowData(r),
sizeof(Real) * mat.NumCols(),
cudaMemcpyDeviceToDevice));
CU_SAFE_CALL(cudaMemcpyAsync(vec_data, mat.RowData(r),
sizeof(Real) * mat.NumCols(),
cudaMemcpyDeviceToDevice,
cudaStreamPerThread));
vec_data += mat.NumCols();
}
}
Expand Down Expand Up @@ -1049,7 +1051,9 @@ void CuVectorBase<Real>::CopyFromVec(const CuVectorBase<Real> &src) {
if (CuDevice::Instantiate().Enabled()) {
if (dim_ == 0) return;
CuTimer tim;
CU_SAFE_CALL(cudaMemcpy(data_, src.data_, src.dim_ * sizeof(Real), cudaMemcpyDeviceToDevice));
CU_SAFE_CALL(
cudaMemcpyAsync(data_, src.data_, src.dim_ * sizeof(Real),
cudaMemcpyDeviceToDevice, cudaStreamPerThread));
CuDevice::Instantiate().AccuProfile(__func__, tim);
} else
#endif
Expand Down