Skip to content

Commit ba29506

Browse files
authored
[cublas] set CUBLAS_POINTER_MODE_HOST after setting it to device (uxlfoundation#94)
* [cublas] set CUBLAS_POINTER_MODE_HOST after setting it to device Since we are reusing the cudaHandle we need to reset this after it has been set to DEVICE. Otherwise we get illigal memory acces later when we expect the defualt value ( CUBLAS_POINTER_MODE_HOST ) to be set. * [cublas] reset CUBLAS_POINTER_MODE to host for rot and add description * [cublas] Don't set pointer mode for rot function
1 parent 58fc2c6 commit ba29506

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

src/blas/backends/cublas/cublas_level1.cpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ inline void asum(Func func, cl::sycl::queue &queue, int64_t n, cl::sycl::buffer<
5656
cublasStatus_t err;
5757
// ASUM does not support negative index
5858
CUBLAS_ERROR_FUNC(func, err, handle, n, x_, std::abs(incx), res_);
59+
// Higher level BLAS functions expect CUBLAS_POINTER_MODE_HOST
60+
// to be set, therfore we need to reset this to the default value
61+
// in order to avoid CUDA_ERROR_ILLEGAL_ADRESS errors
62+
cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_HOST);
5963
});
6064
});
6165
}
@@ -160,6 +164,10 @@ inline void rotg(Func func, cl::sycl::queue &queue, cl::sycl::buffer<T1, 1> &a,
160164
auto s_ = sc.get_mem<cuDataType1 *>(ih, s_acc);
161165
cublasStatus_t err;
162166
CUBLAS_ERROR_FUNC(func, err, handle, a_, b_, c_, s_);
167+
// Higher level BLAS functions expect CUBLAS_POINTER_MODE_HOST
168+
// to be set, therfore we need to reset this to the default value
169+
// in order to avoid CUDA_ERROR_ILLEGAL_ADRESS errors
170+
cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_HOST);
163171
});
164172
});
165173
}
@@ -201,6 +209,10 @@ inline void rotm(Func func, cl::sycl::queue &queue, int64_t n, cl::sycl::buffer<
201209
auto param_ = sc.get_mem<cuDataType *>(ih, param_acc);
202210
cublasStatus_t err;
203211
CUBLAS_ERROR_FUNC(func, err, handle, n, x_, incx, y_, incy, param_);
212+
// Higher level BLAS functions expect CUBLAS_POINTER_MODE_HOST
213+
// to be set, therfore we need to reset this to the default value
214+
// in order to avoid CUDA_ERROR_ILLEGAL_ADRESS errors
215+
cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_HOST);
204216
});
205217
});
206218
}
@@ -270,6 +282,10 @@ inline void dot(Func func, cl::sycl::queue &queue, int64_t n, cl::sycl::buffer<T
270282
auto res_ = sc.get_mem<cuDataType *>(ih, res_acc);
271283
cublasStatus_t err;
272284
CUBLAS_ERROR_FUNC(func, err, handle, n, x_, incx, y_, incy, res_);
285+
// Higher level BLAS functions expect CUBLAS_POINTER_MODE_HOST
286+
// to be set, therfore we need to reset this to the default value
287+
// in order to avoid CUDA_ERROR_ILLEGAL_ADRESS errors
288+
cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_HOST);
273289
});
274290
});
275291
}
@@ -301,12 +317,6 @@ inline void rot(Func func, cl::sycl::queue &queue, int64_t n, cl::sycl::buffer<T
301317
cgh.interop_task([=](cl::sycl::interop_handler ih) {
302318
auto sc = CublasScopedContextHandler(queue);
303319
auto handle = sc.get_handle(queue);
304-
// By default the pointer mode is the CUBLAS_POINTER_MODE_HOST
305-
// when the data is on buffer, it must be set to
306-
// CUBLAS_POINTER_MODE_DEVICE mode otherwise it causes the segmentation
307-
// fault. When it is set to device it is users responsibility to
308-
// synchronise as the function is completely asynchronous.
309-
// cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_DEVICE);
310320
auto x_ = sc.get_mem<cuDataType1 *>(ih, x_acc);
311321
auto y_ = sc.get_mem<cuDataType1 *>(ih, y_acc);
312322
cublasStatus_t err;
@@ -351,6 +361,10 @@ void sdsdot(cl::sycl::queue &queue, int64_t n, float sb, cl::sycl::buffer<float,
351361
auto res_ = sc.get_mem<float *>(ih, res_acc);
352362
cublasStatus_t err;
353363
CUBLAS_ERROR_FUNC(cublasSdot, err, handle, n, x_, incx, y_, incy, res_);
364+
// Higher level BLAS functions expect CUBLAS_POINTER_MODE_HOST
365+
// to be set, therfore we need to reset this to the default value
366+
// in order to avoid CUDA_ERROR_ILLEGAL_ADRESS errors
367+
cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_HOST);
354368
});
355369
});
356370
// Since SB is a host pointer we need to bring the result back to the host and
@@ -391,6 +405,10 @@ inline void rotmg(Func func, cl::sycl::queue &queue, cl::sycl::buffer<T, 1> &d1,
391405
auto param_ = sc.get_mem<cuDataType *>(ih, param_acc);
392406
cublasStatus_t err;
393407
CUBLAS_ERROR_FUNC(func, err, handle, d1_, d2_, x1_, y1_, param_);
408+
// Higher level BLAS functions expect CUBLAS_POINTER_MODE_HOST
409+
// to be set, therfore we need to reset this to the default value
410+
// in order to avoid CUDA_ERROR_ILLEGAL_ADRESS errors
411+
cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_HOST);
394412
});
395413
});
396414
}
@@ -437,6 +455,10 @@ inline void iamax(Func func, cl::sycl::queue &queue, int64_t n, cl::sycl::buffer
437455
// For negative incx, iamax returns 0. This behaviour is similar to that of
438456
// reference netlib BLAS.
439457
CUBLAS_ERROR_FUNC(func, err, handle, n, x_, incx, int_res_);
458+
// Higher level BLAS functions expect CUBLAS_POINTER_MODE_HOST
459+
// to be set, therfore we need to reset this to the default value
460+
// in order to avoid CUDA_ERROR_ILLEGAL_ADRESS errors
461+
cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_HOST);
440462
});
441463
});
442464
// This requires to bring the data to host, copy it, and return it back to
@@ -519,6 +541,10 @@ inline void iamin(Func func, cl::sycl::queue &queue, int64_t n, cl::sycl::buffer
519541
// For negative incx, iamin returns 0. This behaviour is similar to that of
520542
// implemented as a reference IAMIN.
521543
CUBLAS_ERROR_FUNC(func, err, handle, n, x_, incx, int_res_);
544+
// Higher level BLAS functions expect CUBLAS_POINTER_MODE_HOST
545+
// to be set, therfore we need to reset this to the default value
546+
// in order to avoid CUDA_ERROR_ILLEGAL_ADRESS errors
547+
cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_HOST);
522548
});
523549
});
524550
result.template get_access<cl::sycl::access::mode::write>()[0] =
@@ -561,6 +587,10 @@ inline void nrm2(Func func, cl::sycl::queue &queue, int64_t n, cl::sycl::buffer<
561587
cublasStatus_t err;
562588
// NRM2 does not support negative index
563589
CUBLAS_ERROR_FUNC(func, err, handle, n, x_, std::abs(incx), res_);
590+
// Higher level BLAS functions expect CUBLAS_POINTER_MODE_HOST
591+
// to be set, therfore we need to reset this to the default value
592+
// in order to avoid CUDA_ERROR_ILLEGAL_ADRESS errors
593+
cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_HOST);
564594
});
565595
});
566596
}

0 commit comments

Comments
 (0)