xref: /petsc/src/mat/impls/shell/shell.c (revision 357feee3d54c040e0db7c3c11e1d9e8946565884)
1 #ifndef lint
2 static char vcid[] = "$Id: bdiag.c,v 1.3 1995/04/24 21:08:05 curfman Exp curfman $";
3 #endif
4 
5 /*
6    This provides a simple shell for Fortran (and C programmers) to
7   create a very simple matrix class for use with KSP without coding
8   mush of anything.
9 */
10 
11 #include "petsc.h"
12 #include "matimpl.h"        /*I "mat.h" I*/
13 #include "vec/vecimpl.h"
14 
15 typedef struct {
16   int  m,n;
17   int  (*mult)(void *,Vec,Vec);
18   int  (*multtransadd)(void*,Vec,Vec,Vec);
19   void *ctx;
20 } MatShell;
21 
22 static int MatShellMult(Mat mat,Vec x,Vec y)
23 {
24   MatShell *shell;
25   shell = (MatShell *) mat->data;
26   return (*shell->mult)(shell->ctx,x,y);
27 }
28 static int MatShellMultTransAdd(Mat mat,Vec x,Vec y,Vec z)
29 {
30   MatShell *shell;
31   shell = (MatShell *) mat->data;
32   return (*shell->multtransadd)(shell->ctx,x,y,z);
33 }
34 static int MatShellDestroy(PetscObject obj)
35 {
36   Mat      mat = (Mat) obj;
37   MatShell *shell;
38   shell = (MatShell *) mat->data;
39   FREE(shell);
40   PLogObjectDestroy(mat);
41   PETSCHEADERDESTROY(mat);
42   return 0;
43 }
44 
45 static struct _MatOps MatOps = {0,0,
46        0,
47        MatShellMult,0,0,MatShellMultTransAdd,
48        0,0,0,0,
49        0,0,
50        0,
51        0,
52        0,0,0,
53        0,
54        0,0,0,
55        0,0,
56        0,
57        0,0,0,0,
58        0,0 };
59 
60 /*@
61    MatShellCreate - Creates a new matrix class for use with a user-defined
62    private data storage format.
63 
64    Input Parameters:
65 .  comm - MPI communicator
66 .  m - number of rows
67 .  n - number of columns
68 .  ctx - pointer to your data needed by matrix-vector multiply
69 
70    Output Parameter:
71 .  mat - the matrix
72 
73    Notes:
74    The shell matrix type is intended to provide a simple class to use
75    with KSP (such as, for use with matrix-free methods). You should not
76    use the shell type if you plan to define a complete matrix class.
77 
78   Usage:
79 $   int (*mult)(void *,Vec,Vec);
80 $   MatShellCreate(m,n,ctx,&mat);
81 $   MatShellSetMult(mat,mult);
82 
83 .keywords: Mat, matrix, shell
84 @*/
85 int MatShellCreate(MPI_Comm comm,int m, int n, void *ctx,Mat *mat)
86 {
87   Mat      newmat;
88   MatShell *shell;
89   PETSCHEADERCREATE(newmat,_Mat,MAT_COOKIE,MATSHELL,comm);
90   PLogObjectCreate(newmat);
91   *mat           = newmat;
92   newmat->factor = 0;
93   newmat->destroy= MatShellDestroy;
94   newmat->ops    = &MatOps;
95   shell          = NEW(MatShell); CHKPTR(shell);
96   newmat->data   = (void *) shell;
97   shell->mult    = 0;
98   shell->m       = m;
99   shell->n       = n;
100   shell->ctx     = ctx;
101   return 0;
102 }
103 
104 /*@
105    MatShellSetMult - sets routine to use as matrix vector multiply.
106 
107   Input Parameters:
108 .  mat - the matrix to add the operation to, created with MatShellCreate()
109 .  mult - the matrix vector multiply routine.
110 
111   Keywords: matrix, multiply
112 @*/
113 int MatShellSetMult(Mat mat, int (*mult)(void*,Vec,Vec))
114 {
115   MatShell *shell;
116   VALIDHEADER(mat,MAT_COOKIE);
117   shell = (MatShell *) mat->data;
118   shell->mult = mult;
119   return 0;
120 }
121 /*@
122    MatShellSetMultTransAdd - sets routine to use as matrix vector multiply.
123 
124   Input Parameters:
125 .  mat - the matrix to add the operation to, created with MatShellCreate()
126 .  mult - the matrix vector multiply routine.
127 
128   Keywords: matrix, multiply, transpose
129 @*/
130 int MatShellSetMultTransAdd(Mat mat, int (*mult)(void*,Vec,Vec,Vec))
131 {
132   MatShell *shell;
133   VALIDHEADER(mat,MAT_COOKIE);
134   shell               = (MatShell *) mat->data;
135   shell->multtransadd = mult;
136   return 0;
137 }
138 
139 
140