1 #ifndef lint 2 static char vcid[] = "$Id: shell.c,v 1.28 1996/03/28 22:00:13 curfman Exp curfman $"; 3 #endif 4 5 /* 6 This provides a simple shell for Fortran (and C programmers) to 7 create a very simple matrix class for use with KSP without coding 8 much of anything. 9 */ 10 11 #include "petsc.h" 12 #include "matimpl.h" /*I "mat.h" I*/ 13 #include "vec/vecimpl.h" 14 15 typedef struct { 16 int m, n; /* rows, columns */ 17 int (*destroy)(void*); 18 void *ctx; 19 } Mat_Shell; 20 21 /*@ 22 MatShellGetContext - Returns the user-provided context associated with a shell matrix. 23 24 Input Parameter: 25 . mat - the matrix, should have been created with MatCreateShell() 26 27 Output Parameter: 28 . ctx - the user provided context 29 30 Notes: 31 This routine is intended for use within various shell matrix routines, 32 as set with MatShellSetOperation(). 33 34 .keywords: matrix, shell, get, context 35 36 .seealso: MatCreateShell(), MatShellSetOperation() 37 @*/ 38 int MatShellGetContext(Mat mat,void **ctx) 39 { 40 PetscValidHeaderSpecific(mat,MAT_COOKIE); 41 if (mat->type != MATSHELL) *ctx = 0; 42 else *ctx = ((Mat_Shell *) (mat->data))->ctx; 43 return 0; 44 } 45 46 static int MatGetSize_Shell(Mat mat,int *m,int *n) 47 { 48 Mat_Shell *shell = (Mat_Shell *) mat->data; 49 *m = shell->m; *n = shell->n; 50 return 0; 51 } 52 53 static int MatDestroy_Shell(PetscObject obj) 54 { 55 int ierr; 56 Mat mat = (Mat) obj; 57 Mat_Shell *shell; 58 59 shell = (Mat_Shell *) mat->data; 60 if (shell->destroy) {ierr = (*shell->destroy)(obj);CHKERRQ(ierr);} 61 PetscFree(shell); 62 return 0; 63 } 64 65 static struct _MatOps MatOps = {0,0, 66 0, 67 0,0,0,0, 68 0,0,0,0, 69 0,0, 70 0, 71 0, 72 0,0,0, 73 0, 74 0,0,0, 75 0,0, 76 0, 77 0,0,0,0, 78 0,0,MatGetSize_Shell, 79 0,0,0,0, 80 0,0,0,0 }; 81 82 /*@C 83 MatCreateShell - Creates a new matrix class for use with a user-defined 84 private data storage format. 85 86 Input Parameters: 87 . comm - MPI communicator 88 . m - number of rows 89 . n - number of columns 90 . ctx - pointer to data needed by the matrix routines 91 92 Output Parameter: 93 . mat - the matrix 94 95 Notes: 96 The shell matrix type is intended to provide a simple class to use 97 with KSP (such as, for use with matrix-free methods). You should not 98 use the shell type if you plan to define a complete matrix class. 99 100 Usage: 101 $ MatCreateShell(m,n,ctx,&mat); 102 $ MatShellSetOperation(mat,MAT_MULT,mult); 103 $ [ Use matrix for operations that have been set ] 104 $ MatDestroy(mat); 105 106 .keywords: matrix, shell, create 107 108 .seealso: MatSetOperation(), MatHasOperation(), MatShellGetContext() 109 @*/ 110 int MatCreateShell(MPI_Comm comm,int m,int n,void *ctx,Mat *mat) 111 { 112 Mat newmat; 113 Mat_Shell *shell; 114 115 PetscHeaderCreate(newmat,_Mat,MAT_COOKIE,MATSHELL,comm); 116 PLogObjectCreate(newmat); 117 *mat = newmat; 118 newmat->factor = 0; 119 newmat->destroy = MatDestroy_Shell; 120 newmat->assembled = PETSC_TRUE; 121 PetscMemcpy(&newmat->ops,&MatOps,sizeof(struct _MatOps)); 122 123 shell = PetscNew(Mat_Shell); CHKPTRQ(shell); 124 PetscMemzero(shell,sizeof(Mat_Shell)); 125 newmat->data = (void *) shell; 126 shell->m = m; 127 shell->n = n; 128 shell->ctx = ctx; 129 return 0; 130 } 131 132 /*@ 133 MatShellSetOperation - Allows user to set a matrix operation for a shell matrix. 134 135 Input Parameters: 136 . mat - the shell matrix 137 . op - the name of the operation 138 . f - the function that provides the operation. 139 140 Usage: 141 $ extern int usermult(Mat,Vec,Vec); 142 $ ierr = MatCreateShell(comm,m,m,ctx,&A); 143 $ ierr = MatShellSetOperation(A,MAT_MULT,usermult); 144 145 Notes: 146 See the file petsc/include/mat.h for a complete list of matrix 147 operations, which all have the form MAT_<OPERATION>, where 148 <OPERATION> is the name (in all capital letters) of the 149 user interface routine (e.g., MatMult() -> MAT_MULT). 150 151 All user-provided functions should have the same calling 152 sequence as the usual matrix interface routines, e.g., 153 $ MatMult(Mat,Vec,Vec) -> usermult(Mat,Vec,Vec) 154 since they are intended to be accessed via the usual 155 matrix interface routines. 156 157 Within each user-defined routine, the user should call 158 MatShellGetContext() to obtain the user-defined context that was 159 set by MatCreateShell(). 160 161 .keywords: matrix, shell, set, operation 162 163 .seealso: MatCreateShell(), MatShellGetContext() 164 @*/ 165 int MatShellSetOperation(Mat mat,MatOperation op, void *f) 166 { 167 PetscValidHeaderSpecific(mat,MAT_COOKIE); 168 169 if (op == MAT_DESTROY) { 170 if (mat->type == MATSHELL) { 171 Mat_Shell *shell = (Mat_Shell *) mat->data; 172 shell->destroy = (int (*)(void*)) f; 173 } 174 else mat->destroy = (int (*)(PetscObject)) f; 175 } 176 else if (op == MAT_VIEW) mat->view = (int (*)(PetscObject,Viewer)) f; 177 else (((void **)&mat->ops)[op]) = f; 178 179 return 0; 180 } 181 182