1be1d678aSKris Buschelman #define PETSCMAT_DLL 27807a1faSBarry Smith 37c4f633dSBarry Smith #include "private/matimpl.h" /*I "petscmat.h" I*/ 4325e03aeSBarry Smith #include "petscsys.h" 57807a1faSBarry Smith 67adad957SLisandro Dalcin #if 0 74a2ae208SSatish Balay #undef __FUNCT__ 84a2ae208SSatish Balay #define __FUNCT__ "MatPublish_Base" 96849ba73SBarry Smith static PetscErrorCode MatPublish_Base(PetscObject obj) 1035d8aa7fSBarry Smith { 1135d8aa7fSBarry Smith PetscFunctionBegin; 1235d8aa7fSBarry Smith PetscFunctionReturn(0); 1335d8aa7fSBarry Smith } 147adad957SLisandro Dalcin #endif 1535d8aa7fSBarry Smith 164a2ae208SSatish Balay #undef __FUNCT__ 174a2ae208SSatish Balay #define __FUNCT__ "MatCreate" 1805869f15SSatish Balay /*@ 1969dd0797SLois Curfman McInnes MatCreate - Creates a matrix where the type is determined 207e5f4302SBarry Smith from either a call to MatSetType() or from the options database 217e5f4302SBarry Smith with a call to MatSetFromOptions(). The default matrix type is 227e5f4302SBarry Smith AIJ, using the routines MatCreateSeqAIJ() or MatCreateMPIAIJ() 237e5f4302SBarry Smith if you do not set a type in the options database. If you never 247e5f4302SBarry Smith call MatSetType() or MatSetFromOptions() it will generate an 25f8ab6608SSatish Balay error when you try to use the matrix. 2683e1b59cSLois Curfman McInnes 27cb13003dSBarry Smith Collective on MPI_Comm 28cb13003dSBarry Smith 29f69a0ea3SMatthew Knepley Input Parameter: 30f69a0ea3SMatthew Knepley . comm - MPI communicator 317807a1faSBarry Smith 327807a1faSBarry Smith Output Parameter: 33dc401e71SLois Curfman McInnes . A - the matrix 34e0b365e2SLois Curfman McInnes 35273d9f13SBarry Smith Options Database Keys: 36273d9f13SBarry Smith + -mat_type seqaij - AIJ type, uses MatCreateSeqAIJ() 37273d9f13SBarry Smith . -mat_type mpiaij - AIJ type, uses MatCreateMPIAIJ() 38273d9f13SBarry Smith . -mat_type mpirowbs - rowbs type, uses MatCreateMPIRowbs() 39273d9f13SBarry Smith . -mat_type seqdense - dense type, uses MatCreateSeqDense() 40273d9f13SBarry Smith . -mat_type mpidense - dense type, uses MatCreateMPIDense() 41273d9f13SBarry Smith . -mat_type seqbaij - block AIJ type, uses MatCreateSeqBAIJ() 42273d9f13SBarry Smith - -mat_type mpibaij - block AIJ type, uses MatCreateMPIBAIJ() 43e0b365e2SLois Curfman McInnes 4483e1b59cSLois Curfman McInnes Even More Options Database Keys: 4583e1b59cSLois Curfman McInnes See the manpages for particular formats (e.g., MatCreateSeqAIJ()) 4683e1b59cSLois Curfman McInnes for additional format-specific options. 47e0b365e2SLois Curfman McInnes 48bd9ce289SLois Curfman McInnes Notes: 49ec6e0d80SSatish Balay 50273d9f13SBarry Smith Level: beginner 51273d9f13SBarry Smith 52a2fc510eSBarry Smith User manual sections: 53a2fc510eSBarry Smith + sec_matcreate 54a2fc510eSBarry Smith - chapter_matrices 55a2fc510eSBarry Smith 56273d9f13SBarry Smith .keywords: matrix, create 57273d9f13SBarry Smith 58cd05a4c0SHong Zhang .seealso: MatCreateSeqAIJ(), MatCreateMPIAIJ(), 59273d9f13SBarry Smith MatCreateSeqDense(), MatCreateMPIDense(), 60273d9f13SBarry Smith MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), 61273d9f13SBarry Smith MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(), 62273d9f13SBarry Smith MatConvert() 63273d9f13SBarry Smith @*/ 64f69a0ea3SMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatCreate(MPI_Comm comm,Mat *A) 65273d9f13SBarry Smith { 66273d9f13SBarry Smith Mat B; 67dfbe8321SBarry Smith PetscErrorCode ierr; 68273d9f13SBarry Smith 69273d9f13SBarry Smith PetscFunctionBegin; 70f69a0ea3SMatthew Knepley PetscValidPointer(A,2); 7197f1f81fSBarry Smith 728ba1e511SMatthew Knepley *A = PETSC_NULL; 738ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 748ba1e511SMatthew Knepley ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr); 758ba1e511SMatthew Knepley #endif 768ba1e511SMatthew Knepley 7752e6d16bSBarry Smith ierr = PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,0,"Mat",comm,MatDestroy,MatView);CHKERRQ(ierr); 78*26283091SBarry Smith ierr = PetscLayoutCreate(comm,&B->rmap);CHKERRQ(ierr); 79*26283091SBarry Smith ierr = PetscLayoutCreate(comm,&B->cmap);CHKERRQ(ierr); 80273d9f13SBarry Smith B->preallocated = PETSC_FALSE; 81273d9f13SBarry Smith *A = B; 82273d9f13SBarry Smith PetscFunctionReturn(0); 83273d9f13SBarry Smith } 84273d9f13SBarry Smith 854a2ae208SSatish Balay #undef __FUNCT__ 86f69a0ea3SMatthew Knepley #define __FUNCT__ "MatSetSizes" 87f69a0ea3SMatthew Knepley /*@ 88f69a0ea3SMatthew Knepley MatSetSizes - Sets the local and global sizes, and checks to determine compatibility 89f69a0ea3SMatthew Knepley 90f69a0ea3SMatthew Knepley Collective on Mat 91f69a0ea3SMatthew Knepley 92f69a0ea3SMatthew Knepley Input Parameters: 93f69a0ea3SMatthew Knepley + A - the matrix 94f69a0ea3SMatthew Knepley . m - number of local rows (or PETSC_DECIDE) 95f69a0ea3SMatthew Knepley . n - number of local columns (or PETSC_DECIDE) 96f69a0ea3SMatthew Knepley . M - number of global rows (or PETSC_DETERMINE) 97f69a0ea3SMatthew Knepley - N - number of global columns (or PETSC_DETERMINE) 98f69a0ea3SMatthew Knepley 99f69a0ea3SMatthew Knepley Notes: 100f69a0ea3SMatthew Knepley m (n) and M (N) cannot be both PETSC_DECIDE 101f69a0ea3SMatthew Knepley If one processor calls this with M (N) of PETSC_DECIDE then all processors must, otherwise the program will hang. 102f69a0ea3SMatthew Knepley 103f69a0ea3SMatthew Knepley If PETSC_DECIDE is not used for the arguments 'm' and 'n', then the 104f69a0ea3SMatthew Knepley user must ensure that they are chosen to be compatible with the 105f69a0ea3SMatthew Knepley vectors. To do this, one first considers the matrix-vector product 106f69a0ea3SMatthew Knepley 'y = A x'. The 'm' that is used in the above routine must match the 107f69a0ea3SMatthew Knepley local size used in the vector creation routine VecCreateMPI() for 'y'. 108f69a0ea3SMatthew Knepley Likewise, the 'n' used must match that used as the local size in 109f69a0ea3SMatthew Knepley VecCreateMPI() for 'x'. 110f69a0ea3SMatthew Knepley 111f69a0ea3SMatthew Knepley Level: beginner 112f69a0ea3SMatthew Knepley 113f69a0ea3SMatthew Knepley .seealso: MatGetSize(), PetscSplitOwnership() 114f69a0ea3SMatthew Knepley @*/ 115f69a0ea3SMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatSetSizes(Mat A, PetscInt m, PetscInt n, PetscInt M, PetscInt N) 116f69a0ea3SMatthew Knepley { 117284134d9SBarry Smith PetscErrorCode ierr; 118284134d9SBarry Smith 119f69a0ea3SMatthew Knepley PetscFunctionBegin; 120f69a0ea3SMatthew Knepley PetscValidHeaderSpecific(A,MAT_COOKIE,1); 121f69a0ea3SMatthew Knepley if (M > 0 && m > M) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Local column size %D cannot be larger than global column size %D",m,M); 122f69a0ea3SMatthew Knepley if (N > 0 && n > N) SETERRQ2(PETSC_ERR_ARG_INCOMP,"Local row size %D cannot be larger than global row size %D",n,N); 123284134d9SBarry Smith if (A->ops->setsizes) { 124284134d9SBarry Smith /* Since this will not be set until the type has been set, this will NOT be called on the initial 125284134d9SBarry Smith call of MatSetSizes() (which must be called BEFORE MatSetType() */ 126284134d9SBarry Smith ierr = (*A->ops->setsizes)(A,m,n,M,N);CHKERRQ(ierr); 127284134d9SBarry Smith } else { 128d0f46423SBarry Smith if ((A->rmap->n >= 0 || A->rmap->N >= 0) && (A->rmap->n != m || A->rmap->N != M)) SETERRQ4(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); 129d0f46423SBarry Smith if ((A->cmap->n >= 0 || A->cmap->N >= 0) && (A->cmap->n != n || A->cmap->N != N)) SETERRQ4(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); 130284134d9SBarry Smith } 131d0f46423SBarry Smith A->rmap->n = m; 132d0f46423SBarry Smith A->cmap->n = n; 133d0f46423SBarry Smith A->rmap->N = M; 134d0f46423SBarry Smith A->cmap->N = N; 135117016b1SBarry Smith if (A->ops->create) { 136117016b1SBarry Smith ierr = (*A->ops->create)(A);CHKERRQ(ierr); 137117016b1SBarry Smith A->ops->create = 0; 138117016b1SBarry Smith } 139117016b1SBarry Smith 140f69a0ea3SMatthew Knepley PetscFunctionReturn(0); 141f69a0ea3SMatthew Knepley } 142f69a0ea3SMatthew Knepley 143f69a0ea3SMatthew Knepley #undef __FUNCT__ 1444a2ae208SSatish Balay #define __FUNCT__ "MatSetFromOptions" 14505869f15SSatish Balay /*@ 146273d9f13SBarry Smith MatSetFromOptions - Creates a matrix where the type is determined 147273d9f13SBarry Smith from the options database. Generates a parallel MPI matrix if the 148273d9f13SBarry Smith communicator has more than one processor. The default matrix type is 1497e5f4302SBarry Smith AIJ, using the routines MatCreateSeqAIJ() and MatCreateMPIAIJ() if 1507e5f4302SBarry Smith you do not select a type in the options database. 151273d9f13SBarry Smith 152273d9f13SBarry Smith Collective on Mat 153273d9f13SBarry Smith 154273d9f13SBarry Smith Input Parameter: 155273d9f13SBarry Smith . A - the matrix 156273d9f13SBarry Smith 157273d9f13SBarry Smith Options Database Keys: 158273d9f13SBarry Smith + -mat_type seqaij - AIJ type, uses MatCreateSeqAIJ() 159273d9f13SBarry Smith . -mat_type mpiaij - AIJ type, uses MatCreateMPIAIJ() 160273d9f13SBarry Smith . -mat_type mpirowbs - rowbs type, uses MatCreateMPIRowbs() 161273d9f13SBarry Smith . -mat_type seqdense - dense type, uses MatCreateSeqDense() 162273d9f13SBarry Smith . -mat_type mpidense - dense type, uses MatCreateMPIDense() 163273d9f13SBarry Smith . -mat_type seqbaij - block AIJ type, uses MatCreateSeqBAIJ() 164273d9f13SBarry Smith - -mat_type mpibaij - block AIJ type, uses MatCreateMPIBAIJ() 165273d9f13SBarry Smith 166273d9f13SBarry Smith Even More Options Database Keys: 167273d9f13SBarry Smith See the manpages for particular formats (e.g., MatCreateSeqAIJ()) 168273d9f13SBarry Smith for additional format-specific options. 169bd9ce289SLois Curfman McInnes 1701d69843bSLois Curfman McInnes Level: beginner 1711d69843bSLois Curfman McInnes 172dc401e71SLois Curfman McInnes .keywords: matrix, create 173e0b365e2SLois Curfman McInnes 174fafbff53SBarry Smith .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(), 17539ddd567SLois Curfman McInnes MatCreateSeqDense(), MatCreateMPIDense(), 176a209d233SLois Curfman McInnes MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), 177a209d233SLois Curfman McInnes MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(), 178273d9f13SBarry Smith MatConvert() 1797807a1faSBarry Smith @*/ 180be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSetFromOptions(Mat B) 1817807a1faSBarry Smith { 182dfbe8321SBarry Smith PetscErrorCode ierr; 183f3be49caSLisandro Dalcin const char *deft = MATAIJ; 184f3be49caSLisandro Dalcin char type[256]; 185273d9f13SBarry Smith PetscTruth flg; 186dbb450caSBarry Smith 1873a40ed3dSBarry Smith PetscFunctionBegin; 188f3be49caSLisandro Dalcin PetscValidHeaderSpecific(B,MAT_COOKIE,1); 189f3be49caSLisandro Dalcin 190f3be49caSLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Matrix options","Mat");CHKERRQ(ierr); 191f3be49caSLisandro Dalcin ierr = PetscOptionsList("-mat_type","Matrix type","MatSetType",MatList,deft,type,256,&flg);CHKERRQ(ierr); 192273d9f13SBarry Smith if (flg) { 193f3be49caSLisandro Dalcin ierr = MatSetType(B,type);CHKERRQ(ierr); 194f3be49caSLisandro Dalcin } else if (!((PetscObject)B)->type_name) { 195f3be49caSLisandro Dalcin ierr = MatSetType(B,deft);CHKERRQ(ierr); 196273d9f13SBarry Smith } 197f3be49caSLisandro Dalcin 198f3be49caSLisandro Dalcin if (B->ops->setfromoptions) { 199f3be49caSLisandro Dalcin ierr = (*B->ops->setfromoptions)(B);CHKERRQ(ierr); 200593c1905SSatish Balay } 201f3be49caSLisandro Dalcin 202f3be49caSLisandro Dalcin ierr = PetscOptionsEnd();CHKERRQ(ierr); 203f3be49caSLisandro Dalcin 2043a40ed3dSBarry Smith PetscFunctionReturn(0); 2057807a1faSBarry Smith } 2067807a1faSBarry Smith 2074a2ae208SSatish Balay #undef __FUNCT__ 2084a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation" 209bc08b0f1SBarry Smith /*@ 210273d9f13SBarry Smith MatSetUpPreallocation 211dae03382SLois Curfman McInnes 212273d9f13SBarry Smith Collective on Mat 213dae03382SLois Curfman McInnes 214273d9f13SBarry Smith Input Parameter: 215273d9f13SBarry Smith . A - the matrix 216d5d45c9bSBarry Smith 217273d9f13SBarry Smith Level: beginner 218d5d45c9bSBarry Smith 219273d9f13SBarry Smith .keywords: matrix, create 220273d9f13SBarry Smith 221273d9f13SBarry Smith .seealso: MatCreateSeqAIJ((), MatCreateMPIAIJ(), 222273d9f13SBarry Smith MatCreateSeqDense(), MatCreateMPIDense(), 223273d9f13SBarry Smith MatCreateMPIRowbs(), MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), 224273d9f13SBarry Smith MatCreateSeqSBAIJ(), MatCreateMPISBAIJ(), 225273d9f13SBarry Smith MatConvert() 226273d9f13SBarry Smith @*/ 227be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSetUpPreallocation(Mat B) 228273d9f13SBarry Smith { 229dfbe8321SBarry Smith PetscErrorCode ierr; 230273d9f13SBarry Smith 231273d9f13SBarry Smith PetscFunctionBegin; 23217667f90SBarry Smith if (!B->preallocated && B->ops->setuppreallocation) { 233ae15b995SBarry Smith ierr = PetscInfo(B,"Warning not preallocating matrix storage\n");CHKERRQ(ierr); 234273d9f13SBarry Smith ierr = (*B->ops->setuppreallocation)(B);CHKERRQ(ierr); 235273d9f13SBarry Smith } 236210f0121SBarry Smith B->preallocated = PETSC_TRUE; 237273d9f13SBarry Smith PetscFunctionReturn(0); 238273d9f13SBarry Smith } 239273d9f13SBarry Smith 240273d9f13SBarry Smith /* 241273d9f13SBarry Smith Copies from Cs header to A 242d0f46423SBarry Smith 243d0f46423SBarry Smith This is somewhat different from MatHeaderReplace() it would be nice to merge the code 244273d9f13SBarry Smith */ 2454a2ae208SSatish Balay #undef __FUNCT__ 2464a2ae208SSatish Balay #define __FUNCT__ "MatHeaderCopy" 247dfbe8321SBarry Smith PetscErrorCode MatHeaderCopy(Mat A,Mat C) 248273d9f13SBarry Smith { 249dfbe8321SBarry Smith PetscErrorCode ierr; 250d44834fbSBarry Smith PetscInt refct; 251273d9f13SBarry Smith PetscOps *Abops; 252273d9f13SBarry Smith MatOps Aops; 253273d9f13SBarry Smith char *mtype,*mname; 25430735b05SKris Buschelman void *spptr; 255273d9f13SBarry Smith 256273d9f13SBarry Smith PetscFunctionBegin; 257273d9f13SBarry Smith /* save the parts of A we need */ 2587adad957SLisandro Dalcin Abops = ((PetscObject)A)->bops; 259273d9f13SBarry Smith Aops = A->ops; 2607adad957SLisandro Dalcin refct = ((PetscObject)A)->refct; 2615c9eb25fSBarry Smith mtype = ((PetscObject)A)->type_name; 2625c9eb25fSBarry Smith mname = ((PetscObject)A)->name; 26330735b05SKris Buschelman spptr = A->spptr; 26430735b05SKris Buschelman 2655c9eb25fSBarry Smith /* zero these so the destroy below does not free them */ 2665c9eb25fSBarry Smith ((PetscObject)A)->type_name = 0; 2675c9eb25fSBarry Smith ((PetscObject)A)->name = 0; 2685c9eb25fSBarry Smith 2697c99f97cSSatish Balay /* free all the interior data structures from mat */ 2707c99f97cSSatish Balay ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 2717c99f97cSSatish Balay 27230735b05SKris Buschelman ierr = PetscFree(C->spptr);CHKERRQ(ierr); 27305b42c5fSBarry Smith 274*26283091SBarry Smith ierr = PetscLayoutDestroy(A->rmap);CHKERRQ(ierr); 275*26283091SBarry Smith ierr = PetscLayoutDestroy(A->cmap);CHKERRQ(ierr); 2765c9eb25fSBarry Smith ierr = PetscFListDestroy(&((PetscObject)A)->qlist);CHKERRQ(ierr); 2775c9eb25fSBarry Smith ierr = PetscOListDestroy(((PetscObject)A)->olist);CHKERRQ(ierr); 278273d9f13SBarry Smith 279273d9f13SBarry Smith /* copy C over to A */ 280273d9f13SBarry Smith ierr = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr); 281273d9f13SBarry Smith 282273d9f13SBarry Smith /* return the parts of A we saved */ 2837adad957SLisandro Dalcin ((PetscObject)A)->bops = Abops; 284273d9f13SBarry Smith A->ops = Aops; 2857adad957SLisandro Dalcin ((PetscObject)A)->refct = refct; 2867adad957SLisandro Dalcin ((PetscObject)A)->type_name = mtype; 2877adad957SLisandro Dalcin ((PetscObject)A)->name = mname; 28830735b05SKris Buschelman A->spptr = spptr; 289273d9f13SBarry Smith 2905c9eb25fSBarry Smith /* since these two are copied into A we do not want them destroyed in C */ 2915c9eb25fSBarry Smith ((PetscObject)C)->qlist = 0; 2925c9eb25fSBarry Smith ((PetscObject)C)->olist = 0; 293d38fa0fbSBarry Smith ierr = PetscHeaderDestroy(C);CHKERRQ(ierr); 294273d9f13SBarry Smith PetscFunctionReturn(0); 295273d9f13SBarry Smith } 2968ab5b326SKris Buschelman /* 2978ab5b326SKris Buschelman Replace A's header with that of C 2988ab5b326SKris Buschelman This is essentially code moved from MatDestroy 299d0f46423SBarry Smith 300d0f46423SBarry Smith This is somewhat different from MatHeaderCopy() it would be nice to merge the code 3018ab5b326SKris Buschelman */ 3028ab5b326SKris Buschelman #undef __FUNCT__ 3038ab5b326SKris Buschelman #define __FUNCT__ "MatHeaderReplace" 3048ab5b326SKris Buschelman PetscErrorCode MatHeaderReplace(Mat A,Mat C) 3058ab5b326SKris Buschelman { 3068ab5b326SKris Buschelman PetscErrorCode ierr; 3078ab5b326SKris Buschelman 3088ab5b326SKris Buschelman PetscFunctionBegin; 3096d7c1e57SBarry Smith if (A == C) PetscFunctionReturn(0); 3106d7c1e57SBarry Smith 3118ab5b326SKris Buschelman /* free all the interior data structures from mat */ 3128ab5b326SKris Buschelman ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 313d2c95936SBarry Smith ierr = PetscHeaderDestroy_Private((PetscObject)A);CHKERRQ(ierr); 314c650bb5fSLisandro Dalcin ierr = PetscFree(A->ops);CHKERRQ(ierr); 315*26283091SBarry Smith ierr = PetscLayoutDestroy(A->rmap);CHKERRQ(ierr); 316*26283091SBarry Smith ierr = PetscLayoutDestroy(A->cmap);CHKERRQ(ierr); 317d95db058SSatish Balay ierr = PetscFree(A->spptr);CHKERRQ(ierr); 3188ab5b326SKris Buschelman 3198ab5b326SKris Buschelman /* copy C over to A */ 3208ab5b326SKris Buschelman if (C) { 3218ab5b326SKris Buschelman ierr = PetscMemcpy(A,C,sizeof(struct _p_Mat));CHKERRQ(ierr); 3228ab5b326SKris Buschelman ierr = PetscLogObjectDestroy((PetscObject)C);CHKERRQ(ierr); 3238ab5b326SKris Buschelman ierr = PetscFree(C);CHKERRQ(ierr); 3248ab5b326SKris Buschelman } 3258ab5b326SKris Buschelman PetscFunctionReturn(0); 3268ab5b326SKris Buschelman } 327