1 2 /* 3 This provides a simple shell for Fortran (and C programmers) to 4 create a very simple matrix class for use with KSP without coding 5 mush of anything. 6 */ 7 8 #include "petsc.h" 9 #include "matimpl.h" /*I "mat.h" I*/ 10 #include "vec/vecimpl.h" 11 12 typedef struct { 13 int m,n; 14 int (*mult)(void *,Vec,Vec); 15 int (*multtransadd)(void*,Vec,Vec,Vec); 16 void *ctx; 17 } MatShell; 18 19 static int MatShellMult(Mat mat,Vec x,Vec y) 20 { 21 MatShell *shell; 22 shell = (MatShell *) mat->data; 23 return (*shell->mult)(shell->ctx,x,y); 24 } 25 static int MatShellMultTransAdd(Mat mat,Vec x,Vec y,Vec z) 26 { 27 MatShell *shell; 28 shell = (MatShell *) mat->data; 29 return (*shell->multtransadd)(shell->ctx,x,y,z); 30 } 31 static int MatShellDestroy(PetscObject obj) 32 { 33 Mat mat = (Mat) obj; 34 MatShell *shell; 35 shell = (MatShell *) mat->data; 36 FREE(shell); FREE(mat); 37 return 0; 38 } 39 40 static struct _MatOps MatOps = {0,0, 41 0, 42 MatShellMult,0,0,MatShellMultTransAdd, 43 0,0,0,0, 44 0,0, 45 0, 46 0, 47 0,0,0, 48 0, 49 0,0,0, 50 0,0, 51 0, 52 0,0,0,0, 53 0,0 }; 54 55 /*@ 56 MatShellCreate - creates a new matrix class for use with your 57 own private data storage format. This is intended to 58 provide a simple class to use with KSP. You should 59 not use this if you plan to make a complete class. 60 61 Input Parameters: 62 . m,n - number of rows and columns in matrix 63 . ctx - pointer to your data needed by matrix multiply. 64 65 Output Parameters: 66 . mat - the matrix 67 68 Keywords: matrix, shell 69 70 Usage: 71 . int (*mult)(void *,Vec,Vec); 72 . MatShellCreate(m,n,ctx,&mat); 73 . MatShellSetMult(mat,mult); 74 75 @*/ 76 int MatShellCreate(int m, int n, void *ctx,Mat *mat) 77 { 78 Mat newmat; 79 MatShell *shell; 80 CREATEHEADER(newmat,_Mat); 81 *mat = newmat; 82 newmat->cookie = MAT_COOKIE; 83 newmat->factor = 0; 84 newmat->row = 0; 85 newmat->col = 0; 86 newmat->destroy= MatShellDestroy; 87 newmat->ops = &MatOps; 88 shell = NEW(MatShell); CHKPTR(shell); 89 newmat->data = (void *) shell; 90 shell->mult = 0; 91 shell->m = m; 92 shell->n = n; 93 shell->ctx = ctx; 94 return 0; 95 } 96 97 /*@ 98 MatShellSetMult - sets routine to use as matrix vector multiply. 99 100 Input Parameters: 101 . mat - the matrix to add the operation to, created with MatShellCreate() 102 . mult - the matrix vector multiply routine. 103 104 Keywords: matrix, multiply 105 @*/ 106 int MatShellSetMult(Mat mat, int (*mult)(void*,Vec,Vec)) 107 { 108 MatShell *shell; 109 VALIDHEADER(mat,MAT_COOKIE); 110 shell = (MatShell *) mat->data; 111 shell->mult = mult; 112 return 0; 113 } 114 /*@ 115 MatShellSetMultTransAdd - sets routine to use as matrix vector multiply. 116 117 Input Parameters: 118 . mat - the matrix to add the operation to, created with MatShellCreate() 119 . mult - the matrix vector multiply routine. 120 121 Keywords: matrix, multiply, transpose 122 @*/ 123 int MatShellSetMultTransAdd(Mat mat, int (*mult)(void*,Vec,Vec,Vec)) 124 { 125 MatShell *shell; 126 VALIDHEADER(mat,MAT_COOKIE); 127 shell = (MatShell *) mat->data; 128 shell->multtransadd = mult; 129 return 0; 130 } 131 132 133