xref: /petsc/src/vec/is/ao/tests/ex3d/ex3.cxx (revision df4cd43f92eaa320656440c40edb1046daee8f75)
1 
2 static char help[] = "AO test contributed by Sebastian Steiger <steiger@purdue.edu>, March 2011\n\n";
3 
4 /*
5   Example of usage:
6     mpiexec -n 12 ./ex3
7     mpiexec -n 30 ./ex3 -ao_type basic
8 */
9 
10 #include <iostream>
11 #include <fstream>
12 #include <vector>
13 #include <petscvec.h>
14 #include <petscao.h>
15 
16 using namespace std;
17 
18 int main(int argc, char **argv)
19 {
20   AO          ao;
21   IS          isapp;
22   char        infile[PETSC_MAX_PATH_LEN], datafiles[PETSC_MAX_PATH_LEN];
23   PetscBool   flg;
24   PetscMPIInt size, rank;
25 
26   PetscFunctionBeginUser;
27   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
28   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
29   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
30 
31   PetscCall(PetscOptionsGetString(NULL, NULL, "-datafiles", datafiles, sizeof(datafiles), &flg));
32   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must specify -datafiles ${DATAFILESPATH}/ao");
33 
34   // read in application indices
35   PetscCall(PetscSNPrintf(infile, sizeof(infile), "%s/AO%dCPUs/ao_p%d_appindices.txt", datafiles, size, rank));
36   ifstream fin(infile);
37   PetscCheck(fin, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "File not found: %s", infile);
38   vector<PetscInt> myapp;
39   int              tmp = -1;
40   while (!fin.eof()) {
41     tmp = -1;
42     fin >> tmp;
43     if (tmp == -1) break;
44     myapp.push_back(tmp);
45   }
46 #if __cplusplus >= 201103L // c++11
47   static_assert(is_same<decltype(myapp.size()), size_t>::value, "");
48 #endif
49   PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d] has %zu indices.\n", rank, myapp.size()));
50   PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT));
51 
52   PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, myapp.size(), &(myapp[0]), PETSC_USE_POINTER, &isapp));
53 
54   PetscCall(AOCreate(PETSC_COMM_WORLD, &ao));
55   PetscCall(AOSetIS(ao, isapp, NULL));
56   PetscCall(AOSetType(ao, AOMEMORYSCALABLE));
57   PetscCall(AOSetFromOptions(ao));
58 
59   if (rank == 0) cout << "AO has been set up." << endl;
60 
61   PetscCall(AODestroy(&ao));
62   PetscCall(ISDestroy(&isapp));
63 
64   if (rank == 0) cout << "AO is done." << endl;
65 
66   PetscCall(PetscFinalize());
67   return 0;
68 }
69 
70 /*TEST
71 
72    build:
73      requires: !defined(PETSC_USE_64BIT_INDICES)
74 
75    test:
76       nsize: 12
77       requires: double !complex datafilespath
78       args: -datafiles ${DATAFILESPATH}/ao
79       output_file: output/ex3_1.out
80 
81    test:
82       suffix: 2
83       nsize: 12
84       requires: double !complex datafilespath
85       args: -ao_type basic -datafiles ${DATAFILESPATH}/ao
86       output_file: output/ex3_1.out
87 
88    test:
89       suffix: 3
90       nsize: 30
91       requires: double !complex datafilespath
92       args: -datafiles ${DATAFILESPATH}/ao
93 
94    test:
95       suffix: 4
96       nsize: 30
97       requires: double !complex datafilespath
98       args: -ao_type basic -datafiles ${DATAFILESPATH}/ao
99       output_file: output/ex3_3.out
100 
101 TEST*/
102