Actual source code: ex29.c
petsc-3.10.2 2018-10-09
2: static char help[] = "Tests VecSetValues() and VecSetValuesBlocked() on MPI vectors.\n\
3: Where atleast 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 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: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
23: VecCreate(PETSC_COMM_WORLD,&x);
24: VecSetSizes(x,PETSC_DECIDE,n*bs);
25: VecSetBlockSize(x,bs);
26: VecSetFromOptions(x);
27: VecDuplicate(x,&y);
29: if (subset) {VecSetOption(x,VEC_SUBSET_OFF_PROC_ENTRIES,PETSC_TRUE);}
31: for (r=0; r<repeat; r++) {
32: /* Assemble the full vector on the first and last iteration, otherwise don't set any values */
33: for (i=0; i<n*bs*(!r || !(repeat-1-r)); i++) {
34: val = i*1.0;
35: VecSetValues(x,1,&i,&val,INSERT_VALUES);
36: }
37: VecAssemblyBegin(x);
38: VecAssemblyEnd(x);
39: if (!r) {VecCopy(x,y);} /* Save result of first assembly */
40: }
42: VecView(x,PETSC_VIEWER_STDOUT_WORLD);
43: VecEqual(x,y,&flg);
44: if (!flg) {PetscPrintf(PETSC_COMM_WORLD,"Vectors from repeat assembly do not match.");}
46: /* Create a new vector because the old stash is a subset. */
47: VecDestroy(&x);
48: VecDuplicate(y,&x);
49: if (subset) {VecSetOption(x,VEC_SUBSET_OFF_PROC_ENTRIES,PETSC_TRUE);}
51: /* Now do the blocksetvalues */
52: VecSet(x,zero);
53: PetscMalloc1(bs,&vals);
54: for (r=0; r<repeat; r++) {
55: /* Assemble the full vector on the first and last iteration, otherwise don't set any values */
56: for (i=0; i<n*(!r || !(repeat-1-r)); i++) {
57: for (j=0; j<bs; j++) vals[j] = (i*bs+j)*1.0;
58: VecSetValuesBlocked(x,1,&i,vals,INSERT_VALUES);
59: }
60: VecAssemblyBegin(x);
61: VecAssemblyEnd(x);
62: if (!r) {VecCopy(x,y);} /* Save result of first assembly */
63: }
65: VecView(x,PETSC_VIEWER_STDOUT_WORLD);
66: VecEqual(x,y,&flg);
67: if (!flg) {PetscPrintf(PETSC_COMM_WORLD,"Vectors from repeat block assembly do not match.");}
69: VecDestroy(&x);
70: VecDestroy(&y);
71: PetscFree(vals);
72: PetscFinalize();
73: return ierr;
74: }
78: /*TEST
80: test:
81: nsize: 3
82: args: -n 126
84: test:
85: suffix: bts
86: nsize: 3
87: args: -n 126 -vec_assembly_legacy
88: output_file: output/ex29_1.out
90: test:
91: suffix: bts_2
92: nsize: 3
93: args: -n 126 -vec_assembly_legacy -repeat 2
94: output_file: output/ex29_1.out
96: test:
97: suffix: bts_2_subset
98: nsize: 3
99: args: -n 126 -vec_assembly_legacy -repeat 2 -subset
100: output_file: output/ex29_1.out
102: test:
103: suffix: bts_2_subset_proper
104: nsize: 3
105: args: -n 126 -vec_assembly_legacy -repeat 5 -subset
106: output_file: output/ex29_1.out
108: TEST*/