Actual source code: ex29.c
1: static char help[] = "Tests VecSetValues() and VecSetValuesBlocked() on MPI vectors.\n\
2: Where at least a couple of mallocs will occur in the stash code.\n\n";
4: #include <petscvec.h>
6: int main(int argc, char **argv)
7: {
8: PetscMPIInt size;
9: PetscInt i, j, r, n = 50, repeat = 1, bs;
10: PetscScalar val, *vals, zero = 0.0;
11: PetscBool inv = PETSC_FALSE, subset = PETSC_FALSE, flg;
12: Vec x, y;
14: PetscFunctionBeginUser;
15: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
16: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
17: bs = size;
19: PetscCall(PetscOptionsGetInt(NULL, NULL, "-repeat", &repeat, NULL));
20: PetscCall(PetscOptionsGetBool(NULL, NULL, "-subset", &subset, NULL));
21: PetscCall(PetscOptionsGetBool(NULL, NULL, "-invert", &inv, NULL));
22: PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
23: PetscCall(PetscOptionsGetInt(NULL, NULL, "-bs", &bs, NULL));
24: PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
25: PetscCall(VecSetSizes(x, PETSC_DECIDE, n * bs));
26: PetscCall(VecSetBlockSize(x, bs));
27: PetscCall(VecSetFromOptions(x));
28: PetscCall(VecDuplicate(x, &y));
30: if (subset) PetscCall(VecSetOption(x, VEC_SUBSET_OFF_PROC_ENTRIES, PETSC_TRUE));
32: for (r = 0; r < repeat; r++) {
33: /* Assemble the full vector on the first and last iteration, otherwise don't set any values */
34: for (i = 0; i < n * bs * (!r || !(repeat - 1 - r)); i++) {
35: val = i * 1.0;
36: PetscCall(VecSetValues(x, 1, &i, &val, INSERT_VALUES));
37: }
38: PetscCall(VecAssemblyBegin(x));
39: PetscCall(VecAssemblyEnd(x));
40: if (!r) PetscCall(VecCopy(x, y)); /* Save result of first assembly */
41: }
43: PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
44: PetscCall(VecEqual(x, y, &flg));
45: if (!flg) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Vectors from repeat assembly do not match."));
47: /* Create a new vector because the old stash is a subset. */
48: PetscCall(VecDestroy(&x));
49: PetscCall(VecDuplicate(y, &x));
50: if (subset) PetscCall(VecSetOption(x, VEC_SUBSET_OFF_PROC_ENTRIES, PETSC_TRUE));
52: /* Now do the blocksetvalues */
53: PetscCall(VecSet(x, zero));
54: PetscCall(PetscMalloc1(bs, &vals));
55: for (r = 0; r < repeat; r++) {
56: PetscInt up = n * (!r || !(repeat - 1 - r));
57: /* Assemble the full vector on the first and last iteration, otherwise don't set any values */
58: for (i = 0; i < up; i++) {
59: PetscInt ii = inv ? up - i - 1 : i;
60: for (j = 0; j < bs; j++) vals[j] = (ii * bs + j) * 1.0;
61: PetscCall(VecSetValuesBlocked(x, 1, &ii, vals, INSERT_VALUES));
62: }
63: PetscCall(VecAssemblyBegin(x));
64: PetscCall(VecAssemblyEnd(x));
65: if (!r) PetscCall(VecCopy(x, y)); /* Save result of first assembly */
66: }
68: PetscCall(VecView(x, PETSC_VIEWER_STDOUT_WORLD));
69: PetscCall(VecEqual(x, y, &flg));
70: if (!flg) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Vectors from repeat block assembly do not match."));
72: PetscCall(VecDestroy(&x));
73: PetscCall(VecDestroy(&y));
74: PetscCall(PetscFree(vals));
75: PetscCall(PetscFinalize());
76: return 0;
77: }
79: /*TEST
81: test:
82: nsize: 3
83: args: -n 126
85: test:
86: suffix: bts_test_inv_error
87: nsize: 3
88: args: -n 4 -invert -bs 2
89: output_file: output/ex29_test_inv_error.out
91: test:
92: suffix: bts
93: nsize: 3
94: args: -n 126 -vec_assembly_legacy
95: output_file: output/ex29_1.out
97: test:
98: suffix: bts_2
99: nsize: 3
100: args: -n 126 -vec_assembly_legacy -repeat 2
101: output_file: output/ex29_1.out
103: test:
104: suffix: bts_2_subset
105: nsize: 3
106: args: -n 126 -vec_assembly_legacy -repeat 2 -subset
107: output_file: output/ex29_1.out
109: test:
110: suffix: bts_2_subset_proper
111: nsize: 3
112: args: -n 126 -vec_assembly_legacy -repeat 5 -subset
113: output_file: output/ex29_1.out
115: TEST*/