forked from NVIDIA/cuopt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcusparse_view.cu
More file actions
400 lines (360 loc) · 17 KB
/
cusparse_view.cu
File metadata and controls
400 lines (360 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
* SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights
* reserved. SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <dual_simplex/dense_vector.hpp>
#include <dual_simplex/pinned_host_allocator.hpp>
#include <dual_simplex/sparse_matrix.hpp>
#include "dual_simplex/cusparse_view.hpp"
#include <utilities/copy_helpers.hpp>
#include <utilities/macros.cuh>
#include <cuopt/error.hpp>
#include <raft/sparse/detail/cusparse_macros.h>
#include <raft/sparse/detail/cusparse_wrappers.h>
#include <raft/sparse/linalg/transpose.cuh>
#include <dlfcn.h>
namespace cuopt::linear_programming::dual_simplex {
#define CUDA_VER_12_4_UP (CUDART_VERSION >= 12040)
#if CUDA_VER_12_4_UP
struct dynamic_load_runtime {
static void* get_cusparse_runtime_handle()
{
auto close_cudart = [](void* handle) { ::dlclose(handle); };
auto open_cudart = []() {
::dlerror();
int major_version;
RAFT_CUSPARSE_TRY(cusparseGetProperty(libraryPropertyType_t::MAJOR_VERSION, &major_version));
const std::string libname_ver_o = "libcusparse.so." + std::to_string(major_version) + ".0";
const std::string libname_ver = "libcusparse.so." + std::to_string(major_version);
const std::string libname = "libcusparse.so";
auto ptr = ::dlopen(libname_ver_o.c_str(), RTLD_LAZY);
if (!ptr) { ptr = ::dlopen(libname_ver.c_str(), RTLD_LAZY); }
if (!ptr) { ptr = ::dlopen(libname.c_str(), RTLD_LAZY); }
if (ptr) { return ptr; }
EXE_CUOPT_FAIL("Unable to dlopen cusparse");
};
static std::unique_ptr<void, decltype(close_cudart)> cudart_handle{open_cudart(), close_cudart};
return cudart_handle.get();
}
template <typename... Args>
using function_sig = std::add_pointer_t<cusparseStatus_t(Args...)>;
template <typename signature>
static std::optional<signature> function(const char* func_name)
{
auto* runtime = get_cusparse_runtime_handle();
auto* handle = ::dlsym(runtime, func_name);
if (!handle) { return std::nullopt; }
auto* function_ptr = reinterpret_cast<signature>(handle);
return std::optional<signature>(function_ptr);
}
};
template <typename... Args>
using cusparse_sig = dynamic_load_runtime::function_sig<Args...>;
using cusparseSpMV_preprocess_sig = cusparse_sig<cusparseHandle_t,
cusparseOperation_t,
const void*,
cusparseConstSpMatDescr_t,
cusparseConstDnVecDescr_t,
const void*,
cusparseDnVecDescr_t,
cudaDataType,
cusparseSpMVAlg_t,
void*>;
// This is tmp until it's added to raft
template <
typename T,
typename std::enable_if_t<std::is_same_v<T, float> || std::is_same_v<T, double>>* = nullptr>
void my_cusparsespmv_preprocess(cusparseHandle_t handle,
cusparseOperation_t opA,
const T* alpha,
cusparseConstSpMatDescr_t matA,
cusparseConstDnVecDescr_t vecX,
const T* beta,
cusparseDnVecDescr_t vecY,
cusparseSpMVAlg_t alg,
void* externalBuffer,
cudaStream_t stream)
{
auto constexpr float_type = []() constexpr {
if constexpr (std::is_same_v<T, float>) {
return CUDA_R_32F;
} else if constexpr (std::is_same_v<T, double>) {
return CUDA_R_64F;
}
}();
// There can be a missmatch between compiled CUDA version and the runtime CUDA version
// Since cusparse is only available post >= 12.4 we need to use dlsym to make sure the symbol is
// present at runtime
static const auto func =
dynamic_load_runtime::function<cusparseSpMV_preprocess_sig>("cusparseSpMV_preprocess");
if (func.has_value()) {
RAFT_CUSPARSE_TRY(cusparseSetStream(handle, stream));
RAFT_CUSPARSE_TRY(
(*func)(handle, opA, alpha, matA, vecX, beta, vecY, float_type, alg, externalBuffer));
}
}
#endif
template <typename i_t, typename f_t>
cusparse_view_t<i_t, f_t>::cusparse_view_t(raft::handle_t const* handle_ptr,
const csc_matrix_t<i_t, f_t>& A)
: handle_ptr_(handle_ptr),
A_offsets_(0, handle_ptr->get_stream()),
A_indices_(0, handle_ptr->get_stream()),
A_data_(0, handle_ptr->get_stream()),
A_T_offsets_(0, handle_ptr->get_stream()),
A_T_indices_(0, handle_ptr->get_stream()),
A_T_data_(0, handle_ptr->get_stream()),
spmv_buffer_(0, handle_ptr->get_stream()),
d_one_(f_t(1), handle_ptr->get_stream()),
d_minus_one_(f_t(-1), handle_ptr->get_stream()),
d_zero_(f_t(0), handle_ptr->get_stream())
{
RAFT_CUBLAS_TRY(raft::linalg::detail::cublassetpointermode(
handle_ptr->get_cublas_handle(), CUBLAS_POINTER_MODE_DEVICE, handle_ptr->get_stream()));
RAFT_CUSPARSE_TRY(raft::sparse::detail::cusparsesetpointermode(
handle_ptr->get_cusparse_handle(), CUSPARSE_POINTER_MODE_DEVICE, handle_ptr->get_stream()));
// TMP matrix data should already be on the GPU
constexpr bool debug = false;
if (debug) { printf("A hash: %zu\n", A.hash()); }
csr_matrix_t<i_t, f_t> A_csr(A.m, A.n, 1);
A.to_compressed_row(A_csr);
i_t rows = A_csr.m;
i_t cols = A_csr.n;
i_t nnz = A_csr.x.size();
const std::vector<i_t>& offsets = A_csr.row_start;
const std::vector<i_t>& indices = A_csr.j;
const std::vector<f_t>& data = A_csr.x;
A_offsets_ = device_copy(offsets, handle_ptr->get_stream());
A_indices_ = device_copy(indices, handle_ptr->get_stream());
A_data_ = device_copy(data, handle_ptr->get_stream());
A_T_offsets_ = device_copy(A.col_start, handle_ptr->get_stream());
A_T_indices_ = device_copy(A.i, handle_ptr->get_stream());
A_T_data_ = device_copy(A.x, handle_ptr->get_stream());
cusparseCreateCsr(&A_,
rows,
cols,
nnz,
A_offsets_.data(),
A_indices_.data(),
A_data_.data(),
CUSPARSE_INDEX_32I,
CUSPARSE_INDEX_32I,
CUSPARSE_INDEX_BASE_ZERO,
CUDA_R_64F);
cusparseCreateCsr(&A_T_,
cols,
rows,
nnz,
A_T_offsets_.data(),
A_T_indices_.data(),
A_T_data_.data(),
CUSPARSE_INDEX_32I,
CUSPARSE_INDEX_32I,
CUSPARSE_INDEX_BASE_ZERO,
CUDA_R_64F);
// Tmp just to init the buffer size and preprocess
cusparseDnVecDescr_t x;
cusparseDnVecDescr_t y;
rmm::device_uvector<f_t> d_x(cols, handle_ptr_->get_stream());
rmm::device_uvector<f_t> d_y(rows, handle_ptr_->get_stream());
RAFT_CUSPARSE_TRY(raft::sparse::detail::cusparsecreatednvec(&x, d_x.size(), d_x.data()));
RAFT_CUSPARSE_TRY(raft::sparse::detail::cusparsecreatednvec(&y, d_y.size(), d_y.data()));
size_t buffer_size_spmv = 0;
RAFT_CUSPARSE_TRY(
raft::sparse::detail::cusparsespmv_buffersize(handle_ptr_->get_cusparse_handle(),
CUSPARSE_OPERATION_NON_TRANSPOSE,
d_one_.data(),
A_,
x,
d_one_.data(),
y,
CUSPARSE_SPMV_CSR_ALG2,
&buffer_size_spmv,
handle_ptr_->get_stream()));
spmv_buffer_.resize(buffer_size_spmv, handle_ptr_->get_stream());
my_cusparsespmv_preprocess(handle_ptr_->get_cusparse_handle(),
CUSPARSE_OPERATION_NON_TRANSPOSE,
d_one_.data(),
A_,
x,
d_one_.data(),
y,
CUSPARSE_SPMV_CSR_ALG2,
spmv_buffer_.data(),
handle_ptr->get_stream());
RAFT_CUSPARSE_TRY(
raft::sparse::detail::cusparsespmv_buffersize(handle_ptr_->get_cusparse_handle(),
CUSPARSE_OPERATION_NON_TRANSPOSE,
d_one_.data(),
A_T_,
y,
d_one_.data(),
x,
CUSPARSE_SPMV_CSR_ALG2,
&buffer_size_spmv,
handle_ptr_->get_stream()));
spmv_buffer_transpose_.resize(buffer_size_spmv, handle_ptr_->get_stream());
my_cusparsespmv_preprocess(handle_ptr_->get_cusparse_handle(),
CUSPARSE_OPERATION_NON_TRANSPOSE,
d_one_.data(),
A_T_,
y,
d_one_.data(),
x,
CUSPARSE_SPMV_CSR_ALG2,
spmv_buffer_transpose_.data(),
handle_ptr->get_stream());
}
template <typename i_t, typename f_t>
cusparseDnVecDescr_t cusparse_view_t<i_t, f_t>::create_vector(const rmm::device_uvector<f_t>& vec)
{
// TODO add to RAFT a const version
// No RAFT version without the const so you will get a linktime issuen hence the const_cast
cusparseDnVecDescr_t cusparse_h;
RAFT_CUSPARSE_TRY(raft::sparse::detail::cusparsecreatednvec(
&cusparse_h, vec.size(), const_cast<f_t*>(vec.data())));
return cusparse_h;
}
template <typename i_t, typename f_t>
template <typename AllocatorA, typename AllocatorB>
void cusparse_view_t<i_t, f_t>::spmv(f_t alpha,
const std::vector<f_t, AllocatorA>& x,
f_t beta,
std::vector<f_t, AllocatorB>& y)
{
auto d_x = device_copy(x, handle_ptr_->get_stream());
auto d_y = device_copy(y, handle_ptr_->get_stream());
cusparseDnVecDescr_t x_cusparse = create_vector(d_x);
cusparseDnVecDescr_t y_cusparse = create_vector(d_y);
spmv(alpha, x_cusparse, beta, y_cusparse);
y = cuopt::host_copy<f_t, AllocatorB>(d_y, handle_ptr_->get_stream());
}
template <typename i_t, typename f_t>
void cusparse_view_t<i_t, f_t>::spmv(f_t alpha,
cusparseDnVecDescr_t x,
f_t beta,
cusparseDnVecDescr_t y)
{
// Would be simpler if we could pass host data direclty but other cusparse calls with the same
// handler depend on device data
cuopt_assert(alpha == f_t(1) || alpha == f_t(-1), "Only alpha 1 or -1 supported");
cuopt_assert(beta == f_t(1) || beta == f_t(-1) || beta == f_t(0),
"Only beta 1 or -1 or 0 supported");
rmm::device_scalar<f_t>* d_beta = &d_one_;
if (beta == f_t(0))
d_beta = &d_zero_;
else if (beta == f_t(-1))
d_beta = &d_minus_one_;
raft::sparse::detail::cusparsespmv(handle_ptr_->get_cusparse_handle(),
CUSPARSE_OPERATION_NON_TRANSPOSE,
(alpha == 1) ? d_one_.data() : d_minus_one_.data(),
A_,
x,
d_beta->data(),
y,
CUSPARSE_SPMV_CSR_ALG2,
(f_t*)spmv_buffer_.data(),
handle_ptr_->get_stream());
}
template <typename i_t, typename f_t>
template <typename AllocatorA, typename AllocatorB>
void cusparse_view_t<i_t, f_t>::transpose_spmv(f_t alpha,
const std::vector<f_t, AllocatorA>& x,
f_t beta,
std::vector<f_t, AllocatorB>& y)
{
auto d_x = device_copy(x, handle_ptr_->get_stream());
auto d_y = device_copy(y, handle_ptr_->get_stream());
cusparseDnVecDescr_t x_cusparse = create_vector(d_x);
cusparseDnVecDescr_t y_cusparse = create_vector(d_y);
transpose_spmv(alpha, x_cusparse, beta, y_cusparse);
y = cuopt::host_copy<f_t, AllocatorB>(d_y, handle_ptr_->get_stream());
}
template <typename i_t, typename f_t>
void cusparse_view_t<i_t, f_t>::transpose_spmv(f_t alpha,
cusparseDnVecDescr_t x,
f_t beta,
cusparseDnVecDescr_t y)
{
// Would be simpler if we could pass host data direct;y but other cusparse calls with the same
// handler depend on device data
cuopt_assert(alpha == f_t(1) || alpha == f_t(-1), "Only alpha 1 or -1 supported");
cuopt_assert(beta == f_t(1) || beta == f_t(-1) || beta == f_t(0),
"Only beta 1 or -1 or 0 supported");
rmm::device_scalar<f_t>* d_beta = &d_one_;
if (beta == f_t(0))
d_beta = &d_zero_;
else if (beta == f_t(-1))
d_beta = &d_minus_one_;
raft::sparse::detail::cusparsespmv(handle_ptr_->get_cusparse_handle(),
CUSPARSE_OPERATION_NON_TRANSPOSE,
(alpha == 1) ? d_one_.data() : d_minus_one_.data(),
A_T_,
x,
d_beta->data(),
y,
CUSPARSE_SPMV_CSR_ALG2,
(f_t*)spmv_buffer_transpose_.data(),
handle_ptr_->get_stream());
}
template class cusparse_view_t<int, double>;
template void
cusparse_view_t<int, double>::spmv<PinnedHostAllocator<double>, PinnedHostAllocator<double>>(
double alpha,
const std::vector<double, PinnedHostAllocator<double>>& x,
double beta,
std::vector<double, PinnedHostAllocator<double>>& y);
template void
cusparse_view_t<int, double>::spmv<PinnedHostAllocator<double>, std::allocator<double>>(
double alpha,
const std::vector<double, PinnedHostAllocator<double>>& x,
double beta,
std::vector<double, std::allocator<double>>& y);
template void
cusparse_view_t<int, double>::spmv<std::allocator<double>, PinnedHostAllocator<double>>(
double alpha,
const std::vector<double, std::allocator<double>>& x,
double beta,
std::vector<double, PinnedHostAllocator<double>>& y);
template void cusparse_view_t<int, double>::spmv<std::allocator<double>, std::allocator<double>>(
double alpha,
const std::vector<double, std::allocator<double>>& x,
double beta,
std::vector<double, std::allocator<double>>& y);
template void cusparse_view_t<int, double>::transpose_spmv<PinnedHostAllocator<double>,
PinnedHostAllocator<double>>(
double alpha,
const std::vector<double, PinnedHostAllocator<double>>& x,
double beta,
std::vector<double, PinnedHostAllocator<double>>& y);
template void
cusparse_view_t<int, double>::transpose_spmv<PinnedHostAllocator<double>, std::allocator<double>>(
double alpha,
const std::vector<double, PinnedHostAllocator<double>>& x,
double beta,
std::vector<double, std::allocator<double>>& y);
template void
cusparse_view_t<int, double>::transpose_spmv<std::allocator<double>, PinnedHostAllocator<double>>(
double alpha,
const std::vector<double, std::allocator<double>>& x,
double beta,
std::vector<double, PinnedHostAllocator<double>>& y);
template void
cusparse_view_t<int, double>::transpose_spmv<std::allocator<double>, std::allocator<double>>(
double alpha,
const std::vector<double, std::allocator<double>>& x,
double beta,
std::vector<double, std::allocator<double>>& y);
} // namespace cuopt::linear_programming::dual_simplex