-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sunlinsol.c
More file actions
555 lines (481 loc) · 15.6 KB
/
test_sunlinsol.c
File metadata and controls
555 lines (481 loc) · 15.6 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
* -----------------------------------------------------------------
* Programmer(s): Daniel R. Reynolds @ SMU
* David J. Gardner @ LLNL
* -----------------------------------------------------------------
* SUNDIALS Copyright Start
* Copyright (c) 2002-2025, 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
* -----------------------------------------------------------------
* These test functions are designed to check a SUNLinSol module
* implementation.
* -----------------------------------------------------------------
*/
#include "test_sunlinsol.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sundials/sundials_linearsolver.h>
#include <sundials/sundials_math.h>
#include <sundials/sundials_types.h>
#if defined(SUNDIALS_HAVE_POSIX_TIMERS)
#include <time.h>
#include <unistd.h>
#endif
/* private functions */
static double get_time(void);
int print_time = 0;
#define PRINT_TIME(format, time) \
if (print_time) printf(format, time)
/* ----------------------------------------------------------------------
* SUNLinSolGetType Test
* --------------------------------------------------------------------*/
int Test_SUNLinSolGetType(SUNLinearSolver S, SUNLinearSolver_Type suntype,
int myid)
{
double start_time, stop_time;
SUNLinearSolver_Type mysuntype;
start_time = get_time();
mysuntype = SUNLinSolGetType(S);
sync_device();
stop_time = get_time();
if (suntype != mysuntype)
{
printf(">>> FAILED test -- SUNLinSolGetType, Proc %d \n", myid);
PRINT_TIME(" SUNLinSolGetType Time: %22.15e \n \n",
stop_time - start_time);
return (1);
}
else if (myid == 0)
{
printf(" PASSED test -- SUNLinSolGetType \n");
PRINT_TIME(" SUNLinSolGetType Time: %22.15e \n \n",
stop_time - start_time);
}
return (0);
}
/* ----------------------------------------------------------------------
* SUNLinSolGetID Test
* --------------------------------------------------------------------*/
int Test_SUNLinSolGetID(SUNLinearSolver S, SUNLinearSolver_ID sunid, int myid)
{
double start_time, stop_time;
SUNLinearSolver_ID mysunid;
start_time = get_time();
mysunid = SUNLinSolGetID(S);
sync_device();
stop_time = get_time();
if (sunid != mysunid)
{
printf(">>> FAILED test -- SUNLinSolGetID, Proc %d \n", myid);
PRINT_TIME(" SUNLinSolGetID Time: %22.15e \n \n", stop_time - start_time);
return (1);
}
else if (myid == 0)
{
printf(" PASSED test -- SUNLinSolGetID \n");
PRINT_TIME(" SUNLinSolGetID Time: %22.15e \n \n", stop_time - start_time);
}
return (0);
}
/* ----------------------------------------------------------------------
* Test_SUNLinSolLastFlag Test
* --------------------------------------------------------------------*/
int Test_SUNLinSolLastFlag(SUNLinearSolver S, int myid)
{
double start_time, stop_time;
sunindextype lastflag;
/* the only way for this test to fail is if S is NULL */
if (S == NULL)
{
printf(">>> FAILED test -- SUNLinSolLastFlag, Proc %d \n", myid);
return (1);
}
start_time = get_time();
lastflag = SUNLinSolLastFlag(S);
sync_device();
stop_time = get_time();
if (myid == 0)
{
printf(" PASSED test -- SUNLinSolLastFlag (%ld) \n", (long int)lastflag);
PRINT_TIME(" SUNLinSolLastFlag Time: %22.15e \n \n",
stop_time - start_time);
}
return (0);
}
/* ----------------------------------------------------------------------
* Test_SUNLinSolSpace Test
* --------------------------------------------------------------------*/
int Test_SUNLinSolSpace(SUNLinearSolver S, int myid)
{
int failure;
double start_time, stop_time;
long int lenrw, leniw;
/* call SUNLinSolSpace (failure based on output flag) */
start_time = get_time();
failure = SUNLinSolSpace(S, &lenrw, &leniw);
sync_device();
stop_time = get_time();
if (failure)
{
printf(">>> FAILED test -- SUNLinSolSpace, Proc %d \n", myid);
PRINT_TIME(" SUNLinSolSpace Time: %22.15e \n \n", stop_time - start_time);
return (1);
}
else if (myid == 0)
{
printf(" PASSED test -- SUNLinSolSpace, lenrw = %li, leniw = %li\n",
lenrw, leniw);
PRINT_TIME(" SUNLinSolSpace Time: %22.15e \n \n", stop_time - start_time);
}
return (0);
}
/* ----------------------------------------------------------------------
* SUNLinSolNumIters Test
* --------------------------------------------------------------------*/
int Test_SUNLinSolNumIters(SUNLinearSolver S, int myid)
{
int numiters;
double start_time, stop_time;
/* the only way to fail this test is if the function is NULL,
which will cause a seg-fault */
start_time = get_time();
numiters = SUNLinSolNumIters(S);
sync_device();
stop_time = get_time();
if (myid == 0)
{
printf(" PASSED test -- SUNLinSolNumIters (%d) \n", numiters);
PRINT_TIME(" SUNLinSolNumIters Time: %22.15e \n \n",
stop_time - start_time);
}
return (0);
}
/* ----------------------------------------------------------------------
* SUNLinSolResNorm Test
* --------------------------------------------------------------------*/
int Test_SUNLinSolResNorm(SUNLinearSolver S, int myid)
{
double start_time, stop_time, resnorm;
/* this test can fail if the function is NULL, which will cause a seg-fault */
start_time = get_time();
resnorm = (double)SUNLinSolResNorm(S);
sync_device();
stop_time = get_time();
/* this test can also fail if the return value is negative */
if (resnorm < ZERO)
{
printf(">>> FAILED test -- SUNLinSolResNorm returned %g on Proc %d \n",
resnorm, myid);
return (1);
}
else if (myid == 0)
{
printf(" PASSED test -- SUNLinSolResNorm\n");
PRINT_TIME(" SUNLinSolResNorm Time: %22.15e \n \n",
stop_time - start_time);
}
return (0);
}
/* ----------------------------------------------------------------------
* SUNLinSolResid Test
* --------------------------------------------------------------------*/
int Test_SUNLinSolResid(SUNLinearSolver S, int myid)
{
double start_time, stop_time;
N_Vector resid;
/* this test can fail if the function returns NULL */
start_time = get_time();
resid = SUNLinSolResid(S);
sync_device();
stop_time = get_time();
/* this test can also fail if the return value is NULL */
if (resid == NULL)
{
printf(">>> FAILED test -- SUNLinSolResid returned NULL N_Vector on Proc "
"%d \n",
myid);
return (1);
}
else if (myid == 0)
{
printf(" PASSED test -- SUNLinSolResid\n");
PRINT_TIME(" SUNLinSolResid Time: %22.15e \n \n", stop_time - start_time);
}
return (0);
}
/* ----------------------------------------------------------------------
* SUNLinSolSetATimes Test
* --------------------------------------------------------------------*/
int Test_SUNLinSolSetATimes(SUNLinearSolver S, void* ATdata, SUNATimesFn ATimes,
int myid)
{
int failure;
double start_time, stop_time;
/* try calling SetATimes routine: should pass/fail based on expected input */
start_time = get_time();
failure = SUNLinSolSetATimes(S, ATdata, ATimes);
sync_device();
stop_time = get_time();
if (failure)
{
printf(">>> FAILED test -- SUNLinSolSetATimes returned %d on Proc %d \n",
failure, myid);
return (1);
}
else if (myid == 0)
{
printf(" PASSED test -- SUNLinSolSetATimes \n");
PRINT_TIME(" SUNLinSolSetATimes Time: %22.15e \n \n",
stop_time - start_time);
}
return (0);
}
/* ----------------------------------------------------------------------
* SUNLinSolSetPreconditioner
* --------------------------------------------------------------------*/
int Test_SUNLinSolSetPreconditioner(SUNLinearSolver S, void* Pdata,
SUNPSetupFn PSetup, SUNPSolveFn PSolve,
int myid)
{
int failure;
double start_time, stop_time;
/* try calling SetPreconditioner routine: should pass/fail based on expected input */
start_time = get_time();
failure = SUNLinSolSetPreconditioner(S, Pdata, PSetup, PSolve);
sync_device();
stop_time = get_time();
if (failure)
{
printf(">>> FAILED test -- SUNLinSolSetPreconditioner returned %d on Proc "
"%d \n",
failure, myid);
return (1);
}
else if (myid == 0)
{
printf(" PASSED test -- SUNLinSolSetPreconditioner \n");
PRINT_TIME(" SUNLinSolSetPreconditioner Time: %22.15e \n \n",
stop_time - start_time);
}
return (0);
}
/* ----------------------------------------------------------------------
* SUNLinSolSetScalingVectors
* --------------------------------------------------------------------*/
int Test_SUNLinSolSetScalingVectors(SUNLinearSolver S, N_Vector s1, N_Vector s2,
int myid)
{
int failure;
double start_time, stop_time;
/* try calling SetScalingVectors routine: should pass/fail based on expected input */
start_time = get_time();
failure = SUNLinSolSetScalingVectors(S, s1, s2);
sync_device();
stop_time = get_time();
if (failure)
{
printf(">>> FAILED test -- SUNLinSolSetScalingVectors returned %d on Proc "
"%d \n",
failure, myid);
return (1);
}
else if (myid == 0)
{
printf(" PASSED test -- SUNLinSolSetScalingVectors \n");
PRINT_TIME(" SUNLinSolSetScalingVectors Time: %22.15e \n \n",
stop_time - start_time);
}
return (0);
}
/* ----------------------------------------------------------------------
* SUNLinSolSetZeroGuess
* --------------------------------------------------------------------*/
int Test_SUNLinSolSetZeroGuess(SUNLinearSolver S, int myid)
{
int failure;
double start_time, stop_time;
/* try calling SetZeroGuess routine: should pass/fail based on expected input */
start_time = get_time();
failure = SUNLinSolSetZeroGuess(S, SUNTRUE);
stop_time = get_time();
if (failure)
{
printf(">>> FAILED test -- SUNLinSolSetZeroGuess returned %d on Proc %d \n",
failure, myid);
return (1);
}
else if (myid == 0)
{
printf(" PASSED test -- SUNLinSolSetZeroGuess \n");
PRINT_TIME(" SUNLinSolSetZeroGuess Time: %22.15e \n \n",
stop_time - start_time);
}
/* try calling SetZeroGuess routine: should pass/fail based on expected input */
start_time = get_time();
failure = SUNLinSolSetZeroGuess(S, SUNFALSE);
stop_time = get_time();
if (failure)
{
printf(">>> FAILED test -- SUNLinSolSetZeroGuess returned %d on Proc %d \n",
failure, myid);
return (1);
}
else if (myid == 0)
{
printf(" PASSED test -- SUNLinSolSetZeroGuess \n");
PRINT_TIME(" SUNLinSolSetZeroGuess Time: %22.15e \n \n",
stop_time - start_time);
}
return (0);
}
/* ----------------------------------------------------------------------
* SUNLinSolInitialize Test
* --------------------------------------------------------------------*/
int Test_SUNLinSolInitialize(SUNLinearSolver S, int myid)
{
int failure;
double start_time, stop_time;
start_time = get_time();
failure = SUNLinSolInitialize(S);
sync_device();
stop_time = get_time();
if (failure)
{
printf(">>> FAILED test -- SUNLinSolInitialize check, Proc %d \n", myid);
PRINT_TIME(" SUNLinSolInitialize Time: %22.15e \n \n",
stop_time - start_time);
return (1);
}
else if (myid == 0)
{
printf(" PASSED test -- SUNLinSolInitialize \n");
PRINT_TIME(" SUNLinSolInitialize Time: %22.15e \n \n",
stop_time - start_time);
}
return (0);
}
/* ----------------------------------------------------------------------
* SUNLinSolSetup Test
*
* This test must follow Test_SUNLinSolInitialize
* --------------------------------------------------------------------*/
int Test_SUNLinSolSetup(SUNLinearSolver S, SUNMatrix A, int myid)
{
int failure;
double start_time, stop_time;
start_time = get_time();
failure = SUNLinSolSetup(S, A);
sync_device();
stop_time = get_time();
if (failure)
{
printf(">>> FAILED test -- SUNLinSolSetup check, Proc %d \n", myid);
PRINT_TIME(" SUNLinSolSetup Time: %22.15e \n \n", stop_time - start_time);
return (1);
}
else if (myid == 0)
{
printf(" PASSED test -- SUNLinSolSetup \n");
PRINT_TIME(" SUNLinSolSetup Time: %22.15e \n \n", stop_time - start_time);
}
return (0);
}
/* ----------------------------------------------------------------------
* SUNLinSolSolve Test
*
* This test must follow Test_SUNLinSolSetup. Also, x must be the
* solution to the linear system A*x = b (for the original A matrix);
* while the 'A' that is supplied to this function should have been
* 'setup' by the Test_SUNLinSolSetup() function prior to this call.
* --------------------------------------------------------------------*/
int Test_SUNLinSolSolve(SUNLinearSolver S, SUNMatrix A, N_Vector x, N_Vector b,
sunrealtype tol, sunbooleantype zeroguess, int myid)
{
int failure;
double start_time, stop_time;
N_Vector y;
/* clone to create solution vector */
y = N_VClone(x);
/* set initial guess for the linear system */
if (zeroguess) { N_VConst(ZERO, y); }
else { N_VAddConst(x, SUNRsqrt(SUN_UNIT_ROUNDOFF), y); }
sync_device();
/* signal if the initial guess is zero or non-zero */
failure = SUNLinSolSetZeroGuess(S, zeroguess);
if (failure)
{
printf(">>> FAILED test -- SUNLinSolSetZeroGuess returned %d on Proc %d \n",
failure, myid);
N_VDestroy(y);
return (1);
}
/* perform solve */
start_time = get_time();
failure = SUNLinSolSolve(S, A, y, b, tol);
sync_device();
stop_time = get_time();
if (failure)
{
printf(">>> FAILED test -- SUNLinSolSolve returned %d on Proc %d \n",
failure, myid);
N_VDestroy(y);
return (1);
}
/* Check solution */
failure = check_vector(x, y, 10.0 * tol);
if (failure)
{
printf(">>> FAILED test -- SUNLinSolSolve check, Proc %d \n", myid);
PRINT_TIME(" SUNLinSolSolve Time: %22.15e \n \n", stop_time - start_time);
N_VDestroy(y);
return (1);
}
else if (myid == 0)
{
printf(" PASSED test -- SUNLinSolSolve \n");
PRINT_TIME(" SUNLinSolSolve Time: %22.15e \n \n", stop_time - start_time);
}
N_VDestroy(y);
return (0);
}
/* ======================================================================
* Private functions
* ====================================================================*/
#if defined(SUNDIALS_HAVE_POSIX_TIMERS)
time_t base_time_tv_sec = 0; /* Base time; makes time values returned
by get_time easier to read when
printed since they will be zero
based.
*/
#endif
void SetTiming(int onoff)
{
print_time = onoff;
#if defined(SUNDIALS_HAVE_POSIX_TIMERS)
struct timespec spec;
clock_gettime(CLOCK_MONOTONIC, &spec);
base_time_tv_sec = spec.tv_sec;
#endif
}
/* ----------------------------------------------------------------------
* Timer
* --------------------------------------------------------------------*/
static double get_time(void)
{
#if defined(SUNDIALS_HAVE_POSIX_TIMERS)
struct timespec spec;
clock_gettime(CLOCK_MONOTONIC, &spec);
double time = (double)(spec.tv_sec - base_time_tv_sec) +
((double)(spec.tv_nsec) / 1E9);
#else
double time = 0;
#endif
return time;
}