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:   PetscFunctionBegin;
 18:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
 19:   first += rank * n * step;
 20:   switch (type) {
 21:   case 0:
 22:     PetscCall(PetscMalloc1(n, &idx));
 23:     for (i = 0, j = first; i < n; i++, j += step) idx[i] = j;
 24:     PetscCall(ISCreateGeneral(comm, n, idx, PETSC_OWN_POINTER, is));
 25:     break;
 26:   case 1:
 27:     PetscCall(ISCreateStride(comm, n, first, step, is));
 28:     break;
 29:   case 2:
 30:     PetscCall(PetscMalloc1(n, &idx));
 31:     for (i = 0, j = first; i < n; i++, j += step) idx[i] = j;
 32:     PetscCall(ISCreateBlock(comm, 1, n, idx, PETSC_OWN_POINTER, is));
 33:     break;
 34:   }
 35:   PetscFunctionReturn(PETSC_SUCCESS);
 36: }

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

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

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

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

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

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

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

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

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

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

109: /*TEST

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

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

121: TEST*/