Actual source code: ex5.c

  1: /*
  2:  Created by Huy Vo on 12/3/18.
  3: */
  4: static char help[] = "Test memory scalable AO.\n\n";

  6: #include <petsc.h>
  7: #include <petscvec.h>
  8: #include <petscmat.h>
  9: #include <petscao.h>

 11: int main(int argc, char *argv[])
 12: {
 13:   PetscInt       n_global = 16;
 14:   MPI_Comm       comm;
 15:   PetscLayout    layout;
 16:   PetscInt       local_size;
 17:   PetscInt       start, end;
 18:   PetscMPIInt    rank;
 19:   PetscInt      *app_indices, *petsc_indices, *ia, *ia0;
 20:   PetscInt       i;
 21:   AO             app2petsc;
 22:   IS             app_is, petsc_is;
 23:   const PetscInt n_loc = 8;

 26:   PetscInitialize(&argc, &argv, (char *)0, help);
 27:   comm = PETSC_COMM_WORLD;
 28:   MPI_Comm_rank(comm, &rank);

 30:   PetscLayoutCreate(comm, &layout);
 31:   PetscLayoutSetSize(layout, n_global);
 32:   PetscLayoutSetLocalSize(layout, PETSC_DECIDE);
 33:   PetscLayoutSetUp(layout);
 34:   PetscLayoutGetLocalSize(layout, &local_size);
 35:   PetscLayoutGetRange(layout, &start, &end);

 37:   PetscMalloc1(local_size, &app_indices);
 38:   PetscMalloc1(local_size, &petsc_indices);
 39:   /*  Add values for local indices for usual states */
 40:   for (i = 0; i < local_size; ++i) {
 41:     app_indices[i]   = start + i;
 42:     petsc_indices[i] = end - 1 - i;
 43:   }

 45:   /* Create the AO object that maps from lexicographic ordering to Petsc Vec ordering */
 46:   ISCreateGeneral(comm, local_size, &app_indices[0], PETSC_COPY_VALUES, &app_is);
 47:   ISCreateGeneral(comm, local_size, &petsc_indices[0], PETSC_COPY_VALUES, &petsc_is);
 48:   AOCreate(comm, &app2petsc);
 49:   AOSetIS(app2petsc, app_is, petsc_is);
 50:   AOSetType(app2petsc, AOMEMORYSCALABLE);
 51:   AOSetFromOptions(app2petsc);
 52:   ISDestroy(&app_is);
 53:   ISDestroy(&petsc_is);
 54:   AOView(app2petsc, PETSC_VIEWER_STDOUT_WORLD);

 56:   /* Test AOApplicationToPetsc */
 57:   PetscMalloc1(n_loc, &ia);
 58:   PetscMalloc1(n_loc, &ia0);
 59:   if (rank == 0) {
 60:     ia[0] = 0;
 61:     ia[1] = -1;
 62:     ia[2] = 1;
 63:     ia[3] = 2;
 64:     ia[4] = -1;
 65:     ia[5] = 4;
 66:     ia[6] = 5;
 67:     ia[7] = 6;
 68:   } else {
 69:     ia[0] = -1;
 70:     ia[1] = 8;
 71:     ia[2] = 9;
 72:     ia[3] = 10;
 73:     ia[4] = -1;
 74:     ia[5] = 12;
 75:     ia[6] = 13;
 76:     ia[7] = 14;
 77:   }
 78:   PetscArraycpy(ia0, ia, n_loc);

 80:   AOApplicationToPetsc(app2petsc, n_loc, ia);

 82:   for (i = 0; i < n_loc; ++i) PetscSynchronizedPrintf(PETSC_COMM_WORLD, "proc = %d : %" PetscInt_FMT " -> %" PetscInt_FMT " \n", rank, ia0[i], ia[i]);
 83:   PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT);
 84:   AODestroy(&app2petsc);
 85:   PetscLayoutDestroy(&layout);
 86:   PetscFree(app_indices);
 87:   PetscFree(petsc_indices);
 88:   PetscFree(ia);
 89:   PetscFree(ia0);
 90:   PetscFinalize();
 91:   return 0;
 92: }

 94: /*TEST

 96:    test:
 97:      nsize: 2

 99: TEST*/