Actual source code: ex51.c

  1: static char help[] = "Test integrity of subvector data, use \n\
  2: use -hdf5 to specify HDF5 viewer format for subvector I/O \n\n";

  4: /*
  5:    Tests for transfer of data from subvectors to parent vectors after
  6:    loading data into subvector. This routine does the following : creates
  7:    a vector of size 50, sets it to 2 and saves it to disk. Creates a
  8:    vector of size 100, set it to 1 and extracts the last 50 elements
  9:    as a subvector. Loads the saved vector from disk into the subvector
 10:    and restores the subvector. To verify that the data has been loaded
 11:    into the parent vector, the sum of its elements is calculated.
 12:    The arithmetic mean is also calculated in order to test VecMean().
 13: */

 15: #include <petscvec.h>
 16: #include <petscviewerhdf5.h>

 18: int main(int argc,char **argv)
 19: {
 20:   Vec            testvec;                 /* parent vector of size 100 */
 21:   Vec            loadvec;                 /* subvector extracted from the parent vector */
 22:   Vec            writevec;                /* vector used to save data to be loaded by loadvec */
 23:   IS             loadis;                  /* index set to extract last 50 elements of testvec */
 24:   PetscInt       low,high;                /* used to store vecownership output */
 25:   PetscInt       issize, isstart;         /* index set params */
 26:   PetscInt       skipuntil = 50;          /* parameter to slice the last N elements of parent vec */
 27:   PetscViewer    viewer;                  /* viewer for I/O */
 28:   PetscScalar    sum;                     /* used to test sum of parent vector elements */
 29:   PetscScalar    mean;                    /* used to test mean of parent vector elements */
 30:   PetscBool      usehdf5 = PETSC_FALSE;

 32:   PetscInitialize(&argc, &argv, (char*) 0, help);

 34:   /* parse input options to determine I/O format */
 35:   PetscOptionsGetBool(NULL,NULL,"-hdf5",&usehdf5,NULL);

 37:   /* Create parent vector with 100 elements, set it to 1 */
 38:   VecCreate(PETSC_COMM_WORLD, &testvec);
 39:   VecSetSizes(testvec, PETSC_DECIDE,100);
 40:   VecSetUp(testvec);
 41:   VecSet(testvec, (PetscScalar) 1);

 43:   /* Create a vector with 50 elements, set it to 2. */
 44:   VecCreate(PETSC_COMM_WORLD, &writevec);
 45:   VecSetSizes(writevec, PETSC_DECIDE,50);
 46:   VecSetUp(writevec);
 47:   VecSet(writevec, (PetscScalar) 2);
 48:   PetscObjectSetName((PetscObject)writevec,"temp");

 50:   /* Save to disk in specified format, destroy vector & viewer */
 51:   if (usehdf5) {
 52:     PetscPrintf(PETSC_COMM_WORLD,"writing vector in hdf5 to vector.dat ...\n");
 53:     PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
 54:   } else {
 55:     PetscPrintf(PETSC_COMM_WORLD,"writing vector in binary to vector.dat ...\n");
 56:     PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
 57:   }
 58:   VecView(writevec,viewer);
 59:   VecDestroy(&writevec);
 60:   PetscViewerDestroy(&viewer);

 62:   /* Create index sets on each mpi rank to select the last 50 elements of parent vec */
 63:   VecGetOwnershipRange(testvec, &low, &high);
 64:   if (low>=skipuntil) {
 65:     isstart = low;
 66:     issize = high - low;
 67:   } else if (low<=skipuntil && high>=skipuntil) {
 68:     isstart = skipuntil;
 69:     issize = high - skipuntil;
 70:   } else {
 71:     isstart = low;
 72:     issize  = 0;
 73:   }
 74:   ISCreateStride(PETSC_COMM_WORLD, issize, isstart, 1, &loadis);

 76:   /* Create subvector using the index set created above */
 77:   VecGetSubVector(testvec, loadis, &loadvec);
 78:   PetscObjectSetName((PetscObject)loadvec,"temp");

 80:   /* Load the previously saved vector into the subvector, destroy viewer */
 81:   if (usehdf5) {
 82:     PetscPrintf(PETSC_COMM_WORLD,"reading vector in hdf5 from vector.dat ...\n");
 83:     PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
 84:   } else {
 85:     PetscPrintf(PETSC_COMM_WORLD,"reading vector in binary from vector.dat ...\n");
 86:     PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
 87:   }
 88:   VecLoad(loadvec, viewer);
 89:   PetscViewerDestroy(&viewer);

 91:   /* Restore subvector to transfer loaded data into parent vector */
 92:   VecRestoreSubVector(testvec, loadis, &loadvec);

 94:   /* Compute sum of parent vector elements */
 95:   VecSum(testvec, &sum);
 96:   VecMean(testvec, &mean);

 98:   /* to verify that the loaded data has been transferred */
100:   PetscPrintf(PETSC_COMM_WORLD,"VecSum on parent vec is : %e\n",(double)PetscAbsScalar(sum));
101:   PetscPrintf(PETSC_COMM_WORLD,"VecMean on parent vec is : %e\n",(double)PetscAbsScalar(mean));

103:   /* destroy parent vector, index set and exit */
104:   VecDestroy(&testvec);
105:   ISDestroy(&loadis);
106:   PetscFinalize();
107:   return 0;
108: }

110: /*TEST

112:   build:
113:     requires: hdf5

115:   test:
116:     nsize: 4

118:   test:
119:     suffix: 2
120:     nsize: 4
121:     args: -hdf5

123: TEST*/