Actual source code: ex1.c

  1: static char help[] = "Demonstrates constructing an application ordering.\n\n";

  3: #include <petscao.h>

  5: int main(int argc, char **argv)
  6: {
  7:   PetscInt        i, n = 5;
  8:   PetscInt        getpetsc[] = {0, 3, 4}, getapp[] = {2, 1, 9, 7};
  9:   PetscInt        getpetsc1[] = {0, 3, 4}, getapp1[] = {2, 1, 9, 7};
 10:   PetscInt        getpetsc2[] = {0, 3, 4}, getapp2[] = {2, 1, 9, 7};
 11:   PetscInt        getpetsc3[] = {0, 3, 4}, getapp3[] = {2, 1, 9, 7};
 12:   PetscInt        getpetsc4[] = {0, 3, 4}, getapp4[] = {2, 1, 9, 7};
 13:   PetscMPIInt     rank, size;
 14:   IS              ispetsc, isapp;
 15:   AO              ao;
 16:   const PetscInt *app;

 18:   PetscFunctionBeginUser;
 19:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 20:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
 21:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 22:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));

 24:   /* create the index sets */
 25:   PetscCall(ISCreateStride(PETSC_COMM_WORLD, n, rank, size, &isapp));
 26:   PetscCall(ISCreateStride(PETSC_COMM_WORLD, n, n * rank, 1, &ispetsc)); /* natural numbering */

 28:   /* create the application ordering */
 29:   PetscCall(AOCreateBasicIS(isapp, ispetsc, &ao));
 30:   PetscCall(AOView(ao, PETSC_VIEWER_STDOUT_WORLD));

 32:   PetscCall(AOPetscToApplication(ao, 4, getapp));
 33:   PetscCall(AOApplicationToPetsc(ao, 3, getpetsc));

 35:   PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d] 2,1,9,7 PetscToApplication %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", rank, getapp[0], getapp[1], getapp[2], getapp[3]));
 36:   PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d] 0,3,4 ApplicationToPetsc %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", rank, getpetsc[0], getpetsc[1], getpetsc[2]));
 37:   PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT));
 38:   PetscCall(AODestroy(&ao));

 40:   /* test MemoryScalable ao */
 41:   /*-------------------------*/
 42:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "\nTest AOCreateMemoryScalable: \n"));
 43:   PetscCall(AOCreateMemoryScalableIS(isapp, ispetsc, &ao));
 44:   PetscCall(AOView(ao, PETSC_VIEWER_STDOUT_WORLD));

 46:   PetscCall(AOPetscToApplication(ao, 4, getapp1));
 47:   PetscCall(AOApplicationToPetsc(ao, 3, getpetsc1));

 49:   /* Check accuracy */
 50:   for (i = 0; i < 4; i++) PetscCheck(getapp1[i] == getapp[i], PETSC_COMM_SELF, PETSC_ERR_USER, "getapp1 %" PetscInt_FMT " != getapp %" PetscInt_FMT, getapp1[i], getapp[i]);
 51:   for (i = 0; i < 3; i++) PetscCheck(getpetsc1[i] == getpetsc[i], PETSC_COMM_SELF, PETSC_ERR_USER, "getpetsc1 %" PetscInt_FMT " != getpetsc %" PetscInt_FMT, getpetsc1[i], getpetsc[i]);

 53:   PetscCall(AODestroy(&ao));

 55:   /* test MemoryScalable ao: ispetsc = NULL */
 56:   /*-----------------------------------------------*/
 57:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "\nTest AOCreateMemoryScalable with ispetsc=NULL:\n"));
 58:   PetscCall(AOCreateMemoryScalableIS(isapp, NULL, &ao));

 60:   PetscCall(AOView(ao, PETSC_VIEWER_STDOUT_WORLD));

 62:   PetscCall(AOPetscToApplication(ao, 4, getapp2));
 63:   PetscCall(AOApplicationToPetsc(ao, 3, getpetsc2));

 65:   /* Check accuracy */
 66:   for (i = 0; i < 4; i++) PetscCheck(getapp2[i] == getapp[i], PETSC_COMM_SELF, PETSC_ERR_USER, "getapp2 %" PetscInt_FMT " != getapp %" PetscInt_FMT, getapp2[i], getapp[i]);
 67:   for (i = 0; i < 3; i++) PetscCheck(getpetsc2[i] == getpetsc[i], PETSC_COMM_SELF, PETSC_ERR_USER, "getpetsc2 %" PetscInt_FMT " != getpetsc %" PetscInt_FMT, getpetsc2[i], getpetsc[i]);
 68:   PetscCall(AODestroy(&ao));

 70:   /* test AOCreateMemoryScalable() ao: */
 71:   PetscCall(ISGetIndices(isapp, &app));
 72:   PetscCall(AOCreateMemoryScalable(PETSC_COMM_WORLD, n, app, NULL, &ao));
 73:   PetscCall(ISRestoreIndices(isapp, &app));

 75:   PetscCall(AOPetscToApplication(ao, 4, getapp4));
 76:   PetscCall(AOApplicationToPetsc(ao, 3, getpetsc4));

 78:   /* Check accuracy */
 79:   for (i = 0; i < 4; i++) PetscCheck(getapp4[i] == getapp[i], PETSC_COMM_SELF, PETSC_ERR_USER, "getapp4 %" PetscInt_FMT " != getapp %" PetscInt_FMT, getapp4[i], getapp[i]);
 80:   for (i = 0; i < 3; i++) PetscCheck(getpetsc4[i] == getpetsc[i], PETSC_COMM_SELF, PETSC_ERR_USER, "getpetsc4 %" PetscInt_FMT " != getpetsc %" PetscInt_FMT, getpetsc4[i], getpetsc[i]);
 81:   PetscCall(AODestroy(&ao));

 83:   /* test general API */
 84:   /*------------------*/
 85:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "\nTest general API: \n"));
 86:   PetscCall(AOCreate(PETSC_COMM_WORLD, &ao));
 87:   PetscCall(AOSetIS(ao, isapp, ispetsc));
 88:   PetscCall(AOSetType(ao, AOMEMORYSCALABLE));
 89:   PetscCall(AOSetFromOptions(ao));

 91:   /* ispetsc and isapp are nolonger used. */
 92:   PetscCall(ISDestroy(&ispetsc));
 93:   PetscCall(ISDestroy(&isapp));

 95:   PetscCall(AOPetscToApplication(ao, 4, getapp3));
 96:   PetscCall(AOApplicationToPetsc(ao, 3, getpetsc3));

 98:   PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d] 2,1,9,7 PetscToApplication %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", rank, getapp3[0], getapp3[1], getapp3[2], getapp3[3]));
 99:   PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d] 0,3,4 ApplicationToPetsc %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", rank, getpetsc3[0], getpetsc3[1], getpetsc3[2]));
100:   PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT));

102:   /* Check accuracy */
103:   for (i = 0; i < 4; i++) PetscCheck(getapp3[i] == getapp[i], PETSC_COMM_SELF, PETSC_ERR_USER, "getapp3 %" PetscInt_FMT " != getapp %" PetscInt_FMT, getapp3[i], getapp[i]);
104:   for (i = 0; i < 3; i++) PetscCheck(getpetsc3[i] == getpetsc[i], PETSC_COMM_SELF, PETSC_ERR_USER, "getpetsc3 %" PetscInt_FMT " != getpetsc %" PetscInt_FMT, getpetsc3[i], getpetsc[i]);

106:   PetscCall(AODestroy(&ao));
107:   PetscCall(PetscFinalize());
108:   return 0;
109: }

111: /*TEST

113:    test:

115:    test:
116:       suffix: 2
117:       nsize: 2

119:    test:
120:       suffix: 3
121:       nsize: 3

123:    test:
124:       suffix: 4
125:       nsize: 3
126:       args: -ao_type basic
127:       output_file: output/ex1_3.out

129: TEST*/