xref: /petsc/src/mat/utils/gcreate.c (revision 4e627d3a034d8dbe6c0b444d105a274c665112f3)
1 #ifndef lint
2 static char vcid[] = "$Id: gcreate.c,v 1.80 1996/04/09 14:41:37 balay Exp balay $";
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 .  V - location to stash resulting matrix
122 
123    Options Database Keywords:
124 $  -mat_seqaij   : AIJ type, uses MatCreateSeqAIJ
125 $  -mat_mpiaij   : AIJ type, uses MatCreateMPIAIJ
126 $  -mat_aij      : AIJ type, (Seq or MPI depending on comm)
127 $  -mat_seqbdiag : block diagonal type, uses
128 $                  MatCreateSeqBDiag()
129 $  -mat_mpibdiag : block diagonal type, uses
130 $                  MatCreateMPIBDiag()
131 $  -mat_bdiag    : block diagonal type,
132 $                  (Seq or MPI depending on comm)
133 $  -mat_mpirowbs : rowbs type, uses MatCreateMPIRowbs()
134 $  -mat_dense    : dense type, (Seq or MPI depending on comm)
135 $  -mat_seqdense : dense type, uses MatCreateSeqDense()
136 $  -mat_mpidense : dense type, uses MatCreateMPIDense()
137 $  -mat_seqbaij  : Block AIJ type, uses MatCreateSeaBAIJ
138 
139    Notes:
140    The default matrix type is AIJ, using MatCreateSeqAIJ() and
141    MatCreateMPIAIJ().
142 
143 .keywords: matrix, create, initial
144 
145 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
146           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
147           MatCreateSeqDense(), MatCreateMPIDense(),
148           MatCreateMPIRowbs(), MatCreateSeqBAIJ,
149           MatConvert(), MatGetTypeFromOptions()
150  @*/
151 int MatCreate(MPI_Comm comm,int m,int n,Mat *V)
152 {
153   MatType type;
154   int     set, ierr, bs=1, flg;
155 
156   ierr = MatGetTypeFromOptions(comm,0,&type,&set); CHKERRQ(ierr);
157   switch (type) {
158   case MATSEQDENSE:
159     ierr = MatCreateSeqDense(comm,m,n,PETSC_NULL,V); CHKERRQ(ierr);
160     break;
161   case MATMPIBDIAG:
162     ierr = MatCreateMPIBDiag(comm,PETSC_DECIDE,m,n,PETSC_DEFAULT,PETSC_DEFAULT,
163            PETSC_NULL,PETSC_NULL,V); CHKERRQ(ierr);
164     break;
165   case MATSEQBDIAG:
166     ierr = MatCreateSeqBDiag(comm,m,n,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_NULL,
167            PETSC_NULL,V); CHKERRQ(ierr);
168     break;
169   case MATMPIROWBS:
170     ierr = MatCreateMPIRowbs(comm,PETSC_DECIDE,m,PETSC_DEFAULT,PETSC_NULL,
171            PETSC_NULL,V); CHKERRQ(ierr);
172     break;
173   case MATMPIDENSE:
174     ierr = MatCreateMPIDense(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_NULL,V); CHKERRQ(ierr);
175     break;
176   case MATMPIAIJ:
177     ierr = MatCreateMPIAIJ(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_DEFAULT,
178            PETSC_NULL,PETSC_DEFAULT,PETSC_NULL,V); CHKERRQ(ierr);
179     break;
180   case MATSEQBAIJ:
181     ierr = OptionsGetInt(PETSC_NULL,"-mat_block_size",&bs,&flg); CHKERRQ(ierr);
182     ierr = MatCreateSeqBAIJ(comm,bs,m,n,PETSC_DEFAULT,PETSC_NULL,V); CHKERRQ(ierr);
183     break;
184   default:
185     ierr = MatCreateSeqAIJ(comm,m,n,PETSC_DEFAULT,PETSC_NULL,V); CHKERRQ(ierr);
186   break;
187   }
188   return 0;
189 }
190 
191 #include "matimpl.h"
192 /*@C
193    MatGetType - Gets the matrix type and name (as a string) from the matrix.
194 
195    Input Parameter:
196 .  mat - the matrix
197 
198    Output Parameter:
199 .  type - the matrix type (or use PETSC_NULL)
200 .  name - name of matrix type (or use PETSC_NULL)
201 
202 .keywords: matrix, get, type, name
203 @*/
204 int MatGetType(Mat mat,MatType *type,char **name)
205 {
206   int  itype = (int)mat->type;
207   char *matname[10];
208 
209   if (type) *type = (MatType) mat->type;
210   if (name) {
211     /* Note:  Be sure that this list corresponds to the enum in mat.h */
212     matname[0] = "MATSEQDENSE";
213     matname[1] = "MATSEQAIJ";
214     matname[2] = "MATMPIAIJ";
215     matname[3] = "MATSHELL";
216     matname[4] = "MATMPIROWBS";
217     matname[5] = "MATSEQBDIAG";
218     matname[6] = "MATMPIBDIAG";
219     matname[7] = "MATMPIDENSE";
220     if (itype < 0 || itype > 7) *name = "Unknown matrix type";
221     else                        *name = matname[itype];
222   }
223   return 0;
224 }
225 
226 
227 
228