Actual source code: ex5.c

petsc-3.10.5 2019-03-28
Report Typos and Errors
  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              ierr;
 14:   PetscInt              n_global = 16;
 15:   MPI_Comm              comm;
 16:   PetscLayout           layout;
 17:   PetscInt              local_size;
 18:   PetscInt              start, end;
 19:   PetscMPIInt           rank;
 20:   PetscInt              *app_indices,*petsc_indices,*ia,*ia0;
 21:   PetscInt              i;
 22:   AO                    app2petsc;
 23:   IS                    app_is, petsc_is;
 24:   const PetscInt        n_loc = 8;

 26:   PetscInitialize(&argc, &argv, (char *) 0, help); if (ierr) return ierr;
 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) {
 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:   PetscMemcpy(ia0,ia,n_loc*sizeof(PetscInt));

 80:   AOApplicationToPetsc(app2petsc, n_loc, ia);

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

 96: /*TEST

 98:    test:
 99:      nsize: 2

101: TEST*/