xref: /petsc/src/mat/impls/shell/shell.c (revision ff756334e2018b5f0451f164c7597e7e4cdc144c)
1e51e0e81SBarry Smith 
2e51e0e81SBarry Smith /*
320563c6bSBarry Smith    This provides a simple shell for Fortran (and C programmers) to
420563c6bSBarry Smith   create a very simple matrix class for use with KSP without coding
520563c6bSBarry Smith   mush of anything.
6e51e0e81SBarry Smith */
7e51e0e81SBarry Smith 
8e51e0e81SBarry Smith #include "petsc.h"
9e51e0e81SBarry Smith #include "matimpl.h"        /*I "mat.h" I*/
10e51e0e81SBarry Smith #include "vec/vecimpl.h"
11e51e0e81SBarry Smith 
1220563c6bSBarry Smith typedef struct {
1320563c6bSBarry Smith   int  m,n;
1420563c6bSBarry Smith   int  (*mult)(void *,Vec,Vec);
15f0479e8cSBarry Smith   int  (*multtransadd)(void*,Vec,Vec,Vec);
1620563c6bSBarry Smith   void *ctx;
1720563c6bSBarry Smith } MatShell;
18e51e0e81SBarry Smith 
1920563c6bSBarry Smith static int MatShellMult(Mat mat,Vec x,Vec y)
20e51e0e81SBarry Smith {
2120563c6bSBarry Smith   MatShell *shell;
2220563c6bSBarry Smith   shell = (MatShell *) mat->data;
2320563c6bSBarry Smith   return (*shell->mult)(shell->ctx,x,y);
24e51e0e81SBarry Smith }
25f0479e8cSBarry Smith static int MatShellMultTransAdd(Mat mat,Vec x,Vec y,Vec z)
26f0479e8cSBarry Smith {
27f0479e8cSBarry Smith   MatShell *shell;
28f0479e8cSBarry Smith   shell = (MatShell *) mat->data;
29f0479e8cSBarry Smith   return (*shell->multtransadd)(shell->ctx,x,y,z);
30f0479e8cSBarry Smith }
3120563c6bSBarry Smith static int MatShellDestroy(PetscObject obj)
32e51e0e81SBarry Smith {
3320563c6bSBarry Smith   Mat      mat = (Mat) obj;
3420563c6bSBarry Smith   MatShell *shell;
3520563c6bSBarry Smith   shell = (MatShell *) mat->data;
369e25ed09SBarry Smith   FREE(shell);
37a5a9c739SBarry Smith   PLogObjectDestroy(mat);
389e25ed09SBarry Smith   PETSCHEADERDESTROY(mat);
39e51e0e81SBarry Smith   return 0;
40e51e0e81SBarry Smith }
41e51e0e81SBarry Smith 
4220563c6bSBarry Smith static struct _MatOps MatOps = {0,0,
4320563c6bSBarry Smith        0,
44f0479e8cSBarry Smith        MatShellMult,0,0,MatShellMultTransAdd,
4520563c6bSBarry Smith        0,0,0,0,
4620563c6bSBarry Smith        0,0,
4720563c6bSBarry Smith        0,
4820563c6bSBarry Smith        0,
4920563c6bSBarry Smith        0,0,0,
5020563c6bSBarry Smith        0,
5120563c6bSBarry Smith        0,0,0,
5220563c6bSBarry Smith        0,0,
5320563c6bSBarry Smith        0,
5420563c6bSBarry Smith        0,0,0,0,
5520563c6bSBarry Smith        0,0 };
56e51e0e81SBarry Smith 
57e51e0e81SBarry Smith /*@
58*ff756334SLois Curfman McInnes    MatShellCreate - Creates a new matrix class for use with a user-defined
59*ff756334SLois Curfman McInnes    private data storage format.
60e51e0e81SBarry Smith 
61e51e0e81SBarry Smith    Input Parameters:
626b5873e3SBarry Smith .  comm - MPI communicator
63*ff756334SLois Curfman McInnes .  m - number of rows
64*ff756334SLois Curfman McInnes .  n - number of columns
65*ff756334SLois Curfman McInnes .  ctx - pointer to your data needed by matrix-vector multiply
66e51e0e81SBarry Smith 
67*ff756334SLois Curfman McInnes    Output Parameter:
68e51e0e81SBarry Smith .  mat - the matrix
69e51e0e81SBarry Smith 
70*ff756334SLois Curfman McInnes    Notes:
71*ff756334SLois Curfman McInnes    The shell matrix type is intended to provide a simple class to use
72*ff756334SLois Curfman McInnes    with KSP (such as, for use with matrix-free methods). You should not
73*ff756334SLois Curfman McInnes    use the shell type if you plan to define a complete matrix class.
74e51e0e81SBarry Smith 
7520563c6bSBarry Smith   Usage:
76*ff756334SLois Curfman McInnes $   int (*mult)(void *,Vec,Vec);
77*ff756334SLois Curfman McInnes $   MatShellCreate(m,n,ctx,&mat);
78*ff756334SLois Curfman McInnes $   MatShellSetMult(mat,mult);
79e51e0e81SBarry Smith 
80*ff756334SLois Curfman McInnes .keywords: Mat, matrix, shell
81e51e0e81SBarry Smith @*/
826b5873e3SBarry Smith int MatShellCreate(MPI_Comm comm,int m, int n, void *ctx,Mat *mat)
83e51e0e81SBarry Smith {
8420563c6bSBarry Smith   Mat      newmat;
8520563c6bSBarry Smith   MatShell *shell;
866b5873e3SBarry Smith   PETSCHEADERCREATE(newmat,_Mat,MAT_COOKIE,MATSHELL,comm);
87a5a9c739SBarry Smith   PLogObjectCreate(newmat);
8820563c6bSBarry Smith   *mat           = newmat;
8920563c6bSBarry Smith   newmat->factor = 0;
9020563c6bSBarry Smith   newmat->row    = 0;
9120563c6bSBarry Smith   newmat->col    = 0;
9220563c6bSBarry Smith   newmat->destroy= MatShellDestroy;
9320563c6bSBarry Smith   newmat->ops    = &MatOps;
9420563c6bSBarry Smith   shell          = NEW(MatShell); CHKPTR(shell);
9520563c6bSBarry Smith   newmat->data   = (void *) shell;
9620563c6bSBarry Smith   shell->mult    = 0;
9720563c6bSBarry Smith   shell->m       = m;
9820563c6bSBarry Smith   shell->n       = n;
9920563c6bSBarry Smith   shell->ctx     = ctx;
100e51e0e81SBarry Smith   return 0;
101e51e0e81SBarry Smith }
102e51e0e81SBarry Smith 
103e51e0e81SBarry Smith /*@
10420563c6bSBarry Smith    MatShellSetMult - sets routine to use as matrix vector multiply.
105e51e0e81SBarry Smith 
106e51e0e81SBarry Smith   Input Parameters:
10720563c6bSBarry Smith .  mat - the matrix to add the operation to, created with MatShellCreate()
10820563c6bSBarry Smith .  mult - the matrix vector multiply routine.
109e51e0e81SBarry Smith 
11020563c6bSBarry Smith   Keywords: matrix, multiply
111e51e0e81SBarry Smith @*/
11220563c6bSBarry Smith int MatShellSetMult(Mat mat, int (*mult)(void*,Vec,Vec))
113e51e0e81SBarry Smith {
11420563c6bSBarry Smith   MatShell *shell;
115e51e0e81SBarry Smith   VALIDHEADER(mat,MAT_COOKIE);
11620563c6bSBarry Smith   shell = (MatShell *) mat->data;
11720563c6bSBarry Smith   shell->mult = mult;
11820563c6bSBarry Smith   return 0;
119e51e0e81SBarry Smith }
120f0479e8cSBarry Smith /*@
121f0479e8cSBarry Smith    MatShellSetMultTransAdd - sets routine to use as matrix vector multiply.
122f0479e8cSBarry Smith 
123f0479e8cSBarry Smith   Input Parameters:
124f0479e8cSBarry Smith .  mat - the matrix to add the operation to, created with MatShellCreate()
125f0479e8cSBarry Smith .  mult - the matrix vector multiply routine.
126f0479e8cSBarry Smith 
127f0479e8cSBarry Smith   Keywords: matrix, multiply, transpose
128f0479e8cSBarry Smith @*/
129f0479e8cSBarry Smith int MatShellSetMultTransAdd(Mat mat, int (*mult)(void*,Vec,Vec,Vec))
130f0479e8cSBarry Smith {
131f0479e8cSBarry Smith   MatShell *shell;
132f0479e8cSBarry Smith   VALIDHEADER(mat,MAT_COOKIE);
133f0479e8cSBarry Smith   shell               = (MatShell *) mat->data;
134f0479e8cSBarry Smith   shell->multtransadd = mult;
135f0479e8cSBarry Smith   return 0;
136f0479e8cSBarry Smith }
137f0479e8cSBarry Smith 
138f0479e8cSBarry Smith 
139