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); 37 PLogObjectDestroy(mat); 38 PETSCHEADERDESTROY(mat); 39 return 0; 40 } 41 42 static struct _MatOps MatOps = {0,0, 43 0, 44 MatShellMult,0,0,MatShellMultTransAdd, 45 0,0,0,0, 46 0,0, 47 0, 48 0, 49 0,0,0, 50 0, 51 0,0,0, 52 0,0, 53 0, 54 0,0,0,0, 55 0,0 }; 56 57 /*@ 58 MatShellCreate - creates a new matrix class for use with your 59 own private data storage format. This is intended to 60 provide a simple class to use with KSP. You should 61 not use this if you plan to make a complete class. 62 63 Input Parameters: 64 . m,n - number of rows and columns in matrix 65 . ctx - pointer to your data needed by matrix multiply. 66 . comm - MPI communicator 67 68 Output Parameters: 69 . mat - the matrix 70 71 Keywords: matrix, shell 72 73 Usage: 74 . int (*mult)(void *,Vec,Vec); 75 . MatShellCreate(m,n,ctx,&mat); 76 . MatShellSetMult(mat,mult); 77 78 @*/ 79 int MatShellCreate(MPI_Comm comm,int m, int n, void *ctx,Mat *mat) 80 { 81 Mat newmat; 82 MatShell *shell; 83 PETSCHEADERCREATE(newmat,_Mat,MAT_COOKIE,MATSHELL,comm); 84 PLogObjectCreate(newmat); 85 *mat = newmat; 86 newmat->factor = 0; 87 newmat->row = 0; 88 newmat->col = 0; 89 newmat->destroy= MatShellDestroy; 90 newmat->ops = &MatOps; 91 shell = NEW(MatShell); CHKPTR(shell); 92 newmat->data = (void *) shell; 93 shell->mult = 0; 94 shell->m = m; 95 shell->n = n; 96 shell->ctx = ctx; 97 return 0; 98 } 99 100 /*@ 101 MatShellSetMult - sets routine to use as matrix vector multiply. 102 103 Input Parameters: 104 . mat - the matrix to add the operation to, created with MatShellCreate() 105 . mult - the matrix vector multiply routine. 106 107 Keywords: matrix, multiply 108 @*/ 109 int MatShellSetMult(Mat mat, int (*mult)(void*,Vec,Vec)) 110 { 111 MatShell *shell; 112 VALIDHEADER(mat,MAT_COOKIE); 113 shell = (MatShell *) mat->data; 114 shell->mult = mult; 115 return 0; 116 } 117 /*@ 118 MatShellSetMultTransAdd - sets routine to use as matrix vector multiply. 119 120 Input Parameters: 121 . mat - the matrix to add the operation to, created with MatShellCreate() 122 . mult - the matrix vector multiply routine. 123 124 Keywords: matrix, multiply, transpose 125 @*/ 126 int MatShellSetMultTransAdd(Mat mat, int (*mult)(void*,Vec,Vec,Vec)) 127 { 128 MatShell *shell; 129 VALIDHEADER(mat,MAT_COOKIE); 130 shell = (MatShell *) mat->data; 131 shell->multtransadd = mult; 132 return 0; 133 } 134 135 136