xref: /petsc/src/mat/impls/shell/shell.c (revision 6b5873e37b549cc139dbd34cfb32e6aff4f310b3)
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 /*@
5820563c6bSBarry Smith    MatShellCreate - creates a new matrix class for use with your
5920563c6bSBarry Smith           own private data storage format. This is intended to
6020563c6bSBarry Smith           provide a simple class to use with KSP. You should
6120563c6bSBarry Smith           not use this if you plan to make a complete class.
62e51e0e81SBarry Smith 
63e51e0e81SBarry Smith   Input Parameters:
6420563c6bSBarry Smith .  m,n - number of rows and columns in matrix
6520563c6bSBarry Smith .  ctx - pointer to your data needed by matrix multiply.
66*6b5873e3SBarry Smith .  comm - MPI communicator
67e51e0e81SBarry Smith 
68e51e0e81SBarry Smith   Output Parameters:
69e51e0e81SBarry Smith .  mat - the matrix
70e51e0e81SBarry Smith 
7120563c6bSBarry Smith   Keywords: matrix, shell
72e51e0e81SBarry Smith 
7320563c6bSBarry Smith   Usage:
7420563c6bSBarry Smith .             int (*mult)(void *,Vec,Vec);
7520563c6bSBarry Smith .             MatShellCreate(m,n,ctx,&mat);
7620563c6bSBarry Smith .             MatShellSetMult(mat,mult);
77e51e0e81SBarry Smith 
78e51e0e81SBarry Smith @*/
79*6b5873e3SBarry Smith int MatShellCreate(MPI_Comm comm,int m, int n, void *ctx,Mat *mat)
80e51e0e81SBarry Smith {
8120563c6bSBarry Smith   Mat      newmat;
8220563c6bSBarry Smith   MatShell *shell;
83*6b5873e3SBarry Smith   PETSCHEADERCREATE(newmat,_Mat,MAT_COOKIE,MATSHELL,comm);
84a5a9c739SBarry Smith   PLogObjectCreate(newmat);
8520563c6bSBarry Smith   *mat           = newmat;
8620563c6bSBarry Smith   newmat->factor = 0;
8720563c6bSBarry Smith   newmat->row    = 0;
8820563c6bSBarry Smith   newmat->col    = 0;
8920563c6bSBarry Smith   newmat->destroy= MatShellDestroy;
9020563c6bSBarry Smith   newmat->ops    = &MatOps;
9120563c6bSBarry Smith   shell          = NEW(MatShell); CHKPTR(shell);
9220563c6bSBarry Smith   newmat->data   = (void *) shell;
9320563c6bSBarry Smith   shell->mult    = 0;
9420563c6bSBarry Smith   shell->m       = m;
9520563c6bSBarry Smith   shell->n       = n;
9620563c6bSBarry Smith   shell->ctx     = ctx;
97e51e0e81SBarry Smith   return 0;
98e51e0e81SBarry Smith }
99e51e0e81SBarry Smith 
100e51e0e81SBarry Smith /*@
10120563c6bSBarry Smith    MatShellSetMult - sets routine to use as matrix vector multiply.
102e51e0e81SBarry Smith 
103e51e0e81SBarry Smith   Input Parameters:
10420563c6bSBarry Smith .  mat - the matrix to add the operation to, created with MatShellCreate()
10520563c6bSBarry Smith .  mult - the matrix vector multiply routine.
106e51e0e81SBarry Smith 
10720563c6bSBarry Smith   Keywords: matrix, multiply
108e51e0e81SBarry Smith @*/
10920563c6bSBarry Smith int MatShellSetMult(Mat mat, int (*mult)(void*,Vec,Vec))
110e51e0e81SBarry Smith {
11120563c6bSBarry Smith   MatShell *shell;
112e51e0e81SBarry Smith   VALIDHEADER(mat,MAT_COOKIE);
11320563c6bSBarry Smith   shell = (MatShell *) mat->data;
11420563c6bSBarry Smith   shell->mult = mult;
11520563c6bSBarry Smith   return 0;
116e51e0e81SBarry Smith }
117f0479e8cSBarry Smith /*@
118f0479e8cSBarry Smith    MatShellSetMultTransAdd - sets routine to use as matrix vector multiply.
119f0479e8cSBarry Smith 
120f0479e8cSBarry Smith   Input Parameters:
121f0479e8cSBarry Smith .  mat - the matrix to add the operation to, created with MatShellCreate()
122f0479e8cSBarry Smith .  mult - the matrix vector multiply routine.
123f0479e8cSBarry Smith 
124f0479e8cSBarry Smith   Keywords: matrix, multiply, transpose
125f0479e8cSBarry Smith @*/
126f0479e8cSBarry Smith int MatShellSetMultTransAdd(Mat mat, int (*mult)(void*,Vec,Vec,Vec))
127f0479e8cSBarry Smith {
128f0479e8cSBarry Smith   MatShell *shell;
129f0479e8cSBarry Smith   VALIDHEADER(mat,MAT_COOKIE);
130f0479e8cSBarry Smith   shell               = (MatShell *) mat->data;
131f0479e8cSBarry Smith   shell->multtransadd = mult;
132f0479e8cSBarry Smith   return 0;
133f0479e8cSBarry Smith }
134f0479e8cSBarry Smith 
135f0479e8cSBarry Smith 
136