forked from LLNL/AMG2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamg.c
3052 lines (2829 loc) · 109 KB
/
amg.c
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* Copyright (c) 1998 Lawrence Livermore National Security, LLC and other
* HYPRE Project Developers. See the top-level COPYRIGHT file for details.
*
* SPDX-License-Identifier: (Apache-2.0 OR MIT)
******************************************************************************/
/******************************************************************************
* This software is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTIBILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the terms and conditions of the
* GNU General Public License for more details.
*
******************************************************************************/
/*--------------------------------------------------------------------------
Driver for AMG benchmark:
Interface: Linear-Algebraic (IJ)
Compile with: make
Description: This driver solves a 3-D diffusion problem with zero boundary
conditions on a cuboid.
Solvers are PCG with AMG or GMRES with AMG
preconditioners.
*--------------------------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "HYPRE_config.h"
#include "_hypre_utilities.h"
#include "seq_mv.h"
#include "HYPRE.h"
#include "HYPRE_parcsr_mv.h"
#include "HYPRE_IJ_mv.h"
#include "HYPRE_parcsr_ls.h"
#include "HYPRE_krylov.h"
#include <time.h>
#ifdef AMG_CMAKE_BUILD
#include "amg-config.h"
#endif
#ifdef USE_CALIPER
#include <caliper/cali.h>
#include <adiak.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
HYPRE_Int BuildIJLaplacian7pt (HYPRE_Int argc, char *argv [], HYPRE_BigInt *size,
HYPRE_IJMatrix *A_ptr, HYPRE_MemoryLocation memory_location );
HYPRE_Int BuildIJLaplacian27pt (HYPRE_Int argc, char *argv [], HYPRE_BigInt *size,
HYPRE_IJMatrix *A_ptr, HYPRE_MemoryLocation memory_location );
HYPRE_BigInt hypre_map27( HYPRE_BigInt ix, HYPRE_BigInt iy, HYPRE_BigInt iz, HYPRE_Int px,
HYPRE_Int py, HYPRE_Int pz, HYPRE_BigInt Cx, HYPRE_BigInt Cy, HYPRE_BigInt Cz, HYPRE_Int nx,
HYPRE_Int nxy);
#ifdef __cplusplus
}
#endif
#define SECOND_TIME 0
hypre_int
main( hypre_int argc,
char *argv[] )
{
HYPRE_Int arg_index;
HYPRE_Int print_usage;
HYPRE_Int problem_id;
HYPRE_Int ioutdat;
HYPRE_Int poutdat;
HYPRE_Int debug_flag;
HYPRE_Int num_iterations;
HYPRE_Int max_iter = 1000;
HYPRE_Int mg_max_iter = 100;
HYPRE_Real final_res_norm;
HYPRE_Real max_row_sum = 1.0;
void *object;
HYPRE_IJMatrix ij_A;
HYPRE_IJVector ij_b;
HYPRE_IJVector ij_x;
HYPRE_ParCSRMatrix parcsr_A;
HYPRE_ParVector b;
HYPRE_ParVector x;
HYPRE_Solver pcg_solver;
HYPRE_Solver pcg_precond = NULL, pcg_precond_gotten;
HYPRE_Int myid = 0;
HYPRE_Int num_procs = 1;
HYPRE_Int agg_num_levels = 1;
HYPRE_Int agg_interp_type = 4;
HYPRE_Int time_index;
MPI_Comm comm = hypre_MPI_COMM_WORLD;
HYPRE_BigInt first_local_row, last_local_row;
HYPRE_BigInt first_local_col, last_local_col;
HYPRE_Int local_num_rows;
/* parameters for BoomerAMG */
HYPRE_Int P_max_elmts = 4;
HYPRE_Int coarsen_type = 8;
HYPRE_Int num_sweeps = 1;
HYPRE_Int relax_type = -1;
HYPRE_Int rap2 = 0;
//HYPRE_Int mod_rap2=0;
HYPRE_Int keepTranspose = 1;
HYPRE_Real tol = 1.e-12, pc_tol = 0.;
HYPRE_Real atol = 0.0;
HYPRE_Real wall_time;
HYPRE_BigInt system_size;
HYPRE_Real cum_nnz_AP = 1;
HYPRE_Real FOM1 = 0, FOM2 = 0;
HYPRE_Real total_time = 0;
/* parameters for GMRES */
HYPRE_Int k_dim = 100;
/* interpolation */
HYPRE_Int interp_type = 17; /* default value */
HYPRE_Int print_system = 0;
HYPRE_Int print_stats = 0;
HYPRE_Int rel_change = 0;
hypre_Vector *values;
#if defined(HYPRE_USING_GPU)
HYPRE_Int spmv_use_vendor = 1;
#if defined(HYPRE_USING_CUDA)
HYPRE_Int spgemm_use_vendor = 0;
#else
HYPRE_Int spgemm_use_vendor = 1;
#endif
#endif
HYPRE_Int dev_pool_size = 4, um_pool_size = 4;
/* default execution policy and memory space */
HYPRE_MemoryLocation memory_location = HYPRE_MEMORY_DEVICE;
HYPRE_ExecutionPolicy default_exec_policy = HYPRE_EXEC_DEVICE;
for (arg_index = 1; arg_index < argc; arg_index ++)
{
if ( strcmp(argv[arg_index], "-memory_host") == 0 )
{
memory_location = HYPRE_MEMORY_HOST;
}
else if ( strcmp(argv[arg_index], "-memory_device") == 0 )
{
memory_location = HYPRE_MEMORY_DEVICE;
}
else if ( strcmp(argv[arg_index], "-exec_host") == 0 )
{
default_exec_policy = HYPRE_EXEC_HOST;
}
else if ( strcmp(argv[arg_index], "-exec_device") == 0 )
{
default_exec_policy = HYPRE_EXEC_DEVICE;
}
}
/*-----------------------------------------------------------
* Initialize MPI
*-----------------------------------------------------------*/
hypre_MPI_Init(&argc, &argv);
hypre_MPI_Comm_size(comm, &num_procs );
hypre_MPI_Comm_rank(comm, &myid );
#ifdef USE_CALIPER
/*-----------------------------------------------------------
* Set Caliper and Adiak metadata
*----------------------------------------------------------*/
adiak_init(&comm);
adiak_collect_all();
adiak_namevalue("compiler", adiak_general, NULL, "%s", AMG2023_COMPILER_ID);
adiak_namevalue("compiler version", adiak_general, NULL, "%s", AMG2023_COMPILER_VERSION);
CALI_MARK_BEGIN("main");
#endif
/*-----------------------------------------------------------
* Set defaults
*-----------------------------------------------------------*/
debug_flag = 0;
problem_id = 1;
ioutdat = 0;
poutdat = 0;
/*-----------------------------------------------------------
* Parse command line
*-----------------------------------------------------------*/
print_usage = 0;
arg_index = 1;
while ( (arg_index < argc) && (!print_usage) )
{
if ( strcmp(argv[arg_index], "-help") == 0 )
{
print_usage = 1;
}
else
{
arg_index++;
}
}
/* defaults for GMRES */
k_dim = 100;
arg_index = 0;
while (arg_index < argc)
{
if ( strcmp(argv[arg_index], "-problem") == 0 )
{
arg_index++;
problem_id = atoi(argv[arg_index++]);
}
else if ( strcmp(argv[arg_index], "-printstats") == 0 )
{
arg_index++;
ioutdat = 1;
poutdat = 1;
print_stats = 1;
}
else if ( strcmp(argv[arg_index], "-printallstats") == 0 )
{
arg_index++;
ioutdat = 3;
poutdat = 1;
print_stats = 1;
}
else if ( strcmp(argv[arg_index], "-print") == 0 )
{
arg_index++;
print_system = 1;
}
else if ( strcmp(argv[arg_index], "-keepT") == 0 )
{
arg_index++;
keepTranspose = 1;
rap2 = 0;
}
else if ( strcmp(argv[arg_index], "-interptype") == 0 )
{
arg_index++;
interp_type = atoi(argv[arg_index++]);
}
else if ( strcmp(argv[arg_index], "-k") == 0 )
{
arg_index++;
k_dim = atoi(argv[arg_index++]);
}
else if ( strcmp(argv[arg_index], "-mxrs") == 0 )
{
arg_index++;
max_row_sum = atof(argv[arg_index++]);
}
else if ( strcmp(argv[arg_index], "-Pmx") == 0 )
{
arg_index++;
P_max_elmts = atoi(argv[arg_index++]);
}
else if ( strcmp(argv[arg_index], "-agg_nl") == 0 )
{
arg_index++;
agg_num_levels = atoi(argv[arg_index++]);
}
else if ( strcmp(argv[arg_index], "-rlx") == 0 )
{
arg_index++;
relax_type = atoi(argv[arg_index++]);
}
else if ( strcmp(argv[arg_index], "-dev_pool_size") == 0 )
{
arg_index++;
dev_pool_size = atoi(argv[arg_index++]);
}
else if ( strcmp(argv[arg_index], "-um_pool_size") == 0 )
{
arg_index++;
um_pool_size = atoi(argv[arg_index++]);
}
else
{
arg_index++;
}
}
/*-----------------------------------------------------------
* Print usage info
*-----------------------------------------------------------*/
if ( (print_usage) && (myid == 0) )
{
hypre_printf("\n");
hypre_printf("Usage: %s [<options>]\n", argv[0]);
hypre_printf("\n");
hypre_printf(" -problem <ID>: problem ID\n");
hypre_printf(" 1 = solves 1 problem with AMG-PCG (default) \n");
hypre_printf(" 2 = solves 1 problem AMG-GMRES(100)\n");
hypre_printf("\n");
hypre_printf(" -n <nx> <ny> <nz>: problem size per MPI process (default: nx=ny=nz=10)\n");
hypre_printf("\n");
hypre_printf(" -P <px> <py> <pz>: processor topology (default: px=py=pz=1)\n");
hypre_printf("\n");
hypre_printf(" -print : prints the system\n");
hypre_printf(" -printstats : prints preconditioning and convergence stats\n");
hypre_printf(" -printallstats : prints preconditioning and convergence stats\n");
hypre_printf(" including residual norms for each iteration\n");
hypre_printf(" -dev_pool_size : size of GPU device memory pool (in GB)\n");
hypre_printf(" -um_pool_size : size of GPU UVM memory pool (in GB)\n");
hypre_printf("\n");
exit(1);
}
/*-----------------------------------------------------------
* Print driver parameters
*-----------------------------------------------------------*/
if (myid == 0)
{
hypre_printf("Running with these driver parameters:\n");
hypre_printf(" Problem ID = %d\n\n", problem_id);
}
/*-----------------------------------------------------------------
* GPU Device binding
* Must be done before HYPRE_Init() and should not be changed after
*-----------------------------------------------------------------*/
hypre_bind_device(myid, num_procs, comm);
#ifdef USE_CALIPER
CALI_MARK_BEGIN("Hypre init");
#endif
time_index = hypre_InitializeTiming("Hypre init");
hypre_BeginTiming(time_index);
/*-----------------------------------------------------------
* Initialize : must be the first HYPRE function to call
*-----------------------------------------------------------*/
HYPRE_Init();
hypre_EndTiming(time_index);
hypre_GetTiming("Hypre init times", &wall_time, comm);
hypre_FinalizeTiming(time_index);
hypre_ClearTiming();
#ifdef USE_CALIPER
CALI_MARK_END("Hypre init");
#endif
/*-----------------------------------------------------------------
* UMPIRE Pools
*-----------------------------------------------------------------*/
#if defined(HYPRE_USING_UMPIRE)
char device_pool_name[] = "AMG_DEVICE_POOL";
char um_pool_name[] = "AMG_UM_POOL";
size_t umpire_dev_pool_size = ((size_t) dev_pool_size) * 1024 * 1024 * 1024;
size_t umpire_um_pool_size = ((size_t) um_pool_size) * 1024 * 1024 * 1024;
size_t umpire_dev_block_size = 512;
size_t umpire_um_block_size = 512;
umpire_resourcemanager umpire_rm;
umpire_resourcemanager_get_instance(&umpire_rm);
umpire_allocator um_allocator, dev_allocator;
/* create device pool */
{
hypre_int device_id;
hypre_GetDevice(&device_id);
char resource_name[16];
hypre_sprintf(resource_name, "%s::%d", "DEVICE", device_id);
umpire_allocator allocator;
umpire_resourcemanager_get_allocator_by_name(&umpire_rm, resource_name, &allocator);
hypre_umpire_resourcemanager_make_allocator_pool(&umpire_rm, device_pool_name, allocator,
umpire_dev_pool_size, umpire_dev_block_size,
&dev_allocator);
}
/* create um pool */
{
char resource_name[] = "UM";
umpire_allocator allocator;
umpire_resourcemanager_get_allocator_by_name(&umpire_rm, resource_name, &allocator);
hypre_umpire_resourcemanager_make_allocator_pool(&umpire_rm, um_pool_name, allocator,
umpire_um_pool_size, umpire_um_block_size,
&um_allocator);
}
/* give hypre the pool names */
HYPRE_SetUmpireDevicePoolName(device_pool_name);
HYPRE_SetUmpireUMPoolName(um_pool_name);
/* allocate and free some memory to make sure pool is allocated */
#if defined(HYPRE_USING_UNIFIED_MEMORY)
char *tmp_ptr = hypre_TAlloc(char, umpire_um_pool_size, memory_location);
#else
char *tmp_ptr = hypre_TAlloc(char, umpire_dev_pool_size, memory_location);
#endif
hypre_TFree(tmp_ptr, memory_location);
/* make sure hypre doesn't own the pools */
assert(hypre_HandleOwnUmpireUMPool(hypre_handle()) == 0);
assert(hypre_HandleOwnUmpireDevicePool(hypre_handle()) == 0);
#endif
/* default memory location */
HYPRE_SetMemoryLocation(memory_location);
/* default execution policy */
HYPRE_SetExecutionPolicy(default_exec_policy);
#if defined(HYPRE_USING_GPU)
HYPRE_SetSpMVUseVendor(spmv_use_vendor);
HYPRE_SetSpGemmUseVendor(spgemm_use_vendor);
#endif
/*-----------------------------------------------------------
* Set up matrix
*-----------------------------------------------------------*/
time_index = hypre_InitializeTiming("Spatial Operator");
hypre_BeginTiming(time_index);
if (problem_id == 1)
{
BuildIJLaplacian27pt(argc, argv, &system_size, &ij_A, memory_location);
}
else
{
BuildIJLaplacian7pt(argc, argv, &system_size, &ij_A, memory_location);
tol = 1.e-8;
}
HYPRE_IJMatrixGetObject(ij_A, &object);
parcsr_A = (HYPRE_ParCSRMatrix) object;
hypre_EndTiming(time_index);
hypre_GetTiming("Generate Matrix", &wall_time, comm);
hypre_FinalizeTiming(time_index);
hypre_ClearTiming();
/*-----------------------------------------------------------
* Set up the RHS and initial guess
*-----------------------------------------------------------*/
time_index = hypre_InitializeTiming("RHS and Initial Guess");
hypre_BeginTiming(time_index);
HYPRE_ParCSRMatrixGetLocalRange( parcsr_A,
&first_local_row, &last_local_row,
&first_local_col, &last_local_col );
local_num_rows = (HYPRE_Int)(last_local_row - first_local_row + 1);
if (myid == 0)
{
hypre_printf(" RHS vector has unit components\n");
hypre_printf(" Initial guess is 0\n");
}
/* RHS */
HYPRE_IJVectorCreate(comm, first_local_row, last_local_row, &ij_b);
HYPRE_IJVectorSetObjectType(ij_b, HYPRE_PARCSR);
HYPRE_IJVectorInitialize(ij_b);
/* Initial guess */
HYPRE_IJVectorCreate(comm, first_local_col, last_local_col, &ij_x);
HYPRE_IJVectorSetObjectType(ij_x, HYPRE_PARCSR);
HYPRE_IJVectorInitialize(ij_x);
values = hypre_SeqVectorCreate(local_num_rows);
hypre_SeqVectorInitialize(values);
hypre_SeqVectorSetConstantValues(values, 0.0);
HYPRE_IJVectorSetValues(ij_x, local_num_rows, NULL, hypre_VectorData(values));
HYPRE_IJVectorAssemble(ij_x);
hypre_SeqVectorSetConstantValues(values, 1.0);
HYPRE_IJVectorSetValues(ij_b, local_num_rows, NULL, hypre_VectorData(values));
HYPRE_IJVectorAssemble(ij_b);
hypre_SeqVectorDestroy(values);
HYPRE_IJVectorGetObject( ij_b, &object );
b = (HYPRE_ParVector) object;
HYPRE_IJVectorGetObject( ij_x, &object );
x = (HYPRE_ParVector) object;
hypre_EndTiming(time_index);
hypre_GetTiming("IJ Vector Setup", &wall_time, comm);
hypre_FinalizeTiming(time_index);
hypre_ClearTiming();
/*-----------------------------------------------------------
* Print out the system and initial guess
*-----------------------------------------------------------*/
if (print_system)
{
HYPRE_IJMatrixPrint(ij_A, "IJ.out.A");
HYPRE_IJVectorPrint(ij_b, "IJ.out.b");
HYPRE_IJVectorPrint(ij_x, "IJ.out.x0");
}
/*-----------------------------------------------------------
* Problem 2: Solve a 7pt 3D Laplace problem with AMG-PCG
*-----------------------------------------------------------*/
if (problem_id == 2 )
{
#ifdef USE_CALIPER
adiak_namevalue("Problem", adiak_general, NULL, "%d", 2);
adiak_namevalue("Solver", adiak_general, NULL, "%s", "PCG");
CALI_MARK_BEGIN("Problem");
#endif
time_index = hypre_InitializeTiming("PCG Setup");
hypre_MPI_Barrier(comm);
#ifdef USE_CALIPER
CALI_MARK_BEGIN("Setup");
#endif
hypre_BeginTiming(time_index);
HYPRE_ParCSRPCGCreate(comm, &pcg_solver);
HYPRE_PCGSetMaxIter(pcg_solver, max_iter);
HYPRE_PCGSetTol(pcg_solver, tol);
HYPRE_PCGSetTwoNorm(pcg_solver, 1);
HYPRE_PCGSetRelChange(pcg_solver, rel_change);
HYPRE_PCGSetPrintLevel(pcg_solver, ioutdat);
HYPRE_PCGSetAbsoluteTol(pcg_solver, atol);
HYPRE_PCGSetRecomputeResidual(pcg_solver, 1);
/* use BoomerAMG as preconditioner */
if (myid == 0 && print_stats) { hypre_printf("Solver: AMG-PCG\n"); }
HYPRE_BoomerAMGCreate(&pcg_precond);
HYPRE_BoomerAMGSetTol(pcg_precond, pc_tol);
/*HYPRE_BoomerAMGSetCoarsenType(pcg_precond, coarsen_type);*/
HYPRE_BoomerAMGSetInterpType(pcg_precond, interp_type);
HYPRE_BoomerAMGSetPMaxElmts(pcg_precond, P_max_elmts);
HYPRE_BoomerAMGSetPrintLevel(pcg_precond, poutdat);
HYPRE_BoomerAMGSetMaxIter(pcg_precond, 1);
HYPRE_BoomerAMGSetNumSweeps(pcg_precond, num_sweeps);
if (relax_type > -1) { HYPRE_BoomerAMGSetRelaxType(pcg_precond, relax_type); }
HYPRE_BoomerAMGSetDebugFlag(pcg_precond, debug_flag);
HYPRE_BoomerAMGSetAggNumLevels(pcg_precond, agg_num_levels);
HYPRE_BoomerAMGSetAggInterpType(pcg_precond, agg_interp_type);
HYPRE_BoomerAMGSetRAP2(pcg_precond, rap2);
HYPRE_BoomerAMGSetKeepTranspose(pcg_precond, keepTranspose);
HYPRE_BoomerAMGSetCumNnzAP(pcg_precond, cum_nnz_AP);
HYPRE_BoomerAMGSetMaxRowSum(pcg_precond, 1.0);
HYPRE_PCGSetMaxIter(pcg_solver, mg_max_iter);
HYPRE_PCGSetPrecond(pcg_solver,
(HYPRE_PtrToSolverFcn) HYPRE_BoomerAMGSolve,
(HYPRE_PtrToSolverFcn) HYPRE_BoomerAMGSetup,
pcg_precond);
HYPRE_PCGGetPrecond(pcg_solver, &pcg_precond_gotten);
if (pcg_precond_gotten != pcg_precond)
{
hypre_printf("HYPRE_ParCSRPCGGetPrecond got bad precond\n");
return (-1);
}
else if (myid == 0 && print_stats)
{
hypre_printf("HYPRE_ParCSRPCGGetPrecond got good precond\n");
}
HYPRE_PCGSetup(pcg_solver, (HYPRE_Matrix)parcsr_A,
(HYPRE_Vector)b, (HYPRE_Vector)x);
hypre_MPI_Barrier(comm);
hypre_EndTiming(time_index);
#ifdef USE_CALIPER
CALI_MARK_END("Setup");
#endif
hypre_GetTiming("Problem 2: AMG Setup Time", &wall_time, comm);
hypre_FinalizeTiming(time_index);
hypre_ClearTiming();
fflush(NULL);
HYPRE_BoomerAMGGetCumNnzAP(pcg_precond, &cum_nnz_AP);
FOM1 = cum_nnz_AP / wall_time;
total_time = wall_time;
#ifdef USE_CALIPER
adiak_namevalue("Setup-FOM", adiak_general, NULL, "%f", FOM1);
#endif
if (myid == 0)
{
hypre_printf ("\nFOM_Setup: nnz_AP / Setup Phase Time: %e\n\n", FOM1);
}
time_index = hypre_InitializeTiming("PCG Solve");
hypre_MPI_Barrier(comm);
#ifdef USE_CALIPER
CALI_MARK_BEGIN("Solve");
#endif
hypre_BeginTiming(time_index);
HYPRE_PCGSolve(pcg_solver, (HYPRE_Matrix)parcsr_A,
(HYPRE_Vector)b, (HYPRE_Vector)x);
hypre_MPI_Barrier(comm);
hypre_EndTiming(time_index);
#ifdef USE_CALIPER
CALI_MARK_END("Solve");
#endif
hypre_GetTiming("Problem 2: AMG-PCG Solve Time", &wall_time, comm);
hypre_FinalizeTiming(time_index);
hypre_ClearTiming();
fflush(NULL);
HYPRE_PCGGetNumIterations(pcg_solver, &num_iterations);
HYPRE_PCGGetFinalRelativeResidualNorm(pcg_solver, &final_res_norm);
HYPRE_BoomerAMGDestroy(pcg_precond);
HYPRE_ParCSRPCGDestroy(pcg_solver);
FOM2 = cum_nnz_AP / wall_time;
total_time += 3*wall_time;
if (myid == 0)
{
hypre_printf("\n");
hypre_printf("Iterations = %d\n", num_iterations);
hypre_printf("Final Relative Residual Norm = %e\n", final_res_norm);
hypre_printf("\n");
hypre_printf ("\nFOM_Solve: nnz_AP * iterations / Solve Phase Time: %e\n\n", FOM2);
FOM1 = cum_nnz_AP/total_time;
#ifdef USE_CALIPER
adiak_namevalue("Solve-FOM", adiak_general, NULL, "%f", FOM2);
adiak_namevalue("Final-FOM", adiak_general, NULL, "%f", FOM1);
#endif
hypre_printf ("\n\nFigure of Merit (FOM): nnz_AP / (Setup Phase Time + 3 * Solve Phase Time) %e\n\n", FOM1);
}
#ifdef USE_CALIPER
CALI_MARK_END("Problem");
#endif
}
/*-----------------------------------------------------------
* Problem 1: Solve a 27pt 3D diffusion problem with AMG-GMRES
*-----------------------------------------------------------*/
if (problem_id == 1)
{
#ifdef USE_CALIPER
adiak_namevalue("Problem", adiak_general, NULL, "%d", 1);
adiak_namevalue("Solver", adiak_general, NULL, "%s", "GMRES");
CALI_MARK_BEGIN("Problem");
#endif
time_index = hypre_InitializeTiming("GMRES Setup");
hypre_MPI_Barrier(comm);
#ifdef USE_CALIPER
CALI_MARK_BEGIN("Setup");
#endif
hypre_BeginTiming(time_index);
HYPRE_ParCSRGMRESCreate(comm, &pcg_solver);
HYPRE_GMRESSetKDim(pcg_solver, k_dim);
HYPRE_GMRESSetMaxIter(pcg_solver, max_iter);
HYPRE_GMRESSetTol(pcg_solver, tol);
HYPRE_GMRESSetAbsoluteTol(pcg_solver, atol);
HYPRE_GMRESSetLogging(pcg_solver, 1);
HYPRE_GMRESSetPrintLevel(pcg_solver, ioutdat);
HYPRE_GMRESSetRelChange(pcg_solver, rel_change);
/* use BoomerAMG as preconditioner */
if (myid == 0 && print_stats) { hypre_printf("Solver: AMG-GMRES\n"); }
HYPRE_BoomerAMGCreate(&pcg_precond);
HYPRE_BoomerAMGSetTol(pcg_precond, pc_tol);
/*HYPRE_BoomerAMGSetCoarsenType(pcg_precond, coarsen_type);*/
HYPRE_BoomerAMGSetInterpType(pcg_precond, interp_type);
HYPRE_BoomerAMGSetPMaxElmts(pcg_precond, P_max_elmts);
HYPRE_BoomerAMGSetPrintLevel(pcg_precond, poutdat);
HYPRE_BoomerAMGSetMaxIter(pcg_precond, 1);
HYPRE_BoomerAMGSetNumSweeps(pcg_precond, num_sweeps);
if (relax_type > -1) { HYPRE_BoomerAMGSetRelaxType(pcg_precond, relax_type); }
HYPRE_BoomerAMGSetDebugFlag(pcg_precond, debug_flag);
HYPRE_BoomerAMGSetRAP2(pcg_precond, rap2);
HYPRE_BoomerAMGSetKeepTranspose(pcg_precond, keepTranspose);
HYPRE_BoomerAMGSetCumNnzAP(pcg_precond, cum_nnz_AP);
HYPRE_BoomerAMGSetMaxRowSum(pcg_precond, max_row_sum);
HYPRE_GMRESSetMaxIter(pcg_solver, mg_max_iter);
HYPRE_GMRESSetPrecond(pcg_solver,
(HYPRE_PtrToSolverFcn) HYPRE_BoomerAMGSolve,
(HYPRE_PtrToSolverFcn) HYPRE_BoomerAMGSetup,
pcg_precond);
HYPRE_GMRESGetPrecond(pcg_solver, &pcg_precond_gotten);
if (pcg_precond_gotten != pcg_precond)
{
hypre_printf("HYPRE_GMRESGetPrecond got bad precond\n");
return (-1);
}
else if (myid == 0 && print_stats)
{
hypre_printf("HYPRE_GMRESGetPrecond got good precond\n");
}
HYPRE_GMRESSetup (pcg_solver, (HYPRE_Matrix)parcsr_A, (HYPRE_Vector)b, (HYPRE_Vector)x);
hypre_MPI_Barrier(comm);
hypre_EndTiming(time_index);
#ifdef USE_CALIPER
CALI_MARK_END("Setup");
#endif
hypre_GetTiming("Problem 1: AMG Setup Time", &wall_time, comm);
hypre_FinalizeTiming(time_index);
hypre_ClearTiming();
fflush(NULL);
HYPRE_BoomerAMGGetCumNnzAP(pcg_precond, &cum_nnz_AP);
FOM1 = cum_nnz_AP / wall_time;
total_time = wall_time;
if (myid == 0)
{
hypre_printf ("\nFOM_Setup: nnz_AP / Setup Phase Time: %e\n\n", FOM1);
}
#ifdef USE_CALIPER
adiak_namevalue("Setup-FOM", adiak_general, NULL, "%f", FOM1);
#endif
time_index = hypre_InitializeTiming("GMRES Solve");
hypre_MPI_Barrier(comm);
#ifdef USE_CALIPER
CALI_MARK_BEGIN("Solve");
#endif
hypre_BeginTiming(time_index);
HYPRE_GMRESSolve (pcg_solver, (HYPRE_Matrix)parcsr_A, (HYPRE_Vector)b, (HYPRE_Vector)x);
hypre_MPI_Barrier(comm);
hypre_EndTiming(time_index);
#ifdef USE_CALIPER
CALI_MARK_END("Solve");
#endif
hypre_GetTiming("Problem 1: AMG-GMRES Solve Time", &wall_time, comm);
hypre_FinalizeTiming(time_index);
hypre_ClearTiming();
fflush(NULL);
HYPRE_GMRESGetNumIterations(pcg_solver, &num_iterations);
HYPRE_GMRESGetFinalRelativeResidualNorm(pcg_solver, &final_res_norm);
HYPRE_BoomerAMGDestroy(pcg_precond);
HYPRE_ParCSRGMRESDestroy(pcg_solver);
FOM2 = cum_nnz_AP / wall_time;
total_time += wall_time;
if (myid == 0)
{
hypre_printf("\n");
hypre_printf("Iterations = %d\n", num_iterations);
hypre_printf("Final Relative Residual Norm = %e\n", final_res_norm);
hypre_printf("\n");
hypre_printf ("\nFOM_Solve: nnz_AP / Solve Phase Time: %e\n\n", FOM2);
FOM1 = cum_nnz_AP / total_time;
#ifdef USE_CALIPER
adiak_namevalue("Solve-FOM", adiak_general, NULL, "%f", FOM2);
adiak_namevalue("Final-FOM", adiak_general, NULL, "%f", FOM1);
#endif
hypre_printf ("\n\nFigure of Merit (FOM): nnz_AP / (Setup Phase Time + Solve Phase Time) %e\n\n", FOM1);
}
#ifdef USE_CALIPER
CALI_MARK_END("Problem");
#endif
}
#if defined(HYPRE_USING_UMPIRE)
if (myid == 0)
{
#if defined(HYPRE_USING_UNIFIED_MEMORY)
size_t um_hwm = umpire_allocator_get_high_watermark(&um_allocator);
printf("UMPIRE UM Pool size %lu bytes, high water mark %lu bytes\n", umpire_um_pool_size, um_hwm);
#else
size_t dev_hwm = umpire_allocator_get_high_watermark(&dev_allocator);
printf("UMPIRE Device Pool size %lu bytes, high water mark %lu bytes\n", umpire_dev_pool_size, dev_hwm);
#endif
}
#endif
/*-----------------------------------------------------------
* Print the solution
*-----------------------------------------------------------*/
if (print_system)
{
HYPRE_IJVectorPrint(ij_x, "IJ.out.x");
}
/*-----------------------------------------------------------
* Finalize things
*-----------------------------------------------------------*/
HYPRE_IJMatrixDestroy(ij_A);
HYPRE_IJVectorDestroy(ij_b);
HYPRE_IJVectorDestroy(ij_x);
/* Finalize hypre */
HYPRE_Finalize();
#if defined(HYPRE_USING_UMPIRE)
umpire_allocator_release(&dev_allocator);
umpire_allocator_release(&um_allocator);
#endif
#ifdef USE_CALIPER
CALI_MARK_END("main");
adiak_fini();
#endif
/* Finalize MPI */
hypre_MPI_Finalize();
return (0);
}
/*----------------------------------------------------------------------
* Build 27-point laplacian in 3D,
* Parameters given in command line.
*----------------------------------------------------------------------*/
HYPRE_Int
BuildIJLaplacian27pt( HYPRE_Int argc,
char *argv[],
HYPRE_BigInt *system_size_ptr,
HYPRE_IJMatrix *ij_A_ptr,
HYPRE_MemoryLocation memory_location )
{
MPI_Comm comm = hypre_MPI_COMM_WORLD;
HYPRE_Int nx, ny, nz;
HYPRE_Int P, Q, R;
HYPRE_IJMatrix ij_A;
HYPRE_Int num_procs, myid;
HYPRE_Int px, py, pz;
HYPRE_Real *value;
HYPRE_Int *diag_i;
HYPRE_Int *offd_i;
HYPRE_BigInt *row_nums, *row_nums_d;
HYPRE_BigInt *col_nums, *col_nums_d;
HYPRE_Int *num_cols, *num_cols_d;
HYPRE_Complex *data, *data_d;
HYPRE_BigInt row_index;
HYPRE_Int i;
HYPRE_Int local_size;
HYPRE_BigInt global_size;
HYPRE_Int nxy;
HYPRE_BigInt nx_global, ny_global, nz_global;
HYPRE_Int all_threads;
HYPRE_Int *nnz;
HYPRE_Int all_nnz = 0;
HYPRE_BigInt Cx, Cy, Cz;
HYPRE_Int arg_index;
/*-----------------------------------------------------------
* Initialize some stuff
*-----------------------------------------------------------*/
hypre_MPI_Comm_size(comm, &num_procs );
hypre_MPI_Comm_rank(comm, &myid );
all_threads = hypre_NumThreads();
nnz = hypre_CTAlloc(HYPRE_Int, all_threads, HYPRE_MEMORY_HOST);
/*-----------------------------------------------------------
* Set defaults
*-----------------------------------------------------------*/
nx = 10;
ny = 10;
nz = 10;
P = num_procs;;
Q = 1;
R = 1;
/*-----------------------------------------------------------
* Parse command line
*-----------------------------------------------------------*/
arg_index = 0;
while (arg_index < argc)
{
if ( strcmp(argv[arg_index], "-n") == 0 )
{
arg_index++;
nx = atoi(argv[arg_index++]);
ny = atoi(argv[arg_index++]);
nz = atoi(argv[arg_index++]);
}
else if ( strcmp(argv[arg_index], "-P") == 0 )
{
arg_index++;
P = atoi(argv[arg_index++]);
Q = atoi(argv[arg_index++]);
R = atoi(argv[arg_index++]);
}
else
{
arg_index++;
}
}
#ifdef USE_CALIPER
adiak_namevalue("Size-x", adiak_general, NULL, "%d", nx);
adiak_namevalue("Size-y", adiak_general, NULL, "%d", ny);
adiak_namevalue("Size-z", adiak_general, NULL, "%d", nz);
adiak_namevalue("Px", adiak_general, NULL, "%d", P);
adiak_namevalue("Py", adiak_general, NULL, "%d", Q);
adiak_namevalue("Pz", adiak_general, NULL, "%d", R);
#endif
/*-----------------------------------------------------------
* Check a few things
*-----------------------------------------------------------*/
if ((P * Q * R) != num_procs)
{
hypre_printf("Error: Invalid number of processors or processor topology \n");
exit(1);
}
/*-----------------------------------------------------------
* Print driver parameters
*-----------------------------------------------------------*/
nx_global = (HYPRE_BigInt)(P * nx);
ny_global = (HYPRE_BigInt)(Q * ny);
nz_global = (HYPRE_BigInt)(R * nz);
global_size = nx_global * ny_global * nz_global;
if (myid == 0)
{
hypre_printf(" Laplacian_27pt:\n");
hypre_printf(" (Nx, Ny, Nz) = (%b, %b, %b)\n", nx_global, ny_global, nz_global);
hypre_printf(" (Px, Py, Pz) = (%d, %d, %d)\n\n", P, Q, R);
}
/*-----------------------------------------------------------
* Set up the grid structure
*-----------------------------------------------------------*/
/* compute px,py,pz from P,Q,R and myid */
px = myid % P;
py = (( myid - px) / P) % Q;
pz = ( myid - px - P * py) / ( P * Q );
/*-----------------------------------------------------------
* Generate the matrix
*-----------------------------------------------------------*/
value = hypre_CTAlloc(HYPRE_Real, 4, HYPRE_MEMORY_HOST);
value[0] = 26.0;
value[1] = -4.0;
value[2] = -0.15;
value[3] = -0.0125;
local_size = nx * ny * nz;
row_index = (HYPRE_BigInt)(myid * local_size);
row_nums = hypre_CTAlloc(HYPRE_BigInt, local_size, HYPRE_MEMORY_HOST);
num_cols = hypre_CTAlloc(HYPRE_Int, local_size, HYPRE_MEMORY_HOST);
HYPRE_IJMatrixCreate( comm, row_index, (HYPRE_BigInt)(row_index + local_size - 1),
row_index, (HYPRE_BigInt)(row_index + local_size - 1),
&ij_A );
HYPRE_IJMatrixSetObjectType( ij_A, HYPRE_PARCSR );
nxy = nx * ny;
diag_i = hypre_CTAlloc(HYPRE_Int, local_size, HYPRE_MEMORY_HOST);
offd_i = hypre_CTAlloc(HYPRE_Int, local_size, HYPRE_MEMORY_HOST);
Cx = (HYPRE_BigInt)(nx * (ny * nz - 1));