xref: /petsc/src/mat/impls/shell/shell.c (revision a5a9c7395dc5f08d0dbb7ed05e148334692018e2)
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);
37*a5a9c739SBarry 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.
66e51e0e81SBarry Smith 
67e51e0e81SBarry Smith   Output Parameters:
68e51e0e81SBarry Smith .  mat - the matrix
69e51e0e81SBarry Smith 
7020563c6bSBarry Smith   Keywords: matrix, shell
71e51e0e81SBarry Smith 
7220563c6bSBarry Smith   Usage:
7320563c6bSBarry Smith .             int (*mult)(void *,Vec,Vec);
7420563c6bSBarry Smith .             MatShellCreate(m,n,ctx,&mat);
7520563c6bSBarry Smith .             MatShellSetMult(mat,mult);
76e51e0e81SBarry Smith 
77e51e0e81SBarry Smith @*/
7820563c6bSBarry Smith int MatShellCreate(int m, int n, void *ctx,Mat *mat)
79e51e0e81SBarry Smith {
8020563c6bSBarry Smith   Mat      newmat;
8120563c6bSBarry Smith   MatShell *shell;
829e25ed09SBarry Smith   PETSCHEADERCREATE(newmat,_Mat,MAT_COOKIE,MATSHELL,MPI_COMM_WORLD);
83*a5a9c739SBarry Smith   PLogObjectCreate(newmat);
8420563c6bSBarry Smith   *mat           = newmat;
8520563c6bSBarry Smith   newmat->factor = 0;
8620563c6bSBarry Smith   newmat->row    = 0;
8720563c6bSBarry Smith   newmat->col    = 0;
8820563c6bSBarry Smith   newmat->destroy= MatShellDestroy;
8920563c6bSBarry Smith   newmat->ops    = &MatOps;
9020563c6bSBarry Smith   shell          = NEW(MatShell); CHKPTR(shell);
9120563c6bSBarry Smith   newmat->data   = (void *) shell;
9220563c6bSBarry Smith   shell->mult    = 0;
9320563c6bSBarry Smith   shell->m       = m;
9420563c6bSBarry Smith   shell->n       = n;
9520563c6bSBarry Smith   shell->ctx     = ctx;
96e51e0e81SBarry Smith   return 0;
97e51e0e81SBarry Smith }
98e51e0e81SBarry Smith 
99e51e0e81SBarry Smith /*@
10020563c6bSBarry Smith    MatShellSetMult - sets routine to use as matrix vector multiply.
101e51e0e81SBarry Smith 
102e51e0e81SBarry Smith   Input Parameters:
10320563c6bSBarry Smith .  mat - the matrix to add the operation to, created with MatShellCreate()
10420563c6bSBarry Smith .  mult - the matrix vector multiply routine.
105e51e0e81SBarry Smith 
10620563c6bSBarry Smith   Keywords: matrix, multiply
107e51e0e81SBarry Smith @*/
10820563c6bSBarry Smith int MatShellSetMult(Mat mat, int (*mult)(void*,Vec,Vec))
109e51e0e81SBarry Smith {
11020563c6bSBarry Smith   MatShell *shell;
111e51e0e81SBarry Smith   VALIDHEADER(mat,MAT_COOKIE);
11220563c6bSBarry Smith   shell = (MatShell *) mat->data;
11320563c6bSBarry Smith   shell->mult = mult;
11420563c6bSBarry Smith   return 0;
115e51e0e81SBarry Smith }
116f0479e8cSBarry Smith /*@
117f0479e8cSBarry Smith    MatShellSetMultTransAdd - sets routine to use as matrix vector multiply.
118f0479e8cSBarry Smith 
119f0479e8cSBarry Smith   Input Parameters:
120f0479e8cSBarry Smith .  mat - the matrix to add the operation to, created with MatShellCreate()
121f0479e8cSBarry Smith .  mult - the matrix vector multiply routine.
122f0479e8cSBarry Smith 
123f0479e8cSBarry Smith   Keywords: matrix, multiply, transpose
124f0479e8cSBarry Smith @*/
125f0479e8cSBarry Smith int MatShellSetMultTransAdd(Mat mat, int (*mult)(void*,Vec,Vec,Vec))
126f0479e8cSBarry Smith {
127f0479e8cSBarry Smith   MatShell *shell;
128f0479e8cSBarry Smith   VALIDHEADER(mat,MAT_COOKIE);
129f0479e8cSBarry Smith   shell               = (MatShell *) mat->data;
130f0479e8cSBarry Smith   shell->multtransadd = mult;
131f0479e8cSBarry Smith   return 0;
132f0479e8cSBarry Smith }
133f0479e8cSBarry Smith 
134f0479e8cSBarry Smith 
135