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