1 #ifndef lint 2 static char vcid[] = "$Id: shell.c,v 1.18 1995/09/21 20:10:33 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 mush 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 (*mult)(void*,Vec,Vec); 18 int (*multtransadd)(void*,Vec,Vec,Vec); 19 int (*destroy)(void*); 20 int (*getsize)(void*,int*,int*); 21 void *ctx; 22 } Mat_Shell; 23 24 static int MatGetSize_Shell(Mat mat,int *m,int *n) 25 { 26 Mat_Shell *shell = (Mat_Shell *) mat->data; 27 *m = shell->m; *n = shell->n; 28 return 0; 29 } 30 31 static int MatMult_Shell(Mat mat,Vec x,Vec y) 32 { 33 Mat_Shell *shell = (Mat_Shell *) mat->data; 34 return (*shell->mult)(shell->ctx,x,y); 35 } 36 37 static int MatMultTransAdd_Shell(Mat mat,Vec x,Vec y,Vec z) 38 { 39 Mat_Shell *shell = (Mat_Shell *) mat->data; 40 return (*shell->multtransadd)(shell->ctx,x,y,z); 41 } 42 43 static int MatDestroy_Shell(PetscObject obj) 44 { 45 int ierr; 46 Mat mat = (Mat) obj; 47 Mat_Shell *shell; 48 shell = (Mat_Shell *) mat->data; 49 if (shell->destroy) {ierr = (*shell->destroy)(shell->ctx);CHKERRQ(ierr);} 50 PETSCFREE(shell); 51 PLogObjectDestroy(mat); 52 PETSCHEADERDESTROY(mat); 53 return 0; 54 } 55 56 static struct _MatOps MatOps = {0,0, 57 0, 58 MatMult_Shell,0,0,MatMultTransAdd_Shell, 59 0,0,0,0, 60 0,0, 61 0, 62 0, 63 0,0,0, 64 0, 65 0,0,0, 66 0,0, 67 0, 68 0,0,0,0, 69 0,0,MatGetSize_Shell, 70 0,0,0,0, 71 0,0,0,0 }; 72 73 /*@C 74 MatShellCreate - Creates a new matrix class for use with a user-defined 75 private data storage format. 76 77 Input Parameters: 78 . comm - MPI communicator 79 . m - number of rows 80 . n - number of columns 81 . ctx - pointer to data needed by matrix-vector multiplication routine(s) 82 83 Output Parameter: 84 . mat - the matrix 85 86 Notes: 87 The shell matrix type is intended to provide a simple class to use 88 with KSP (such as, for use with matrix-free methods). You should not 89 use the shell type if you plan to define a complete matrix class. 90 91 Usage: 92 $ MatShellCreate(m,n,ctx,&mat); 93 $ MatShellSetMult(mat,mult); 94 95 .keywords: matrix, shell, create 96 97 .seealso: MatShellSetMult(), MatShellSetMultTransAdd() 98 @*/ 99 int MatShellCreate(MPI_Comm comm,int m,int n,void *ctx,Mat *mat) 100 { 101 Mat newmat; 102 Mat_Shell *shell; 103 PETSCHEADERCREATE(newmat,_Mat,MAT_COOKIE,MATSHELL,comm); 104 PLogObjectCreate(newmat); 105 *mat = newmat; 106 newmat->factor = 0; 107 newmat->destroy= MatDestroy_Shell; 108 PetscMemcpy(&newmat->ops,&MatOps,sizeof(struct _MatOps)); 109 shell = PETSCNEW(Mat_Shell); CHKPTRQ(shell); 110 PetscZero(shell,sizeof(Mat_Shell)); 111 newmat->data = (void *) shell; 112 shell->mult = 0; 113 shell->m = m; 114 shell->n = n; 115 shell->ctx = ctx; 116 return 0; 117 } 118 119 /*@C 120 MatShellSetMult - Sets the routine for computing the matrix-vector product. 121 122 Input Parameters: 123 . mat - the matrix associated with this operation, created 124 with MatShellCreate() 125 . mult - the user-defined routine 126 127 Calling sequence of mult: 128 int mult (void *ptr,Vec xin,Vec xout) 129 . ptr - the application context for matrix data 130 . xin - input vector 131 . xout - output vector 132 133 .keywords: matrix, multiply, shell, set 134 135 .seealso: MatShellCreate(), MatShellSetMultTransAdd() 136 @*/ 137 int MatShellSetMult(Mat mat,int (*mult)(void*,Vec,Vec)) 138 { 139 Mat_Shell *shell; 140 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 141 shell = (Mat_Shell *) mat->data; 142 shell->mult = mult; 143 return 0; 144 } 145 /*@C 146 MatShellSetMultTransAdd - Sets the routine for computing v3 = v2 + A' * v1. 147 148 Input Parameters: 149 . mat - the matrix associated with this operation, created 150 with MatShellCreate() 151 . mult - the user-defined routine 152 153 Calling sequence of mult: 154 int mult (void *ptr,Vec v1,Vec v2,Vec v3) 155 . ptr - the application context for matrix data 156 . v1, v2 - the input vectors 157 . v3 - the result 158 159 .keywords: matrix, multiply, transpose 160 161 .seealso: MatShellCreate(), MatShellSetMult() 162 @*/ 163 int MatShellSetMultTransAdd(Mat mat,int (*mult)(void*,Vec,Vec,Vec)) 164 { 165 Mat_Shell *shell; 166 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 167 shell = (Mat_Shell *) mat->data; 168 shell->multtransadd = mult; 169 return 0; 170 } 171 /*@C 172 MatShellSetDestroy - Set the routine to use to destroy the 173 private contents of your MatShell. 174 175 Input Parameters: 176 . mat - the matrix associated with this operation, created 177 with MatShellCreate() 178 . destroy - the user-defined routine 179 180 Calling sequence of mult: 181 int destroy (void *ptr) 182 . ptr - the application context for matrix data 183 184 .keywords: matrix, destroy, shell, set 185 186 .seealso: MatShellCreate() 187 @*/ 188 int MatShellSetDestroy(Mat mat,int (*destroy)(void*)) 189 { 190 Mat_Shell *shell; 191 PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 192 shell = (Mat_Shell *) mat->data; 193 shell->destroy = destroy; 194 return 0; 195 } 196 197 198