Actual source code: ex13.c


  2: static char help[] = "Tests ISDuplicate(), ISCopy(), ISShift(), ISEqualUnsorted(), ISEqual().\n\n";

  4: #include <petscis.h>
  5: #include <petscviewer.h>

  7: /*
  8: type = 0 general
  9: type = 1 stride
 10: type = 2 block
 11: */
 12: static PetscErrorCode CreateIS(MPI_Comm comm, PetscInt type, PetscInt n, PetscInt first, PetscInt step, IS *is)
 13: {
 14:   PetscInt   *idx, i, j;
 15:   PetscMPIInt rank;

 17:   MPI_Comm_rank(comm, &rank);
 18:   first += rank * n * step;
 19:   switch (type) {
 20:   case 0:
 21:     PetscMalloc1(n, &idx);
 22:     for (i = 0, j = first; i < n; i++, j += step) idx[i] = j;
 23:     ISCreateGeneral(comm, n, idx, PETSC_OWN_POINTER, is);
 24:     break;
 25:   case 1:
 26:     ISCreateStride(comm, n, first, step, is);
 27:     break;
 28:   case 2:
 29:     PetscMalloc1(n, &idx);
 30:     for (i = 0, j = first; i < n; i++, j += step) idx[i] = j;
 31:     ISCreateBlock(comm, 1, n, idx, PETSC_OWN_POINTER, is);
 32:     break;
 33:   }
 34:   return 0;
 35: }

 37: int main(int argc, char **argv)
 38: {
 39:   IS        is[128];
 40:   IS        tmp;
 41:   PetscInt  n = 10, first = 0, step = 1, offset = 0;
 42:   PetscInt  i, j = 0, type;
 43:   PetscBool verbose = PETSC_FALSE, flg;
 44:   MPI_Comm  comm;

 47:   PetscInitialize(&argc, &argv, (char *)0, help);
 48:   comm = PETSC_COMM_WORLD;
 49:   PetscArrayzero(is, sizeof(is) / sizeof(is[0]));
 50:   PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL);
 51:   PetscOptionsGetInt(NULL, NULL, "-first", &first, NULL);
 52:   PetscOptionsGetInt(NULL, NULL, "-step", &step, NULL);
 53:   PetscOptionsGetInt(NULL, NULL, "-offset", &offset, NULL);
 54:   PetscOptionsGetBool(NULL, NULL, "-verbose", &verbose, NULL);

 56:   for (type = 0; type < 3; type++) {
 57:     CreateIS(comm, type, n, first + offset, step, &is[j]);
 58:     j++;

 60:     CreateIS(comm, type, n, first + offset, step, &is[j]);
 61:     ISCopy(is[j], is[j]);
 62:     j++;

 64:     CreateIS(comm, type, n, first + offset, step, &tmp);
 65:     ISDuplicate(tmp, &is[j]);
 66:     ISCopy(tmp, is[j]);
 67:     ISDestroy(&tmp);
 68:     j++;

 70:     CreateIS(comm, type, n, first + offset, step, &is[j]);
 71:     ISShift(is[j], 0, is[j]);
 72:     j++;

 74:     CreateIS(comm, type, n, first, step, &is[j]);
 75:     ISShift(is[j], offset, is[j]);
 76:     j++;

 78:     CreateIS(comm, type, n, first + offset, step, &tmp);
 79:     ISDuplicate(tmp, &is[j]);
 80:     ISShift(tmp, 0, is[j]);
 81:     ISDestroy(&tmp);
 82:     j++;

 84:     CreateIS(comm, type, n, first, step, &tmp);
 85:     ISDuplicate(tmp, &is[j]);
 86:     ISShift(tmp, offset, is[j]);
 87:     ISDestroy(&tmp);
 88:     j++;

 90:     CreateIS(comm, type, n, first + 2 * offset, step, &is[j]);
 91:     ISShift(is[j], -offset, is[j]);
 92:     j++;
 93:   }
 94:   PetscAssert(j < (PetscInt)(sizeof(is) / sizeof(is[0])), comm, PETSC_ERR_ARG_OUTOFRANGE, "assertion failed: j < sizeof(is)/sizeof(is[0])");
 95:   ISViewFromOptions(is[0], NULL, "-is0_view");
 96:   ISViewFromOptions(is[j / 2], NULL, "-is1_view");
 97:   for (i = 0; i < j; i++) {
 98:     if (!is[i]) continue;
 99:     ISEqualUnsorted(is[i], is[0], &flg);
101:     if (verbose) PetscPrintf(comm, "is[%02" PetscInt_FMT "] identical to is[0]\n", i);
102:   }
103:   for (i = 0; i < j; i++) ISDestroy(&is[i]);
104:   PetscFinalize();
105:   return 0;
106: }

108: /*TEST

110:     test:
111:       suffix: 1
112:       nsize: 3
113:       args: -n 6 -first {{-2 0 1 3}} -step {{-2 0 1 3}}

115:     test:
116:       suffix: 2
117:       nsize: 2
118:       args: -n 3 -first 2 -step -1 -is0_view -is1_view -verbose

120: TEST*/