xref: /petsc/src/mat/utils/gcreate.c (revision f630a525398fe18bec863feadb1b5ff26330c5af)
1 #ifndef lint
2 static char vcid[] = "$Id: gcreate.c,v 1.83 1996/04/11 22:04:34 curfman Exp curfman $";
3 #endif
4 
5 #include "sys.h"
6 #include "mat.h"       /*I "mat.h"  I*/
7 
8 /*@C
9    MatGetTypeFromOptions - Determines from the options database what matrix
10    format the user has specified.
11 
12    Input Parameter:
13 .  comm - the MPI communicator
14 .  type - the type of matrix desired, for example MATSEQAIJ, MATMPIAIJ
15 .  pre - optional string to prepend to the name
16 
17    Output Parameters:
18 .  set - flag indicating whether user set matrix type option.
19 
20    Note:
21    This routine is automatically called within MatCreate().
22 
23 .keywords: matrix, get, format, from, options
24 
25 .seealso: MatCreate()
26 @*/
27 
28 int MatGetTypeFromOptions(MPI_Comm comm,char *pre,MatType *type,int *set)
29 {
30   int  size,flg1,flg2,flg3,flg4,flg5,flg8,flg9,flg10,flg12,flg13,ierr,flg11;
31   char p[64];
32 
33   PetscStrcpy(p,"-");
34   if (pre) PetscStrcat(p,pre);
35 
36   MPI_Comm_size(comm,&size);
37   ierr = OptionsHasName(PETSC_NULL,"-help",&flg1); CHKERRQ(ierr);
38   if (flg1) {
39     PetscPrintf(comm,"Matrix format options:\n");
40     PetscPrintf(comm,"  %smat_aij, %smat_seqaij, %smat_mpiaij\n",p,p,p);
41     PetscPrintf(comm,"  %smat_dense, %smat_seqdense, %smat_mpidense\n",p,p,p);
42     PetscPrintf(comm,"  %smat_mpirowbs, %smat_bdiag, %smat_seqbdiag, %smat_mpibdiag\n",p,p,p,p);
43   }
44   ierr = OptionsHasName(pre,"-mat_seqdense",&flg1); CHKERRQ(ierr);
45   ierr = OptionsHasName(pre,"-mat_mpidense",&flg2); CHKERRQ(ierr);
46   ierr = OptionsHasName(pre,"-mat_seqbdiag",&flg3); CHKERRQ(ierr);
47   ierr = OptionsHasName(pre,"-mat_mpibdiag",&flg4); CHKERRQ(ierr);
48   ierr = OptionsHasName(pre,"-mat_mpirowbs",&flg5); CHKERRQ(ierr);
49   ierr = OptionsHasName(pre,"-mat_seqbaij",&flg11); CHKERRQ(ierr);
50   ierr = OptionsHasName(pre,"-mat_mpiaij",&flg8); CHKERRQ(ierr);
51   ierr = OptionsHasName(pre,"-mat_seqaij",&flg9); CHKERRQ(ierr);
52   ierr = OptionsHasName(pre,"-mat_aij",&flg10); CHKERRQ(ierr);
53   ierr = OptionsHasName(pre,"-mat_bdiag",&flg12); CHKERRQ(ierr);
54   ierr = OptionsHasName(pre,"-mat_dense",&flg13); CHKERRQ(ierr);
55   if (flg1) {
56     *type = MATSEQDENSE;
57     *set = 1;
58   }
59   else if (flg2) {
60     *type = MATMPIDENSE;
61     *set = 1;
62   }
63   else if (flg3) {
64     *type = MATSEQBDIAG;
65     *set = 1;
66   }
67   else if (flg4) {
68     *type = MATMPIBDIAG;
69     *set = 1;
70   }
71   else if (flg5) {
72     *type = MATMPIROWBS;
73     *set = 1;
74   }
75   else if (flg8) {
76     *type = MATMPIAIJ;
77     *set = 1;
78   }
79   else if (flg9){
80     *type = MATSEQAIJ;
81     *set = 1;
82   }
83   else if (flg10){
84     if (size == 1) *type = MATSEQAIJ;
85     else *type = MATMPIAIJ;
86     *set = 1;
87   }
88   else if (flg11){
89     *type = MATSEQBAIJ;
90     *set = 1;
91   }
92   else if (flg12){
93     if (size == 1) *type = MATSEQBDIAG;
94     else *type = MATMPIBDIAG;
95     *set = 1;
96   }
97   else if (flg13){
98     if (size == 1) *type = MATSEQDENSE;
99     else *type = MATMPIDENSE;
100     *set = 1;
101   }
102   else {
103     if (size == 1) *type = MATSEQAIJ;
104     else *type = MATMPIAIJ;
105     *set = 0;
106   }
107   return 0;
108 }
109 
110 /*@C
111    MatCreate - Creates a matrix, where the type is determined
112    from the options database. Generates a parallel MPI matrix if the
113    communicator has more than one processor.
114 
115    Input Parameters:
116 .  m - number of global rows
117 .  n - number of global columns
118 .  comm - MPI communicator
119 
120    Output Parameter:
121 .  A - the matrix
122 
123    Basic Options Database Keys:
124    These options use MatCreateSeqXXX or MatCreateMPIXXX,
125    depending on the communicator, comm.
126 $    -mat_aij      : AIJ type
127 $    -mat_dense    : dense type
128 $    -mat_bdiag    : block diagonal type
129 
130    More Options Database Keys:
131 $    -mat_seqaij   : AIJ type, uses MatCreateSeqAIJ
132 $    -mat_mpiaij   : AIJ type, uses MatCreateMPIAIJ
133 $    -mat_seqbdiag : block diagonal type, uses
134 $                    MatCreateSeqBDiag()
135 $    -mat_mpibdiag : block diagonal type, uses
136 $                    MatCreateMPIBDiag()
137 $    -mat_mpirowbs : rowbs type, uses MatCreateMPIRowbs()
138 $    -mat_seqdense : dense type, uses MatCreateSeqDense()
139 $    -mat_mpidense : dense type, uses MatCreateMPIDense()
140 $    -mat_seqbaij  : block AIJ type, uses MatCreateSeqBAIJ()
141 
142    Notes:
143    The default matrix type is AIJ, using MatCreateSeqAIJ() and
144    MatCreateMPIAIJ().
145 
146 .keywords: matrix, create
147 
148 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
149           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
150           MatCreateSeqDense(), MatCreateMPIDense(),
151           MatCreateMPIRowbs(), MatCreateSeqBAIJ,
152           MatConvert(), MatGetTypeFromOptions()
153  @*/
154 int MatCreate(MPI_Comm comm,int m,int n,Mat *A)
155 {
156   MatType type;
157   int     set, ierr, bs=1, flg;
158 
159   ierr = MatGetTypeFromOptions(comm,0,&type,&set); CHKERRQ(ierr);
160   switch (type) {
161   case MATSEQDENSE:
162     ierr = MatCreateSeqDense(comm,m,n,PETSC_NULL,A); CHKERRQ(ierr);
163     break;
164   case MATMPIBDIAG:
165     ierr = MatCreateMPIBDiag(comm,PETSC_DECIDE,m,n,PETSC_DEFAULT,PETSC_DEFAULT,
166            PETSC_NULL,PETSC_NULL,A); CHKERRQ(ierr);
167     break;
168   case MATSEQBDIAG:
169     ierr = MatCreateSeqBDiag(comm,m,n,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_NULL,
170            PETSC_NULL,A); CHKERRQ(ierr);
171     break;
172   case MATMPIROWBS:
173     ierr = MatCreateMPIRowbs(comm,PETSC_DECIDE,m,PETSC_DEFAULT,PETSC_NULL,
174            PETSC_NULL,A); CHKERRQ(ierr);
175     break;
176   case MATMPIDENSE:
177     ierr = MatCreateMPIDense(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_NULL,A); CHKERRQ(ierr);
178     break;
179   case MATMPIAIJ:
180     ierr = MatCreateMPIAIJ(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_DEFAULT,
181            PETSC_NULL,PETSC_DEFAULT,PETSC_NULL,A); CHKERRQ(ierr);
182     break;
183   case MATSEQBAIJ:
184     ierr = OptionsGetInt(PETSC_NULL,"-mat_block_size",&bs,&flg); CHKERRQ(ierr);
185     ierr = MatCreateSeqBAIJ(comm,bs,m,n,PETSC_DEFAULT,PETSC_NULL,A); CHKERRQ(ierr);
186     break;
187   default:
188     ierr = MatCreateSeqAIJ(comm,m,n,PETSC_DEFAULT,PETSC_NULL,A); CHKERRQ(ierr);
189     break;
190   }
191   return 0;
192 }
193 
194 #include "matimpl.h"
195 /*@C
196    MatGetType - Gets the matrix type and name (as a string) from the matrix.
197 
198    Input Parameter:
199 .  mat - the matrix
200 
201    Output Parameter:
202 .  type - the matrix type (or use PETSC_NULL)
203 .  name - name of matrix type (or use PETSC_NULL)
204 
205 .keywords: matrix, get, type, name
206 @*/
207 int MatGetType(Mat mat,MatType *type,char **name)
208 {
209   int  itype = (int)mat->type;
210   char *matname[10];
211 
212   if (type) *type = (MatType) mat->type;
213   if (name) {
214     /* Note:  Be sure that this list corresponds to the enum in mat.h */
215     matname[0] = "MATSEQDENSE";
216     matname[1] = "MATSEQAIJ";
217     matname[2] = "MATMPIAIJ";
218     matname[3] = "MATSHELL";
219     matname[4] = "MATMPIROWBS";
220     matname[5] = "MATSEQBDIAG";
221     matname[6] = "MATMPIBDIAG";
222     matname[7] = "MATMPIDENSE";
223     if (itype < 0 || itype > 7) *name = "Unknown matrix type";
224     else                        *name = matname[itype];
225   }
226   return 0;
227 }
228 
229 
230 
231