xref: /petsc/src/mat/utils/gcreate.c (revision 9ae297151d0c16d3b63ede8f29d1d0a9ba134875)
17807a1faSBarry Smith 
2af0996ceSBarry Smith #include <petsc/private/matimpl.h>       /*I "petscmat.h"  I*/
37807a1faSBarry Smith 
44a2ae208SSatish Balay #undef __FUNCT__
57d68702bSBarry Smith #define __FUNCT__ "MatShift_Basic"
6db87b0f2SBarry Smith PETSC_INTERN PetscErrorCode MatShift_Basic(Mat Y,PetscScalar a)
77d68702bSBarry Smith {
87d68702bSBarry Smith   PetscErrorCode ierr;
97d68702bSBarry Smith   PetscInt       i,start,end;
107d68702bSBarry Smith   PetscScalar    alpha = a;
117d68702bSBarry Smith   PetscBool      prevoption;
127d68702bSBarry Smith 
137d68702bSBarry Smith   PetscFunctionBegin;
147d68702bSBarry Smith   ierr = MatGetOption(Y,MAT_NO_OFF_PROC_ENTRIES,&prevoption);CHKERRQ(ierr);
157d68702bSBarry Smith   ierr = MatSetOption(Y,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE);CHKERRQ(ierr);
167d68702bSBarry Smith   ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr);
177d68702bSBarry Smith   for (i=start; i<end; i++) {
187d68702bSBarry Smith     ierr = MatSetValues(Y,1,&i,1,&i,&alpha,ADD_VALUES);CHKERRQ(ierr);
197d68702bSBarry Smith   }
207d68702bSBarry Smith   ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
217d68702bSBarry Smith   ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
227d68702bSBarry Smith   ierr = MatSetOption(Y,MAT_NO_OFF_PROC_ENTRIES,prevoption);CHKERRQ(ierr);
237d68702bSBarry Smith   PetscFunctionReturn(0);
247d68702bSBarry Smith }
257d68702bSBarry Smith 
267d68702bSBarry Smith #undef __FUNCT__
274a2ae208SSatish Balay #define __FUNCT__ "MatCreate"
2805869f15SSatish Balay /*@
2969dd0797SLois Curfman McInnes    MatCreate - Creates a matrix where the type is determined
307e5f4302SBarry Smith    from either a call to MatSetType() or from the options database
317e5f4302SBarry Smith    with a call to MatSetFromOptions(). The default matrix type is
3269b1f4b7SBarry Smith    AIJ, using the routines MatCreateSeqAIJ() or MatCreateAIJ()
337e5f4302SBarry Smith    if you do not set a type in the options database. If you never
347e5f4302SBarry Smith    call MatSetType() or MatSetFromOptions() it will generate an
35f8ab6608SSatish Balay    error when you try to use the matrix.
3683e1b59cSLois Curfman McInnes 
37cb13003dSBarry Smith    Collective on MPI_Comm
38cb13003dSBarry Smith 
39f69a0ea3SMatthew Knepley    Input Parameter:
40f69a0ea3SMatthew Knepley .  comm - MPI communicator
417807a1faSBarry Smith 
427807a1faSBarry Smith    Output Parameter:
43dc401e71SLois Curfman McInnes .  A - the matrix
44e0b365e2SLois Curfman McInnes 
45273d9f13SBarry Smith    Options Database Keys:
46273d9f13SBarry Smith +    -mat_type seqaij   - AIJ type, uses MatCreateSeqAIJ()
4769b1f4b7SBarry Smith .    -mat_type mpiaij   - AIJ type, uses MatCreateAIJ()
48273d9f13SBarry Smith .    -mat_type seqdense - dense type, uses MatCreateSeqDense()
4969b1f4b7SBarry Smith .    -mat_type mpidense - dense type, uses MatCreateDense()
50273d9f13SBarry Smith .    -mat_type seqbaij  - block AIJ type, uses MatCreateSeqBAIJ()
5169b1f4b7SBarry Smith -    -mat_type mpibaij  - block AIJ type, uses MatCreateBAIJ()
52e0b365e2SLois Curfman McInnes 
5383e1b59cSLois Curfman McInnes    Even More Options Database Keys:
5483e1b59cSLois Curfman McInnes    See the manpages for particular formats (e.g., MatCreateSeqAIJ())
5583e1b59cSLois Curfman McInnes    for additional format-specific options.
56e0b365e2SLois Curfman McInnes 
57bd9ce289SLois Curfman McInnes    Notes:
58ec6e0d80SSatish Balay 
59273d9f13SBarry Smith    Level: beginner
60273d9f13SBarry Smith 
61a2fc510eSBarry Smith    User manual sections:
62a2fc510eSBarry Smith +   sec_matcreate
63a2fc510eSBarry Smith -   chapter_matrices
64a2fc510eSBarry Smith 
65273d9f13SBarry Smith .keywords: matrix, create
66273d9f13SBarry Smith 
6769b1f4b7SBarry Smith .seealso: MatCreateSeqAIJ(), MatCreateAIJ(),
6869b1f4b7SBarry Smith           MatCreateSeqDense(), MatCreateDense(),
6969b1f4b7SBarry Smith           MatCreateSeqBAIJ(), MatCreateBAIJ(),
7069b1f4b7SBarry Smith           MatCreateSeqSBAIJ(), MatCreateSBAIJ(),
71273d9f13SBarry Smith           MatConvert()
72273d9f13SBarry Smith @*/
737087cfbeSBarry Smith PetscErrorCode  MatCreate(MPI_Comm comm,Mat *A)
74273d9f13SBarry Smith {
75273d9f13SBarry Smith   Mat            B;
76dfbe8321SBarry Smith   PetscErrorCode ierr;
77273d9f13SBarry Smith 
78273d9f13SBarry Smith   PetscFunctionBegin;
79f69a0ea3SMatthew Knepley   PetscValidPointer(A,2);
8097f1f81fSBarry Smith 
810298fd71SBarry Smith   *A = NULL;
82607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
838ba1e511SMatthew Knepley 
8473107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(B,MAT_CLASSID,"Mat","Matrix","Mat",comm,MatDestroy,MatView);CHKERRQ(ierr);
8526283091SBarry Smith   ierr = PetscLayoutCreate(comm,&B->rmap);CHKERRQ(ierr);
8626283091SBarry Smith   ierr = PetscLayoutCreate(comm,&B->cmap);CHKERRQ(ierr);
8726fbe8dcSKarl Rupp 
88*9ae29715SStefano Zampini   B->congruentlayouts = -1;
89273d9f13SBarry Smith   B->preallocated     = PETSC_FALSE;
90273d9f13SBarry Smith   *A                  = B;
91273d9f13SBarry Smith   PetscFunctionReturn(0);
92273d9f13SBarry Smith }
93273d9f13SBarry Smith 
944a2ae208SSatish Balay #undef __FUNCT__
9584d44b13SHong Zhang #define __FUNCT__ "MatSetErrorIfFailure"
96422a814eSBarry Smith /*@
9784d44b13SHong Zhang    MatSetErrorIfFailure - Causes Mat to generate an error, for example a zero pivot, is detected.
98422a814eSBarry Smith 
99422a814eSBarry Smith    Logically Collective on Mat
100422a814eSBarry Smith 
101422a814eSBarry Smith    Input Parameters:
102422a814eSBarry Smith +  mat -  matrix obtained from MatCreate()
103422a814eSBarry Smith -  flg - PETSC_TRUE indicates you want the error generated
104422a814eSBarry Smith 
105422a814eSBarry Smith    Level: advanced
106422a814eSBarry Smith 
107422a814eSBarry Smith .keywords: Mat, set, initial guess, nonzero
108422a814eSBarry Smith 
109422a814eSBarry Smith .seealso: PCSetErrorIfFailure()
110422a814eSBarry Smith @*/
11184d44b13SHong Zhang PetscErrorCode  MatSetErrorIfFailure(Mat mat,PetscBool flg)
112422a814eSBarry Smith {
113422a814eSBarry Smith   PetscFunctionBegin;
114422a814eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
115422a814eSBarry Smith   PetscValidLogicalCollectiveBool(mat,flg,2);
11684d44b13SHong Zhang   mat->erroriffailure = flg;
117422a814eSBarry Smith   PetscFunctionReturn(0);
118422a814eSBarry Smith }
119422a814eSBarry Smith 
120422a814eSBarry Smith #undef __FUNCT__
121f69a0ea3SMatthew Knepley #define __FUNCT__ "MatSetSizes"
122f69a0ea3SMatthew Knepley /*@
123f69a0ea3SMatthew Knepley   MatSetSizes - Sets the local and global sizes, and checks to determine compatibility
124f69a0ea3SMatthew Knepley 
125f69a0ea3SMatthew Knepley   Collective on Mat
126f69a0ea3SMatthew Knepley 
127f69a0ea3SMatthew Knepley   Input Parameters:
128f69a0ea3SMatthew Knepley +  A - the matrix
129f69a0ea3SMatthew Knepley .  m - number of local rows (or PETSC_DECIDE)
130f69a0ea3SMatthew Knepley .  n - number of local columns (or PETSC_DECIDE)
131f69a0ea3SMatthew Knepley .  M - number of global rows (or PETSC_DETERMINE)
132f69a0ea3SMatthew Knepley -  N - number of global columns (or PETSC_DETERMINE)
133f69a0ea3SMatthew Knepley 
134f69a0ea3SMatthew Knepley    Notes:
135f69a0ea3SMatthew Knepley    m (n) and M (N) cannot be both PETSC_DECIDE
136f69a0ea3SMatthew Knepley    If one processor calls this with M (N) of PETSC_DECIDE then all processors must, otherwise the program will hang.
137f69a0ea3SMatthew Knepley 
138f69a0ea3SMatthew Knepley    If PETSC_DECIDE is not used for the arguments 'm' and 'n', then the
139f69a0ea3SMatthew Knepley    user must ensure that they are chosen to be compatible with the
140f69a0ea3SMatthew Knepley    vectors. To do this, one first considers the matrix-vector product
141f69a0ea3SMatthew Knepley    'y = A x'. The 'm' that is used in the above routine must match the
142f69a0ea3SMatthew Knepley    local size used in the vector creation routine VecCreateMPI() for 'y'.
143f69a0ea3SMatthew Knepley    Likewise, the 'n' used must match that used as the local size in
144f69a0ea3SMatthew Knepley    VecCreateMPI() for 'x'.
145f69a0ea3SMatthew Knepley 
146f73d5cc4SBarry Smith    You cannot change the sizes once they have been set.
147f73d5cc4SBarry Smith 
148f73d5cc4SBarry Smith    The sizes must be set before MatSetUp() or MatXXXSetPreallocation() is called.
149f73d5cc4SBarry Smith 
150f69a0ea3SMatthew Knepley   Level: beginner
151f69a0ea3SMatthew Knepley 
152f69a0ea3SMatthew Knepley .seealso: MatGetSize(), PetscSplitOwnership()
153f69a0ea3SMatthew Knepley @*/
1547087cfbeSBarry Smith PetscErrorCode  MatSetSizes(Mat A, PetscInt m, PetscInt n, PetscInt M, PetscInt N)
155f69a0ea3SMatthew Knepley {
156f69a0ea3SMatthew Knepley   PetscFunctionBegin;
1570700a824SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
158b28e6c9fSJed Brown   if (M > 0) PetscValidLogicalCollectiveInt(A,M,4);
159b28e6c9fSJed Brown   if (N > 0) PetscValidLogicalCollectiveInt(A,N,5);
160e32f2f54SBarry Smith   if (M > 0 && m > M) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local column size %D cannot be larger than global column size %D",m,M);
161e32f2f54SBarry Smith   if (N > 0 && n > N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local row size %D cannot be larger than global row size %D",n,N);
162461878b2SBarry Smith   if ((A->rmap->n >= 0 && A->rmap->N >= 0) && (A->rmap->n != m || A->rmap->N != M)) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot change/reset row sizes to %D local %D global after previously setting them to %D local %D global",m,M,A->rmap->n,A->rmap->N);
163461878b2SBarry Smith   if ((A->cmap->n >= 0 && A->cmap->N >= 0) && (A->cmap->n != n || A->cmap->N != N)) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot change/reset column sizes to %D local %D global after previously setting them to %D local %D global",n,N,A->cmap->n,A->cmap->N);
164d0f46423SBarry Smith   A->rmap->n = m;
165d0f46423SBarry Smith   A->cmap->n = n;
166d0f46423SBarry Smith   A->rmap->N = M;
167d0f46423SBarry Smith   A->cmap->N = N;
168f69a0ea3SMatthew Knepley   PetscFunctionReturn(0);
169f69a0ea3SMatthew Knepley }
170f69a0ea3SMatthew Knepley 
171f69a0ea3SMatthew Knepley #undef __FUNCT__
1724a2ae208SSatish Balay #define __FUNCT__ "MatSetFromOptions"
17305869f15SSatish Balay /*@
174273d9f13SBarry Smith    MatSetFromOptions - Creates a matrix where the type is determined
175273d9f13SBarry Smith    from the options database. Generates a parallel MPI matrix if the
176273d9f13SBarry Smith    communicator has more than one processor.  The default matrix type is
17769b1f4b7SBarry Smith    AIJ, using the routines MatCreateSeqAIJ() and MatCreateAIJ() if
1787e5f4302SBarry Smith    you do not select a type in the options database.
179273d9f13SBarry Smith 
180273d9f13SBarry Smith    Collective on Mat
181273d9f13SBarry Smith 
182273d9f13SBarry Smith    Input Parameter:
183273d9f13SBarry Smith .  A - the matrix
184273d9f13SBarry Smith 
185273d9f13SBarry Smith    Options Database Keys:
186273d9f13SBarry Smith +    -mat_type seqaij   - AIJ type, uses MatCreateSeqAIJ()
18769b1f4b7SBarry Smith .    -mat_type mpiaij   - AIJ type, uses MatCreateAIJ()
188273d9f13SBarry Smith .    -mat_type seqdense - dense type, uses MatCreateSeqDense()
18969b1f4b7SBarry Smith .    -mat_type mpidense - dense type, uses MatCreateDense()
190273d9f13SBarry Smith .    -mat_type seqbaij  - block AIJ type, uses MatCreateSeqBAIJ()
19169b1f4b7SBarry Smith -    -mat_type mpibaij  - block AIJ type, uses MatCreateBAIJ()
192273d9f13SBarry Smith 
193273d9f13SBarry Smith    Even More Options Database Keys:
194273d9f13SBarry Smith    See the manpages for particular formats (e.g., MatCreateSeqAIJ())
195273d9f13SBarry Smith    for additional format-specific options.
196bd9ce289SLois Curfman McInnes 
1971d69843bSLois Curfman McInnes    Level: beginner
1981d69843bSLois Curfman McInnes 
199dc401e71SLois Curfman McInnes .keywords: matrix, create
200e0b365e2SLois Curfman McInnes 
20169b1f4b7SBarry Smith .seealso: MatCreateSeqAIJ((), MatCreateAIJ(),
20269b1f4b7SBarry Smith           MatCreateSeqDense(), MatCreateDense(),
20369b1f4b7SBarry Smith           MatCreateSeqBAIJ(), MatCreateBAIJ(),
20469b1f4b7SBarry Smith           MatCreateSeqSBAIJ(), MatCreateSBAIJ(),
205273d9f13SBarry Smith           MatConvert()
2067807a1faSBarry Smith @*/
2077087cfbeSBarry Smith PetscErrorCode  MatSetFromOptions(Mat B)
2087807a1faSBarry Smith {
209dfbe8321SBarry Smith   PetscErrorCode ierr;
210f3be49caSLisandro Dalcin   const char     *deft = MATAIJ;
211f3be49caSLisandro Dalcin   char           type[256];
21269df5c0cSJed Brown   PetscBool      flg,set;
213dbb450caSBarry Smith 
2143a40ed3dSBarry Smith   PetscFunctionBegin;
2150700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
216f3be49caSLisandro Dalcin 
2173194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)B);CHKERRQ(ierr);
218535b19f3SBarry Smith 
219535b19f3SBarry Smith   if (B->rmap->bs < 0) {
220535b19f3SBarry Smith     PetscInt newbs = -1;
221535b19f3SBarry Smith     ierr = PetscOptionsInt("-mat_block_size","Set the blocksize used to store the matrix","MatSetBlockSize",newbs,&newbs,&flg);CHKERRQ(ierr);
222535b19f3SBarry Smith     if (flg) {
223535b19f3SBarry Smith       ierr = PetscLayoutSetBlockSize(B->rmap,newbs);CHKERRQ(ierr);
224535b19f3SBarry Smith       ierr = PetscLayoutSetBlockSize(B->cmap,newbs);CHKERRQ(ierr);
225535b19f3SBarry Smith     }
226535b19f3SBarry Smith   }
227535b19f3SBarry Smith 
228a264d7a6SBarry Smith   ierr = PetscOptionsFList("-mat_type","Matrix type","MatSetType",MatList,deft,type,256,&flg);CHKERRQ(ierr);
229273d9f13SBarry Smith   if (flg) {
230f3be49caSLisandro Dalcin     ierr = MatSetType(B,type);CHKERRQ(ierr);
231f3be49caSLisandro Dalcin   } else if (!((PetscObject)B)->type_name) {
232f3be49caSLisandro Dalcin     ierr = MatSetType(B,deft);CHKERRQ(ierr);
233273d9f13SBarry Smith   }
234f3be49caSLisandro Dalcin 
235840d65ccSBarry Smith   ierr = PetscOptionsName("-mat_is_symmetric","Checks if mat is symmetric on MatAssemblyEnd()","MatIsSymmetric",&B->checksymmetryonassembly);CHKERRQ(ierr);
23694ae4db5SBarry Smith   ierr = PetscOptionsReal("-mat_is_symmetric","Checks if mat is symmetric on MatAssemblyEnd()","MatIsSymmetric",B->checksymmetrytol,&B->checksymmetrytol,NULL);CHKERRQ(ierr);
23794ae4db5SBarry Smith   ierr = PetscOptionsBool("-mat_null_space_test","Checks if provided null space is correct in MatAssemblyEnd()","MatSetNullSpaceTest",B->checknullspaceonassembly,&B->checknullspaceonassembly,NULL);CHKERRQ(ierr);
238840d65ccSBarry Smith 
239f3be49caSLisandro Dalcin   if (B->ops->setfromoptions) {
240e55864a3SBarry Smith     ierr = (*B->ops->setfromoptions)(PetscOptionsObject,B);CHKERRQ(ierr);
241593c1905SSatish Balay   }
242f3be49caSLisandro Dalcin 
24369df5c0cSJed Brown   flg  = PETSC_FALSE;
24469df5c0cSJed Brown   ierr = PetscOptionsBool("-mat_new_nonzero_location_err","Generate an error if new nonzeros are created in the matrix structure (useful to test preallocation)","MatSetOption",flg,&flg,&set);CHKERRQ(ierr);
24569df5c0cSJed Brown   if (set) {ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,flg);CHKERRQ(ierr);}
24669df5c0cSJed Brown   flg  = PETSC_FALSE;
24769df5c0cSJed Brown   ierr = PetscOptionsBool("-mat_new_nonzero_allocation_err","Generate an error if new nonzeros are allocated in the matrix structure (useful to test preallocation)","MatSetOption",flg,&flg,&set);CHKERRQ(ierr);
24869df5c0cSJed Brown   if (set) {ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,flg);CHKERRQ(ierr);}
24969df5c0cSJed Brown 
2505d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
2510633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)B);CHKERRQ(ierr);
252f3be49caSLisandro Dalcin   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2533a40ed3dSBarry Smith   PetscFunctionReturn(0);
2547807a1faSBarry Smith }
2557807a1faSBarry Smith 
2564a2ae208SSatish Balay #undef __FUNCT__
25763562e91SJed Brown #define __FUNCT__ "MatXAIJSetPreallocation"
25863562e91SJed Brown /*@
25963562e91SJed Brown    MatXAIJSetPreallocation - set preallocation for serial and parallel AIJ, BAIJ, and SBAIJ matrices
26063562e91SJed Brown 
26163562e91SJed Brown    Collective on Mat
26263562e91SJed Brown 
26363562e91SJed Brown    Input Arguments:
26463562e91SJed Brown +  A - matrix being preallocated
26563562e91SJed Brown .  bs - block size
2663e5f4774SJed Brown .  dnnz - number of nonzero blocks per block row of diagonal part of parallel matrix
2673e5f4774SJed Brown .  onnz - number of nonzero blocks per block row of off-diagonal part of parallel matrix
2683e5f4774SJed Brown .  dnnzu - number of nonzero blocks per block row of upper-triangular part of diagonal part of parallel matrix
2693e5f4774SJed Brown -  onnzu - number of nonzero blocks per block row of upper-triangular part of off-diagonal part of parallel matrix
27063562e91SJed Brown 
27163562e91SJed Brown    Level: beginner
27263562e91SJed Brown 
273ab978733SBarry Smith .seealso: MatSeqAIJSetPreallocation(), MatMPIAIJSetPreallocation(), MatSeqBAIJSetPreallocation(), MatMPIBAIJSetPreallocation(), MatSeqSBAIJSetPreallocation(), MatMPISBAIJSetPreallocation(),
274ab978733SBarry Smith           PetscSplitOwnership()
27563562e91SJed Brown @*/
2764cce697cSJed Brown PetscErrorCode MatXAIJSetPreallocation(Mat A,PetscInt bs,const PetscInt dnnz[],const PetscInt onnz[],const PetscInt dnnzu[],const PetscInt onnzu[])
27763562e91SJed Brown {
27863562e91SJed Brown   PetscErrorCode ierr;
27963562e91SJed Brown   void           (*aij)(void);
28063562e91SJed Brown 
28163562e91SJed Brown   PetscFunctionBegin;
282dec54756SJed Brown   ierr = MatSetBlockSize(A,bs);CHKERRQ(ierr);
28369bbac97SJed Brown   ierr = MatGetBlockSize(A,&bs);CHKERRQ(ierr);
284dec54756SJed Brown   ierr = PetscLayoutSetUp(A->rmap);CHKERRQ(ierr);
285dec54756SJed Brown   ierr = PetscLayoutSetUp(A->cmap);CHKERRQ(ierr);
2863e5f4774SJed Brown   ierr = MatSeqBAIJSetPreallocation(A,bs,0,dnnz);CHKERRQ(ierr);
2873e5f4774SJed Brown   ierr = MatMPIBAIJSetPreallocation(A,bs,0,dnnz,0,onnz);CHKERRQ(ierr);
2883e5f4774SJed Brown   ierr = MatSeqSBAIJSetPreallocation(A,bs,0,dnnzu);CHKERRQ(ierr);
2893e5f4774SJed Brown   ierr = MatMPISBAIJSetPreallocation(A,bs,0,dnnzu,0,onnzu);CHKERRQ(ierr);
29063562e91SJed Brown   /*
29163562e91SJed Brown     In general, we have to do extra work to preallocate for scalar (AIJ) matrices so we check whether it will do any
29263562e91SJed Brown     good before going on with it.
29363562e91SJed Brown   */
29463562e91SJed Brown   ierr = PetscObjectQueryFunction((PetscObject)A,"MatMPIAIJSetPreallocation_C",&aij);CHKERRQ(ierr);
29563562e91SJed Brown   if (!aij) {
29663562e91SJed Brown     ierr = PetscObjectQueryFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",&aij);CHKERRQ(ierr);
29763562e91SJed Brown   }
29863562e91SJed Brown   if (aij) {
29963562e91SJed Brown     if (bs == 1) {
3003e5f4774SJed Brown       ierr = MatSeqAIJSetPreallocation(A,0,dnnz);CHKERRQ(ierr);
3013e5f4774SJed Brown       ierr = MatMPIAIJSetPreallocation(A,0,dnnz,0,onnz);CHKERRQ(ierr);
3023e5f4774SJed Brown     } else {                    /* Convert block-row precallocation to scalar-row */
30363562e91SJed Brown       PetscInt i,m,*sdnnz,*sonnz;
3040298fd71SBarry Smith       ierr = MatGetLocalSize(A,&m,NULL);CHKERRQ(ierr);
305dcca6d9dSJed Brown       ierr = PetscMalloc2((!!dnnz)*m,&sdnnz,(!!onnz)*m,&sonnz);CHKERRQ(ierr);
306dec54756SJed Brown       for (i=0; i<m; i++) {
30763562e91SJed Brown         if (dnnz) sdnnz[i] = dnnz[i/bs] * bs;
30863562e91SJed Brown         if (onnz) sonnz[i] = onnz[i/bs] * bs;
30963562e91SJed Brown       }
3100298fd71SBarry Smith       ierr = MatSeqAIJSetPreallocation(A,0,dnnz ? sdnnz : NULL);CHKERRQ(ierr);
3110298fd71SBarry Smith       ierr = MatMPIAIJSetPreallocation(A,0,dnnz ? sdnnz : NULL,0,onnz ? sonnz : NULL);CHKERRQ(ierr);
31263562e91SJed Brown       ierr = PetscFree2(sdnnz,sonnz);CHKERRQ(ierr);
31363562e91SJed Brown     }
31463562e91SJed Brown   }
31563562e91SJed Brown   PetscFunctionReturn(0);
31663562e91SJed Brown }
31763562e91SJed Brown 
318273d9f13SBarry Smith /*
319eb6b5d47SBarry Smith         Merges some information from Cs header to A; the C object is then destroyed
320d0f46423SBarry Smith 
321d0f46423SBarry Smith         This is somewhat different from MatHeaderReplace() it would be nice to merge the code
322273d9f13SBarry Smith */
3234a2ae208SSatish Balay #undef __FUNCT__
324eb6b5d47SBarry Smith #define __FUNCT__ "MatHeaderMerge"
32528be2f97SBarry Smith PetscErrorCode MatHeaderMerge(Mat A,Mat *C)
326273d9f13SBarry Smith {
327dfbe8321SBarry Smith   PetscErrorCode ierr;
328d44834fbSBarry Smith   PetscInt       refct;
32973107ff1SLisandro Dalcin   PetscOps       Abops;
33073107ff1SLisandro Dalcin   struct _MatOps Aops;
331273d9f13SBarry Smith   char           *mtype,*mname;
33230735b05SKris Buschelman   void           *spptr;
333273d9f13SBarry Smith 
334273d9f13SBarry Smith   PetscFunctionBegin;
335273d9f13SBarry Smith   /* save the parts of A we need */
33673107ff1SLisandro Dalcin   Abops = ((PetscObject)A)->bops[0];
33773107ff1SLisandro Dalcin   Aops  = A->ops[0];
3387adad957SLisandro Dalcin   refct = ((PetscObject)A)->refct;
3395c9eb25fSBarry Smith   mtype = ((PetscObject)A)->type_name;
3405c9eb25fSBarry Smith   mname = ((PetscObject)A)->name;
34130735b05SKris Buschelman   spptr = A->spptr;
34230735b05SKris Buschelman 
3435c9eb25fSBarry Smith   /* zero these so the destroy below does not free them */
3445c9eb25fSBarry Smith   ((PetscObject)A)->type_name = 0;
3455c9eb25fSBarry Smith   ((PetscObject)A)->name      = 0;
3465c9eb25fSBarry Smith 
3477c99f97cSSatish Balay   /* free all the interior data structures from mat */
3487c99f97cSSatish Balay   ierr = (*A->ops->destroy)(A);CHKERRQ(ierr);
3497c99f97cSSatish Balay 
35028be2f97SBarry Smith   ierr = PetscFree((*C)->spptr);CHKERRQ(ierr);
35105b42c5fSBarry Smith 
3526bf464f9SBarry Smith   ierr = PetscLayoutDestroy(&A->rmap);CHKERRQ(ierr);
3536bf464f9SBarry Smith   ierr = PetscLayoutDestroy(&A->cmap);CHKERRQ(ierr);
354140e18c1SBarry Smith   ierr = PetscFunctionListDestroy(&((PetscObject)A)->qlist);CHKERRQ(ierr);
355140e18c1SBarry Smith   ierr = PetscObjectListDestroy(&((PetscObject)A)->olist);CHKERRQ(ierr);
356273d9f13SBarry Smith 
357273d9f13SBarry Smith   /* copy C over to A */
35828be2f97SBarry Smith   ierr = PetscMemcpy(A,*C,sizeof(struct _p_Mat));CHKERRQ(ierr);
359273d9f13SBarry Smith 
360273d9f13SBarry Smith   /* return the parts of A we saved */
36173107ff1SLisandro Dalcin   ((PetscObject)A)->bops[0]   = Abops;
36273107ff1SLisandro Dalcin   A->ops[0]                   = Aops;
3637adad957SLisandro Dalcin   ((PetscObject)A)->refct     = refct;
3647adad957SLisandro Dalcin   ((PetscObject)A)->type_name = mtype;
3657adad957SLisandro Dalcin   ((PetscObject)A)->name      = mname;
36630735b05SKris Buschelman   A->spptr                    = spptr;
367273d9f13SBarry Smith 
3685c9eb25fSBarry Smith   /* since these two are copied into A we do not want them destroyed in C */
36928be2f97SBarry Smith   ((PetscObject)*C)->qlist = 0;
37028be2f97SBarry Smith   ((PetscObject)*C)->olist = 0;
37126fbe8dcSKarl Rupp 
37228be2f97SBarry Smith   ierr = PetscHeaderDestroy(C);CHKERRQ(ierr);
373273d9f13SBarry Smith   PetscFunctionReturn(0);
374273d9f13SBarry Smith }
3758ab5b326SKris Buschelman /*
376eb6b5d47SBarry Smith         Replace A's header with that of C; the C object is then destroyed
377d0f46423SBarry Smith 
378eb6b5d47SBarry Smith         This is essentially code moved from MatDestroy()
379eb6b5d47SBarry Smith 
380eb6b5d47SBarry Smith         This is somewhat different from MatHeaderMerge() it would be nice to merge the code
381b30237c6SBarry Smith 
382b30237c6SBarry Smith         Used in DM hence is declared PETSC_EXTERN
3838ab5b326SKris Buschelman */
3848ab5b326SKris Buschelman #undef __FUNCT__
3858ab5b326SKris Buschelman #define __FUNCT__ "MatHeaderReplace"
38628be2f97SBarry Smith PETSC_EXTERN PetscErrorCode MatHeaderReplace(Mat A,Mat *C)
3878ab5b326SKris Buschelman {
3888ab5b326SKris Buschelman   PetscErrorCode   ierr;
38927b31e29SJed Brown   PetscInt         refct;
390fefd9316SJose E. Roman   PetscObjectState state;
39128be2f97SBarry Smith   struct _p_Mat    buffer;
3928ab5b326SKris Buschelman 
3938ab5b326SKris Buschelman   PetscFunctionBegin;
39427b31e29SJed Brown   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
39528be2f97SBarry Smith   PetscValidHeaderSpecific(*C,MAT_CLASSID,2);
39628be2f97SBarry Smith   if (A == *C) PetscFunctionReturn(0);
39728be2f97SBarry Smith   PetscCheckSameComm(A,1,*C,2);
39828be2f97SBarry Smith   if (((PetscObject)*C)->refct != 1) SETERRQ1(PetscObjectComm((PetscObject)C),PETSC_ERR_ARG_WRONGSTATE,"Object C has refct %D > 1, would leave hanging reference",((PetscObject)*C)->refct);
3996d7c1e57SBarry Smith 
40028be2f97SBarry Smith   /* swap C and A */
40127b31e29SJed Brown   refct = ((PetscObject)A)->refct;
402fefd9316SJose E. Roman   state = ((PetscObject)A)->state;
40328be2f97SBarry Smith   ierr  = PetscMemcpy(&buffer,A,sizeof(struct _p_Mat));CHKERRQ(ierr);
40428be2f97SBarry Smith   ierr  = PetscMemcpy(A,*C,sizeof(struct _p_Mat));CHKERRQ(ierr);
40528be2f97SBarry Smith   ierr  = PetscMemcpy(*C,&buffer,sizeof(struct _p_Mat));CHKERRQ(ierr);
40627b31e29SJed Brown   ((PetscObject)A)->refct = refct;
407fefd9316SJose E. Roman   ((PetscObject)A)->state = state + 1;
40826fbe8dcSKarl Rupp 
409c32d4117SBarry Smith   ((PetscObject)*C)->refct = 1;
41028be2f97SBarry Smith   ierr = MatDestroy(C);CHKERRQ(ierr);
4118ab5b326SKris Buschelman   PetscFunctionReturn(0);
4128ab5b326SKris Buschelman }
413