xref: /petsc/src/mat/impls/shell/shell.c (revision 20563c6b1ea7b82b48c81bbd22ce9170a8c92d3b)
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   void *ctx;
16 } MatShell;
17 
18 static int MatShellMult(Mat mat,Vec x,Vec y)
19 {
20   MatShell *shell;
21   shell = (MatShell *) mat->data;
22   return (*shell->mult)(shell->ctx,x,y);
23 }
24 static int MatShellDestroy(PetscObject obj)
25 {
26   Mat      mat = (Mat) obj;
27   MatShell *shell;
28   shell = (MatShell *) mat->data;
29   FREE(shell); FREE(mat);
30   return 0;
31 }
32 
33 static struct _MatOps MatOps = {0,0,
34        0,
35        MatShellMult,0,0,0,
36        0,0,0,0,
37        0,0,
38        0,
39        0,
40        0,0,0,
41        0,
42        0,0,0,
43        0,0,
44        0,
45        0,0,0,0,
46        0,0 };
47 
48 /*@
49    MatShellCreate - creates a new matrix class for use with your
50           own private data storage format. This is intended to
51           provide a simple class to use with KSP. You should
52           not use this if you plan to make a complete class.
53 
54   Input Parameters:
55 .  m,n - number of rows and columns in matrix
56 .  ctx - pointer to your data needed by matrix multiply.
57 
58   Output Parameters:
59 .  mat - the matrix
60 
61   Keywords: matrix, shell
62 
63   Usage:
64 .             int (*mult)(void *,Vec,Vec);
65 .             MatShellCreate(m,n,ctx,&mat);
66 .             MatShellSetMult(mat,mult);
67 
68 @*/
69 int MatShellCreate(int m, int n, void *ctx,Mat *mat)
70 {
71   Mat      newmat;
72   MatShell *shell;
73   CREATEHEADER(newmat,_Mat);
74   *mat           = newmat;
75   newmat->cookie = MAT_COOKIE;
76   newmat->factor = 0;
77   newmat->row    = 0;
78   newmat->col    = 0;
79   newmat->destroy= MatShellDestroy;
80   newmat->ops    = &MatOps;
81   shell          = NEW(MatShell); CHKPTR(shell);
82   newmat->data   = (void *) shell;
83   shell->mult    = 0;
84   shell->m       = m;
85   shell->n       = n;
86   shell->ctx     = ctx;
87   return 0;
88 }
89 
90 /*@
91    MatShellSetMult - sets routine to use as matrix vector multiply.
92 
93   Input Parameters:
94 .  mat - the matrix to add the operation to, created with MatShellCreate()
95 .  mult - the matrix vector multiply routine.
96 
97   Keywords: matrix, multiply
98 @*/
99 int MatShellSetMult(Mat mat, int (*mult)(void*,Vec,Vec))
100 {
101   MatShell *shell;
102   VALIDHEADER(mat,MAT_COOKIE);
103   shell = (MatShell *) mat->data;
104   shell->mult = mult;
105   return 0;
106 }
107