xref: /petsc/src/mat/impls/shell/shell.c (revision 357feee3d54c040e0db7c3c11e1d9e8946565884)
1*357feee3SLois Curfman McInnes #ifndef lint
2*357feee3SLois Curfman McInnes static char vcid[] = "$Id: bdiag.c,v 1.3 1995/04/24 21:08:05 curfman Exp curfman $";
3*357feee3SLois 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);
1920563c6bSBarry Smith   void *ctx;
2020563c6bSBarry Smith } MatShell;
21e51e0e81SBarry Smith 
2220563c6bSBarry Smith static int MatShellMult(Mat mat,Vec x,Vec y)
23e51e0e81SBarry Smith {
2420563c6bSBarry Smith   MatShell *shell;
2520563c6bSBarry Smith   shell = (MatShell *) mat->data;
2620563c6bSBarry Smith   return (*shell->mult)(shell->ctx,x,y);
27e51e0e81SBarry Smith }
28f0479e8cSBarry Smith static int MatShellMultTransAdd(Mat mat,Vec x,Vec y,Vec z)
29f0479e8cSBarry Smith {
30f0479e8cSBarry Smith   MatShell *shell;
31f0479e8cSBarry Smith   shell = (MatShell *) mat->data;
32f0479e8cSBarry Smith   return (*shell->multtransadd)(shell->ctx,x,y,z);
33f0479e8cSBarry Smith }
3420563c6bSBarry Smith static int MatShellDestroy(PetscObject obj)
35e51e0e81SBarry Smith {
3620563c6bSBarry Smith   Mat      mat = (Mat) obj;
3720563c6bSBarry Smith   MatShell *shell;
3820563c6bSBarry Smith   shell = (MatShell *) mat->data;
399e25ed09SBarry Smith   FREE(shell);
40a5a9c739SBarry Smith   PLogObjectDestroy(mat);
419e25ed09SBarry Smith   PETSCHEADERDESTROY(mat);
42e51e0e81SBarry Smith   return 0;
43e51e0e81SBarry Smith }
44e51e0e81SBarry Smith 
4520563c6bSBarry Smith static struct _MatOps MatOps = {0,0,
4620563c6bSBarry Smith        0,
47f0479e8cSBarry Smith        MatShellMult,0,0,MatShellMultTransAdd,
4820563c6bSBarry Smith        0,0,0,0,
4920563c6bSBarry Smith        0,0,
5020563c6bSBarry Smith        0,
5120563c6bSBarry Smith        0,
5220563c6bSBarry Smith        0,0,0,
5320563c6bSBarry Smith        0,
5420563c6bSBarry Smith        0,0,0,
5520563c6bSBarry Smith        0,0,
5620563c6bSBarry Smith        0,
5720563c6bSBarry Smith        0,0,0,0,
5820563c6bSBarry Smith        0,0 };
59e51e0e81SBarry Smith 
60e51e0e81SBarry Smith /*@
61ff756334SLois Curfman McInnes    MatShellCreate - Creates a new matrix class for use with a user-defined
62ff756334SLois Curfman McInnes    private data storage format.
63e51e0e81SBarry Smith 
64e51e0e81SBarry Smith    Input Parameters:
656b5873e3SBarry Smith .  comm - MPI communicator
66ff756334SLois Curfman McInnes .  m - number of rows
67ff756334SLois Curfman McInnes .  n - number of columns
68ff756334SLois Curfman McInnes .  ctx - pointer to your data needed by matrix-vector multiply
69e51e0e81SBarry Smith 
70ff756334SLois Curfman McInnes    Output Parameter:
71e51e0e81SBarry Smith .  mat - the matrix
72e51e0e81SBarry Smith 
73ff756334SLois Curfman McInnes    Notes:
74ff756334SLois Curfman McInnes    The shell matrix type is intended to provide a simple class to use
75ff756334SLois Curfman McInnes    with KSP (such as, for use with matrix-free methods). You should not
76ff756334SLois Curfman McInnes    use the shell type if you plan to define a complete matrix class.
77e51e0e81SBarry Smith 
7820563c6bSBarry Smith   Usage:
79ff756334SLois Curfman McInnes $   int (*mult)(void *,Vec,Vec);
80ff756334SLois Curfman McInnes $   MatShellCreate(m,n,ctx,&mat);
81ff756334SLois Curfman McInnes $   MatShellSetMult(mat,mult);
82e51e0e81SBarry Smith 
83ff756334SLois Curfman McInnes .keywords: Mat, matrix, shell
84e51e0e81SBarry Smith @*/
856b5873e3SBarry Smith int MatShellCreate(MPI_Comm comm,int m, int n, void *ctx,Mat *mat)
86e51e0e81SBarry Smith {
8720563c6bSBarry Smith   Mat      newmat;
8820563c6bSBarry Smith   MatShell *shell;
896b5873e3SBarry Smith   PETSCHEADERCREATE(newmat,_Mat,MAT_COOKIE,MATSHELL,comm);
90a5a9c739SBarry Smith   PLogObjectCreate(newmat);
9120563c6bSBarry Smith   *mat           = newmat;
9220563c6bSBarry Smith   newmat->factor = 0;
9320563c6bSBarry Smith   newmat->destroy= MatShellDestroy;
9420563c6bSBarry Smith   newmat->ops    = &MatOps;
9520563c6bSBarry Smith   shell          = NEW(MatShell); CHKPTR(shell);
9620563c6bSBarry Smith   newmat->data   = (void *) shell;
9720563c6bSBarry Smith   shell->mult    = 0;
9820563c6bSBarry Smith   shell->m       = m;
9920563c6bSBarry Smith   shell->n       = n;
10020563c6bSBarry Smith   shell->ctx     = ctx;
101e51e0e81SBarry Smith   return 0;
102e51e0e81SBarry Smith }
103e51e0e81SBarry Smith 
104e51e0e81SBarry Smith /*@
10520563c6bSBarry Smith    MatShellSetMult - sets routine to use as matrix vector multiply.
106e51e0e81SBarry Smith 
107e51e0e81SBarry Smith   Input Parameters:
10820563c6bSBarry Smith .  mat - the matrix to add the operation to, created with MatShellCreate()
10920563c6bSBarry Smith .  mult - the matrix vector multiply routine.
110e51e0e81SBarry Smith 
11120563c6bSBarry Smith   Keywords: matrix, multiply
112e51e0e81SBarry Smith @*/
11320563c6bSBarry Smith int MatShellSetMult(Mat mat, int (*mult)(void*,Vec,Vec))
114e51e0e81SBarry Smith {
11520563c6bSBarry Smith   MatShell *shell;
116e51e0e81SBarry Smith   VALIDHEADER(mat,MAT_COOKIE);
11720563c6bSBarry Smith   shell = (MatShell *) mat->data;
11820563c6bSBarry Smith   shell->mult = mult;
11920563c6bSBarry Smith   return 0;
120e51e0e81SBarry Smith }
121f0479e8cSBarry Smith /*@
122f0479e8cSBarry Smith    MatShellSetMultTransAdd - sets routine to use as matrix vector multiply.
123f0479e8cSBarry Smith 
124f0479e8cSBarry Smith   Input Parameters:
125f0479e8cSBarry Smith .  mat - the matrix to add the operation to, created with MatShellCreate()
126f0479e8cSBarry Smith .  mult - the matrix vector multiply routine.
127f0479e8cSBarry Smith 
128f0479e8cSBarry Smith   Keywords: matrix, multiply, transpose
129f0479e8cSBarry Smith @*/
130f0479e8cSBarry Smith int MatShellSetMultTransAdd(Mat mat, int (*mult)(void*,Vec,Vec,Vec))
131f0479e8cSBarry Smith {
132f0479e8cSBarry Smith   MatShell *shell;
133f0479e8cSBarry Smith   VALIDHEADER(mat,MAT_COOKIE);
134f0479e8cSBarry Smith   shell               = (MatShell *) mat->data;
135f0479e8cSBarry Smith   shell->multtransadd = mult;
136f0479e8cSBarry Smith   return 0;
137f0479e8cSBarry Smith }
138f0479e8cSBarry Smith 
139f0479e8cSBarry Smith 
140