Actual source code: ex29.c


  2: static char help[] = "Tests VecSetValues() and VecSetValuesBlocked() on MPI vectors.\n\
  3: Where at least a couple of mallocs will occur in the stash code.\n\n";

  5: #include <petscvec.h>

  7: int main(int argc, char **argv)
  8: {
  9:   PetscMPIInt size;
 10:   PetscInt    i, j, r, n = 50, repeat = 1, bs;
 11:   PetscScalar val, *vals, zero = 0.0;
 12:   PetscBool   inv = PETSC_FALSE, subset = PETSC_FALSE, flg;
 13:   Vec         x, y;

 15:   PetscFunctionBeginUser;
 16:   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
 17:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 18:   bs = size;

 20:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-repeat", &repeat, NULL));
 21:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-subset", &subset, NULL));
 22:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-invert", &inv, NULL));
 23:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
 24:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-bs", &bs, NULL));
 25:   PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
 26:   PetscCall(VecSetSizes(x, PETSC_DECIDE, n * bs));
 27:   PetscCall(VecSetBlockSize(x, bs));
 28:   PetscCall(VecSetFromOptions(x));
 29:   PetscCall(VecDuplicate(x, &y));

 31:   if (subset) PetscCall(VecSetOption(x, VEC_SUBSET_OFF_PROC_ENTRIES, PETSC_TRUE));

 33:   for (r = 0; r < repeat; r++) {
 34:     /* Assemble the full vector on the first and last iteration, otherwise don't set any values */
 35:     for (i = 0; i < n * bs * (!r || !(repeat - 1 - r)); i++) {
 36:       val = i * 1.0;
 37:       PetscCall(VecSetValues(x, 1, &i, &val, INSERT_VALUES));
 38:     }
 39:     PetscCall(VecAssemblyBegin(x));
 40:     PetscCall(VecAssemblyEnd(x));
 41:     if (!r) PetscCall(VecCopy(x, y)); /* Save result of first assembly */
 42:   }

 44:   PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
 45:   PetscCall(VecEqual(x, y, &flg));
 46:   if (!flg) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Vectors from repeat assembly do not match."));

 48:   /* Create a new vector because the old stash is a subset. */
 49:   PetscCall(VecDestroy(&x));
 50:   PetscCall(VecDuplicate(y, &x));
 51:   if (subset) PetscCall(VecSetOption(x, VEC_SUBSET_OFF_PROC_ENTRIES, PETSC_TRUE));

 53:   /* Now do the blocksetvalues */
 54:   PetscCall(VecSet(x, zero));
 55:   PetscCall(PetscMalloc1(bs, &vals));
 56:   for (r = 0; r < repeat; r++) {
 57:     PetscInt up = n * (!r || !(repeat - 1 - r));
 58:     /* Assemble the full vector on the first and last iteration, otherwise don't set any values */
 59:     for (i = 0; i < up; i++) {
 60:       PetscInt ii = inv ? up - i - 1 : i;
 61:       for (j = 0; j < bs; j++) vals[j] = (ii * bs + j) * 1.0;
 62:       PetscCall(VecSetValuesBlocked(x, 1, &ii, vals, INSERT_VALUES));
 63:     }
 64:     PetscCall(VecAssemblyBegin(x));
 65:     PetscCall(VecAssemblyEnd(x));
 66:     if (!r) PetscCall(VecCopy(x, y)); /* Save result of first assembly */
 67:   }

 69:   PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
 70:   PetscCall(VecEqual(x, y, &flg));
 71:   if (!flg) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Vectors from repeat block assembly do not match."));

 73:   PetscCall(VecDestroy(&x));
 74:   PetscCall(VecDestroy(&y));
 75:   PetscCall(PetscFree(vals));
 76:   PetscCall(PetscFinalize());
 77:   return 0;
 78: }

 80: /*TEST

 82:    test:
 83:       nsize: 3
 84:       args: -n 126

 86:    test:
 87:       suffix: bts_test_inv_error
 88:       nsize: 3
 89:       args: -n 4 -invert -bs 2
 90:       output_file: output/ex29_test_inv_error.out

 92:    test:
 93:       suffix: bts
 94:       nsize: 3
 95:       args: -n 126 -vec_assembly_legacy
 96:       output_file: output/ex29_1.out

 98:    test:
 99:       suffix: bts_2
100:       nsize: 3
101:       args: -n 126 -vec_assembly_legacy -repeat 2
102:       output_file: output/ex29_1.out

104:    test:
105:       suffix: bts_2_subset
106:       nsize: 3
107:       args: -n 126 -vec_assembly_legacy -repeat 2 -subset
108:       output_file: output/ex29_1.out

110:    test:
111:       suffix: bts_2_subset_proper
112:       nsize: 3
113:       args: -n 126 -vec_assembly_legacy -repeat 5 -subset
114:       output_file: output/ex29_1.out

116: TEST*/