-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspgemm.cpp
More file actions
325 lines (286 loc) · 14.2 KB
/
Copy pathspgemm.cpp
File metadata and controls
325 lines (286 loc) · 14.2 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
/**
* @file spgemm.cpp
* @brief Demonstrates SpGEMM (sparse general matrix-matrix multiplication)
* on GPU using hipSPARSE with CSR matrices.
*
* Example output (measured on 1 AMD MI300A APU):
* \code
* Matrix A: 10000000 x 10000000 with nnz = 100000000
* Matrix B: 10000000 x 10000000 with nnz = 100000000
* Matrix C: 10000000 x 10000000 with nnz = 999994750
* First few entries of C:
* C[0] = 25.9424 (col 21866)
* C[1] = 24.7442 (col 51201)
* C[2] = 22.9642 (col 298749)
* C[3] = 0.909916 (col 379633)
* C[4] = 37.8533 (col 383441)
* C[5] = 27.9135 (col 393337)
* C[6] = 25.0793 (col 406619)
* C[7] = 15.1923 (col 426698)
* C[8] = 28.3851 (col 576559)
* C[9] = 18.1633 (col 703616)
* SpGEMM completed successfully.
* \endcode
*
* Hardware and Software Environment:
* - ROCm 7.1.1
*
* Demonstrates:
* - Generating large random sparse CSR matrices on host
* - Allocating and copying CSR matrices to GPU
* - Using hipSPARSE SpGEMM routines for sparse matrix multiplication
* - Querying and allocating output CSR matrix
* - Copying results back to host for inspection
*
* @note Requires HIP and hipSPARSE.
*
* @author Marco Zank
* @date 2026-01-07
*/
#include <cstdint>
#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
#include <hip/hip_runtime.h>
#include <hipsparse/hipsparse.h>
/**
* @brief Macro to check HIP runtime API errors.
*
* Prints the error message and exits if the HIP function fails.
*/
#define HIP_CHECK(err) \
if (err != hipSuccess) \
{ \
std::cerr << "HIP error: " << hipGetErrorString(err) \
<< " at line " << __LINE__ << std::endl; \
std::exit(EXIT_FAILURE); \
}
/**
* @brief Macro to check hipSPARSE API errors.
*
* Prints the error message and exits if the hipSPARSE function fails.
*/
#define HIPSPARSE_CHECK(err) \
if (err != HIPSPARSE_STATUS_SUCCESS) \
{ \
std::cerr << "hipSPARSE error at line " << __LINE__ << std::endl; \
std::exit(EXIT_FAILURE); \
}
/**
* @brief Main function demonstrating large SpGEMM using hipSPARSE.
*
* Steps:
* 1. Generate large random sparse matrices A and B in CSR format on host.
* 2. Allocate GPU memory and copy A and B.
* 3. Create hipSPARSE CSR descriptors.
* 4. Perform SpGEMM (work estimation, compute, copy) on GPU.
* 5. Query output matrix C, allocate memory, and copy first few entries to host.
* 6. Cleanup GPU resources.
*
* @return int Returns EXIT_SUCCESS on success.
*/
int main()
{
// ------------------------------------------------------------
// Create hipSPARSE handle
// ------------------------------------------------------------
hipsparseHandle_t handle;
HIPSPARSE_CHECK(hipsparseCreate(&handle));
// ------------------------------------------------------------
// Matrix dimensions
// ------------------------------------------------------------
constexpr size_t A_rows = 10'000'000; /**< Number of rows of A */
constexpr size_t A_cols = 10'000'000; /**< Number of columns of A */
constexpr size_t B_rows = A_cols; /**< Number of rows of B */
constexpr size_t B_cols = 10'000'000; /**< Number of columns of B */
constexpr size_t nnzA = 100'000'000; /**< Non-zero elements in A */
constexpr size_t nnzB = 100'000'000; /**< Non-zero elements in B */
// ------------------------------------------------------------
// Host CSR memory allocation
// ------------------------------------------------------------
std::vector<int> hA_rp(A_rows + 1, 0); /**< Row pointers of A */
std::vector<int> hA_ci(nnzA); /**< Column indices of A */
std::vector<double> hA_v(nnzA); /**< Values of A */
std::vector<int> hB_rp(B_rows + 1, 0); /**< Row pointers of B */
std::vector<int> hB_ci(nnzB); /**< Column indices of B */
std::vector<double> hB_v(nnzB); /**< Values of B */
// Random number generation
std::mt19937 rng(123);
std::uniform_int_distribution<size_t> col_dist(0, A_cols - 1);
std::uniform_real_distribution<double> val_dist(0.1, 10.0);
// ------------------------------------------------------------
// Generate random sparse CSR matrix A
// ------------------------------------------------------------
for (size_t row = 0; row < A_rows; ++row)
{
size_t row_nnz = nnzA / A_rows;
hA_rp[row + 1] = hA_rp[row] + static_cast<int>(row_nnz);
for (int i = hA_rp[row]; i < hA_rp[row + 1]; ++i)
{
size_t i_size_t = static_cast<size_t>(i);
hA_ci[i_size_t] = static_cast<int>(col_dist(rng));
hA_v[i_size_t] = val_dist(rng);
}
}
// ------------------------------------------------------------
// Generate random sparse CSR matrix B
// ------------------------------------------------------------
for (size_t row = 0; row < B_rows; ++row)
{
size_t row_nnz = nnzB / B_rows;
hB_rp[row + 1] = hB_rp[row] + static_cast<int>(row_nnz);
for (int i = hB_rp[row]; i < hB_rp[row + 1]; ++i)
{
size_t i_size_t = static_cast<size_t>(i);
hB_ci[i_size_t] = static_cast<int>(col_dist(rng));
hB_v[i_size_t] = val_dist(rng);
}
}
// ------------------------------------------------------------
// Device memory allocation
// ------------------------------------------------------------
int *dA_rp = nullptr, *dA_ci = nullptr, *dB_rp = nullptr, *dB_ci = nullptr;
double *dA_v = nullptr, *dB_v = nullptr;
HIP_CHECK(hipMalloc(&dA_rp, (A_rows + 1) * sizeof(int)));
HIP_CHECK(hipMalloc(&dA_ci, nnzA * sizeof(int)));
HIP_CHECK(hipMalloc(&dA_v, nnzA * sizeof(double)));
HIP_CHECK(hipMalloc(&dB_rp, (B_rows + 1) * sizeof(int)));
HIP_CHECK(hipMalloc(&dB_ci, nnzB * sizeof(int)));
HIP_CHECK(hipMalloc(&dB_v, nnzB * sizeof(double)));
HIP_CHECK(hipMemcpy(dA_rp, hA_rp.data(), (A_rows + 1) * sizeof(int), hipMemcpyHostToDevice));
HIP_CHECK(hipMemcpy(dA_ci, hA_ci.data(), nnzA * sizeof(int), hipMemcpyHostToDevice));
HIP_CHECK(hipMemcpy(dA_v, hA_v.data(), nnzA * sizeof(double), hipMemcpyHostToDevice));
HIP_CHECK(hipMemcpy(dB_rp, hB_rp.data(), (B_rows + 1) * sizeof(int), hipMemcpyHostToDevice));
HIP_CHECK(hipMemcpy(dB_ci, hB_ci.data(), nnzB * sizeof(int), hipMemcpyHostToDevice));
HIP_CHECK(hipMemcpy(dB_v, hB_v.data(), nnzB * sizeof(double), hipMemcpyHostToDevice));
// ------------------------------------------------------------
// Create CSR descriptors
// ------------------------------------------------------------
hipsparseSpMatDescr_t matA, matB, matC;
HIPSPARSE_CHECK(hipsparseCreateCsr(&matA,
static_cast<int64_t>(A_rows),
static_cast<int64_t>(A_cols),
static_cast<int64_t>(nnzA),
dA_rp, dA_ci, dA_v,
HIPSPARSE_INDEX_32I, HIPSPARSE_INDEX_32I,
HIPSPARSE_INDEX_BASE_ZERO, HIP_R_64F));
HIPSPARSE_CHECK(hipsparseCreateCsr(&matB,
static_cast<int64_t>(B_rows),
static_cast<int64_t>(B_cols),
static_cast<int64_t>(nnzB),
dB_rp, dB_ci, dB_v,
HIPSPARSE_INDEX_32I, HIPSPARSE_INDEX_32I,
HIPSPARSE_INDEX_BASE_ZERO, HIP_R_64F));
HIPSPARSE_CHECK(hipsparseCreateCsr(&matC,
static_cast<int64_t>(A_rows),
static_cast<int64_t>(B_cols),
0, nullptr, nullptr, nullptr,
HIPSPARSE_INDEX_32I, HIPSPARSE_INDEX_32I,
HIPSPARSE_INDEX_BASE_ZERO, HIP_R_64F));
// ------------------------------------------------------------
// SpGEMM parameters and descriptor
// ------------------------------------------------------------
double alpha = 1.0, beta = 0.0;
hipsparseSpGEMMDescr_t spgemmDesc;
HIPSPARSE_CHECK(hipsparseSpGEMM_createDescr(&spgemmDesc));
// ------------------------------------------------------------
// Step 1: Work estimation
// ------------------------------------------------------------
size_t bufferSize1 = 0, bufferSize2 = 0;
void *dBuffer1 = nullptr, *dBuffer2 = nullptr;
HIPSPARSE_CHECK(hipsparseSpGEMM_workEstimation(handle,
HIPSPARSE_OPERATION_NON_TRANSPOSE,
HIPSPARSE_OPERATION_NON_TRANSPOSE,
&alpha, matA, matB, &beta, matC,
HIP_R_64F, HIPSPARSE_SPGEMM_DEFAULT,
spgemmDesc, &bufferSize1, nullptr));
HIP_CHECK(hipMalloc(&dBuffer1, bufferSize1));
HIPSPARSE_CHECK(hipsparseSpGEMM_workEstimation(handle,
HIPSPARSE_OPERATION_NON_TRANSPOSE,
HIPSPARSE_OPERATION_NON_TRANSPOSE,
&alpha, matA, matB, &beta, matC,
HIP_R_64F, HIPSPARSE_SPGEMM_DEFAULT,
spgemmDesc, &bufferSize1, dBuffer1));
// ------------------------------------------------------------
// Step 2: Compute
// ------------------------------------------------------------
HIPSPARSE_CHECK(hipsparseSpGEMM_compute(handle,
HIPSPARSE_OPERATION_NON_TRANSPOSE,
HIPSPARSE_OPERATION_NON_TRANSPOSE,
&alpha, matA, matB, &beta, matC,
HIP_R_64F, HIPSPARSE_SPGEMM_DEFAULT,
spgemmDesc, &bufferSize2, nullptr));
HIP_CHECK(hipMalloc(&dBuffer2, bufferSize2));
HIPSPARSE_CHECK(hipsparseSpGEMM_compute(handle,
HIPSPARSE_OPERATION_NON_TRANSPOSE,
HIPSPARSE_OPERATION_NON_TRANSPOSE,
&alpha, matA, matB, &beta, matC,
HIP_R_64F, HIPSPARSE_SPGEMM_DEFAULT,
spgemmDesc, &bufferSize2, dBuffer2));
// ------------------------------------------------------------
// Step 3: Query nnz of C
// ------------------------------------------------------------
int64_t C_rows_int, C_cols_int, nnzC_int;
HIPSPARSE_CHECK(hipsparseSpMatGetSize(matC, &C_rows_int, &C_cols_int, &nnzC_int));
size_t C_rows = static_cast<size_t>(C_rows_int);
size_t C_cols = static_cast<size_t>(C_cols_int);
size_t nnzC = static_cast<size_t>(nnzC_int);
std::cout << "Matrix A: " << A_rows << " x " << A_cols
<< " with nnz = " << nnzA << "\n";
std::cout << "Matrix B: " << B_rows << " x " << B_cols
<< " with nnz = " << nnzB << "\n";
std::cout << "Matrix C: " << C_rows << " x " << C_cols
<< " with nnz = " << nnzC << "\n";
// ------------------------------------------------------------
// Step 4: Allocate C
// ------------------------------------------------------------
int *dC_rp = nullptr, *dC_ci = nullptr;
double *dC_v = nullptr;
HIP_CHECK(hipMalloc(&dC_rp, (C_rows + 1) * sizeof(int)));
HIP_CHECK(hipMalloc(&dC_ci, nnzC * sizeof(int)));
HIP_CHECK(hipMalloc(&dC_v, nnzC * sizeof(double)));
HIPSPARSE_CHECK(hipsparseCsrSetPointers(matC, dC_rp, dC_ci, dC_v));
// ------------------------------------------------------------
// Step 5: Copy result
// ------------------------------------------------------------
HIPSPARSE_CHECK(hipsparseSpGEMM_copy(handle,
HIPSPARSE_OPERATION_NON_TRANSPOSE,
HIPSPARSE_OPERATION_NON_TRANSPOSE,
&alpha, matA, matB, &beta, matC,
HIP_R_64F, HIPSPARSE_SPGEMM_DEFAULT,
spgemmDesc));
// ------------------------------------------------------------
// Copy first 10 entries to host
// ------------------------------------------------------------
size_t print_nnz = std::min<size_t>(nnzC, 10);
std::vector<int> hC_ci(print_nnz);
std::vector<double> hC_v(print_nnz);
HIP_CHECK(hipMemcpy(hC_ci.data(), dC_ci, print_nnz * sizeof(int), hipMemcpyDeviceToHost));
HIP_CHECK(hipMemcpy(hC_v.data(), dC_v, print_nnz * sizeof(double), hipMemcpyDeviceToHost));
std::cout << "First few entries of C:\n";
for (size_t i = 0; i < print_nnz; ++i)
std::cout << "C[" << i << "] = " << hC_v[i]
<< " (col " << hC_ci[i] << ")\n";
// ------------------------------------------------------------
// Cleanup
// ------------------------------------------------------------
HIP_CHECK(hipFree(dA_rp));
HIP_CHECK(hipFree(dA_ci));
HIP_CHECK(hipFree(dA_v));
HIP_CHECK(hipFree(dB_rp));
HIP_CHECK(hipFree(dB_ci));
HIP_CHECK(hipFree(dB_v));
HIP_CHECK(hipFree(dC_rp));
HIP_CHECK(hipFree(dC_ci));
HIP_CHECK(hipFree(dC_v));
HIP_CHECK(hipFree(dBuffer1));
HIP_CHECK(hipFree(dBuffer2));
HIPSPARSE_CHECK(hipsparseDestroySpMat(matA));
HIPSPARSE_CHECK(hipsparseDestroySpMat(matB));
HIPSPARSE_CHECK(hipsparseDestroySpMat(matC));
HIPSPARSE_CHECK(hipsparseSpGEMM_destroyDescr(spgemmDesc));
HIPSPARSE_CHECK(hipsparseDestroy(handle));
std::cout << "SpGEMM completed successfully.\n";
return EXIT_SUCCESS;
}