Actual source code: gcreate.c
1: /*$Id: gcreate.c,v 1.131 2001/07/20 21:22:13 bsmith Exp $*/
3: #include src/mat/matimpl.h
4: #include petscsys.h
6: static int MatPublish_Base(PetscObject obj)
7: {
8: #if defined(PETSC_HAVE_AMS)
9: Mat mat = (Mat)obj;
11: #endif
14: #if defined(PETSC_HAVE_AMS)
15: /* if it is already published then return */
16: if (mat->amem >=0) return(0);
18: PetscObjectPublishBaseBegin(obj);
19: AMS_Memory_add_field((AMS_Memory)mat->amem,"globalsizes",&mat->M,2,AMS_INT,AMS_READ,
20: AMS_COMMON,AMS_REDUCT_UNDEF);
21: AMS_Memory_add_field((AMS_Memory)mat->amem,"localsizes",&mat->m,2,AMS_INT,AMS_READ,
22: AMS_DISTRIBUTED,AMS_REDUCT_UNDEF);
23: PetscObjectPublishBaseEnd(obj);
24: #endif
26: return(0);
27: }
30: /*@C
31: MatCreate - Creates a matrix where the type is determined
32: from either a call to MatSetType() or from the options database
33: with a call to MatSetFromOptions(). The default matrix type is
34: AIJ, using the routines MatCreateSeqAIJ() or MatCreateMPIAIJ()
35: if you do not set a type in the options database. If you never
36: call MatSetType() or MatSetFromOptions() it will generate an
37: eorror when you try to use the matrix.
39: Collective on MPI_Comm
41: Input Parameters:
42: + m - number of local rows (or PETSC_DECIDE)
43: . n - number of local columns (or PETSC_DECIDE)
44: . M - number of global rows (or PETSC_DETERMINE)
45: . N - number of global columns (or PETSC_DETERMINE)
46: - comm - MPI communicator
47:
48: Output Parameter:
49: . A - the matrix
51: Options Database Keys:
52: + -mat_type seqaij - AIJ type, uses MatCreateSeqAIJ()
53: . -mat_type mpiaij - AIJ type, uses MatCreateMPIAIJ()
54: . -mat_type seqbdiag - block diagonal type, uses MatCreateSeqBDiag()
55: . -mat_type mpibdiag - block diagonal type, uses MatCreateMPIBDiag()
56: . -mat_type mpirowbs - rowbs type, uses MatCreateMPIRowbs()
57: . -mat_type seqdense - dense type, uses MatCreateSeqDense()
58: . -mat_type mpidense - dense type, uses MatCreateMPIDense()
59: . -mat_type seqbaij - block AIJ type, uses MatCreateSeqBAIJ()
60: - -mat_type mpibaij - block AIJ type, uses MatCreateMPIBAIJ()
62: Even More Options Database Keys:
63: See the manpages for particular formats (e.g., MatCreateSeqAIJ())
64: for additional format-specific options.
66: Notes:
67: If PETSC_DECIDE is not used for the arguments 'm' and 'n', then the
68: user must ensure that they are chosen to be compatible with the
69: vectors. To do this, one first considers the matrix-vector product
70: 'y = A x'. The 'm' that is used in the above routine must match the
71: local size used in the vector creation routine VecCreateMPI() for 'y'.
72: Likewise, the 'n' used must match that used as the local size in
73: VecCreateMPI() for 'x'.
75: Level: beginner
77: .keywords: matrix, create
79: .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
80: MatCreateSeqBDiag(),MatCreateMPIBDiag(),
81: MatCreateSeqDense(), MatCreateMPIDense(),
82: MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
83: MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
84: MatConvert()
85: @*/
86: int MatCreate(MPI_Comm comm,int m,int n,int M,int N,Mat *A)
87: {
88: Mat B;
89: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
91: #endif
95: *A = PETSC_NULL;
96: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
97: MatInitializePackage(PETSC_NULL);
98: #endif
100: PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,0,"Mat",comm,MatDestroy,MatView);
101: PetscLogObjectCreate(B);
103: B->m = m;
104: B->n = n;
105: B->M = M;
106: B->N = N;
108: B->preallocated = PETSC_FALSE;
109: B->bops->publish = MatPublish_Base;
110: *A = B;
111: return(0);
112: }
114: /*@C
115: MatSetFromOptions - Creates a matrix where the type is determined
116: from the options database. Generates a parallel MPI matrix if the
117: communicator has more than one processor. The default matrix type is
118: AIJ, using the routines MatCreateSeqAIJ() and MatCreateMPIAIJ() if
119: you do not select a type in the options database.
121: Collective on Mat
123: Input Parameter:
124: . A - the matrix
126: Options Database Keys:
127: + -mat_type seqaij - AIJ type, uses MatCreateSeqAIJ()
128: . -mat_type mpiaij - AIJ type, uses MatCreateMPIAIJ()
129: . -mat_type seqbdiag - block diagonal type, uses MatCreateSeqBDiag()
130: . -mat_type mpibdiag - block diagonal type, uses MatCreateMPIBDiag()
131: . -mat_type mpirowbs - rowbs type, uses MatCreateMPIRowbs()
132: . -mat_type seqdense - dense type, uses MatCreateSeqDense()
133: . -mat_type mpidense - dense type, uses MatCreateMPIDense()
134: . -mat_type seqbaij - block AIJ type, uses MatCreateSeqBAIJ()
135: - -mat_type mpibaij - block AIJ type, uses MatCreateMPIBAIJ()
137: Even More Options Database Keys:
138: See the manpages for particular formats (e.g., MatCreateSeqAIJ())
139: for additional format-specific options.
141: Level: beginner
143: .keywords: matrix, create
145: .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
146: MatCreateSeqBDiag(),MatCreateMPIBDiag(),
147: MatCreateSeqDense(), MatCreateMPIDense(),
148: MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
149: MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
150: MatConvert()
151: @*/
152: int MatSetFromOptions(Mat B)
153: {
154: int ierr,size;
155: char mtype[256];
156: PetscTruth flg;
159: PetscOptionsGetString(B->prefix,"-mat_type",mtype,256,&flg);
160: if (flg) {
161: MatSetType(B,mtype);
162: }
163: if (!B->type_name) {
164: MPI_Comm_size(B->comm,&size);
165: if (size == 1) {
166: MatSetType(B,MATSEQAIJ);
167: } else {
168: MatSetType(B,MATMPIAIJ);
169: }
170: }
171: #if defined(__cplusplus) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_MATSINGLE) && defined(PETSC_HAVE_CXX_NAMESPACE)
172: MatESISetFromOptions(B);
173: #endif
174: return(0);
175: }
177: /*@C
178: MatSetUpPreallocation
180: Collective on Mat
182: Input Parameter:
183: . A - the matrix
185: Level: beginner
187: .keywords: matrix, create
189: .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(),
190: MatCreateSeqBDiag(),MatCreateMPIBDiag(),
191: MatCreateSeqDense(), MatCreateMPIDense(),
192: MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(),
193: MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(),
194: MatConvert()
195: @*/
196: int MatSetUpPreallocation(Mat B)
197: {
198: int ierr;
201: if (B->ops->setuppreallocation) {
202: PetscLogInfo(B,"MatSetUpPreallocation: Warning not preallocating matrix storage");
203: (*B->ops->setuppreallocation)(B);
204: B->ops->setuppreallocation = 0;
205: B->preallocated = PETSC_TRUE;
206: }
207: return(0);
208: }
210: /*
211: Copies from Cs header to A
212: */
213: int MatHeaderCopy(Mat A,Mat C)
214: {
215: int ierr,refct;
216: PetscOps *Abops;
217: MatOps Aops;
218: char *mtype,*mname;
221: /* free all the interior data structures from mat */
222: (*A->ops->destroy)(A);
224: PetscMapDestroy(A->rmap);
225: PetscMapDestroy(A->cmap);
227: /* save the parts of A we need */
228: Abops = A->bops;
229: Aops = A->ops;
230: refct = A->refct;
231: mtype = A->type_name;
232: mname = A->name;
234: /* copy C over to A */
235: ierr = PetscMemcpy(A,C,sizeof(struct _p_Mat));
237: /* return the parts of A we saved */
238: A->bops = Abops;
239: A->ops = Aops;
240: A->qlist = 0;
241: A->refct = refct;
242: A->type_name = mtype;
243: A->name = mname;
245: PetscLogObjectDestroy(C);
246: PetscHeaderDestroy(C);
247: return(0);
248: }