Skip to content

Commit 1b62c41

Browse files
authored
Fix lanczos solver integer overflow (#2536)
Partially answers rapidsai/cuml#6204 Authors: - Victor Lafargue (https://github.com/viclafargue) - Corey J. Nolet (https://github.com/cjnolet) Approvers: - Corey J. Nolet (https://github.com/cjnolet) - Micka (https://github.com/lowener) URL: #2536
1 parent 8fc988e commit 1b62c41

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

cpp/include/raft/sparse/detail/coo.cuh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class COO {
182182
* @param n_rows: number of rows in the dense matrix
183183
* @param n_cols: number of columns in the dense matrix
184184
*/
185-
void setSize(int n_rows, int n_cols)
185+
void setSize(Index_Type n_rows, Index_Type n_cols)
186186
{
187187
this->n_rows = n_rows;
188188
this->n_cols = n_cols;
@@ -192,7 +192,7 @@ class COO {
192192
* @brief Set the number of rows and cols for a square dense matrix
193193
* @param n: number of rows and cols
194194
*/
195-
void setSize(int n)
195+
void setSize(Index_Type n)
196196
{
197197
this->n_rows = n;
198198
this->n_cols = n;
@@ -204,7 +204,10 @@ class COO {
204204
* @param init: should values be initialized to 0?
205205
* @param stream: CUDA stream to use
206206
*/
207-
void allocate(int nnz, bool init, cudaStream_t stream) { this->allocate(nnz, 0, init, stream); }
207+
void allocate(Index_Type nnz, bool init, cudaStream_t stream)
208+
{
209+
this->allocate(nnz, 0, init, stream);
210+
}
208211

209212
/**
210213
* @brief Allocate the underlying arrays
@@ -213,7 +216,7 @@ class COO {
213216
* @param init: should values be initialized to 0?
214217
* @param stream: CUDA stream to use
215218
*/
216-
void allocate(int nnz, int size, bool init, cudaStream_t stream)
219+
void allocate(Index_Type nnz, Index_Type size, bool init, cudaStream_t stream)
217220
{
218221
this->allocate(nnz, size, size, init, stream);
219222
}
@@ -226,7 +229,8 @@ class COO {
226229
* @param init: should values be initialized to 0?
227230
* @param stream: stream to use for init
228231
*/
229-
void allocate(int nnz, int n_rows, int n_cols, bool init, cudaStream_t stream)
232+
void allocate(
233+
Index_Type nnz, Index_Type n_rows, Index_Type n_cols, bool init, cudaStream_t stream)
230234
{
231235
this->n_rows = n_rows;
232236
this->n_cols = n_cols;

cpp/include/raft/sparse/solver/detail/lanczos.cuh

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ static int lanczosRestart(raft::resources const& handle,
624624
value_type_t* shifts_host;
625625

626626
// Orthonormal matrix for similarity transform
627-
value_type_t* V_dev = work_dev + n * iter;
627+
value_type_t* V_dev = work_dev + (size_t)n * (size_t)iter;
628628

629629
// -------------------------------------------------------
630630
// Implementation
@@ -641,7 +641,7 @@ static int lanczosRestart(raft::resources const& handle,
641641
// std::cout <<std::endl;
642642

643643
// Initialize similarity transform with identity matrix
644-
memset(V_host, 0, iter * iter * sizeof(value_type_t));
644+
memset(V_host, 0, (size_t)iter * (size_t)iter * (size_t)sizeof(value_type_t));
645645
for (i = 0; i < iter; ++i)
646646
V_host[IDX(i, i, iter)] = 1;
647647

@@ -679,8 +679,11 @@ static int lanczosRestart(raft::resources const& handle,
679679
WARNING("error in implicitly shifted QR algorithm");
680680

681681
// Obtain new residual
682-
RAFT_CUDA_TRY(cudaMemcpyAsync(
683-
V_dev, V_host, iter * iter * sizeof(value_type_t), cudaMemcpyHostToDevice, stream));
682+
RAFT_CUDA_TRY(cudaMemcpyAsync(V_dev,
683+
V_host,
684+
(size_t)iter * (size_t)iter * (size_t)sizeof(value_type_t),
685+
cudaMemcpyHostToDevice,
686+
stream));
684687

685688
beta_host[iter - 1] = beta_host[iter - 1] * V_host[IDX(iter - 1, iter_new - 1, iter)];
686689
RAFT_CUBLAS_TRY(raft::linalg::detail::cublasgemv(cublas_h,
@@ -716,7 +719,7 @@ static int lanczosRestart(raft::resources const& handle,
716719

717720
RAFT_CUDA_TRY(cudaMemcpyAsync(lanczosVecs_dev,
718721
work_dev,
719-
n * iter_new * sizeof(value_type_t),
722+
(size_t)n * (size_t)iter_new * (size_t)sizeof(value_type_t),
720723
cudaMemcpyDeviceToDevice,
721724
stream));
722725

@@ -1045,10 +1048,10 @@ int computeSmallestEigenvectors(
10451048
unsigned long long seed = 1234567)
10461049
{
10471050
// Matrix dimension
1048-
index_type_t n = A.nrows_;
1051+
size_t n = A.nrows_;
10491052

10501053
// Check that parameters are valid
1051-
RAFT_EXPECTS(nEigVecs > 0 && nEigVecs <= n, "Invalid number of eigenvectors.");
1054+
RAFT_EXPECTS(nEigVecs > 0 && (size_t)nEigVecs <= n, "Invalid number of eigenvectors.");
10521055
RAFT_EXPECTS(restartIter > 0, "Invalid restartIter.");
10531056
RAFT_EXPECTS(tol > 0, "Invalid tolerance.");
10541057
RAFT_EXPECTS(maxIter >= nEigVecs, "Invalid maxIter.");
@@ -1395,10 +1398,10 @@ int computeLargestEigenvectors(
13951398
unsigned long long seed = 123456)
13961399
{
13971400
// Matrix dimension
1398-
index_type_t n = A.nrows_;
1401+
size_t n = A.nrows_;
13991402

14001403
// Check that parameters are valid
1401-
RAFT_EXPECTS(nEigVecs > 0 && nEigVecs <= n, "Invalid number of eigenvectors.");
1404+
RAFT_EXPECTS(nEigVecs > 0 && (size_t)nEigVecs <= n, "Invalid number of eigenvectors.");
14021405
RAFT_EXPECTS(restartIter > 0, "Invalid restartIter.");
14031406
RAFT_EXPECTS(tol > 0, "Invalid tolerance.");
14041407
RAFT_EXPECTS(maxIter >= nEigVecs, "Invalid maxIter.");

cpp/include/raft/spectral/detail/matrix_wrappers.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@
3939
// =========================================================
4040

4141
// Get index of matrix entry
42-
#define IDX(i, j, lda) ((i) + (j) * (lda))
42+
#define IDX(i, j, lda) ((size_t)(i) + (j) * (lda))
4343

4444
namespace raft {
4545
namespace spectral {
4646
namespace matrix {
4747
namespace detail {
4848

49-
using size_type = int; // for now; TODO: move it in appropriate header
49+
using size_type = size_t; // for now; TODO: move it in appropriate header
5050

5151
// Apply diagonal matrix to vector:
5252
//
@@ -326,7 +326,7 @@ struct laplacian_matrix_t : sparse_matrix_t<index_type, value_type> {
326326
raft_handle, row_offsets, col_indices, values, nrows, nnz),
327327
diagonal_(raft_handle, nrows)
328328
{
329-
vector_t<value_type> ones{raft_handle, nrows};
329+
vector_t<value_type> ones{raft_handle, (size_t)nrows};
330330
ones.fill(1.0);
331331
sparse_matrix_t<index_type, value_type>::mv(1, ones.raw(), 0, diagonal_.raw());
332332
}
@@ -341,7 +341,7 @@ struct laplacian_matrix_t : sparse_matrix_t<index_type, value_type> {
341341
csr_m.nnz_),
342342
diagonal_(raft_handle, csr_m.nrows_)
343343
{
344-
vector_t<value_type> ones{raft_handle, csr_m.nrows_};
344+
vector_t<value_type> ones{raft_handle, (size_t)csr_m.nrows_};
345345
ones.fill(1.0);
346346
sparse_matrix_t<index_type, value_type>::mv(1, ones.raw(), 0, diagonal_.raw());
347347
}

0 commit comments

Comments
 (0)