File: test_sunlinsol_onemkldense.cpp

package info (click to toggle)
sundials 6.4.1%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 79,368 kB
  • sloc: ansic: 218,700; f90: 62,503; cpp: 61,511; fortran: 5,166; python: 4,642; sh: 4,114; makefile: 562; perl: 123
file content (377 lines) | stat: -rw-r--r-- 10,224 bytes parent folder | download
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
/* ---------------------------------------------------------------------------
 * Programmer(s): David J. Gardner @ LLNL
 * ---------------------------------------------------------------------------
 * SUNDIALS Copyright Start
 * Copyright (c) 2002-2022, Lawrence Livermore National Security
 * and Southern Methodist University.
 * All rights reserved.
 *
 * See the top-level LICENSE and NOTICE files for details.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 * SUNDIALS Copyright End
 * ---------------------------------------------------------------------------
 * This is the testing routine to check the SUNLinSol Dense module
 * implementation.
 * ---------------------------------------------------------------------------*/

#include <cstdio>
#include <cstdlib>

#include <sundials/sundials_math.h>
#include <sundials/sundials_types.h>
#include <sunlinsol/sunlinsol_onemkldense.h>
#include <sunmatrix/sunmatrix_onemkldense.h>
#include <sunmemory/sunmemory_sycl.h>
#include <nvector/nvector_sycl.h>
#include "test_sunlinsol.h"

/* ---------------------------------------------------------------------------
 * SUNLinSol_OneMklDense Testing Routine
 * ---------------------------------------------------------------------------*/


