xref: /petsc/src/mat/impls/shell/shell.c (revision c01c455d43b147fbcccdf73d28be4d01ad7032e0)
1357feee3SLois Curfman McInnes #ifndef lint
2*c01c455dSBarry Smith static char vcid[] = "$Id: shell.c,v 1.14 1995/08/07 18:52:42 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
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);
19b9fa9cd0SBarry Smith   int  (*destroy)(void *);
2020563c6bSBarry Smith   void *ctx;
2188cf3e7dSBarry Smith } Mat_Shell;
22e51e0e81SBarry Smith 
2388cf3e7dSBarry Smith static int MatMult_Shell(Mat mat,Vec x,Vec y)
24e51e0e81SBarry Smith {
2588cf3e7dSBarry Smith   Mat_Shell *shell;
2688cf3e7dSBarry Smith   shell = (Mat_Shell *) mat->data;
2720563c6bSBarry Smith   return (*shell->mult)(shell->ctx,x,y);
28e51e0e81SBarry Smith }
2988cf3e7dSBarry Smith static int MatMultTransAdd_Shell(Mat mat,Vec x,Vec y,Vec z)
30f0479e8cSBarry Smith {
3188cf3e7dSBarry Smith   Mat_Shell *shell;
3288cf3e7dSBarry Smith   shell = (Mat_Shell *) mat->data;
33f0479e8cSBarry Smith   return (*shell->multtransadd)(shell->ctx,x,y,z);
34f0479e8cSBarry Smith }
3588cf3e7dSBarry Smith static int MatDestroy_Shell(PetscObject obj)
36e51e0e81SBarry Smith {
37b9fa9cd0SBarry Smith   int      ierr;
3820563c6bSBarry Smith   Mat      mat = (Mat) obj;
3988cf3e7dSBarry Smith   Mat_Shell *shell;
4088cf3e7dSBarry Smith   shell = (Mat_Shell *) mat->data;
41b9fa9cd0SBarry 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,
5088cf3e7dSBarry Smith        MatMult_Shell,0,0,MatMultTransAdd_Shell,
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 /*@
64*c01c455dSBarry Smith    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;
9288cf3e7dSBarry Smith   Mat_Shell *shell;
936b5873e3SBarry Smith   PETSCHEADERCREATE(newmat,_Mat,MAT_COOKIE,MATSHELL,comm);
94a5a9c739SBarry Smith   PLogObjectCreate(newmat);
9520563c6bSBarry Smith   *mat           = newmat;
9620563c6bSBarry Smith   newmat->factor = 0;
9788cf3e7dSBarry Smith   newmat->destroy= MatDestroy_Shell;
9820563c6bSBarry Smith   newmat->ops    = &MatOps;
9988cf3e7dSBarry Smith   shell          = PETSCNEW(Mat_Shell); CHKPTRQ(shell);
10088cf3e7dSBarry Smith   PETSCMEMSET(shell,0,sizeof(Mat_Shell));
10120563c6bSBarry Smith   newmat->data   = (void *) shell;
10220563c6bSBarry Smith   shell->mult    = 0;
10320563c6bSBarry Smith   shell->m       = m;
10420563c6bSBarry Smith   shell->n       = n;
10520563c6bSBarry Smith   shell->ctx     = ctx;
106e51e0e81SBarry Smith   return 0;
107e51e0e81SBarry Smith }
108e51e0e81SBarry Smith 
109e51e0e81SBarry Smith /*@
1100b627109SLois Curfman McInnes    MatShellSetMult - Sets the routine for computing the matrix-vector product.
111e51e0e81SBarry Smith 
112e51e0e81SBarry Smith    Input Parameters:
1130b627109SLois Curfman McInnes .  mat - the matrix associated with this operation, created
1140b627109SLois Curfman McInnes          with MatShellCreate()
1150b627109SLois Curfman McInnes .  mult - the user-defined routine
116e51e0e81SBarry Smith 
1170b627109SLois Curfman McInnes    Calling sequence of mult:
1180b627109SLois Curfman McInnes    int mult (void *ptr,Vec xin,Vec xout)
1190b627109SLois Curfman McInnes .  ptr - the application context for matrix data
1200b627109SLois Curfman McInnes .  xin - input vector
1210b627109SLois Curfman McInnes .  xout - output vector
1220b627109SLois Curfman McInnes 
1230b627109SLois Curfman McInnes .keywords: matrix, multiply, shell, set
1240b627109SLois Curfman McInnes 
1250b627109SLois Curfman McInnes .seealso: MatShellCreate(), MatShellSetMultTransAdd()
126e51e0e81SBarry Smith @*/
12720563c6bSBarry Smith int MatShellSetMult(Mat mat,int (*mult)(void*,Vec,Vec))
128e51e0e81SBarry Smith {
12988cf3e7dSBarry Smith   Mat_Shell *shell;
1301a941147SBarry Smith   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
13188cf3e7dSBarry Smith   shell = (Mat_Shell *) mat->data;
13220563c6bSBarry Smith   shell->mult = mult;
13320563c6bSBarry Smith   return 0;
134e51e0e81SBarry Smith }
135f0479e8cSBarry Smith /*@
1360b627109SLois Curfman McInnes    MatShellSetMultTransAdd - Sets the routine for computing v3 = v2 + A' * v1.
137f0479e8cSBarry Smith 
138f0479e8cSBarry Smith    Input Parameters:
1390b627109SLois Curfman McInnes .  mat - the matrix associated with this operation, created
1400b627109SLois Curfman McInnes          with MatShellCreate()
1410b627109SLois Curfman McInnes .  mult - the user-defined routine
142f0479e8cSBarry Smith 
1430b627109SLois Curfman McInnes    Calling sequence of mult:
1440b627109SLois Curfman McInnes    int mult (void *ptr,Vec v1,Vec v2,Vec v3)
1450b627109SLois Curfman McInnes .  ptr - the application context for matrix data
1460b627109SLois Curfman McInnes .  v1, v2 - the input vectors
1470b627109SLois Curfman McInnes .  v3 - the result
1480b627109SLois Curfman McInnes 
1490b627109SLois Curfman McInnes .keywords: matrix, multiply, transpose
1500b627109SLois Curfman McInnes 
1510b627109SLois Curfman McInnes .seealso: MatShellCreate(), MatShellSetMult()
152f0479e8cSBarry Smith @*/
153f0479e8cSBarry Smith int MatShellSetMultTransAdd(Mat mat,int (*mult)(void*,Vec,Vec,Vec))
154f0479e8cSBarry Smith {
15588cf3e7dSBarry Smith   Mat_Shell *shell;
1561a941147SBarry Smith   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
15788cf3e7dSBarry Smith   shell               = (Mat_Shell *) mat->data;
158f0479e8cSBarry Smith   shell->multtransadd = mult;
159f0479e8cSBarry Smith   return 0;
160f0479e8cSBarry Smith }
161b9fa9cd0SBarry Smith /*@
162b9fa9cd0SBarry Smith    MatShellSetDestroy - Set the routine to use to destroy the
163b9fa9cd0SBarry Smith         private contents of your MatShell.
164b9fa9cd0SBarry Smith 
165b9fa9cd0SBarry Smith    Input Parameters:
166b9fa9cd0SBarry Smith .  mat - the matrix associated with this operation, created
167b9fa9cd0SBarry Smith          with MatShellCreate()
168b9fa9cd0SBarry Smith .  destroy - the user-defined routine
169b9fa9cd0SBarry Smith 
170b9fa9cd0SBarry Smith    Calling sequence of mult:
171b9fa9cd0SBarry Smith    int destroy (void *ptr)
172b9fa9cd0SBarry Smith .  ptr - the application context for matrix data
173b9fa9cd0SBarry Smith 
174b9fa9cd0SBarry Smith .keywords: matrix, destroy, shell, set
175b9fa9cd0SBarry Smith 
176b9fa9cd0SBarry Smith .seealso: MatShellCreate()
177b9fa9cd0SBarry Smith @*/
178b9fa9cd0SBarry Smith int MatShellSetDestroy(Mat mat,int (*destroy)(void*))
179b9fa9cd0SBarry Smith {
18088cf3e7dSBarry Smith   Mat_Shell *shell;
1811a941147SBarry Smith   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
18288cf3e7dSBarry Smith   shell = (Mat_Shell *) mat->data;
183b9fa9cd0SBarry Smith   shell->destroy = destroy;
184b9fa9cd0SBarry Smith   return 0;
185b9fa9cd0SBarry Smith }
186f0479e8cSBarry Smith 
187f0479e8cSBarry Smith 
188