xref: /petsc/src/mat/impls/shell/shell.c (revision 9e25ed09632ec91e2227f4e6f1ce3bfe794e20d8)
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;
36*9e25ed09SBarry Smith   FREE(shell);
37*9e25ed09SBarry Smith   PETSCHEADERDESTROY(mat);
38e51e0e81SBarry Smith   return 0;
39e51e0e81SBarry Smith }
40e51e0e81SBarry Smith 
4120563c6bSBarry Smith static struct _MatOps MatOps = {0,0,
4220563c6bSBarry Smith        0,
43f0479e8cSBarry Smith        MatShellMult,0,0,MatShellMultTransAdd,
4420563c6bSBarry Smith        0,0,0,0,
4520563c6bSBarry Smith        0,0,
4620563c6bSBarry Smith        0,
4720563c6bSBarry Smith        0,
4820563c6bSBarry Smith        0,0,0,
4920563c6bSBarry Smith        0,
5020563c6bSBarry Smith        0,0,0,
5120563c6bSBarry Smith        0,0,
5220563c6bSBarry Smith        0,
5320563c6bSBarry Smith        0,0,0,0,
5420563c6bSBarry Smith        0,0 };
55e51e0e81SBarry Smith 
56e51e0e81SBarry Smith /*@
5720563c6bSBarry Smith    MatShellCreate - creates a new matrix class for use with your
5820563c6bSBarry Smith           own private data storage format. This is intended to
5920563c6bSBarry Smith           provide a simple class to use with KSP. You should
6020563c6bSBarry Smith           not use this if you plan to make a complete class.
61e51e0e81SBarry Smith 
62e51e0e81SBarry Smith   Input Parameters:
6320563c6bSBarry Smith .  m,n - number of rows and columns in matrix
6420563c6bSBarry Smith .  ctx - pointer to your data needed by matrix multiply.
65e51e0e81SBarry Smith 
66e51e0e81SBarry Smith   Output Parameters:
67e51e0e81SBarry Smith .  mat - the matrix
68e51e0e81SBarry Smith 
6920563c6bSBarry Smith   Keywords: matrix, shell
70e51e0e81SBarry Smith 
7120563c6bSBarry Smith   Usage:
7220563c6bSBarry Smith .             int (*mult)(void *,Vec,Vec);
7320563c6bSBarry Smith .             MatShellCreate(m,n,ctx,&mat);
7420563c6bSBarry Smith .             MatShellSetMult(mat,mult);
75e51e0e81SBarry Smith 
76e51e0e81SBarry Smith @*/
7720563c6bSBarry Smith int MatShellCreate(int m, int n, void *ctx,Mat *mat)
78e51e0e81SBarry Smith {
7920563c6bSBarry Smith   Mat      newmat;
8020563c6bSBarry Smith   MatShell *shell;
81*9e25ed09SBarry Smith   PETSCHEADERCREATE(newmat,_Mat,MAT_COOKIE,MATSHELL,MPI_COMM_WORLD);
8220563c6bSBarry Smith   *mat           = newmat;
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 }
114f0479e8cSBarry Smith /*@
115f0479e8cSBarry Smith    MatShellSetMultTransAdd - sets routine to use as matrix vector multiply.
116f0479e8cSBarry Smith 
117f0479e8cSBarry Smith   Input Parameters:
118f0479e8cSBarry Smith .  mat - the matrix to add the operation to, created with MatShellCreate()
119f0479e8cSBarry Smith .  mult - the matrix vector multiply routine.
120f0479e8cSBarry Smith 
121f0479e8cSBarry Smith   Keywords: matrix, multiply, transpose
122f0479e8cSBarry Smith @*/
123f0479e8cSBarry Smith int MatShellSetMultTransAdd(Mat mat, int (*mult)(void*,Vec,Vec,Vec))
124f0479e8cSBarry Smith {
125f0479e8cSBarry Smith   MatShell *shell;
126f0479e8cSBarry Smith   VALIDHEADER(mat,MAT_COOKIE);
127f0479e8cSBarry Smith   shell               = (MatShell *) mat->data;
128f0479e8cSBarry Smith   shell->multtransadd = mult;
129f0479e8cSBarry Smith   return 0;
130f0479e8cSBarry Smith }
131f0479e8cSBarry Smith 
132f0479e8cSBarry Smith 
133