1357feee3SLois Curfman McInnes #ifndef lint 2*ed3cc1f0SBarry Smith static char vcid[] = "$Id: shell.c,v 1.21 1995/11/01 23:18:32 bsmith 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 8*ed3cc1f0SBarry Smith much 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 { 1671b459e3SLois Curfman McInnes int m, n; /* rows, columns */ 1720563c6bSBarry Smith int (*mult)(void*,Vec,Vec); 18f0479e8cSBarry Smith int (*multtransadd)(void*,Vec,Vec,Vec); 19b9fa9cd0SBarry Smith int (*destroy)(void*); 2071b459e3SLois Curfman McInnes int (*getsize)(void*,int*,int*); 2120563c6bSBarry Smith void *ctx; 2288cf3e7dSBarry Smith } Mat_Shell; 23e51e0e81SBarry Smith 2471b459e3SLois Curfman McInnes static int MatGetSize_Shell(Mat mat,int *m,int *n) 2571b459e3SLois Curfman McInnes { 2671b459e3SLois Curfman McInnes Mat_Shell *shell = (Mat_Shell *) mat->data; 2771b459e3SLois Curfman McInnes *m = shell->m; *n = shell->n; 2871b459e3SLois Curfman McInnes return 0; 2971b459e3SLois Curfman McInnes } 3071b459e3SLois Curfman McInnes 3188cf3e7dSBarry Smith static int MatMult_Shell(Mat mat,Vec x,Vec y) 32e51e0e81SBarry Smith { 3371b459e3SLois Curfman McInnes Mat_Shell *shell = (Mat_Shell *) mat->data; 3420563c6bSBarry Smith return (*shell->mult)(shell->ctx,x,y); 35e51e0e81SBarry Smith } 3671b459e3SLois Curfman McInnes 3788cf3e7dSBarry Smith static int MatMultTransAdd_Shell(Mat mat,Vec x,Vec y,Vec z) 38f0479e8cSBarry Smith { 3971b459e3SLois Curfman McInnes Mat_Shell *shell = (Mat_Shell *) mat->data; 40f0479e8cSBarry Smith return (*shell->multtransadd)(shell->ctx,x,y,z); 41f0479e8cSBarry Smith } 4271b459e3SLois Curfman McInnes 4388cf3e7dSBarry Smith static int MatDestroy_Shell(PetscObject obj) 44e51e0e81SBarry Smith { 45b9fa9cd0SBarry Smith int ierr; 4620563c6bSBarry Smith Mat mat = (Mat) obj; 4788cf3e7dSBarry Smith Mat_Shell *shell; 48*ed3cc1f0SBarry Smith 4988cf3e7dSBarry Smith shell = (Mat_Shell *) mat->data; 50b9fa9cd0SBarry Smith if (shell->destroy) {ierr = (*shell->destroy)(shell->ctx);CHKERRQ(ierr);} 510452661fSBarry Smith PetscFree(shell); 52a5a9c739SBarry Smith PLogObjectDestroy(mat); 530452661fSBarry Smith PetscHeaderDestroy(mat); 54e51e0e81SBarry Smith return 0; 55e51e0e81SBarry Smith } 56e51e0e81SBarry Smith 5720563c6bSBarry Smith static struct _MatOps MatOps = {0,0, 5820563c6bSBarry Smith 0, 5988cf3e7dSBarry Smith MatMult_Shell,0,0,MatMultTransAdd_Shell, 6020563c6bSBarry Smith 0,0,0,0, 6120563c6bSBarry Smith 0,0, 6220563c6bSBarry Smith 0, 6320563c6bSBarry Smith 0, 6420563c6bSBarry Smith 0,0,0, 6520563c6bSBarry Smith 0, 6620563c6bSBarry Smith 0,0,0, 6720563c6bSBarry Smith 0,0, 6820563c6bSBarry Smith 0, 6920563c6bSBarry Smith 0,0,0,0, 7071b459e3SLois Curfman McInnes 0,0,MatGetSize_Shell, 7171b459e3SLois Curfman McInnes 0,0,0,0, 7271b459e3SLois Curfman McInnes 0,0,0,0 }; 73e51e0e81SBarry Smith 744b828684SBarry Smith /*@C 75c01c455dSBarry Smith MatShellCreate - Creates a new matrix class for use with a user-defined 76ff756334SLois Curfman McInnes private data storage format. 77e51e0e81SBarry Smith 78e51e0e81SBarry Smith Input Parameters: 796b5873e3SBarry Smith . comm - MPI communicator 80ff756334SLois Curfman McInnes . m - number of rows 81ff756334SLois Curfman McInnes . n - number of columns 820b627109SLois Curfman McInnes . ctx - pointer to data needed by matrix-vector multiplication routine(s) 83e51e0e81SBarry Smith 84ff756334SLois Curfman McInnes Output Parameter: 85e51e0e81SBarry Smith . mat - the matrix 86e51e0e81SBarry Smith 87ff756334SLois Curfman McInnes Notes: 88ff756334SLois Curfman McInnes The shell matrix type is intended to provide a simple class to use 89ff756334SLois Curfman McInnes with KSP (such as, for use with matrix-free methods). You should not 90ff756334SLois Curfman McInnes use the shell type if you plan to define a complete matrix class. 91e51e0e81SBarry Smith 9220563c6bSBarry Smith Usage: 93ff756334SLois Curfman McInnes $ MatShellCreate(m,n,ctx,&mat); 94ff756334SLois Curfman McInnes $ MatShellSetMult(mat,mult); 95e51e0e81SBarry Smith 960b627109SLois Curfman McInnes .keywords: matrix, shell, create 970b627109SLois Curfman McInnes 980b627109SLois Curfman McInnes .seealso: MatShellSetMult(), MatShellSetMultTransAdd() 99e51e0e81SBarry Smith @*/ 1006b5873e3SBarry Smith int MatShellCreate(MPI_Comm comm,int m,int n,void *ctx,Mat *mat) 101e51e0e81SBarry Smith { 10220563c6bSBarry Smith Mat newmat; 10388cf3e7dSBarry Smith Mat_Shell *shell; 104*ed3cc1f0SBarry Smith 1050452661fSBarry Smith PetscHeaderCreate(newmat,_Mat,MAT_COOKIE,MATSHELL,comm); 106a5a9c739SBarry Smith PLogObjectCreate(newmat); 10720563c6bSBarry Smith *mat = newmat; 10820563c6bSBarry Smith newmat->factor = 0; 10988cf3e7dSBarry Smith newmat->destroy= MatDestroy_Shell; 110416022c9SBarry Smith PetscMemcpy(&newmat->ops,&MatOps,sizeof(struct _MatOps)); 1110452661fSBarry Smith shell = PetscNew(Mat_Shell); CHKPTRQ(shell); 112cddf8d76SBarry Smith PetscMemzero(shell,sizeof(Mat_Shell)); 11320563c6bSBarry Smith newmat->data = (void *) shell; 11420563c6bSBarry Smith shell->mult = 0; 11520563c6bSBarry Smith shell->m = m; 11620563c6bSBarry Smith shell->n = n; 11720563c6bSBarry Smith shell->ctx = ctx; 118e51e0e81SBarry Smith return 0; 119e51e0e81SBarry Smith } 120e51e0e81SBarry Smith 1214b828684SBarry Smith /*@C 1220b627109SLois Curfman McInnes MatShellSetMult - Sets the routine for computing the matrix-vector product. 123e51e0e81SBarry Smith 124e51e0e81SBarry Smith Input Parameters: 1250b627109SLois Curfman McInnes . mat - the matrix associated with this operation, created 1260b627109SLois Curfman McInnes with MatShellCreate() 1270b627109SLois Curfman McInnes . mult - the user-defined routine 128e51e0e81SBarry Smith 1290b627109SLois Curfman McInnes Calling sequence of mult: 1300b627109SLois Curfman McInnes int mult (void *ptr,Vec xin,Vec xout) 1310b627109SLois Curfman McInnes . ptr - the application context for matrix data 1320b627109SLois Curfman McInnes . xin - input vector 1330b627109SLois Curfman McInnes . xout - output vector 1340b627109SLois Curfman McInnes 1350b627109SLois Curfman McInnes .keywords: matrix, multiply, shell, set 1360b627109SLois Curfman McInnes 1370b627109SLois Curfman McInnes .seealso: MatShellCreate(), MatShellSetMultTransAdd() 138e51e0e81SBarry Smith @*/ 13920563c6bSBarry Smith int MatShellSetMult(Mat mat,int (*mult)(void*,Vec,Vec)) 140e51e0e81SBarry Smith { 14188cf3e7dSBarry Smith Mat_Shell *shell; 1421a941147SBarry Smith PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 14388cf3e7dSBarry Smith shell = (Mat_Shell *) mat->data; 14420563c6bSBarry Smith shell->mult = mult; 14520563c6bSBarry Smith return 0; 146e51e0e81SBarry Smith } 1474b828684SBarry Smith /*@C 1480b627109SLois Curfman McInnes MatShellSetMultTransAdd - Sets the routine for computing v3 = v2 + A' * v1. 149f0479e8cSBarry Smith 150f0479e8cSBarry Smith Input Parameters: 1510b627109SLois Curfman McInnes . mat - the matrix associated with this operation, created 1520b627109SLois Curfman McInnes with MatShellCreate() 1530b627109SLois Curfman McInnes . mult - the user-defined routine 154f0479e8cSBarry Smith 1550b627109SLois Curfman McInnes Calling sequence of mult: 1560b627109SLois Curfman McInnes int mult (void *ptr,Vec v1,Vec v2,Vec v3) 1570b627109SLois Curfman McInnes . ptr - the application context for matrix data 1580b627109SLois Curfman McInnes . v1, v2 - the input vectors 1590b627109SLois Curfman McInnes . v3 - the result 1600b627109SLois Curfman McInnes 1610b627109SLois Curfman McInnes .keywords: matrix, multiply, transpose 1620b627109SLois Curfman McInnes 1630b627109SLois Curfman McInnes .seealso: MatShellCreate(), MatShellSetMult() 164f0479e8cSBarry Smith @*/ 165f0479e8cSBarry Smith int MatShellSetMultTransAdd(Mat mat,int (*mult)(void*,Vec,Vec,Vec)) 166f0479e8cSBarry Smith { 16788cf3e7dSBarry Smith Mat_Shell *shell; 1681a941147SBarry Smith PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 16988cf3e7dSBarry Smith shell = (Mat_Shell *) mat->data; 170f0479e8cSBarry Smith shell->multtransadd = mult; 171f0479e8cSBarry Smith return 0; 172f0479e8cSBarry Smith } 1734b828684SBarry Smith /*@C 174b9fa9cd0SBarry Smith MatShellSetDestroy - Set the routine to use to destroy the 175b9fa9cd0SBarry Smith private contents of your MatShell. 176b9fa9cd0SBarry Smith 177b9fa9cd0SBarry Smith Input Parameters: 178b9fa9cd0SBarry Smith . mat - the matrix associated with this operation, created 179b9fa9cd0SBarry Smith with MatShellCreate() 180b9fa9cd0SBarry Smith . destroy - the user-defined routine 181b9fa9cd0SBarry Smith 182b9fa9cd0SBarry Smith Calling sequence of mult: 183b9fa9cd0SBarry Smith int destroy (void *ptr) 184b9fa9cd0SBarry Smith . ptr - the application context for matrix data 185b9fa9cd0SBarry Smith 186b9fa9cd0SBarry Smith .keywords: matrix, destroy, shell, set 187b9fa9cd0SBarry Smith 188b9fa9cd0SBarry Smith .seealso: MatShellCreate() 189b9fa9cd0SBarry Smith @*/ 190b9fa9cd0SBarry Smith int MatShellSetDestroy(Mat mat,int (*destroy)(void*)) 191b9fa9cd0SBarry Smith { 19288cf3e7dSBarry Smith Mat_Shell *shell; 1931a941147SBarry Smith PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE); 19488cf3e7dSBarry Smith shell = (Mat_Shell *) mat->data; 195b9fa9cd0SBarry Smith shell->destroy = destroy; 196b9fa9cd0SBarry Smith return 0; 197b9fa9cd0SBarry Smith } 198f0479e8cSBarry Smith 199f0479e8cSBarry Smith 200