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 67 Output Parameters: 68 . mat - the matrix 69 70 Keywords: matrix, shell 71 72 Usage: 73 . int (*mult)(void *,Vec,Vec); 74 . MatShellCreate(m,n,ctx,&mat); 75 . MatShellSetMult(mat,mult); 76 77 @*/ 78 int MatShellCreate(int m, int n, void *ctx,Mat *mat) 79 { 80 Mat newmat; 81 MatShell *shell; 82 PETSCHEADERCREATE(newmat,_Mat,MAT_COOKIE,MATSHELL,MPI_COMM_WORLD); 83 PLogObjectCreate(newmat); 84 *mat = newmat; 85 newmat->factor = 0; 86 newmat->row = 0; 87 newmat->col = 0; 88 newmat->destroy= MatShellDestroy; 89 newmat->ops = &MatOps; 90 shell = NEW(MatShell); CHKPTR(shell); 91 newmat->data = (void *) shell; 92 shell->mult = 0; 93 shell->m = m; 94 shell->n = n; 95 shell->ctx = ctx; 96 return 0; 97 } 98 99 /*@ 100 MatShellSetMult - sets routine to use as matrix vector multiply. 101 102 Input Parameters: 103 . mat - the matrix to add the operation to, created with MatShellCreate() 104 . mult - the matrix vector multiply routine. 105 106 Keywords: matrix, multiply 107 @*/ 108 int MatShellSetMult(Mat mat, int (*mult)(void*,Vec,Vec)) 109 { 110 MatShell *shell; 111 VALIDHEADER(mat,MAT_COOKIE); 112 shell = (MatShell *) mat->data; 113 shell->mult = mult; 114 return 0; 115 } 116 /*@ 117 MatShellSetMultTransAdd - sets routine to use as matrix vector multiply. 118 119 Input Parameters: 120 . mat - the matrix to add the operation to, created with MatShellCreate() 121 . mult - the matrix vector multiply routine. 122 123 Keywords: matrix, multiply, transpose 124 @*/ 125 int MatShellSetMultTransAdd(Mat mat, int (*mult)(void*,Vec,Vec,Vec)) 126 { 127 MatShell *shell; 128 VALIDHEADER(mat,MAT_COOKIE); 129 shell = (MatShell *) mat->data; 130 shell->multtransadd = mult; 131 return 0; 132 } 133 134 135