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;
15: PetscInitialize(&argc,&argv,(char*)0,help);
16: MPI_Comm_size(PETSC_COMM_WORLD,&size);
17: bs = size;
19: PetscOptionsGetInt(NULL,NULL,"-repeat",&repeat,NULL);
20: PetscOptionsGetBool(NULL,NULL,"-subset",&subset,NULL);
21: PetscOptionsGetBool(NULL,NULL,"-invert",&inv,NULL);
22: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
23: PetscOptionsGetInt(NULL,NULL,"-bs",&bs,NULL);
24: VecCreate(PETSC_COMM_WORLD,&x);
25: VecSetSizes(x,PETSC_DECIDE,n*bs);
26: VecSetBlockSize(x,bs);
27: VecSetFromOptions(x);
28: VecDuplicate(x,&y);
30: if (subset) 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: VecSetValues(x,1,&i,&val,INSERT_VALUES);
37: }
38: VecAssemblyBegin(x);
39: VecAssemblyEnd(x);
40: if (!r) VecCopy(x,y); /* Save result of first assembly */
41: }
43: VecView(x,PETSC_VIEWER_STDOUT_WORLD);
44: VecEqual(x,y,&flg);
45: if (!flg) PetscPrintf(PETSC_COMM_WORLD,"Vectors from repeat assembly do not match.");
47: /* Create a new vector because the old stash is a subset. */
48: VecDestroy(&x);
49: VecDuplicate(y,&x);
50: if (subset) VecSetOption(x,VEC_SUBSET_OFF_PROC_ENTRIES,PETSC_TRUE);
52: /* Now do the blocksetvalues */
53: VecSet(x,zero);
54: 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: VecSetValuesBlocked(x,1,&ii,vals,INSERT_VALUES);
62: }
63: VecAssemblyBegin(x);
64: VecAssemblyEnd(x);
65: if (!r) VecCopy(x,y); /* Save result of first assembly */
66: }
68: VecView(x,PETSC_VIEWER_STDOUT_WORLD);
69: VecEqual(x,y,&flg);
70: if (!flg) PetscPrintf(PETSC_COMM_WORLD,"Vectors from repeat block assembly do not match.");
72: VecDestroy(&x);
73: VecDestroy(&y);
74: PetscFree(vals);
75: 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*/