xref: /petsc/src/mat/impls/shell/shell.c (revision f0479e8c2158d05ab5c06def5bd82110ef658075)
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);
15*f0479e8cSBarry 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 }
25*f0479e8cSBarry Smith static int MatShellMultTransAdd(Mat mat,Vec x,Vec y,Vec z)
26*f0479e8cSBarry Smith {
27*f0479e8cSBarry Smith   MatShell *shell;
28*f0479e8cSBarry Smith   shell = (MatShell *) mat->data;
29*f0479e8cSBarry Smith   return (*shell->multtransadd)(shell->ctx,x,y,z);
30*f0479e8cSBarry 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;
3620563c6bSBarry Smith   FREE(shell); FREE(mat);
37e51e0e81SBarry Smith   return 0;
38e51e0e81SBarry Smith }
39e51e0e81SBarry Smith 
4020563c6bSBarry Smith static struct _MatOps MatOps = {0,0,
4120563c6bSBarry Smith        0,
42*f0479e8cSBarry Smith        MatShellMult,0,0,MatShellMultTransAdd,
4320563c6bSBarry Smith        0,0,0,0,
4420563c6bSBarry Smith        0,0,
4520563c6bSBarry Smith        0,
4620563c6bSBarry Smith        0,
4720563c6bSBarry Smith        0,0,0,
4820563c6bSBarry Smith        0,
4920563c6bSBarry Smith        0,0,0,
5020563c6bSBarry Smith        0,0,
5120563c6bSBarry Smith        0,
5220563c6bSBarry Smith        0,0,0,0,
5320563c6bSBarry Smith        0,0 };
54e51e0e81SBarry Smith 
55e51e0e81SBarry Smith /*@
5620563c6bSBarry Smith    MatShellCreate - creates a new matrix class for use with your
5720563c6bSBarry Smith           own private data storage format. This is intended to
5820563c6bSBarry Smith           provide a simple class to use with KSP. You should
5920563c6bSBarry Smith           not use this if you plan to make a complete class.
60e51e0e81SBarry Smith 
61e51e0e81SBarry Smith   Input Parameters:
6220563c6bSBarry Smith .  m,n - number of rows and columns in matrix
6320563c6bSBarry Smith .  ctx - pointer to your data needed by matrix multiply.
64e51e0e81SBarry Smith 
65e51e0e81SBarry Smith   Output Parameters:
66e51e0e81SBarry Smith .  mat - the matrix
67e51e0e81SBarry Smith 
6820563c6bSBarry Smith   Keywords: matrix, shell
69e51e0e81SBarry Smith 
7020563c6bSBarry Smith   Usage:
7120563c6bSBarry Smith .             int (*mult)(void *,Vec,Vec);
7220563c6bSBarry Smith .             MatShellCreate(m,n,ctx,&mat);
7320563c6bSBarry Smith .             MatShellSetMult(mat,mult);
74e51e0e81SBarry Smith 
75e51e0e81SBarry Smith @*/
7620563c6bSBarry Smith int MatShellCreate(int m, int n, void *ctx,Mat *mat)
77e51e0e81SBarry Smith {
7820563c6bSBarry Smith   Mat      newmat;
7920563c6bSBarry Smith   MatShell *shell;
8020563c6bSBarry Smith   CREATEHEADER(newmat,_Mat);
8120563c6bSBarry Smith   *mat           = newmat;
8220563c6bSBarry Smith   newmat->cookie = MAT_COOKIE;
8320563c6bSBarry Smith   newmat->factor = 0;
8420563c6bSBarry Smith   newmat->row    = 0;
8520563c6bSBarry Smith   newmat->col    = 0;
8620563c6bSBarry Smith   newmat->destroy= MatShellDestroy;
8720563c6bSBarry Smith   newmat->ops    = &MatOps;
8820563c6bSBarry Smith   shell          = NEW(MatShell); CHKPTR(shell);
8920563c6bSBarry Smith   newmat->data   = (void *) shell;
9020563c6bSBarry Smith   shell->mult    = 0;
9120563c6bSBarry Smith   shell->m       = m;
9220563c6bSBarry Smith   shell->n       = n;
9320563c6bSBarry Smith   shell->ctx     = ctx;
94e51e0e81SBarry Smith   return 0;
95e51e0e81SBarry Smith }
96e51e0e81SBarry Smith 
97e51e0e81SBarry Smith /*@
9820563c6bSBarry Smith    MatShellSetMult - sets routine to use as matrix vector multiply.
99e51e0e81SBarry Smith 
100e51e0e81SBarry Smith   Input Parameters:
10120563c6bSBarry Smith .  mat - the matrix to add the operation to, created with MatShellCreate()
10220563c6bSBarry Smith .  mult - the matrix vector multiply routine.
103e51e0e81SBarry Smith 
10420563c6bSBarry Smith   Keywords: matrix, multiply
105e51e0e81SBarry Smith @*/
10620563c6bSBarry Smith int MatShellSetMult(Mat mat, int (*mult)(void*,Vec,Vec))
107e51e0e81SBarry Smith {
10820563c6bSBarry Smith   MatShell *shell;
109e51e0e81SBarry Smith   VALIDHEADER(mat,MAT_COOKIE);
11020563c6bSBarry Smith   shell = (MatShell *) mat->data;
11120563c6bSBarry Smith   shell->mult = mult;
11220563c6bSBarry Smith   return 0;
113e51e0e81SBarry Smith }
114*f0479e8cSBarry Smith /*@
115*f0479e8cSBarry Smith    MatShellSetMultTransAdd - sets routine to use as matrix vector multiply.
116*f0479e8cSBarry Smith 
117*f0479e8cSBarry Smith   Input Parameters:
118*f0479e8cSBarry Smith .  mat - the matrix to add the operation to, created with MatShellCreate()
119*f0479e8cSBarry Smith .  mult - the matrix vector multiply routine.
120*f0479e8cSBarry Smith 
121*f0479e8cSBarry Smith   Keywords: matrix, multiply, transpose
122*f0479e8cSBarry Smith @*/
123*f0479e8cSBarry Smith int MatShellSetMultTransAdd(Mat mat, int (*mult)(void*,Vec,Vec,Vec))
124*f0479e8cSBarry Smith {
125*f0479e8cSBarry Smith   MatShell *shell;
126*f0479e8cSBarry Smith   VALIDHEADER(mat,MAT_COOKIE);
127*f0479e8cSBarry Smith   shell               = (MatShell *) mat->data;
128*f0479e8cSBarry Smith   shell->multtransadd = mult;
129*f0479e8cSBarry Smith   return 0;
130*f0479e8cSBarry Smith }
131*f0479e8cSBarry Smith 
132*f0479e8cSBarry Smith 
133