1 2 /* 3 Mechanism for register PETSc matrix types 4 */ 5 #include <petsc-private/matimpl.h> /*I "petscmat.h" I*/ 6 7 PetscBool MatRegisterAllCalled = PETSC_FALSE; 8 9 /* 10 Contains the list of registered Mat routines 11 */ 12 PetscFunctionList MatList = 0; 13 14 #undef __FUNCT__ 15 #define __FUNCT__ "MatSetType" 16 /*@C 17 MatSetType - Builds matrix object for a particular matrix type 18 19 Collective on Mat 20 21 Input Parameters: 22 + mat - the matrix object 23 - matype - matrix type 24 25 Options Database Key: 26 . -mat_type <method> - Sets the type; use -help for a list 27 of available methods (for instance, seqaij) 28 29 Notes: 30 See "${PETSC_DIR}/include/petscmat.h" for available methods 31 32 Level: intermediate 33 34 .keywords: Mat, MatType, set, method 35 36 .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat 37 @*/ 38 PetscErrorCode MatSetType(Mat mat, MatType matype) 39 { 40 PetscErrorCode ierr,(*r)(Mat); 41 PetscBool sametype,found; 42 MatBaseName names = MatBaseNameList; 43 44 PetscFunctionBegin; 45 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 46 47 while (names) { 48 ierr = PetscStrcmp(matype,names->bname,&found);CHKERRQ(ierr); 49 if (found) { 50 PetscMPIInt size; 51 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr); 52 if (size == 1) matype = names->sname; 53 else matype = names->mname; 54 break; 55 } 56 names = names->next; 57 } 58 59 ierr = PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr); 60 if (sametype) PetscFunctionReturn(0); 61 62 ierr = PetscFunctionListFind(PetscObjectComm((PetscObject)mat),MatList,matype,PETSC_TRUE,(void(**)(void))&r);CHKERRQ(ierr); 63 if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype); 64 65 /* free the old data structure if it existed */ 66 if (mat->ops->destroy) { 67 ierr = (*mat->ops->destroy)(mat);CHKERRQ(ierr); 68 69 mat->ops->destroy = NULL; 70 } 71 mat->preallocated = PETSC_FALSE; 72 73 /* create the new data structure */ 74 ierr = (*r)(mat);CHKERRQ(ierr); 75 PetscFunctionReturn(0); 76 } 77 78 79 #undef __FUNCT__ 80 #define __FUNCT__ "MatRegisterDestroy" 81 /*@C 82 MatRegisterDestroy - Frees the list of matrix types that were 83 registered by MatRegister()/MatRegisterDynamic(). 84 85 Not Collective 86 87 Level: advanced 88 89 .keywords: Mat, register, destroy 90 91 .seealso: MatRegister(), MatRegisterAll(), MatRegisterDynamic() 92 @*/ 93 PetscErrorCode MatRegisterDestroy(void) 94 { 95 PetscErrorCode ierr; 96 97 PetscFunctionBegin; 98 ierr = PetscFunctionListDestroy(&MatList);CHKERRQ(ierr); 99 100 MatRegisterAllCalled = PETSC_FALSE; 101 PetscFunctionReturn(0); 102 } 103 104 #undef __FUNCT__ 105 #define __FUNCT__ "MatGetType" 106 /*@C 107 MatGetType - Gets the matrix type as a string from the matrix object. 108 109 Not Collective 110 111 Input Parameter: 112 . mat - the matrix 113 114 Output Parameter: 115 . name - name of matrix type 116 117 Level: intermediate 118 119 .keywords: Mat, MatType, get, method, name 120 121 .seealso: MatSetType() 122 @*/ 123 PetscErrorCode MatGetType(Mat mat,MatType *type) 124 { 125 PetscFunctionBegin; 126 PetscValidHeaderSpecific(mat,MAT_CLASSID,1); 127 PetscValidPointer(type,2); 128 *type = ((PetscObject)mat)->type_name; 129 PetscFunctionReturn(0); 130 } 131 132 133 #undef __FUNCT__ 134 #define __FUNCT__ "MatRegister" 135 /*@C 136 MatRegister - See MatRegisterDynamic() 137 138 Level: advanced 139 @*/ 140 PetscErrorCode MatRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(Mat)) 141 { 142 PetscErrorCode ierr; 143 char fullname[PETSC_MAX_PATH_LEN]; 144 145 PetscFunctionBegin; 146 ierr = PetscFunctionListConcat(path,name,fullname);CHKERRQ(ierr); 147 ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,&MatList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 148 PetscFunctionReturn(0); 149 } 150 151 MatBaseName MatBaseNameList = 0; 152 153 #undef __FUNCT__ 154 #define __FUNCT__ "MatRegisterBaseName" 155 /*@C 156 MatRegisterBaseName - Registers a name that can be used for either a sequential or its corresponding parallel matrix type. 157 158 Input Parameters: 159 + bname - the basename, for example, MATAIJ 160 . sname - the name of the sequential matrix type, for example, MATSEQAIJ 161 - mname - the name of the parallel matrix type, for example, MATMPIAIJ 162 163 164 Level: advanced 165 @*/ 166 PetscErrorCode MatRegisterBaseName(const char bname[],const char sname[],const char mname[]) 167 { 168 PetscErrorCode ierr; 169 MatBaseName names; 170 171 PetscFunctionBegin; 172 ierr = PetscNew(struct _p_MatBaseName,&names);CHKERRQ(ierr); 173 ierr = PetscStrallocpy(bname,&names->bname);CHKERRQ(ierr); 174 ierr = PetscStrallocpy(sname,&names->sname);CHKERRQ(ierr); 175 ierr = PetscStrallocpy(mname,&names->mname);CHKERRQ(ierr); 176 if (!MatBaseNameList) { 177 MatBaseNameList = names; 178 } else { 179 MatBaseName next = MatBaseNameList; 180 while (next->next) next = next->next; 181 next->next = names; 182 } 183 PetscFunctionReturn(0); 184 } 185 186 187 188 189 190 191 192