xref: /petsc/src/mat/impls/shell/shell.c (revision 78b31e5415da8002fdf1e4c89af509906889cf86)
1357feee3SLois Curfman McInnes #ifndef lint
2*78b31e54SBarry Smith static char vcid[] = "$Id: shell.c,v 1.9 1995/05/11 03:46:44 curfman Exp bsmith $";
3357feee3SLois Curfman McInnes #endif
4e51e0e81SBarry Smith 
5e51e0e81SBarry Smith /*
620563c6bSBarry Smith    This provides a simple shell for Fortran (and C programmers) to
720563c6bSBarry Smith   create a very simple matrix class for use with KSP without coding
820563c6bSBarry Smith   mush of anything.
9e51e0e81SBarry Smith */
10e51e0e81SBarry Smith 
11e51e0e81SBarry Smith #include "petsc.h"
12e51e0e81SBarry Smith #include "matimpl.h"        /*I "mat.h" I*/
13e51e0e81SBarry Smith #include "vec/vecimpl.h"
14e51e0e81SBarry Smith 
1520563c6bSBarry Smith typedef struct {
1620563c6bSBarry Smith   int  m,n;
1720563c6bSBarry Smith   int  (*mult)(void *,Vec,Vec);
18f0479e8cSBarry Smith   int  (*multtransadd)(void*,Vec,Vec,Vec);
1920563c6bSBarry Smith   void *ctx;
2020563c6bSBarry Smith } MatShell;
21e51e0e81SBarry Smith 
2220563c6bSBarry Smith static int MatShellMult(Mat mat,Vec x,Vec y)
23e51e0e81SBarry Smith {
2420563c6bSBarry Smith   MatShell *shell;
2520563c6bSBarry Smith   shell = (MatShell *) mat->data;
2620563c6bSBarry Smith   return (*shell->mult)(shell->ctx,x,y);
27e51e0e81SBarry Smith }
28f0479e8cSBarry Smith static int MatShellMultTransAdd(Mat mat,Vec x,Vec y,Vec z)
29f0479e8cSBarry Smith {
30f0479e8cSBarry Smith   MatShell *shell;
31f0479e8cSBarry Smith   shell = (MatShell *) mat->data;
32f0479e8cSBarry Smith   return (*shell->multtransadd)(shell->ctx,x,y,z);
33f0479e8cSBarry Smith }
3420563c6bSBarry Smith static int MatShellDestroy(PetscObject obj)
35e51e0e81SBarry Smith {
3620563c6bSBarry Smith   Mat      mat = (Mat) obj;
3720563c6bSBarry Smith   MatShell *shell;
3820563c6bSBarry Smith   shell = (MatShell *) mat->data;
39*78b31e54SBarry Smith   PETSCFREE(shell);
40a5a9c739SBarry Smith   PLogObjectDestroy(mat);
419e25ed09SBarry Smith   PETSCHEADERDESTROY(mat);
42e51e0e81SBarry Smith   return 0;
43e51e0e81SBarry Smith }
44e51e0e81SBarry Smith 
4520563c6bSBarry Smith static struct _MatOps MatOps = {0,0,
4620563c6bSBarry Smith        0,
47f0479e8cSBarry Smith        MatShellMult,0,0,MatShellMultTransAdd,
4820563c6bSBarry Smith        0,0,0,0,
4920563c6bSBarry Smith        0,0,
5020563c6bSBarry Smith        0,
5120563c6bSBarry Smith        0,
5220563c6bSBarry Smith        0,0,0,
5320563c6bSBarry Smith        0,
5420563c6bSBarry Smith        0,0,0,
5520563c6bSBarry Smith        0,0,
5620563c6bSBarry Smith        0,
5720563c6bSBarry Smith        0,0,0,0,
5820563c6bSBarry Smith        0,0 };
59e51e0e81SBarry Smith 
60e51e0e81SBarry Smith /*@
61ff756334SLois Curfman McInnes    MatShellCreate - Creates a new matrix class for use with a user-defined
62ff756334SLois Curfman McInnes    private data storage format.
63e51e0e81SBarry Smith 
64e51e0e81SBarry Smith    Input Parameters:
656b5873e3SBarry Smith .  comm - MPI communicator
66ff756334SLois Curfman McInnes .  m - number of rows
67ff756334SLois Curfman McInnes .  n - number of columns
680b627109SLois Curfman McInnes .  ctx - pointer to data needed by matrix-vector multiplication routine(s)
69e51e0e81SBarry Smith 
70ff756334SLois Curfman McInnes    Output Parameter:
71e51e0e81SBarry Smith .  mat - the matrix
72e51e0e81SBarry Smith 
73ff756334SLois Curfman McInnes    Notes:
74ff756334SLois Curfman McInnes    The shell matrix type is intended to provide a simple class to use
75ff756334SLois Curfman McInnes    with KSP (such as, for use with matrix-free methods). You should not
76ff756334SLois Curfman McInnes    use the shell type if you plan to define a complete matrix class.
77e51e0e81SBarry Smith 
7820563c6bSBarry Smith   Usage:
79ff756334SLois Curfman McInnes $   MatShellCreate(m,n,ctx,&mat);
80ff756334SLois Curfman McInnes $   MatShellSetMult(mat,mult);
81e51e0e81SBarry Smith 
820b627109SLois Curfman McInnes .keywords: matrix, shell, create
830b627109SLois Curfman McInnes 
840b627109SLois Curfman McInnes .seealso: MatShellSetMult(), MatShellSetMultTransAdd()
85e51e0e81SBarry Smith @*/
866b5873e3SBarry Smith int MatShellCreate(MPI_Comm comm,int m, int n, void *ctx,Mat *mat)
87e51e0e81SBarry Smith {
8820563c6bSBarry Smith   Mat      newmat;
8920563c6bSBarry Smith   MatShell *shell;
906b5873e3SBarry Smith   PETSCHEADERCREATE(newmat,_Mat,MAT_COOKIE,MATSHELL,comm);
91a5a9c739SBarry Smith   PLogObjectCreate(newmat);
9220563c6bSBarry Smith   *mat           = newmat;
9320563c6bSBarry Smith   newmat->factor = 0;
9420563c6bSBarry Smith   newmat->destroy= MatShellDestroy;
9520563c6bSBarry Smith   newmat->ops    = &MatOps;
96*78b31e54SBarry Smith   shell          = PETSCNEW(MatShell); CHKPTRQ(shell);
9720563c6bSBarry Smith   newmat->data   = (void *) shell;
9820563c6bSBarry Smith   shell->mult    = 0;
9920563c6bSBarry Smith   shell->m       = m;
10020563c6bSBarry Smith   shell->n       = n;
10120563c6bSBarry Smith   shell->ctx     = ctx;
102e51e0e81SBarry Smith   return 0;
103e51e0e81SBarry Smith }
104e51e0e81SBarry Smith 
105e51e0e81SBarry Smith /*@
1060b627109SLois Curfman McInnes    MatShellSetMult - Sets the routine for computing the matrix-vector product.
107e51e0e81SBarry Smith 
108e51e0e81SBarry Smith    Input Parameters:
1090b627109SLois Curfman McInnes .  mat - the matrix associated with this operation, created
1100b627109SLois Curfman McInnes          with MatShellCreate()
1110b627109SLois Curfman McInnes .  mult - the user-defined routine
112e51e0e81SBarry Smith 
1130b627109SLois Curfman McInnes    Calling sequence of mult:
1140b627109SLois Curfman McInnes    int mult (void *ptr,Vec xin,Vec xout)
1150b627109SLois Curfman McInnes .  ptr - the application context for matrix data
1160b627109SLois Curfman McInnes .  xin - input vector
1170b627109SLois Curfman McInnes .  xout - output vector
1180b627109SLois Curfman McInnes 
1190b627109SLois Curfman McInnes .keywords: matrix, multiply, shell, set
1200b627109SLois Curfman McInnes 
1210b627109SLois Curfman McInnes .seealso: MatShellCreate(), MatShellSetMultTransAdd()
122e51e0e81SBarry Smith @*/
12320563c6bSBarry Smith int MatShellSetMult(Mat mat, int (*mult)(void*,Vec,Vec))
124e51e0e81SBarry Smith {
12520563c6bSBarry Smith   MatShell *shell;
126e51e0e81SBarry Smith   VALIDHEADER(mat,MAT_COOKIE);
12720563c6bSBarry Smith   shell = (MatShell *) mat->data;
12820563c6bSBarry Smith   shell->mult = mult;
12920563c6bSBarry Smith   return 0;
130e51e0e81SBarry Smith }
131f0479e8cSBarry Smith /*@
1320b627109SLois Curfman McInnes    MatShellSetMultTransAdd - Sets the routine for computing v3 = v2 + A' * v1.
133f0479e8cSBarry Smith 
134f0479e8cSBarry Smith    Input Parameters:
1350b627109SLois Curfman McInnes .  mat - the matrix associated with this operation, created
1360b627109SLois Curfman McInnes          with MatShellCreate()
1370b627109SLois Curfman McInnes .  mult - the user-defined routine
138f0479e8cSBarry Smith 
1390b627109SLois Curfman McInnes    Calling sequence of mult:
1400b627109SLois Curfman McInnes    int mult (void *ptr,Vec v1,Vec v2,Vec v3)
1410b627109SLois Curfman McInnes .  ptr - the application context for matrix data
1420b627109SLois Curfman McInnes .  v1, v2 - the input vectors
1430b627109SLois Curfman McInnes .  v3 - the result
1440b627109SLois Curfman McInnes 
1450b627109SLois Curfman McInnes .keywords: matrix, multiply, transpose
1460b627109SLois Curfman McInnes 
1470b627109SLois Curfman McInnes .seealso: MatShellCreate(), MatShellSetMult()
148f0479e8cSBarry Smith @*/
149f0479e8cSBarry Smith int MatShellSetMultTransAdd(Mat mat, int (*mult)(void*,Vec,Vec,Vec))
150f0479e8cSBarry Smith {
151f0479e8cSBarry Smith   MatShell *shell;
152f0479e8cSBarry Smith   VALIDHEADER(mat,MAT_COOKIE);
153f0479e8cSBarry Smith   shell               = (MatShell *) mat->data;
154f0479e8cSBarry Smith   shell->multtransadd = mult;
155f0479e8cSBarry Smith   return 0;
156f0479e8cSBarry Smith }
157f0479e8cSBarry Smith 
158f0479e8cSBarry Smith 
159