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