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;

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

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

 31:   if (subset) 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:       VecSetValues(x, 1, &i, &val, INSERT_VALUES);
 38:     }
 39:     VecAssemblyBegin(x);
 40:     VecAssemblyEnd(x);
 41:     if (!r) VecCopy(x, y); /* Save result of first assembly */
 42:   }

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

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

 53:   /* Now do the blocksetvalues */
 54:   VecSet(x, zero);
 55:   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:       VecSetValuesBlocked(x, 1, &ii, vals, INSERT_VALUES);
 63:     }
 64:     VecAssemblyBegin(x);
 65:     VecAssemblyEnd(x);
 66:     if (!r) VecCopy(x, y); /* Save result of first assembly */
 67:   }

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

 73:   VecDestroy(&x);
 74:   VecDestroy(&y);
 75:   PetscFree(vals);
 76:   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*/