xref: /petsc/src/mat/utils/gcreate.c (revision af6b99e9b189e8e8dab39012dffe0bbbf03b23b9)
1 #ifndef lint
2 static char vcid[] = "$Id: gcreate.c,v 1.72 1996/03/19 21:27:41 bsmith 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 
138    Notes:
139    The default matrix type is AIJ, using MatCreateSeqAIJ() and
140    MatCreateMPIAIJ().
141 
142 .keywords: matrix, create, initial
143 
144 .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
145           MatCreateSeqBDiag(),MatCreateMPIBDiag(),
146           MatCreateSeqDense(), MatCreateMPIDense(),
147           MatCreateMPIRowbs(), MatConvert()
148           MatGetTypeFromOptions()
149  @*/
150 int MatCreate(MPI_Comm comm,int m,int n,Mat *V)
151 {
152   MatType type;
153   int     set,ierr,bs=1,flg;
154 
155   ierr = MatGetTypeFromOptions(comm,0,&type,&set); CHKERRQ(ierr);
156   if (type == MATSEQDENSE)
157     return MatCreateSeqDense(comm,m,n,PETSC_NULL,V);
158   if (type == MATMPIBDIAG)
159     return MatCreateMPIBDiag(comm,PETSC_DECIDE,m,n,PETSC_DEFAULT,PETSC_DEFAULT,
160            PETSC_NULL,PETSC_NULL,V);
161   if (type == MATSEQBDIAG)
162     return MatCreateSeqBDiag(comm,m,n,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_NULL,
163            PETSC_NULL,V);
164   if (type == MATMPIROWBS)
165     return MatCreateMPIRowbs(comm,PETSC_DECIDE,m,PETSC_DEFAULT,PETSC_NULL,PETSC_NULL,V);
166   if (type == MATMPIDENSE)
167     return MatCreateMPIDense(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_NULL,V);
168   if (type == MATMPIAIJ)
169     return MatCreateMPIAIJ(comm,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_DEFAULT,
170            PETSC_NULL,PETSC_DEFAULT,PETSC_NULL,V);
171   if (type == MATSEQBAIJ){
172     ierr = OptionsGetInt(PETSC_NULL,"-mat_block_size",&bs,&flg);CHKERRQ(ierr);
173     return MatCreateSeqBAIJ(comm,bs,m,n,PETSC_DEFAULT,PETSC_NULL,V);
174   }
175   return MatCreateSeqAIJ(comm,m,n,PETSC_DEFAULT,PETSC_NULL,V);
176 }
177 
178 #include "matimpl.h"
179 /*@C
180    MatGetType - Gets the matrix type and name (as a string) from the matrix.
181 
182    Input Parameter:
183 .  mat - the matrix
184 
185    Output Parameter:
186 .  type - the matrix type (or use PETSC_NULL)
187 .  name - name of matrix type (or use PETSC_NULL)
188 
189 .keywords: matrix, get, type, name
190 @*/
191 int MatGetType(Mat mat,MatType *type,char **name)
192 {
193   int  itype = (int)mat->type;
194   char *matname[10];
195 
196   if (type) *type = (MatType) mat->type;
197   if (name) {
198     /* Note:  Be sure that this list corresponds to the enum in mat.h */
199     matname[0] = "MATSEQDENSE";
200     matname[1] = "MATSEQAIJ";
201     matname[2] = "MATMPIAIJ";
202     matname[3] = "MATSHELL";
203     matname[4] = "MATMPIROWBS";
204     matname[5] = "MATSEQBDIAG";
205     matname[6] = "MATMPIBDIAG";
206     matname[7] = "MATMPIDENSE";
207     if (itype < 0 || itype > 7) *name = "Unknown matrix type";
208     else                        *name = matname[itype];
209   }
210   return 0;
211 }
212 
213 
214 
215