Actual source code: ex16.c
petsc-3.12.2 2019-11-22
1: static char help[] = "Tests VecSetValuesBlocked() on MPI vectors.\n\n";
3: #include <petscvec.h>
5: int main(int argc,char **argv)
6: {
8: PetscMPIInt size,rank;
9: PetscInt i,n = 8,bs = 2,indices[2];
10: PetscScalar values[4];
11: Vec x;
13: PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
14: MPI_Comm_size(PETSC_COMM_WORLD,&size);
15: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
17: if (size != 2) SETERRQ(PETSC_COMM_SELF,1,"Must be run with two processors");
19: /* create vector */
20: VecCreate(PETSC_COMM_WORLD,&x);
21: VecSetSizes(x,PETSC_DECIDE,n);
22: VecSetBlockSize(x,bs);
23: VecSetFromOptions(x);
25: if (!rank) {
26: for (i=0; i<4; i++) values[i] = i+1;
27: indices[0] = 0;
28: indices[1] = 2;
29: VecSetValuesBlocked(x,2,indices,values,INSERT_VALUES);
30: }
31: VecAssemblyBegin(x);
32: VecAssemblyEnd(x);
34: /*
35: Resulting vector should be 1 2 0 0 3 4 0 0
36: */
37: VecView(x,PETSC_VIEWER_STDOUT_WORLD);
39: /* test insertion with negative indices */
40: VecSetOption(x,VEC_IGNORE_NEGATIVE_INDICES,PETSC_TRUE);
41: if (!rank) {
42: for (i=0; i<4; i++) values[i] = -(i+1);
43: indices[0] = -1;
44: indices[1] = 3;
45: VecSetValuesBlocked(x,2,indices,values,INSERT_VALUES);
46: }
47: VecAssemblyBegin(x);
48: VecAssemblyEnd(x);
50: /*
51: Resulting vector should be 1 2 0 0 3 4 -3 -4
52: */
53: VecView(x,PETSC_VIEWER_STDOUT_WORLD);
55: VecDestroy(&x);
57: PetscFinalize();
58: return ierr;
59: }
63: /*TEST
65: test:
66: nsize: 2
68: TEST*/