1 #ifndef lint 2 static char vcid[] = "$Id: shell.c,v 1.27 1996/03/26 04:46:43 bsmith Exp bsmith $"; 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 23 with a MatShell. 24 25 Input Parameter: 26 . mat - the matrix, should have been created with MatCreateShell() 27 28 Output Parameter: 29 . ctx - the user provided context 30 31 @*/ 32 int MatShellGetContext(Mat mat,void **ctx) 33 { 34 PetscValidHeaderSpecific(mat,MAT_COOKIE); 35 if (mat->type != MATSHELL) *ctx = 0; 36 else *ctx = ((Mat_Shell *) (mat->data))->ctx; 37 return 0; 38 } 39 40 static int MatGetSize_Shell(Mat mat,int *m,int *n) 41 { 42 Mat_Shell *shell = (Mat_Shell *) mat->data; 43 *m = shell->m; *n = shell->n; 44 return 0; 45 } 46 47 static int MatDestroy_Shell(PetscObject obj) 48 { 49 int ierr; 50 Mat mat = (Mat) obj; 51 Mat_Shell *shell; 52 53 shell = (Mat_Shell *) mat->data; 54 if (shell->destroy) {ierr = (*shell->destroy)(shell->ctx);CHKERRQ(ierr);} 55 PetscFree(shell); 56 return 0; 57 } 58 59 static struct _MatOps MatOps = {0,0, 60 0, 61 0,0,0,0, 62 0,0,0,0, 63 0,0, 64 0, 65 0, 66 0,0,0, 67 0, 68 0,0,0, 69 0,0, 70 0, 71 0,0,0,0, 72 0,0,MatGetSize_Shell, 73 0,0,0,0, 74 0,0,0,0 }; 75 76 /*@C 77 MatCreateShell - Creates a new matrix class for use with a user-defined 78 private data storage format. 79 80 Input Parameters: 81 . comm - MPI communicator 82 . m - number of rows 83 . n - number of columns 84 . ctx - pointer to data needed by the matrix routines 85 86 Output Parameter: 87 . mat - the matrix 88 89 Notes: 90 The shell matrix type is intended to provide a simple class to use 91 with KSP (such as, for use with matrix-free methods). You should not 92 use the shell type if you plan to define a complete matrix class. 93 94 Usage: 95 $ MatCreateShell(m,n,ctx,&mat); 96 $ MatSetOperation(mat,MAT_MULT,mult); 97 98 .keywords: matrix, shell, create 99 100 .seealso: MatSetOperation(), MatHasOperation(), MatShellGetContext() 101 @*/ 102 int MatCreateShell(MPI_Comm comm,int m,int n,void *ctx,Mat *mat) 103 { 104 Mat newmat; 105 Mat_Shell *shell; 106 107 PetscHeaderCreate(newmat,_Mat,MAT_COOKIE,MATSHELL,comm); 108 PLogObjectCreate(newmat); 109 *mat = newmat; 110 newmat->factor = 0; 111 newmat->destroy = MatDestroy_Shell; 112 newmat->assembled = PETSC_TRUE; 113 PetscMemcpy(&newmat->ops,&MatOps,sizeof(struct _MatOps)); 114 115 shell = PetscNew(Mat_Shell); CHKPTRQ(shell); 116 PetscMemzero(shell,sizeof(Mat_Shell)); 117 newmat->data = (void *) shell; 118 shell->m = m; 119 shell->n = n; 120 shell->ctx = ctx; 121 return 0; 122 } 123 124 /*@ 125 MatShellSetOperation - Allows use to set a matrix operation for a shell matrix. 126 127 Input Parameters: 128 . mat - the shell matrix 129 . op - the name of the operation 130 . f - the function that provides the operation. 131 132 Usage: 133 extern int mult(Mat,Vec,Vec); 134 ierr = MatCreateShell(comm,m,m,ctx,&A); 135 ierr = MatSetOperation(A,MAT_MULT,mult); 136 137 In the user provided function, use MatShellGetContext() to obtain the 138 context passed into MatCreateShell(). 139 @*/ 140 int MatShellSetOperation(Mat mat,MatOperation op, void *f) 141 { 142 PetscValidHeaderSpecific(mat,MAT_COOKIE); 143 144 if (op == MAT_DESTROY) mat->destroy = (int (*)(PetscObject)) f; 145 else if (op == MAT_VIEW) mat->view = (int (*)(PetscObject,Viewer)) f; 146 else (((void **)&mat->ops)[op])= f; 147 return 0; 148 } 149 150