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