Debian Codegraph @

hic/tests/test_hicsparse.cc

raw
1 /*
2 * (C) Copyright 2024- ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10 #include <cassert>
11 #include <functional>
12 #include <iostream>
13 #include <tuple>
14 #include <utility>
15 #include <vector>
16
17 #include "hic/hic.h"
18 #include "hic/hicsparse.h"
19
20 // -----------------------------------------------------------------------------
21
22 int test_hicsparseCreate() {
23 std::cout << "--- " << __FUNCTION__ << std::endl;
24 hicsparseHandle_t handle;
25 HICSPARSE_CALL(hicsparseCreate(&handle));
26 HICSPARSE_CALL(hicsparseDestroy(handle));
27 std::cout << "--- " << __FUNCTION__ << " SUCCEEDED " << std::endl;
28 return 0; // success
29 }
30
31 // -----------------------------------------------------------------------------
32
33 int test_hicsparseSpMV() {
34 std::cout << "--- " << __FUNCTION__ << std::endl;
35
36 // Create a sparse matrix
37 const int rows = 3;
38 const int cols = 3;
39 const int nnz = 3;
40 double values[nnz] = {1.0, 2.0, 3.0};
41 int row_offsets[rows + 1] = {0, 1, 2, 3};
42 int column_indices[nnz] = {0, 1, 2};
43
44 // Put the sparse matrix onto the device
45 double* dvalues;
46 int* drow_offsets;
47 int* dcolumn_indices;
48 HIC_CALL(hicMalloc((void**)&dvalues, nnz * sizeof(double)));
49 HIC_CALL(hicMalloc((void**)&drow_offsets, (rows + 1) * sizeof(int)));
50 HIC_CALL(hicMalloc((void**)&dcolumn_indices, nnz * sizeof(int)));
51 HIC_CALL(hicMemcpy(dvalues, values, nnz * sizeof(double), hicMemcpyHostToDevice));
52 HIC_CALL(hicMemcpy(drow_offsets, row_offsets, (rows + 1) * sizeof(int), hicMemcpyHostToDevice));
53 HIC_CALL(hicMemcpy(dcolumn_indices, column_indices, nnz * sizeof(int), hicMemcpyHostToDevice));
54
55 // Create a dense vector
56 const int N = 3;
57 double x[N] = {1.0, 2.0, 3.0};
58 double y[N] = {0.0, 0.0, 0.0};
59
60 // Put the dense vector onto the device
61 double* dx;
62 double* dy;
63 HIC_CALL(hicMalloc((void**)&dx, N * sizeof(double)));
64 HIC_CALL(hicMalloc((void**)&dy, N * sizeof(double)));
65 HIC_CALL(hicMemcpy(dx, x, N * sizeof(double), hicMemcpyHostToDevice));
66 HIC_CALL(hicMemcpy(dy, y, N * sizeof(double), hicMemcpyHostToDevice));
67
68 // Create sparse library handle
69 hicsparseHandle_t handle;
70 HICSPARSE_CALL(hicsparseCreate(&handle));
71
72 // Create a sparse matrix descriptor
73 hicsparseConstSpMatDescr_t matA;
74 HICSPARSE_CALL( hicsparseCreateConstCsr(
75 &matA,
76 rows, cols, nnz,
77 drow_offsets,
78 dcolumn_indices,
79 dvalues,
80 HICSPARSE_INDEX_32I,
81 HICSPARSE_INDEX_32I,
82 HICSPARSE_INDEX_BASE_ZERO,
83 HIC_R_64F) );
84
85 // Create dense matrix descriptors
86 hicsparseConstDnVecDescr_t vecX;
87 HICSPARSE_CALL( hicsparseCreateConstDnVec(
88 &vecX,
89 N,
90 dx,
91 HIC_R_64F) );
92
93 hicsparseDnVecDescr_t vecY;
94 HICSPARSE_CALL( hicsparseCreateDnVec(
95 &vecY,
96 N,
97 dy,
98 HIC_R_64F) );
99
100 // Set parameters
101 const double alpha = 1.0;
102 const double beta = 0.0;
103
104 // Determine buffer size
105 size_t bufferSize = 0;
106 HICSPARSE_CALL( hicsparseSpMV_bufferSize(
107 handle,
108 HICSPARSE_OPERATION_NON_TRANSPOSE,
109 &alpha,
110 matA,
111 vecX,
112 &beta,
113 vecY,
114 HIC_R_64F,
115 HICSPARSE_SPMV_ALG_DEFAULT,
116 &bufferSize) );
117
118 // Allocate buffer
119 char* buffer;
120 HIC_CALL(hicMalloc(&buffer, bufferSize));
121
122 // Perform SpMV
123 // y = alpha * A * x + beta * y
124 HICSPARSE_CALL( hicsparseSpMV(
125 handle,
126 HICSPARSE_OPERATION_NON_TRANSPOSE,
127 &alpha,
128 matA,
129 vecX,
130 &beta,
131 vecY,
132 HIC_R_64F,
133 HICSPARSE_SPMV_ALG_DEFAULT,
134 buffer) );
135
136 // Copy result back to host
137 HIC_CALL(hicMemcpy(y, dy, N * sizeof(double), hicMemcpyDeviceToHost));
138
139 // Check result
140 const double expected_y[N] = {1.0, 4.0, 9.0};
141 for (int i = 0; i < N; ++i) {
142 if (y[i] != expected_y[i]) {
143 throw std::runtime_error("Error: y[" + std::to_string(i) + "] = " + std::to_string(y[i]) +
144 " != " + std::to_string(expected_y[i]));
145 }
146 }
147
148 // Clean up
149 HIC_CALL(hicFree(dy));
150 HIC_CALL(hicFree(dx));
151 HIC_CALL(hicFree(dcolumn_indices));
152 HIC_CALL(hicFree(drow_offsets));
153 HIC_CALL(hicFree(dvalues));
154 HICSPARSE_CALL(hicsparseDestroyDnVec(vecY));
155 HICSPARSE_CALL(hicsparseDestroyDnVec(vecX));
156 HICSPARSE_CALL(hicsparseDestroySpMat(matA));
157 HICSPARSE_CALL(hicsparseDestroy(handle));
158
159 std::cout << "--- " << __FUNCTION__ << " SUCCEEDED " << std::endl;
160 return 0; // success
161 }
162
163 // -----------------------------------------------------------------------------
164
165 int test_hicsparseSpMM() {
166 std::cout << "--- " << __FUNCTION__ << std::endl;
167
168 // Create a sparse matrix
169 const int rows = 3;
170 const int cols = 3;
171 const int nnz = 3;
172 double values[nnz] = {1.0, 2.0, 3.0};
173 int row_offsets[rows + 1] = {0, 1, 2, 3};
174 int column_indices[nnz] = {0, 1, 2};
175
176 // Put the sparse matrix onto the device
177 double* dvalues;
178 int* drow_offsets;
179 int* dcolumn_indices;
180 HIC_CALL(hicMalloc((void**)&dvalues, nnz * sizeof(double)));
181 HIC_CALL(hicMalloc((void**)&drow_offsets, (rows + 1) * sizeof(int)));
182 HIC_CALL(hicMalloc((void**)&dcolumn_indices, nnz * sizeof(int)));
183 HIC_CALL(hicMemcpy(dvalues, values, nnz * sizeof(double), hicMemcpyHostToDevice));
184 HIC_CALL(hicMemcpy(drow_offsets, row_offsets, (rows + 1) * sizeof(int), hicMemcpyHostToDevice));
185 HIC_CALL(hicMemcpy(dcolumn_indices, column_indices, nnz * sizeof(int), hicMemcpyHostToDevice));
186
187 // Create dense matrices
188 const int B_rows = 3;
189 const int B_cols = 3;
190 double B[B_rows * B_cols] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
191 double C[rows * B_cols] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
192
193 // Put the dense matrices onto the device
194 double* dB;
195 double* dC;
196 HIC_CALL(hicMalloc((void**)&dB, B_rows * B_cols * sizeof(double)));
197 HIC_CALL(hicMalloc((void**)&dC, rows * B_cols * sizeof(double)));
198 HIC_CALL(hicMemcpy(dB, B, B_rows * B_cols * sizeof(double), hicMemcpyHostToDevice));
199 HIC_CALL(hicMemcpy(dC, C, rows * B_cols * sizeof(double), hicMemcpyHostToDevice));
200
201 // Create sparse library handle
202 hicsparseHandle_t handle;
203 HICSPARSE_CALL(hicsparseCreate(&handle));
204
205 // Create a sparse matrix descriptor
206 hicsparseConstSpMatDescr_t matA;
207 HICSPARSE_CALL(hicsparseCreateConstCsr(
208 &matA,
209 rows, cols, nnz,
210 drow_offsets,
211 dcolumn_indices,
212 dvalues,
213 HICSPARSE_INDEX_32I,
214 HICSPARSE_INDEX_32I,
215 HICSPARSE_INDEX_BASE_ZERO,
216 HIC_R_64F));
217
218 // Create dense matrix descriptors
219 hicsparseConstDnMatDescr_t matB;
220 HICSPARSE_CALL(hicsparseCreateConstDnMat(
221 &matB,
222 B_rows,
223 B_cols,
224 B_cols,
225 dB,
226 HIC_R_64F,
227 HICSPARSE_ORDER_ROW));
228
229 hicsparseDnMatDescr_t matC;
230 HICSPARSE_CALL(hicsparseCreateDnMat(
231 &matC,
232 rows,
233 B_cols,
234 B_cols,
235 dC,
236 HIC_R_64F,
237 HICSPARSE_ORDER_ROW));
238
239 // Set parameters
240 const double alpha = 1.0;
241 const double beta = 0.0;
242
243 // Determine buffer size
244 size_t bufferSize = 0;
245 HICSPARSE_CALL(hicsparseSpMM_bufferSize(
246 handle,
247 HICSPARSE_OPERATION_NON_TRANSPOSE,
248 HICSPARSE_OPERATION_NON_TRANSPOSE,
249 &alpha,
250 matA,
251 matB,
252 &beta,
253 matC,
254 HIC_R_64F,
255 HICSPARSE_SPMM_ALG_DEFAULT,
256 &bufferSize));
257
258 // Allocate buffer
259 char* buffer;
260 HIC_CALL(hicMalloc(&buffer, bufferSize));
261
262 // Perform SpMM
263 HICSPARSE_CALL(hicsparseSpMM(
264 handle,
265 HICSPARSE_OPERATION_NON_TRANSPOSE,
266 HICSPARSE_OPERATION_NON_TRANSPOSE,
267 &alpha,
268 matA,
269 matB,
270 &beta,
271 matC,
272 HIC_R_64F,
273 HICSPARSE_SPMM_ALG_DEFAULT,
274 buffer));
275
276 // Copy result back to host
277 HIC_CALL(hicMemcpy(C, dC, rows * B_cols * sizeof(double), hicMemcpyDeviceToHost));
278
279 // Check result
280 const double expected_C[rows * B_cols] = {1.0, 2.0, 3.0, 8.0, 10.0, 12.0, 21.0, 24.0, 27.0};
281 for (int i = 0; i < rows * B_cols; ++i) {
282 if (C[i] != expected_C[i]) {
283 throw std::runtime_error("Error: C[" + std::to_string(i) + "] = " + std::to_string(C[i]) +
284 " != " + std::to_string(expected_C[i]));
285 }
286 }
287
288 // Clean up
289 HIC_CALL(hicFree(buffer));
290 HIC_CALL(hicFree(dC));
291 HIC_CALL(hicFree(dB));
292 HIC_CALL(hicFree(dcolumn_indices));
293 HIC_CALL(hicFree(drow_offsets));
294 HIC_CALL(hicFree(dvalues));
295 HICSPARSE_CALL(hicsparseDestroyDnMat(matC));
296 HICSPARSE_CALL(hicsparseDestroyDnMat(matB));
297 HICSPARSE_CALL(hicsparseDestroySpMat(matA));
298 HICSPARSE_CALL(hicsparseDestroy(handle));
299
300 std::cout << "--- " << __FUNCTION__ << " SUCCEEDED " << std::endl;
301 return 0; // success
302 }
303
304 // -----------------------------------------------------------------------------
305
306 std::vector<std::function<int()>> tests = {
307 test_hicsparseCreate,
308 test_hicsparseSpMV,
309 test_hicsparseSpMM,
310 };
311
312 int main(int argc, char* argv[]) {
313 int num_devices = 0;
314 hicGetDeviceCount(&num_devices);
315 if (num_devices == 0) {
316 std::ignore = hicGetLastError();
317 std::cout << "TEST IGNORED, hicGetDeviceCount -> 0" << std::endl;
318 return 0;
319 }
320 std::cout << "hicGetDeviceCount -> " << num_devices << std::endl;
321 int error = 0;
322 for (auto& test : tests) {
323 try {
324 error += test();
325 }
326 catch (std::exception& e) {
327 error += 1;
328 std::cout << e.what() << std::endl;
329 }
330 }
331 return error;
332 }