xref: /petsc/src/mat/tests/ex156.c (revision 7887b771373103dc739b0b611c31b90938b2841a)
1*7887b771SAlex Lindsay static char help[] = "Tests resetting preallocation after filling the full sparsity pattern";
2*7887b771SAlex Lindsay 
3*7887b771SAlex Lindsay #include <petscmat.h>
4*7887b771SAlex Lindsay 
5*7887b771SAlex Lindsay PetscErrorCode Assemble(Mat mat)
6*7887b771SAlex Lindsay {
7*7887b771SAlex Lindsay   PetscInt    idx[4], i;
8*7887b771SAlex Lindsay   PetscScalar vals[16];
9*7887b771SAlex Lindsay   int         rank;
10*7887b771SAlex Lindsay 
11*7887b771SAlex Lindsay   PetscFunctionBegin;
12*7887b771SAlex Lindsay   MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
13*7887b771SAlex Lindsay   for (i = 0; i < 16; ++i) vals[i] = 1;
14*7887b771SAlex Lindsay   if (rank == 0) {
15*7887b771SAlex Lindsay     // element 0
16*7887b771SAlex Lindsay     idx[0] = 0;
17*7887b771SAlex Lindsay     idx[1] = 1;
18*7887b771SAlex Lindsay     idx[2] = 2;
19*7887b771SAlex Lindsay     idx[3] = 3;
20*7887b771SAlex Lindsay     PetscCall(MatSetValues(mat, 4, idx, 4, idx, vals, ADD_VALUES));
21*7887b771SAlex Lindsay     // element 1
22*7887b771SAlex Lindsay     idx[0] = 3;
23*7887b771SAlex Lindsay     idx[1] = 2;
24*7887b771SAlex Lindsay     idx[2] = 4;
25*7887b771SAlex Lindsay     idx[3] = 5;
26*7887b771SAlex Lindsay     PetscCall(MatSetValues(mat, 4, idx, 4, idx, vals, ADD_VALUES));
27*7887b771SAlex Lindsay   } else {
28*7887b771SAlex Lindsay     // element 2
29*7887b771SAlex Lindsay     idx[0] = 6;
30*7887b771SAlex Lindsay     idx[1] = 0;
31*7887b771SAlex Lindsay     idx[2] = 3;
32*7887b771SAlex Lindsay     idx[3] = 7;
33*7887b771SAlex Lindsay     PetscCall(MatSetValues(mat, 4, idx, 4, idx, vals, ADD_VALUES));
34*7887b771SAlex Lindsay     // element 3
35*7887b771SAlex Lindsay     idx[0] = 7;
36*7887b771SAlex Lindsay     idx[1] = 3;
37*7887b771SAlex Lindsay     idx[2] = 5;
38*7887b771SAlex Lindsay     idx[3] = 8;
39*7887b771SAlex Lindsay     PetscCall(MatSetValues(mat, 4, idx, 4, idx, vals, ADD_VALUES));
40*7887b771SAlex Lindsay   }
41*7887b771SAlex Lindsay   PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
42*7887b771SAlex Lindsay   PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
43*7887b771SAlex Lindsay   PetscFunctionReturn(PETSC_SUCCESS);
44*7887b771SAlex Lindsay }
45*7887b771SAlex Lindsay 
46*7887b771SAlex Lindsay int main(int argc, char **argv)
47*7887b771SAlex Lindsay {
48*7887b771SAlex Lindsay   Mat mat;
49*7887b771SAlex Lindsay   int rank;
50*7887b771SAlex Lindsay 
51*7887b771SAlex Lindsay   PetscFunctionBeginUser;
52*7887b771SAlex Lindsay   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
53*7887b771SAlex Lindsay   MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
54*7887b771SAlex Lindsay   PetscCall(MatCreate(PETSC_COMM_WORLD, &mat));
55*7887b771SAlex Lindsay   if (rank == 0) PetscCall(MatSetSizes(mat, 6, 6, PETSC_DETERMINE, PETSC_DETERMINE));
56*7887b771SAlex Lindsay   else PetscCall(MatSetSizes(mat, 3, 3, PETSC_DETERMINE, PETSC_DETERMINE));
57*7887b771SAlex Lindsay   PetscCall(MatSetFromOptions(mat));
58*7887b771SAlex Lindsay   if (rank == 0) {
59*7887b771SAlex Lindsay     PetscInt ndz[6], noz[6];
60*7887b771SAlex Lindsay     ndz[0] = 4;
61*7887b771SAlex Lindsay     noz[0] = 2;
62*7887b771SAlex Lindsay     ndz[1] = 4;
63*7887b771SAlex Lindsay     noz[1] = 0;
64*7887b771SAlex Lindsay     ndz[2] = 6;
65*7887b771SAlex Lindsay     noz[2] = 0;
66*7887b771SAlex Lindsay     ndz[3] = 6;
67*7887b771SAlex Lindsay     noz[3] = 3;
68*7887b771SAlex Lindsay     ndz[4] = 4;
69*7887b771SAlex Lindsay     noz[4] = 0;
70*7887b771SAlex Lindsay     ndz[5] = 4;
71*7887b771SAlex Lindsay     noz[5] = 2;
72*7887b771SAlex Lindsay     PetscCall(MatMPIAIJSetPreallocation(mat, 0, ndz, 0, noz));
73*7887b771SAlex Lindsay   } else {
74*7887b771SAlex Lindsay     PetscInt ndz[3], noz[3];
75*7887b771SAlex Lindsay     ndz[0] = 2;
76*7887b771SAlex Lindsay     noz[0] = 2;
77*7887b771SAlex Lindsay     ndz[1] = 3;
78*7887b771SAlex Lindsay     noz[1] = 3;
79*7887b771SAlex Lindsay     ndz[2] = 2;
80*7887b771SAlex Lindsay     noz[2] = 2;
81*7887b771SAlex Lindsay     PetscCall(MatMPIAIJSetPreallocation(mat, 0, ndz, 0, noz));
82*7887b771SAlex Lindsay   }
83*7887b771SAlex Lindsay   PetscCall(MatSetUp(mat));
84*7887b771SAlex Lindsay   PetscCall(Assemble(mat));
85*7887b771SAlex Lindsay   PetscCall(MatView(mat, NULL));
86*7887b771SAlex Lindsay   PetscCall(MatResetPreallocation(mat));
87*7887b771SAlex Lindsay   PetscCall(Assemble(mat));
88*7887b771SAlex Lindsay   PetscCall(MatView(mat, NULL));
89*7887b771SAlex Lindsay   PetscCall(MatDestroy(&mat));
90*7887b771SAlex Lindsay   PetscCall(PetscFinalize());
91*7887b771SAlex Lindsay   return 0;
92*7887b771SAlex Lindsay }
93*7887b771SAlex Lindsay 
94*7887b771SAlex Lindsay /*TEST
95*7887b771SAlex Lindsay 
96*7887b771SAlex Lindsay    test:
97*7887b771SAlex Lindsay       suffix: 1
98*7887b771SAlex Lindsay       nsize: 2
99*7887b771SAlex Lindsay 
100*7887b771SAlex Lindsay TEST*/
101