Actual source code: ex49.c
1: static const char help[] = "Test VEC_SUBSET_OFF_PROC_ENTRIES\n\n";
3: #include <petsc.h>
4: #include <petscvec.h>
6: /* Unlike most finite element applications, IBAMR does assembly on many cells
7: that are not locally owned; in some cases the processor may own zero finite
8: element cells but still do assembly on a small number of cells anyway. To
9: simulate this, this code assembles a PETSc vector by adding contributions
10: to every entry in the vector on every processor. This causes a deadlock
11: when we save the communication pattern via
13: VecSetOption(vec, VEC_SUBSET_OFF_PROC_ENTRIES, PETSC_TRUE).
15: Contributed-by: David Wells <drwells@email.unc.edu>
17: Petsc developers' notes: this test tests how Petsc knows it can reuse existing communication
18: pattern. All processes must come to the same conclusion, otherwise deadlock may happen due
19: to mismatched MPI_Send/Recv. It also tests changing VEC_SUBSET_OFF_PROC_ENTRIES back and forth.
20: */
21: int main(int argc, char **argv)
22: {
23: Vec v;
24: PetscInt i, j, k, *ln, n, rstart;
25: PetscBool saveCommunicationPattern = PETSC_FALSE;
26: PetscMPIInt size, rank, p;
28: PetscInitialize(&argc, &argv, NULL, help);
29: PetscOptionsGetBool(NULL, NULL, "-save_comm", &saveCommunicationPattern, NULL);
30: MPI_Comm_size(PETSC_COMM_WORLD, &size);
31: MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
33: PetscMalloc1(size, &ln);
34: /* This bug is triggered when one of the local lengths is small. Sometimes in IBAMR this value is actually zero. */
35: for (p=0; p<size; ++p) ln[p] = 10;
36: ln[0] = 2;
37: PetscPrintf(PETSC_COMM_WORLD, "local lengths are:\n");
38: PetscIntView(1, &ln[rank], PETSC_VIEWER_STDOUT_WORLD);
39: n = ln[rank];
40: VecCreateMPI(MPI_COMM_WORLD, n, PETSC_DECIDE, &v);
41: VecGetOwnershipRange(v, &rstart, NULL);
43: for (k=0; k<5; ++k) { /* 5 iterations of VecAssembly */
44: PetscReal norm = 0.0;
45: PetscBool flag = (k == 2) ? PETSC_FALSE : PETSC_TRUE;
46: PetscInt shift = (k < 2) ? 0 : (k == 2) ? 1 : 0; /* Used to change patterns */
48: /* If saveCommunicationPattern, let's see what should happen in the 5 iterations:
49: iter 0: flag is true, and this is the first assebmly, so petsc should keep the
50: communication pattern built during this assembly.
51: iter 1: flag is true, reuse the pattern.
52: iter 2: flag is false, discard/free the pattern built in iter 0; rebuild a new
53: pattern, but do not keep it after VecAssemblyEnd since the flag is false.
54: iter 3: flag is true again, this is the new first assembly with a true flag. So
55: petsc should keep the communication pattern built during this assembly.
56: iter 4: flag is true, reuse the pattern built in iter 3.
58: When the vector is destroyed, memory used by the pattern is freed. One can also do it early with a call
59: VecSetOption(v, VEC_SUBSET_OFF_PROC_ENTRIES, PETSC_FALSE);
60: */
61: if (saveCommunicationPattern) VecSetOption(v, VEC_SUBSET_OFF_PROC_ENTRIES, flag);
62: VecSet(v, 0.0);
64: for (i=0; i<n; ++i) {
65: PetscScalar val = 1.0;
66: PetscInt r = rstart + i;
68: VecSetValue(v, r, val, ADD_VALUES);
69: /* do assembly on all other processors too (the 'neighbors') */
70: {
71: const PetscMPIInt neighbor = (i+shift) % size; /* Adjust communication patterns between iterations */
72: const PetscInt nn = ln[neighbor];
73: PetscInt nrstart = 0;
75: for (p=0; p<neighbor; ++p) nrstart += ln[p];
76: for (j=0; j<nn/4; j+= 3) {
77: PetscScalar val = 0.01;
78: PetscInt nr = nrstart + j;
80: VecSetValue(v, nr, val, ADD_VALUES);
81: }
82: }
83: }
84: VecAssemblyBegin(v);
85: VecAssemblyEnd(v);
86: VecNorm(v, NORM_1, &norm);
87: PetscPrintf(PETSC_COMM_WORLD, "norm is %g\n", (double)norm);
88: }
89: PetscFree(ln);
90: VecDestroy(&v);
91: PetscFinalize();
92: return 0;
93: }
95: /*TEST
97: test:
98: suffix: 1
99: nsize: 4
100: test:
101: suffix: 1_save
102: args: -save_comm
103: nsize: 4
104: output_file: output/ex49_1.out
105: TEST*/