int main(int argc, char *argv[])
{
  int          fails = 0; // counter for test failures
  sunindextype i, j, k;
  SUNContext   sunctx;

  if (SUNContext_Create(NULL, &sunctx)) {
    printf("ERROR: SUNContext_Create failed\n");
    return(-1);
  }

  // Check inputs and set matrix dimensions
  if (argc < 4){
    printf("ERROR: THREE (3) Inputs required: matrix cols, number of blocks, print timing \n");
    return -1;
  }

  // Number of matrix columns and rows
  sunindextype cols = (sunindextype) atol(argv[1]);
  if (cols <= 0) {
    printf("ERROR: number of matrix columns must be a positive integer \n");
    return -1;
  }
  sunindextype rows = cols;

  // Number of matrix blocks
  sunindextype nblocks = (sunindextype) atol(argv[2]);
  if (nblocks <= 0) {
    printf("ERROR: number of blocks must be a positive integer \n");
    return -1;
  }

  // Timing flag
  int print_timing = atoi(argv[3]);
  SetTiming(print_timing);

  printf("\noneMKL dense linear solver test: size %ld, blocks %ld\n\n",
         (long int) cols, (long int) nblocks);

  // Create an in-order GPU queue
  sycl::gpu_selector selector;
  sycl::queue myQueue(selector,
                      sycl::property_list{sycl::property::queue::in_order{}});

  sycl::device dev = myQueue.get_device();
  std::cout << "Running on "
            << (dev.get_info<sycl::info::device::name>())
            << std::endl;
  std::cout << " is host? "
            << (dev.is_host() ? "Yes" : "No")
            << std::endl;
  std::cout << " is cpu? "
            << (dev.is_cpu() ? "Yes" : "No")
            << std::endl;
  std::cout << " is gpu? "
            << (dev.is_gpu() ? "Yes" : "No")
            << std::endl;
  std::cout << " is accelerator? "
            << (dev.is_accelerator() ? "Yes" : "No")
            << std::endl;
  std::cout << " is the queue in order? "
            << (myQueue.is_in_order() ? "Yes" : "No")
            << std::endl;
  std::cout << " supports usm host allocations? "
            << (dev.get_info<sycl::info::device::usm_host_allocations>() ?
                "Yes" : "No")
            << std::endl;
  std::cout << " supports usm device allocations? "
            << (dev.get_info<sycl::info::device::usm_device_allocations>() ?
                "Yes" : "No")
            << std::endl;
  std::cout << " suports usm shared allocations? "
            << (dev.get_info<sycl::info::device::usm_shared_allocations>() ?
                "Yes" : "No")
            << std::endl;
  std::cout << " max work group size: "
            << dev.get_info<sycl::info::device::max_work_group_size>()
            << std::endl;
  std::cout << " max global memory size (bytes): "
            << dev.get_info<sycl::info::device::global_mem_size>()
            << std::endl;
  std::cout << " max local memory size (bytes): "
            << dev.get_info<sycl::info::device::local_mem_size>()
            << std::endl;
  std::cout << std::endl;

  // Create Sycl memory helper
  SUNMemoryHelper memhelper = SUNMemoryHelper_Sycl(sunctx);
  if (!memhelper)
  {
    printf("Memory helper creation failed\n");
    return 1;
  }

  // Create vectors and matrices
  N_Vector x = N_VNew_Sycl(cols * nblocks, &myQueue, sunctx);
  if (!x)
  {
    printf("Vector creation failed\n");
    return 1;
  }

  N_Vector y = N_VClone(x);
  if (!y)
  {
    printf("Vector creation failed\n");
    N_VDestroy(x);
    return 1;
  }

  N_Vector b = N_VClone(x);
  if (!b)
  {
    printf("Vector creation failed\n");
    N_VDestroy(x);
    N_VDestroy(y);
    return 1;
  }

  SUNMatrix A;
  if (nblocks > 1)
  {
    A = SUNMatrix_OneMklDenseBlock(nblocks, rows, cols, SUNMEMTYPE_DEVICE,
                                   memhelper, &myQueue, sunctx);
  }
  else
  {
    A = SUNMatrix_OneMklDense(rows, cols, SUNMEMTYPE_DEVICE, memhelper,
                              &myQueue, sunctx);
  }

  if (!A)
  {
    printf("Matrix creation failed\n");
    N_VDestroy(x);
    N_VDestroy(y);
    N_VDestroy(b);
  }

  SUNMatrix B = SUNMatClone(A);
  if (!B)
  {
    printf("Matrix creation failed\n");
    N_VDestroy(x);
    N_VDestroy(y);
    N_VDestroy(b);
    SUNMatDestroy(A);
  }

  SUNMatrix I = SUNMatClone(A);
  if (!I)
  {
    printf("Matrix creation failed\n");
    N_VDestroy(x);
    N_VDestroy(y);
    N_VDestroy(b);
    SUNMatDestroy(A);
    SUNMatDestroy(B);
  }

  // Allocate host data
  realtype* Adata = (realtype*) malloc(sizeof(realtype) *
                                       SUNMatrix_OneMklDense_LData(A));
  if (!Adata)
  {
    printf("Data allocation failed\n");
    N_VDestroy(x);
    N_VDestroy(y);
    N_VDestroy(b);
    SUNMatDestroy(A);
    SUNMatDestroy(B);
    SUNMatDestroy(I);
  }

  realtype* Idata = (realtype*) malloc(sizeof(realtype) *
                                       SUNMatrix_OneMklDense_LData(I));
  if (!Idata)
  {
    printf("Data allocation failed\n");
    N_VDestroy(x);
    N_VDestroy(y);
    N_VDestroy(b);
    SUNMatDestroy(A);
    SUNMatDestroy(B);
    SUNMatDestroy(I);
    free(Adata);
  }

  // Fill A matrix with uniform random data in [0,1/cols]
  for (k = 0; k < nblocks; k++)
    for (j = 0; j < cols; j++)
      for (i = 0; i < rows; i++)
        Adata[k * cols * rows + j * rows + i] =
          (realtype) rand() / (realtype) RAND_MAX / cols;

  // Create anti-identity matrix
  for (k = 0; k < nblocks; k++)
    for(j = 0; j < cols; j++)
      for (i = 0; i < rows; i++)
        Idata[k * cols * rows + j * rows + i] =
          ((rows-1-i) == j) ? RCONST(1.0) : RCONST(0.0);

  // Add anti-identity to ensure the solver needs to do row-swapping
  for (k = 0; k < nblocks; k++)
    for (i = 0; i < rows; i++)
      for(j = 0; j < cols; j++)
        Adata[k * cols * rows + j * rows + i] +=
          Idata[k * cols * rows + j * rows + i];

  SUNMatrix_OneMklDense_CopyToDevice(A, Adata);
  SUNMatrix_OneMklDense_CopyToDevice(I, Idata);

  // Fill x vector with uniform random data in [0,1]
  realtype* xdata = N_VGetArrayPointer(x);
  for (j = 0; j < cols * nblocks; j++)
    xdata[j] = (realtype) rand() / (realtype) RAND_MAX;

  N_VCopyToDevice_Sycl(x);

  // copy A and x into B and y to print in case of solver failure
  SUNMatCopy(A, B);
  N_VScale(ONE, x, y);

  // create right-hand side vector for linear solve
  fails += SUNMatMatvecSetup(A);
  fails += SUNMatMatvec(A, x, b);
  if (fails)
  {
    printf("FAIL: SUNLinSol SUNMatMatvec failure\n");

    // Free matrices and vectors
    N_VDestroy(x);
    N_VDestroy(y);
    N_VDestroy(b);
    SUNMatDestroy(A);
    SUNMatDestroy(B);
    SUNMatDestroy(I);
    free(Adata);
    free(Idata);
    return 1;
  }

  // Create dense linear solver
  SUNLinearSolver LS = SUNLinSol_OneMklDense(x, A, sunctx);
  if (!LS)
  {
    printf("FAIL: SUNLinSol_OneMklDense failure\n");

    // Free matrices and vectors
    N_VDestroy(x);
    N_VDestroy(y);
    N_VDestroy(b);
    SUNMatDestroy(A);
    SUNMatDestroy(B);
    SUNMatDestroy(I);
    free(Adata);
    free(Idata);

    return 1;
  }

  // Run Tests
  fails += Test_SUNLinSolInitialize(LS, 0);
  fails += Test_SUNLinSolSetup(LS, A, 0);
  fails += Test_SUNLinSolSolve(LS, A, x, b, RCONST(1e-10), SUNTRUE, 0);
  fails += Test_SUNLinSolGetType(LS, SUNLINEARSOLVER_DIRECT, 0);
  fails += Test_SUNLinSolGetID(LS, SUNLINEARSOLVER_ONEMKLDENSE, 0);
  fails += Test_SUNLinSolLastFlag(LS, 0);
  fails += Test_SUNLinSolSpace(LS, 0);

  // Print result
  if (fails)
  {
    printf("FAIL: SUNLinSol module failed %i tests \n \n", fails);
    printf("\nx (original) =\n");
    N_VCopyFromDevice_Sycl(y);
    N_VPrint(y);
    printf("\nx (computed) =\n");
    N_VCopyFromDevice_Sycl(x);
    N_VPrint(x);
    printf("\nb =\n");
    N_VCopyFromDevice_Sycl(b);
    N_VPrint(b);
  }
  else
  {
    printf("SUCCESS: SUNLinSol module passed all tests \n \n");
  }

  // Free solver, matrix and vectors
  SUNLinSolFree(LS);
  SUNMatDestroy(A);
  SUNMatDestroy(B);
  SUNMatDestroy(I);
  N_VDestroy(x);
  N_VDestroy(y);
  N_VDestroy(b);
  free(Adata);
  free(Idata);
  SUNMemoryHelper_Destroy(memhelper);
  SUNContext_Free(&sunctx);

  return fails;
}


/* ---------------------------------------------------------------------------
 * Implementation-specific 'check' routines
 * ---------------------------------------------------------------------------*/


int check_vector(N_Vector X, N_Vector Y, realtype tol)
{
  int failure = 0;
  sunindextype i = 0;
  sunindextype local_length = N_VGetLength(X);
  realtype* Xdata = N_VGetArrayPointer(X);
  realtype* Ydata = N_VGetArrayPointer(Y);

  // Copy data to host
  N_VCopyFromDevice_Sycl(X);
  N_VCopyFromDevice_Sycl(Y);

  // Check vector data
  for (i = 0; i < local_length; i++)
    failure += SUNRCompareTol(Xdata[i], Ydata[i], tol);

  if (failure > ZERO)
  {
    realtype maxerr = ZERO;
    for(i = 0; i < local_length; i++)
      maxerr = SUNMAX(SUNRabs(Xdata[i] - Ydata[i]), maxerr);
    printf("check err failure: maxerr = %g (tol = %g)\n", maxerr, tol);
    return 1;
  }
  else
  {
    return 0;
  }
}

void sync_device()
{
}