1357feee3SLois Curfman McInnes #ifndef lint 2*b9fa9cd0SBarry Smith static char vcid[] = "$Id: shell.c,v 1.11 1995/08/01 19:24:44 curfman Exp bsmith $"; 3357feee3SLois Curfman McInnes #endif 4e51e0e81SBarry Smith 5e51e0e81SBarry Smith /* 620563c6bSBarry Smith This provides a simple shell for Fortran (and C programmers) to 720563c6bSBarry Smith create a very simple matrix class for use with KSP without coding 820563c6bSBarry Smith mush of anything. 9e51e0e81SBarry Smith */ 10e51e0e81SBarry Smith 11e51e0e81SBarry Smith #include "petsc.h" 12e51e0e81SBarry Smith #include "matimpl.h" /*I "mat.h" I*/ 13e51e0e81SBarry Smith #include "vec/vecimpl.h" 14e51e0e81SBarry Smith 1520563c6bSBarry Smith typedef struct { 1620563c6bSBarry Smith int m,n; 1720563c6bSBarry Smith int (*mult)(void *,Vec,Vec); 18f0479e8cSBarry Smith int (*multtransadd)(void*,Vec,Vec,Vec); 19*b9fa9cd0SBarry Smith int (*destroy)(void *); 2020563c6bSBarry Smith void *ctx; 2120563c6bSBarry Smith } MatShell; 22e51e0e81SBarry Smith 2320563c6bSBarry Smith static int MatShellMult(Mat mat,Vec x,Vec y) 24e51e0e81SBarry Smith { 2520563c6bSBarry Smith MatShell *shell; 2620563c6bSBarry Smith shell = (MatShell *) mat->data; 2720563c6bSBarry Smith return (*shell->mult)(shell->ctx,x,y); 28e51e0e81SBarry Smith } 29f0479e8cSBarry Smith static int MatShellMultTransAdd(Mat mat,Vec x,Vec y,Vec z) 30f0479e8cSBarry Smith { 31f0479e8cSBarry Smith MatShell *shell; 32f0479e8cSBarry Smith shell = (MatShell *) mat->data; 33f0479e8cSBarry Smith return (*shell->multtransadd)(shell->ctx,x,y,z); 34f0479e8cSBarry Smith } 3520563c6bSBarry Smith static int MatShellDestroy(PetscObject obj) 36e51e0e81SBarry Smith { 37*b9fa9cd0SBarry Smith int ierr; 3820563c6bSBarry Smith Mat mat = (Mat) obj; 3920563c6bSBarry Smith MatShell *shell; 4020563c6bSBarry Smith shell = (MatShell *) mat->data; 41*b9fa9cd0SBarry Smith if (shell->destroy) {ierr = (*shell->destroy)(shell->ctx);CHKERRQ(ierr);} 4278b31e54SBarry Smith PETSCFREE(shell); 43a5a9c739SBarry Smith PLogObjectDestroy(mat); 449e25ed09SBarry Smith PETSCHEADERDESTROY(mat); 45e51e0e81SBarry Smith return 0; 46e51e0e81SBarry Smith } 47e51e0e81SBarry Smith 4820563c6bSBarry Smith static struct _MatOps MatOps = {0,0, 4920563c6bSBarry Smith 0, 50f0479e8cSBarry Smith MatShellMult,0,0,MatShellMultTransAdd, 5120563c6bSBarry Smith 0,0,0,0, 5220563c6bSBarry Smith 0,0, 5320563c6bSBarry Smith 0, 5420563c6bSBarry Smith 0, 5520563c6bSBarry Smith 0,0,0, 5620563c6bSBarry Smith 0, 5720563c6bSBarry Smith 0,0,0, 5820563c6bSBarry Smith 0,0, 5920563c6bSBarry Smith 0, 6020563c6bSBarry Smith 0,0,0,0, 6120563c6bSBarry Smith 0,0 }; 62e51e0e81SBarry Smith 63e51e0e81SBarry Smith /*@ 64ff756334SLois Curfman McInnes MatShellCreate - Creates a new matrix class for use with a user-defined 65ff756334SLois Curfman McInnes private data storage format. 66e51e0e81SBarry Smith 67e51e0e81SBarry Smith Input Parameters: 686b5873e3SBarry Smith . comm - MPI communicator 69ff756334SLois Curfman McInnes . m - number of rows 70ff756334SLois Curfman McInnes . n - number of columns 710b627109SLois Curfman McInnes . ctx - pointer to data needed by matrix-vector multiplication routine(s) 72e51e0e81SBarry Smith 73ff756334SLois Curfman McInnes Output Parameter: 74e51e0e81SBarry Smith . mat - the matrix 75e51e0e81SBarry Smith 76ff756334SLois Curfman McInnes Notes: 77ff756334SLois Curfman McInnes The shell matrix type is intended to provide a simple class to use 78ff756334SLois Curfman McInnes with KSP (such as, for use with matrix-free methods). You should not 79ff756334SLois Curfman McInnes use the shell type if you plan to define a complete matrix class. 80e51e0e81SBarry Smith 8120563c6bSBarry Smith Usage: 82ff756334SLois Curfman McInnes $ MatShellCreate(m,n,ctx,&mat); 83ff756334SLois Curfman McInnes $ MatShellSetMult(mat,mult); 84e51e0e81SBarry Smith 850b627109SLois Curfman McInnes .keywords: matrix, shell, create 860b627109SLois Curfman McInnes 870b627109SLois Curfman McInnes .seealso: MatShellSetMult(), MatShellSetMultTransAdd() 88e51e0e81SBarry Smith @*/ 896b5873e3SBarry Smith int MatShellCreate(MPI_Comm comm,int m,int n,void *ctx,Mat *mat) 90e51e0e81SBarry Smith { 9120563c6bSBarry Smith Mat newmat; 9220563c6bSBarry Smith MatShell *shell; 936b5873e3SBarry Smith PETSCHEADERCREATE(newmat,_Mat,MAT_COOKIE,MATSHELL,comm); 94a5a9c739SBarry Smith PLogObjectCreate(newmat); 9520563c6bSBarry Smith *mat = newmat; 9620563c6bSBarry Smith newmat->factor = 0; 9720563c6bSBarry Smith newmat->destroy= MatShellDestroy; 9820563c6bSBarry Smith newmat->ops = &MatOps; 9978b31e54SBarry Smith shell = PETSCNEW(MatShell); CHKPTRQ(shell); 10020563c6bSBarry Smith newmat->data = (void *) shell; 10120563c6bSBarry Smith shell->mult = 0; 10220563c6bSBarry Smith shell->m = m; 10320563c6bSBarry Smith shell->n = n; 10420563c6bSBarry Smith shell->ctx = ctx; 105e51e0e81SBarry Smith return 0; 106e51e0e81SBarry Smith } 107e51e0e81SBarry Smith 108e51e0e81SBarry Smith /*@ 1090b627109SLois Curfman McInnes MatShellSetMult - Sets the routine for computing the matrix-vector product. 110e51e0e81SBarry Smith 111e51e0e81SBarry Smith Input Parameters: 1120b627109SLois Curfman McInnes . mat - the matrix associated with this operation, created 1130b627109SLois Curfman McInnes with MatShellCreate() 1140b627109SLois Curfman McInnes . mult - the user-defined routine 115e51e0e81SBarry Smith 1160b627109SLois Curfman McInnes Calling sequence of mult: 1170b627109SLois Curfman McInnes int mult (void *ptr,Vec xin,Vec xout) 1180b627109SLois Curfman McInnes . ptr - the application context for matrix data 1190b627109SLois Curfman McInnes . xin - input vector 1200b627109SLois Curfman McInnes . xout - output vector 1210b627109SLois Curfman McInnes 1220b627109SLois Curfman McInnes .keywords: matrix, multiply, shell, set 1230b627109SLois Curfman McInnes 1240b627109SLois Curfman McInnes .seealso: MatShellCreate(), MatShellSetMultTransAdd() 125e51e0e81SBarry Smith @*/ 12620563c6bSBarry Smith int MatShellSetMult(Mat mat,int (*mult)(void*,Vec,Vec)) 127e51e0e81SBarry Smith { 12820563c6bSBarry Smith MatShell *shell; 129e51e0e81SBarry Smith VALIDHEADER(mat,MAT_COOKIE); 13020563c6bSBarry Smith shell = (MatShell *) mat->data; 13120563c6bSBarry Smith shell->mult = mult; 13220563c6bSBarry Smith return 0; 133e51e0e81SBarry Smith } 134f0479e8cSBarry Smith /*@ 1350b627109SLois Curfman McInnes MatShellSetMultTransAdd - Sets the routine for computing v3 = v2 + A' * v1. 136f0479e8cSBarry Smith 137f0479e8cSBarry Smith Input Parameters: 1380b627109SLois Curfman McInnes . mat - the matrix associated with this operation, created 1390b627109SLois Curfman McInnes with MatShellCreate() 1400b627109SLois Curfman McInnes . mult - the user-defined routine 141f0479e8cSBarry Smith 1420b627109SLois Curfman McInnes Calling sequence of mult: 1430b627109SLois Curfman McInnes int mult (void *ptr,Vec v1,Vec v2,Vec v3) 1440b627109SLois Curfman McInnes . ptr - the application context for matrix data 1450b627109SLois Curfman McInnes . v1, v2 - the input vectors 1460b627109SLois Curfman McInnes . v3 - the result 1470b627109SLois Curfman McInnes 1480b627109SLois Curfman McInnes .keywords: matrix, multiply, transpose 1490b627109SLois Curfman McInnes 1500b627109SLois Curfman McInnes .seealso: MatShellCreate(), MatShellSetMult() 151f0479e8cSBarry Smith @*/ 152f0479e8cSBarry Smith int MatShellSetMultTransAdd(Mat mat,int (*mult)(void*,Vec,Vec,Vec)) 153f0479e8cSBarry Smith { 154f0479e8cSBarry Smith MatShell *shell; 155f0479e8cSBarry Smith VALIDHEADER(mat,MAT_COOKIE); 156f0479e8cSBarry Smith shell = (MatShell *) mat->data; 157f0479e8cSBarry Smith shell->multtransadd = mult; 158f0479e8cSBarry Smith return 0; 159f0479e8cSBarry Smith } 160*b9fa9cd0SBarry Smith /*@ 161*b9fa9cd0SBarry Smith MatShellSetDestroy - Set the routine to use to destroy the 162*b9fa9cd0SBarry Smith private contents of your MatShell. 163*b9fa9cd0SBarry Smith 164*b9fa9cd0SBarry Smith Input Parameters: 165*b9fa9cd0SBarry Smith . mat - the matrix associated with this operation, created 166*b9fa9cd0SBarry Smith with MatShellCreate() 167*b9fa9cd0SBarry Smith . destroy - the user-defined routine 168*b9fa9cd0SBarry Smith 169*b9fa9cd0SBarry Smith Calling sequence of mult: 170*b9fa9cd0SBarry Smith int destroy (void *ptr) 171*b9fa9cd0SBarry Smith . ptr - the application context for matrix data 172*b9fa9cd0SBarry Smith 173*b9fa9cd0SBarry Smith .keywords: matrix, destroy, shell, set 174*b9fa9cd0SBarry Smith 175*b9fa9cd0SBarry Smith .seealso: MatShellCreate() 176*b9fa9cd0SBarry Smith @*/ 177*b9fa9cd0SBarry Smith int MatShellSetDestroy(Mat mat,int (*destroy)(void*)) 178*b9fa9cd0SBarry Smith { 179*b9fa9cd0SBarry Smith MatShell *shell; 180*b9fa9cd0SBarry Smith VALIDHEADER(mat,MAT_COOKIE); 181*b9fa9cd0SBarry Smith shell = (MatShell *) mat->data; 182*b9fa9cd0SBarry Smith shell->destroy = destroy; 183*b9fa9cd0SBarry Smith return 0; 184*b9fa9cd0SBarry Smith } 185f0479e8cSBarry Smith 186f0479e8cSBarry Smith 187