17807a1faSBarry Smith 2af0996ceSBarry Smith #include <petsc/private/matimpl.h> /*I "petscmat.h" I*/ 37807a1faSBarry Smith 44a2ae208SSatish Balay #undef __FUNCT__ 546533700Sstefano_zampini #define __FUNCT__ "MatSetBlockSizes_Default" 646533700Sstefano_zampini PETSC_INTERN PetscErrorCode MatSetBlockSizes_Default(Mat mat,PetscInt rbs, PetscInt cbs) 746533700Sstefano_zampini { 846533700Sstefano_zampini PetscFunctionBegin; 9*5c577a9aSstefano_zampini if (!mat->preallocated) PetscFunctionReturn(0); 1046533700Sstefano_zampini if (mat->rmap->bs > 0 && mat->rmap->bs != rbs) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot change row block size %D to %D\n",mat->rmap->bs,rbs); 1146533700Sstefano_zampini if (mat->cmap->bs > 0 && mat->cmap->bs != cbs) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot change column block size %D to %D\n",mat->cmap->bs,cbs); 1246533700Sstefano_zampini PetscFunctionReturn(0); 1346533700Sstefano_zampini } 1446533700Sstefano_zampini 1546533700Sstefano_zampini #undef __FUNCT__ 167d68702bSBarry Smith #define __FUNCT__ "MatShift_Basic" 17db87b0f2SBarry Smith PETSC_INTERN PetscErrorCode MatShift_Basic(Mat Y,PetscScalar a) 187d68702bSBarry Smith { 197d68702bSBarry Smith PetscErrorCode ierr; 207d68702bSBarry Smith PetscInt i,start,end; 217d68702bSBarry Smith PetscScalar alpha = a; 227d68702bSBarry Smith PetscBool prevoption; 237d68702bSBarry Smith 247d68702bSBarry Smith PetscFunctionBegin; 257d68702bSBarry Smith ierr = MatGetOption(Y,MAT_NO_OFF_PROC_ENTRIES,&prevoption);CHKERRQ(ierr); 267d68702bSBarry Smith ierr = MatSetOption(Y,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE);CHKERRQ(ierr); 277d68702bSBarry Smith ierr = MatGetOwnershipRange(Y,&start,&end);CHKERRQ(ierr); 287d68702bSBarry Smith for (i=start; i<end; i++) { 297d68702bSBarry Smith ierr = MatSetValues(Y,1,&i,1,&i,&alpha,ADD_VALUES);CHKERRQ(ierr); 307d68702bSBarry Smith } 317d68702bSBarry Smith ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 327d68702bSBarry Smith ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 337d68702bSBarry Smith ierr = MatSetOption(Y,MAT_NO_OFF_PROC_ENTRIES,prevoption);CHKERRQ(ierr); 347d68702bSBarry Smith PetscFunctionReturn(0); 357d68702bSBarry Smith } 367d68702bSBarry Smith 377d68702bSBarry Smith #undef __FUNCT__ 384a2ae208SSatish Balay #define __FUNCT__ "MatCreate" 3905869f15SSatish Balay /*@ 4069dd0797SLois Curfman McInnes MatCreate - Creates a matrix where the type is determined 417e5f4302SBarry Smith from either a call to MatSetType() or from the options database 427e5f4302SBarry Smith with a call to MatSetFromOptions(). The default matrix type is 4369b1f4b7SBarry Smith AIJ, using the routines MatCreateSeqAIJ() or MatCreateAIJ() 447e5f4302SBarry Smith if you do not set a type in the options database. If you never 457e5f4302SBarry Smith call MatSetType() or MatSetFromOptions() it will generate an 46f8ab6608SSatish Balay error when you try to use the matrix. 4783e1b59cSLois Curfman McInnes 48cb13003dSBarry Smith Collective on MPI_Comm 49cb13003dSBarry Smith 50f69a0ea3SMatthew Knepley Input Parameter: 51f69a0ea3SMatthew Knepley . comm - MPI communicator 527807a1faSBarry Smith 537807a1faSBarry Smith Output Parameter: 54dc401e71SLois Curfman McInnes . A - the matrix 55e0b365e2SLois Curfman McInnes 56273d9f13SBarry Smith Options Database Keys: 57273d9f13SBarry Smith + -mat_type seqaij - AIJ type, uses MatCreateSeqAIJ() 5869b1f4b7SBarry Smith . -mat_type mpiaij - AIJ type, uses MatCreateAIJ() 59273d9f13SBarry Smith . -mat_type seqdense - dense type, uses MatCreateSeqDense() 6069b1f4b7SBarry Smith . -mat_type mpidense - dense type, uses MatCreateDense() 61273d9f13SBarry Smith . -mat_type seqbaij - block AIJ type, uses MatCreateSeqBAIJ() 6269b1f4b7SBarry Smith - -mat_type mpibaij - block AIJ type, uses MatCreateBAIJ() 63e0b365e2SLois Curfman McInnes 6483e1b59cSLois Curfman McInnes Even More Options Database Keys: 6583e1b59cSLois Curfman McInnes See the manpages for particular formats (e.g., MatCreateSeqAIJ()) 6683e1b59cSLois Curfman McInnes for additional format-specific options. 67e0b365e2SLois Curfman McInnes 68bd9ce289SLois Curfman McInnes Notes: 69ec6e0d80SSatish Balay 70273d9f13SBarry Smith Level: beginner 71273d9f13SBarry Smith 72a2fc510eSBarry Smith User manual sections: 73a2fc510eSBarry Smith + sec_matcreate 74a2fc510eSBarry Smith - chapter_matrices 75a2fc510eSBarry Smith 76273d9f13SBarry Smith .keywords: matrix, create 77273d9f13SBarry Smith 7869b1f4b7SBarry Smith .seealso: MatCreateSeqAIJ(), MatCreateAIJ(), 7969b1f4b7SBarry Smith MatCreateSeqDense(), MatCreateDense(), 8069b1f4b7SBarry Smith MatCreateSeqBAIJ(), MatCreateBAIJ(), 8169b1f4b7SBarry Smith MatCreateSeqSBAIJ(), MatCreateSBAIJ(), 82273d9f13SBarry Smith MatConvert() 83273d9f13SBarry Smith @*/ 847087cfbeSBarry Smith PetscErrorCode MatCreate(MPI_Comm comm,Mat *A) 85273d9f13SBarry Smith { 86273d9f13SBarry Smith Mat B; 87dfbe8321SBarry Smith PetscErrorCode ierr; 88273d9f13SBarry Smith 89273d9f13SBarry Smith PetscFunctionBegin; 90f69a0ea3SMatthew Knepley PetscValidPointer(A,2); 9197f1f81fSBarry Smith 920298fd71SBarry Smith *A = NULL; 93607a6623SBarry Smith ierr = MatInitializePackage();CHKERRQ(ierr); 948ba1e511SMatthew Knepley 9573107ff1SLisandro Dalcin ierr = PetscHeaderCreate(B,MAT_CLASSID,"Mat","Matrix","Mat",comm,MatDestroy,MatView);CHKERRQ(ierr); 9626283091SBarry Smith ierr = PetscLayoutCreate(comm,&B->rmap);CHKERRQ(ierr); 9726283091SBarry Smith ierr = PetscLayoutCreate(comm,&B->cmap);CHKERRQ(ierr); 9826fbe8dcSKarl Rupp 999ae29715SStefano Zampini B->congruentlayouts = -1; 100273d9f13SBarry Smith B->preallocated = PETSC_FALSE; 101273d9f13SBarry Smith *A = B; 102273d9f13SBarry Smith PetscFunctionReturn(0); 103273d9f13SBarry Smith } 104273d9f13SBarry Smith 1054a2ae208SSatish Balay #undef __FUNCT__ 10684d44b13SHong Zhang #define __FUNCT__ "MatSetErrorIfFailure" 107422a814eSBarry Smith /*@ 10884d44b13SHong Zhang MatSetErrorIfFailure - Causes Mat to generate an error, for example a zero pivot, is detected. 109422a814eSBarry Smith 110422a814eSBarry Smith Logically Collective on Mat 111422a814eSBarry Smith 112422a814eSBarry Smith Input Parameters: 113422a814eSBarry Smith + mat - matrix obtained from MatCreate() 114422a814eSBarry Smith - flg - PETSC_TRUE indicates you want the error generated 115422a814eSBarry Smith 116422a814eSBarry Smith Level: advanced 117422a814eSBarry Smith 118422a814eSBarry Smith .keywords: Mat, set, initial guess, nonzero 119422a814eSBarry Smith 120422a814eSBarry Smith .seealso: PCSetErrorIfFailure() 121422a814eSBarry Smith @*/ 12284d44b13SHong Zhang PetscErrorCode MatSetErrorIfFailure(Mat mat,PetscBool flg) 123422a814eSBarry Smith { 124422a814eSBarry Smith PetscFunctionBegin; 125422a814eSBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 126422a814eSBarry Smith PetscValidLogicalCollectiveBool(mat,flg,2); 12784d44b13SHong Zhang mat->erroriffailure = flg; 128422a814eSBarry Smith PetscFunctionReturn(0); 129422a814eSBarry Smith } 130422a814eSBarry Smith 131422a814eSBarry Smith #undef __FUNCT__ 132f69a0ea3SMatthew Knepley #define __FUNCT__ "MatSetSizes" 133f69a0ea3SMatthew Knepley /*@ 134f69a0ea3SMatthew Knepley MatSetSizes - Sets the local and global sizes, and checks to determine compatibility 135f69a0ea3SMatthew Knepley 136f69a0ea3SMatthew Knepley Collective on Mat 137f69a0ea3SMatthew Knepley 138f69a0ea3SMatthew Knepley Input Parameters: 139f69a0ea3SMatthew Knepley + A - the matrix 140f69a0ea3SMatthew Knepley . m - number of local rows (or PETSC_DECIDE) 141f69a0ea3SMatthew Knepley . n - number of local columns (or PETSC_DECIDE) 142f69a0ea3SMatthew Knepley . M - number of global rows (or PETSC_DETERMINE) 143f69a0ea3SMatthew Knepley - N - number of global columns (or PETSC_DETERMINE) 144f69a0ea3SMatthew Knepley 145f69a0ea3SMatthew Knepley Notes: 146f69a0ea3SMatthew Knepley m (n) and M (N) cannot be both PETSC_DECIDE 147f69a0ea3SMatthew Knepley If one processor calls this with M (N) of PETSC_DECIDE then all processors must, otherwise the program will hang. 148f69a0ea3SMatthew Knepley 149f69a0ea3SMatthew Knepley If PETSC_DECIDE is not used for the arguments 'm' and 'n', then the 150f69a0ea3SMatthew Knepley user must ensure that they are chosen to be compatible with the 151f69a0ea3SMatthew Knepley vectors. To do this, one first considers the matrix-vector product 152f69a0ea3SMatthew Knepley 'y = A x'. The 'm' that is used in the above routine must match the 153f69a0ea3SMatthew Knepley local size used in the vector creation routine VecCreateMPI() for 'y'. 154f69a0ea3SMatthew Knepley Likewise, the 'n' used must match that used as the local size in 155f69a0ea3SMatthew Knepley VecCreateMPI() for 'x'. 156f69a0ea3SMatthew Knepley 157f73d5cc4SBarry Smith You cannot change the sizes once they have been set. 158f73d5cc4SBarry Smith 159f73d5cc4SBarry Smith The sizes must be set before MatSetUp() or MatXXXSetPreallocation() is called. 160f73d5cc4SBarry Smith 161f69a0ea3SMatthew Knepley Level: beginner 162f69a0ea3SMatthew Knepley 163f69a0ea3SMatthew Knepley .seealso: MatGetSize(), PetscSplitOwnership() 164f69a0ea3SMatthew Knepley @*/ 1657087cfbeSBarry Smith PetscErrorCode MatSetSizes(Mat A, PetscInt m, PetscInt n, PetscInt M, PetscInt N) 166f69a0ea3SMatthew Knepley { 167f69a0ea3SMatthew Knepley PetscFunctionBegin; 1680700a824SBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,1); 169b28e6c9fSJed Brown if (M > 0) PetscValidLogicalCollectiveInt(A,M,4); 170b28e6c9fSJed Brown if (N > 0) PetscValidLogicalCollectiveInt(A,N,5); 171e32f2f54SBarry 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); 172e32f2f54SBarry 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); 173461878b2SBarry 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); 174461878b2SBarry 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); 175d0f46423SBarry Smith A->rmap->n = m; 176d0f46423SBarry Smith A->cmap->n = n; 177d0f46423SBarry Smith A->rmap->N = M; 178d0f46423SBarry Smith A->cmap->N = N; 179f69a0ea3SMatthew Knepley PetscFunctionReturn(0); 180f69a0ea3SMatthew Knepley } 181f69a0ea3SMatthew Knepley 182f69a0ea3SMatthew Knepley #undef __FUNCT__ 1834a2ae208SSatish Balay #define __FUNCT__ "MatSetFromOptions" 18405869f15SSatish Balay /*@ 185273d9f13SBarry Smith MatSetFromOptions - Creates a matrix where the type is determined 186273d9f13SBarry Smith from the options database. Generates a parallel MPI matrix if the 187273d9f13SBarry Smith communicator has more than one processor. The default matrix type is 18869b1f4b7SBarry Smith AIJ, using the routines MatCreateSeqAIJ() and MatCreateAIJ() if 1897e5f4302SBarry Smith you do not select a type in the options database. 190273d9f13SBarry Smith 191273d9f13SBarry Smith Collective on Mat 192273d9f13SBarry Smith 193273d9f13SBarry Smith Input Parameter: 194273d9f13SBarry Smith . A - the matrix 195273d9f13SBarry Smith 196273d9f13SBarry Smith Options Database Keys: 197273d9f13SBarry Smith + -mat_type seqaij - AIJ type, uses MatCreateSeqAIJ() 19869b1f4b7SBarry Smith . -mat_type mpiaij - AIJ type, uses MatCreateAIJ() 199273d9f13SBarry Smith . -mat_type seqdense - dense type, uses MatCreateSeqDense() 20069b1f4b7SBarry Smith . -mat_type mpidense - dense type, uses MatCreateDense() 201273d9f13SBarry Smith . -mat_type seqbaij - block AIJ type, uses MatCreateSeqBAIJ() 20269b1f4b7SBarry Smith - -mat_type mpibaij - block AIJ type, uses MatCreateBAIJ() 203273d9f13SBarry Smith 204273d9f13SBarry Smith Even More Options Database Keys: 205273d9f13SBarry Smith See the manpages for particular formats (e.g., MatCreateSeqAIJ()) 206273d9f13SBarry Smith for additional format-specific options. 207bd9ce289SLois Curfman McInnes 2081d69843bSLois Curfman McInnes Level: beginner 2091d69843bSLois Curfman McInnes 210dc401e71SLois Curfman McInnes .keywords: matrix, create 211e0b365e2SLois Curfman McInnes 21269b1f4b7SBarry Smith .seealso: MatCreateSeqAIJ((), MatCreateAIJ(), 21369b1f4b7SBarry Smith MatCreateSeqDense(), MatCreateDense(), 21469b1f4b7SBarry Smith MatCreateSeqBAIJ(), MatCreateBAIJ(), 21569b1f4b7SBarry Smith MatCreateSeqSBAIJ(), MatCreateSBAIJ(), 216273d9f13SBarry Smith MatConvert() 2177807a1faSBarry Smith @*/ 2187087cfbeSBarry Smith PetscErrorCode MatSetFromOptions(Mat B) 2197807a1faSBarry Smith { 220dfbe8321SBarry Smith PetscErrorCode ierr; 221f3be49caSLisandro Dalcin const char *deft = MATAIJ; 222f3be49caSLisandro Dalcin char type[256]; 22369df5c0cSJed Brown PetscBool flg,set; 224dbb450caSBarry Smith 2253a40ed3dSBarry Smith PetscFunctionBegin; 2260700a824SBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,1); 227f3be49caSLisandro Dalcin 2283194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)B);CHKERRQ(ierr); 229535b19f3SBarry Smith 230535b19f3SBarry Smith if (B->rmap->bs < 0) { 231535b19f3SBarry Smith PetscInt newbs = -1; 232535b19f3SBarry Smith ierr = PetscOptionsInt("-mat_block_size","Set the blocksize used to store the matrix","MatSetBlockSize",newbs,&newbs,&flg);CHKERRQ(ierr); 233535b19f3SBarry Smith if (flg) { 234535b19f3SBarry Smith ierr = PetscLayoutSetBlockSize(B->rmap,newbs);CHKERRQ(ierr); 235535b19f3SBarry Smith ierr = PetscLayoutSetBlockSize(B->cmap,newbs);CHKERRQ(ierr); 236535b19f3SBarry Smith } 237535b19f3SBarry Smith } 238535b19f3SBarry Smith 239a264d7a6SBarry Smith ierr = PetscOptionsFList("-mat_type","Matrix type","MatSetType",MatList,deft,type,256,&flg);CHKERRQ(ierr); 240273d9f13SBarry Smith if (flg) { 241f3be49caSLisandro Dalcin ierr = MatSetType(B,type);CHKERRQ(ierr); 242f3be49caSLisandro Dalcin } else if (!((PetscObject)B)->type_name) { 243f3be49caSLisandro Dalcin ierr = MatSetType(B,deft);CHKERRQ(ierr); 244273d9f13SBarry Smith } 245f3be49caSLisandro Dalcin 246840d65ccSBarry Smith ierr = PetscOptionsName("-mat_is_symmetric","Checks if mat is symmetric on MatAssemblyEnd()","MatIsSymmetric",&B->checksymmetryonassembly);CHKERRQ(ierr); 24794ae4db5SBarry Smith ierr = PetscOptionsReal("-mat_is_symmetric","Checks if mat is symmetric on MatAssemblyEnd()","MatIsSymmetric",B->checksymmetrytol,&B->checksymmetrytol,NULL);CHKERRQ(ierr); 24894ae4db5SBarry Smith ierr = PetscOptionsBool("-mat_null_space_test","Checks if provided null space is correct in MatAssemblyEnd()","MatSetNullSpaceTest",B->checknullspaceonassembly,&B->checknullspaceonassembly,NULL);CHKERRQ(ierr); 249840d65ccSBarry Smith 250f3be49caSLisandro Dalcin if (B->ops->setfromoptions) { 251e55864a3SBarry Smith ierr = (*B->ops->setfromoptions)(PetscOptionsObject,B);CHKERRQ(ierr); 252593c1905SSatish Balay } 253f3be49caSLisandro Dalcin 25469df5c0cSJed Brown flg = PETSC_FALSE; 25569df5c0cSJed 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); 25669df5c0cSJed Brown if (set) {ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,flg);CHKERRQ(ierr);} 25769df5c0cSJed Brown flg = PETSC_FALSE; 25869df5c0cSJed 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); 25969df5c0cSJed Brown if (set) {ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,flg);CHKERRQ(ierr);} 26069df5c0cSJed Brown 2615d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 2620633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)B);CHKERRQ(ierr); 263f3be49caSLisandro Dalcin ierr = PetscOptionsEnd();CHKERRQ(ierr); 2643a40ed3dSBarry Smith PetscFunctionReturn(0); 2657807a1faSBarry Smith } 2667807a1faSBarry Smith 2674a2ae208SSatish Balay #undef __FUNCT__ 26863562e91SJed Brown #define __FUNCT__ "MatXAIJSetPreallocation" 26963562e91SJed Brown /*@ 270e8bd9bafSStefano Zampini MatXAIJSetPreallocation - set preallocation for serial and parallel AIJ, BAIJ, and SBAIJ matrices and their unassembled versions. 27163562e91SJed Brown 27263562e91SJed Brown Collective on Mat 27363562e91SJed Brown 27463562e91SJed Brown Input Arguments: 27563562e91SJed Brown + A - matrix being preallocated 27663562e91SJed Brown . bs - block size 2773e5f4774SJed Brown . dnnz - number of nonzero blocks per block row of diagonal part of parallel matrix 2783e5f4774SJed Brown . onnz - number of nonzero blocks per block row of off-diagonal part of parallel matrix 2793e5f4774SJed Brown . dnnzu - number of nonzero blocks per block row of upper-triangular part of diagonal part of parallel matrix 2803e5f4774SJed Brown - onnzu - number of nonzero blocks per block row of upper-triangular part of off-diagonal part of parallel matrix 28163562e91SJed Brown 28263562e91SJed Brown Level: beginner 28363562e91SJed Brown 284ab978733SBarry Smith .seealso: MatSeqAIJSetPreallocation(), MatMPIAIJSetPreallocation(), MatSeqBAIJSetPreallocation(), MatMPIBAIJSetPreallocation(), MatSeqSBAIJSetPreallocation(), MatMPISBAIJSetPreallocation(), 285ab978733SBarry Smith PetscSplitOwnership() 28663562e91SJed Brown @*/ 2874cce697cSJed Brown PetscErrorCode MatXAIJSetPreallocation(Mat A,PetscInt bs,const PetscInt dnnz[],const PetscInt onnz[],const PetscInt dnnzu[],const PetscInt onnzu[]) 28863562e91SJed Brown { 28963562e91SJed Brown PetscErrorCode ierr; 29063562e91SJed Brown void (*aij)(void); 291e8bd9bafSStefano Zampini void (*is)(void); 29263562e91SJed Brown 29363562e91SJed Brown PetscFunctionBegin; 294dec54756SJed Brown ierr = MatSetBlockSize(A,bs);CHKERRQ(ierr); 29569bbac97SJed Brown ierr = MatGetBlockSize(A,&bs);CHKERRQ(ierr); 296dec54756SJed Brown ierr = PetscLayoutSetUp(A->rmap);CHKERRQ(ierr); 297dec54756SJed Brown ierr = PetscLayoutSetUp(A->cmap);CHKERRQ(ierr); 2983e5f4774SJed Brown ierr = MatSeqBAIJSetPreallocation(A,bs,0,dnnz);CHKERRQ(ierr); 2993e5f4774SJed Brown ierr = MatMPIBAIJSetPreallocation(A,bs,0,dnnz,0,onnz);CHKERRQ(ierr); 3003e5f4774SJed Brown ierr = MatSeqSBAIJSetPreallocation(A,bs,0,dnnzu);CHKERRQ(ierr); 3013e5f4774SJed Brown ierr = MatMPISBAIJSetPreallocation(A,bs,0,dnnzu,0,onnzu);CHKERRQ(ierr); 30263562e91SJed Brown /* 303e8bd9bafSStefano Zampini In general, we have to do extra work to preallocate for scalar (AIJ) or unassembled (IS) matrices so we check whether it will do any 30463562e91SJed Brown good before going on with it. 30563562e91SJed Brown */ 30663562e91SJed Brown ierr = PetscObjectQueryFunction((PetscObject)A,"MatMPIAIJSetPreallocation_C",&aij);CHKERRQ(ierr); 307e8bd9bafSStefano Zampini ierr = PetscObjectQueryFunction((PetscObject)A,"MatISSetPreallocation_C",&is);CHKERRQ(ierr); 308e8bd9bafSStefano Zampini if (!aij && !is) { 30963562e91SJed Brown ierr = PetscObjectQueryFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",&aij);CHKERRQ(ierr); 31063562e91SJed Brown } 311e8bd9bafSStefano Zampini if (aij || is) { 31263562e91SJed Brown if (bs == 1) { 3133e5f4774SJed Brown ierr = MatSeqAIJSetPreallocation(A,0,dnnz);CHKERRQ(ierr); 3143e5f4774SJed Brown ierr = MatMPIAIJSetPreallocation(A,0,dnnz,0,onnz);CHKERRQ(ierr); 315e8bd9bafSStefano Zampini ierr = MatISSetPreallocation(A,0,dnnz,0,onnz);CHKERRQ(ierr); 3163e5f4774SJed Brown } else { /* Convert block-row precallocation to scalar-row */ 31763562e91SJed Brown PetscInt i,m,*sdnnz,*sonnz; 3180298fd71SBarry Smith ierr = MatGetLocalSize(A,&m,NULL);CHKERRQ(ierr); 319dcca6d9dSJed Brown ierr = PetscMalloc2((!!dnnz)*m,&sdnnz,(!!onnz)*m,&sonnz);CHKERRQ(ierr); 320dec54756SJed Brown for (i=0; i<m; i++) { 32163562e91SJed Brown if (dnnz) sdnnz[i] = dnnz[i/bs] * bs; 32263562e91SJed Brown if (onnz) sonnz[i] = onnz[i/bs] * bs; 32363562e91SJed Brown } 3240298fd71SBarry Smith ierr = MatSeqAIJSetPreallocation(A,0,dnnz ? sdnnz : NULL);CHKERRQ(ierr); 3250298fd71SBarry Smith ierr = MatMPIAIJSetPreallocation(A,0,dnnz ? sdnnz : NULL,0,onnz ? sonnz : NULL);CHKERRQ(ierr); 326e8bd9bafSStefano Zampini ierr = MatISSetPreallocation(A,0,dnnz ? sdnnz : NULL,0,onnz ? sonnz : NULL);CHKERRQ(ierr); 32763562e91SJed Brown ierr = PetscFree2(sdnnz,sonnz);CHKERRQ(ierr); 32863562e91SJed Brown } 32963562e91SJed Brown } 33063562e91SJed Brown PetscFunctionReturn(0); 33163562e91SJed Brown } 33263562e91SJed Brown 333273d9f13SBarry Smith /* 334eb6b5d47SBarry Smith Merges some information from Cs header to A; the C object is then destroyed 335d0f46423SBarry Smith 336d0f46423SBarry Smith This is somewhat different from MatHeaderReplace() it would be nice to merge the code 337273d9f13SBarry Smith */ 3384a2ae208SSatish Balay #undef __FUNCT__ 339eb6b5d47SBarry Smith #define __FUNCT__ "MatHeaderMerge" 34028be2f97SBarry Smith PetscErrorCode MatHeaderMerge(Mat A,Mat *C) 341273d9f13SBarry Smith { 342dfbe8321SBarry Smith PetscErrorCode ierr; 343d44834fbSBarry Smith PetscInt refct; 34473107ff1SLisandro Dalcin PetscOps Abops; 34573107ff1SLisandro Dalcin struct _MatOps Aops; 346273d9f13SBarry Smith char *mtype,*mname; 34730735b05SKris Buschelman void *spptr; 348273d9f13SBarry Smith 349273d9f13SBarry Smith PetscFunctionBegin; 350273d9f13SBarry Smith /* save the parts of A we need */ 35173107ff1SLisandro Dalcin Abops = ((PetscObject)A)->bops[0]; 35273107ff1SLisandro Dalcin Aops = A->ops[0]; 3537adad957SLisandro Dalcin refct = ((PetscObject)A)->refct; 3545c9eb25fSBarry Smith mtype = ((PetscObject)A)->type_name; 3555c9eb25fSBarry Smith mname = ((PetscObject)A)->name; 35630735b05SKris Buschelman spptr = A->spptr; 35730735b05SKris Buschelman 3585c9eb25fSBarry Smith /* zero these so the destroy below does not free them */ 3595c9eb25fSBarry Smith ((PetscObject)A)->type_name = 0; 3605c9eb25fSBarry Smith ((PetscObject)A)->name = 0; 3615c9eb25fSBarry Smith 3627c99f97cSSatish Balay /* free all the interior data structures from mat */ 3637c99f97cSSatish Balay ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 3647c99f97cSSatish Balay 36528be2f97SBarry Smith ierr = PetscFree((*C)->spptr);CHKERRQ(ierr); 36605b42c5fSBarry Smith 3676bf464f9SBarry Smith ierr = PetscLayoutDestroy(&A->rmap);CHKERRQ(ierr); 3686bf464f9SBarry Smith ierr = PetscLayoutDestroy(&A->cmap);CHKERRQ(ierr); 369140e18c1SBarry Smith ierr = PetscFunctionListDestroy(&((PetscObject)A)->qlist);CHKERRQ(ierr); 370140e18c1SBarry Smith ierr = PetscObjectListDestroy(&((PetscObject)A)->olist);CHKERRQ(ierr); 371273d9f13SBarry Smith 372273d9f13SBarry Smith /* copy C over to A */ 37328be2f97SBarry Smith ierr = PetscMemcpy(A,*C,sizeof(struct _p_Mat));CHKERRQ(ierr); 374273d9f13SBarry Smith 375273d9f13SBarry Smith /* return the parts of A we saved */ 37673107ff1SLisandro Dalcin ((PetscObject)A)->bops[0] = Abops; 37773107ff1SLisandro Dalcin A->ops[0] = Aops; 3787adad957SLisandro Dalcin ((PetscObject)A)->refct = refct; 3797adad957SLisandro Dalcin ((PetscObject)A)->type_name = mtype; 3807adad957SLisandro Dalcin ((PetscObject)A)->name = mname; 38130735b05SKris Buschelman A->spptr = spptr; 382273d9f13SBarry Smith 3835c9eb25fSBarry Smith /* since these two are copied into A we do not want them destroyed in C */ 38428be2f97SBarry Smith ((PetscObject)*C)->qlist = 0; 38528be2f97SBarry Smith ((PetscObject)*C)->olist = 0; 38626fbe8dcSKarl Rupp 38728be2f97SBarry Smith ierr = PetscHeaderDestroy(C);CHKERRQ(ierr); 388273d9f13SBarry Smith PetscFunctionReturn(0); 389273d9f13SBarry Smith } 3908ab5b326SKris Buschelman /* 391eb6b5d47SBarry Smith Replace A's header with that of C; the C object is then destroyed 392d0f46423SBarry Smith 393eb6b5d47SBarry Smith This is essentially code moved from MatDestroy() 394eb6b5d47SBarry Smith 395eb6b5d47SBarry Smith This is somewhat different from MatHeaderMerge() it would be nice to merge the code 396b30237c6SBarry Smith 397b30237c6SBarry Smith Used in DM hence is declared PETSC_EXTERN 3988ab5b326SKris Buschelman */ 3998ab5b326SKris Buschelman #undef __FUNCT__ 4008ab5b326SKris Buschelman #define __FUNCT__ "MatHeaderReplace" 40128be2f97SBarry Smith PETSC_EXTERN PetscErrorCode MatHeaderReplace(Mat A,Mat *C) 4028ab5b326SKris Buschelman { 4038ab5b326SKris Buschelman PetscErrorCode ierr; 40427b31e29SJed Brown PetscInt refct; 405fefd9316SJose E. Roman PetscObjectState state; 40628be2f97SBarry Smith struct _p_Mat buffer; 4078ab5b326SKris Buschelman 4088ab5b326SKris Buschelman PetscFunctionBegin; 40927b31e29SJed Brown PetscValidHeaderSpecific(A,MAT_CLASSID,1); 41028be2f97SBarry Smith PetscValidHeaderSpecific(*C,MAT_CLASSID,2); 41128be2f97SBarry Smith if (A == *C) PetscFunctionReturn(0); 41228be2f97SBarry Smith PetscCheckSameComm(A,1,*C,2); 41328be2f97SBarry 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); 4146d7c1e57SBarry Smith 41528be2f97SBarry Smith /* swap C and A */ 41627b31e29SJed Brown refct = ((PetscObject)A)->refct; 417fefd9316SJose E. Roman state = ((PetscObject)A)->state; 41828be2f97SBarry Smith ierr = PetscMemcpy(&buffer,A,sizeof(struct _p_Mat));CHKERRQ(ierr); 41928be2f97SBarry Smith ierr = PetscMemcpy(A,*C,sizeof(struct _p_Mat));CHKERRQ(ierr); 42028be2f97SBarry Smith ierr = PetscMemcpy(*C,&buffer,sizeof(struct _p_Mat));CHKERRQ(ierr); 42127b31e29SJed Brown ((PetscObject)A)->refct = refct; 422fefd9316SJose E. Roman ((PetscObject)A)->state = state + 1; 42326fbe8dcSKarl Rupp 424c32d4117SBarry Smith ((PetscObject)*C)->refct = 1; 42528be2f97SBarry Smith ierr = MatDestroy(C);CHKERRQ(ierr); 4268ab5b326SKris Buschelman PetscFunctionReturn(0); 4278ab5b326SKris Buschelman } 428