Actual source code: ex29.c
petsc-3.12.2 2019-11-22
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: {
10: PetscMPIInt size;
11: PetscInt i,j,r,n = 50,repeat = 1,bs;
12: PetscScalar val,*vals,zero=0.0;
13: PetscBool inv = PETSC_FALSE, subset = PETSC_FALSE,flg;
14: Vec x,y;
16: PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
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 ierr;
78: }
82: /*TEST
84: test:
85: nsize: 3
86: args: -n 126
88: test:
89: suffix: bts_test_inv_error
90: nsize: 3
91: args: -n 4 -invert -bs 2
92: output_file: output/ex29_test_inv_error.out
94: test:
95: suffix: bts
96: nsize: 3
97: args: -n 126 -vec_assembly_legacy
98: output_file: output/ex29_1.out
100: test:
101: suffix: bts_2
102: nsize: 3
103: args: -n 126 -vec_assembly_legacy -repeat 2
104: output_file: output/ex29_1.out
106: test:
107: suffix: bts_2_subset
108: nsize: 3
109: args: -n 126 -vec_assembly_legacy -repeat 2 -subset
110: output_file: output/ex29_1.out
112: test:
113: suffix: bts_2_subset_proper
114: nsize: 3
115: args: -n 126 -vec_assembly_legacy -repeat 5 -subset
116: output_file: output/ex29_1.out
118: TEST*/