xref: /petsc/src/vec/is/ao/tests/ex2.c (revision 732aec7a18f2199fb53bb9a2f3aef439a834ce31)
1 static char help[] = "Tests application ordering.\n\n";
2 
3 #include <petscsys.h>
4 #include <petscao.h>
5 #include <petscviewer.h>
6 
main(int argc,char ** argv)7 int main(int argc, char **argv)
8 {
9   PetscMPIInt rank, size;
10   PetscInt    n, *ispetsc, *isapp, start, N, i;
11   AO          ao;
12 
13   PetscFunctionBeginUser;
14   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
15   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
16   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
17   n = rank + 2;
18   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
19 
20   /* create the orderings */
21   PetscCall(PetscMalloc2(n, &ispetsc, n, &isapp));
22 
23   PetscCallMPI(MPI_Scan(&n, &start, 1, MPIU_INT, MPI_SUM, PETSC_COMM_WORLD));
24   PetscCallMPI(MPIU_Allreduce(&n, &N, 1, MPIU_INT, MPI_SUM, PETSC_COMM_WORLD));
25   start -= n;
26 
27   for (i = 0; i < n; i++) {
28     ispetsc[i] = start + i;
29     isapp[i]   = N - start - i - 1;
30   }
31 
32   /* create the application ordering */
33   PetscCall(AOCreateBasic(PETSC_COMM_WORLD, n, isapp, ispetsc, &ao));
34   PetscCall(AOView(ao, PETSC_VIEWER_STDOUT_WORLD));
35 
36   /* check the mapping */
37   PetscCall(AOPetscToApplication(ao, n, ispetsc));
38   for (i = 0; i < n; i++) {
39     if (ispetsc[i] != isapp[i]) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "[%d] Problem with mapping %" PetscInt_FMT " to %" PetscInt_FMT "\n", rank, i, ispetsc[i]));
40   }
41   PetscCall(PetscFree2(ispetsc, isapp));
42 
43   PetscCall(AODestroy(&ao));
44   PetscCall(PetscFinalize());
45   return 0;
46 }
47 
48 /*TEST
49 
50    test:
51 
52    test:
53       suffix: 2
54       nsize: 2
55 
56    test:
57       suffix: 3
58       nsize: 3
59 
60 TEST*/
61