xref: /petsc/src/mat/tests/ex52.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
1 
2 static char help[] = "Tests various routines in MatMPIBAIJ format.\n";
3 
4 #include <petscmat.h>
5 
6 int main(int argc,char **args)
7 {
8   Mat            A;
9   PetscInt       m=2,bs=1,M,row,col,start,end,i,j,k;
10   PetscMPIInt    rank,size;
11   PetscScalar    data=100;
12   PetscBool      flg;
13 
14   PetscFunctionBeginUser;
15   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
16   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
17   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
18 
19   /* Test MatSetValues() and MatGetValues() */
20   PetscCall(PetscOptionsGetInt(NULL,NULL,"-mat_block_size",&bs,NULL));
21   PetscCall(PetscOptionsGetInt(NULL,NULL,"-mat_size",&m,NULL));
22 
23   M    = m*bs*size;
24   PetscCall(MatCreateBAIJ(PETSC_COMM_WORLD,bs,PETSC_DECIDE,PETSC_DECIDE,M,M,PETSC_DECIDE,NULL,PETSC_DECIDE,NULL,&A));
25 
26   PetscCall(MatGetOwnershipRange(A,&start,&end));
27   PetscCall(PetscOptionsHasName(NULL,NULL,"-column_oriented",&flg));
28   if (flg) PetscCall(MatSetOption(A,MAT_ROW_ORIENTED,PETSC_FALSE));
29 
30   /* inproc assembly */
31   for (row=start; row<end; row++) {
32     for (col=start; col<end; col++,data+=1) {
33       PetscCall(MatSetValues(A,1,&row,1,&col,&data,INSERT_VALUES));
34     }
35   }
36   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
37   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
38 
39   /* offproc assembly */
40   data = 5.0;
41   row  = (M+start-1)%M;
42   for (col=0; col<M; col++) {
43     PetscCall(MatSetValues(A,1,&row,1,&col,&data,ADD_VALUES));
44   }
45   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
46   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
47 
48   /* Test MatSetValuesBlocked() */
49   PetscCall(PetscOptionsHasName(NULL,NULL,"-test_setvaluesblocked",&flg));
50   if (flg) {
51     PetscScalar *bval;
52     row /= bs;
53     col  = start/bs;
54     PetscCall(PetscMalloc1(bs*bs,&bval));
55     k = 1;
56     /* row oriented - default */
57     for (i=0; i<bs; i++) {
58       for (j=0; j<bs; j++) {
59         bval[i*bs+j] = (PetscScalar)k; k++;
60       }
61     }
62     PetscCall(MatSetValuesBlocked(A,1,&row,1,&col,bval,INSERT_VALUES));
63     PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
64     PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
65     PetscCall(PetscFree(bval));
66   }
67 
68   PetscCall(MatView(A,PETSC_VIEWER_STDOUT_WORLD));
69   PetscCall(MatDestroy(&A));
70   PetscCall(PetscFinalize());
71   return 0;
72 }
73 
74 /*TEST
75 
76    test:
77       suffix: 1
78       nsize: 3
79       args: -mat_block_size 2 -test_setvaluesblocked
80 
81    test:
82       suffix: 2
83       nsize: 3
84       args: -mat_block_size 2 -test_setvaluesblocked -column_oriented
85 
86    test:
87       suffix: 3
88       nsize: 3
89       args: -mat_block_size 1 -test_setvaluesblocked
90 
91    test:
92       suffix: 4
93       nsize: 3
94       args: -mat_block_size 1 -test_setvaluesblocked -column_oriented
95 
96 TEST*/
97