xref: /petsc/src/mat/impls/shell/shell.c (revision 9e25ed09632ec91e2227f4e6f1ce3bfe794e20d8)
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   PETSCHEADERDESTROY(mat);
38   return 0;
39 }
40 
41 static struct _MatOps MatOps = {0,0,
42        0,
43        MatShellMult,0,0,MatShellMultTransAdd,
44        0,0,0,0,
45        0,0,
46        0,
47        0,
48        0,0,0,
49        0,
50        0,0,0,
51        0,0,
52        0,
53        0,0,0,0,
54        0,0 };
55 
56 /*@
57    MatShellCreate - creates a new matrix class for use with your
58           own private data storage format. This is intended to
59           provide a simple class to use with KSP. You should
60           not use this if you plan to make a complete class.
61 
62   Input Parameters:
63 .  m,n - number of rows and columns in matrix
64 .  ctx - pointer to your data needed by matrix multiply.
65 
66   Output Parameters:
67 .  mat - the matrix
68 
69   Keywords: matrix, shell
70 
71   Usage:
72 .             int (*mult)(void *,Vec,Vec);
73 .             MatShellCreate(m,n,ctx,&mat);
74 .             MatShellSetMult(mat,mult);
75 
76 @*/
77 int MatShellCreate(int m, int n, void *ctx,Mat *mat)
78 {
79   Mat      newmat;
80   MatShell *shell;
81   PETSCHEADERCREATE(newmat,_Mat,MAT_COOKIE,MATSHELL,MPI_COMM_WORLD);
82   *mat           = newmat;
83   newmat->factor = 0;
84   newmat->row    = 0;
85   newmat->col    = 0;
86   newmat->destroy= MatShellDestroy;
87   newmat->ops    = &MatOps;
88   shell          = NEW(MatShell); CHKPTR(shell);
89   newmat->data   = (void *) shell;
90   shell->mult    = 0;
91   shell->m       = m;
92   shell->n       = n;
93   shell->ctx     = ctx;
94   return 0;
95 }
96 
97 /*@
98    MatShellSetMult - sets routine to use as matrix vector multiply.
99 
100   Input Parameters:
101 .  mat - the matrix to add the operation to, created with MatShellCreate()
102 .  mult - the matrix vector multiply routine.
103 
104   Keywords: matrix, multiply
105 @*/
106 int MatShellSetMult(Mat mat, int (*mult)(void*,Vec,Vec))
107 {
108   MatShell *shell;
109   VALIDHEADER(mat,MAT_COOKIE);
110   shell = (MatShell *) mat->data;
111   shell->mult = mult;
112   return 0;
113 }
114 /*@
115    MatShellSetMultTransAdd - sets routine to use as matrix vector multiply.
116 
117   Input Parameters:
118 .  mat - the matrix to add the operation to, created with MatShellCreate()
119 .  mult - the matrix vector multiply routine.
120 
121   Keywords: matrix, multiply, transpose
122 @*/
123 int MatShellSetMultTransAdd(Mat mat, int (*mult)(void*,Vec,Vec,Vec))
124 {
125   MatShell *shell;
126   VALIDHEADER(mat,MAT_COOKIE);
127   shell               = (MatShell *) mat->data;
128   shell->multtransadd = mult;
129   return 0;
130 }
131 
132 
133