xref: /petsc/src/mat/impls/shell/shell.c (revision b9fa9cd00acd5827c9866f4329ced14637d43fca)
1 #ifndef lint
2 static char vcid[] = "$Id: shell.c,v 1.11 1995/08/01 19:24:44 curfman Exp bsmith $";
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   int  (*destroy)(void *);
20   void *ctx;
21 } MatShell;
22 
23 static int MatShellMult(Mat mat,Vec x,Vec y)
24 {
25   MatShell *shell;
26   shell = (MatShell *) mat->data;
27   return (*shell->mult)(shell->ctx,x,y);
28 }
29 static int MatShellMultTransAdd(Mat mat,Vec x,Vec y,Vec z)
30 {
31   MatShell *shell;
32   shell = (MatShell *) mat->data;
33   return (*shell->multtransadd)(shell->ctx,x,y,z);
34 }
35 static int MatShellDestroy(PetscObject obj)
36 {
37   int      ierr;
38   Mat      mat = (Mat) obj;
39   MatShell *shell;
40   shell = (MatShell *) mat->data;
41   if (shell->destroy) {ierr = (*shell->destroy)(shell->ctx);CHKERRQ(ierr);}
42   PETSCFREE(shell);
43   PLogObjectDestroy(mat);
44   PETSCHEADERDESTROY(mat);
45   return 0;
46 }
47 
48 static struct _MatOps MatOps = {0,0,
49        0,
50        MatShellMult,0,0,MatShellMultTransAdd,
51        0,0,0,0,
52        0,0,
53        0,
54        0,
55        0,0,0,
56        0,
57        0,0,0,
58        0,0,
59        0,
60        0,0,0,0,
61        0,0 };
62 
63 /*@
64    MatShellCreate - Creates a new matrix class for use with a user-defined
65    private data storage format.
66 
67    Input Parameters:
68 .  comm - MPI communicator
69 .  m - number of rows
70 .  n - number of columns
71 .  ctx - pointer to data needed by matrix-vector multiplication routine(s)
72 
73    Output Parameter:
74 .  mat - the matrix
75 
76    Notes:
77    The shell matrix type is intended to provide a simple class to use
78    with KSP (such as, for use with matrix-free methods). You should not
79    use the shell type if you plan to define a complete matrix class.
80 
81   Usage:
82 $   MatShellCreate(m,n,ctx,&mat);
83 $   MatShellSetMult(mat,mult);
84 
85 .keywords: matrix, shell, create
86 
87 .seealso: MatShellSetMult(), MatShellSetMultTransAdd()
88 @*/
89 int MatShellCreate(MPI_Comm comm,int m,int n,void *ctx,Mat *mat)
90 {
91   Mat      newmat;
92   MatShell *shell;
93   PETSCHEADERCREATE(newmat,_Mat,MAT_COOKIE,MATSHELL,comm);
94   PLogObjectCreate(newmat);
95   *mat           = newmat;
96   newmat->factor = 0;
97   newmat->destroy= MatShellDestroy;
98   newmat->ops    = &MatOps;
99   shell          = PETSCNEW(MatShell); CHKPTRQ(shell);
100   newmat->data   = (void *) shell;
101   shell->mult    = 0;
102   shell->m       = m;
103   shell->n       = n;
104   shell->ctx     = ctx;
105   return 0;
106 }
107 
108 /*@
109    MatShellSetMult - Sets the routine for computing the matrix-vector product.
110 
111    Input Parameters:
112 .  mat - the matrix associated with this operation, created
113          with MatShellCreate()
114 .  mult - the user-defined routine
115 
116    Calling sequence of mult:
117    int mult (void *ptr,Vec xin,Vec xout)
118 .  ptr - the application context for matrix data
119 .  xin - input vector
120 .  xout - output vector
121 
122 .keywords: matrix, multiply, shell, set
123 
124 .seealso: MatShellCreate(), MatShellSetMultTransAdd()
125 @*/
126 int MatShellSetMult(Mat mat,int (*mult)(void*,Vec,Vec))
127 {
128   MatShell *shell;
129   VALIDHEADER(mat,MAT_COOKIE);
130   shell = (MatShell *) mat->data;
131   shell->mult = mult;
132   return 0;
133 }
134 /*@
135    MatShellSetMultTransAdd - Sets the routine for computing v3 = v2 + A' * v1.
136 
137    Input Parameters:
138 .  mat - the matrix associated with this operation, created
139          with MatShellCreate()
140 .  mult - the user-defined routine
141 
142    Calling sequence of mult:
143    int mult (void *ptr,Vec v1,Vec v2,Vec v3)
144 .  ptr - the application context for matrix data
145 .  v1, v2 - the input vectors
146 .  v3 - the result
147 
148 .keywords: matrix, multiply, transpose
149 
150 .seealso: MatShellCreate(), MatShellSetMult()
151 @*/
152 int MatShellSetMultTransAdd(Mat mat,int (*mult)(void*,Vec,Vec,Vec))
153 {
154   MatShell *shell;
155   VALIDHEADER(mat,MAT_COOKIE);
156   shell               = (MatShell *) mat->data;
157   shell->multtransadd = mult;
158   return 0;
159 }
160 /*@
161    MatShellSetDestroy - Set the routine to use to destroy the
162         private contents of your MatShell.
163 
164    Input Parameters:
165 .  mat - the matrix associated with this operation, created
166          with MatShellCreate()
167 .  destroy - the user-defined routine
168 
169    Calling sequence of mult:
170    int destroy (void *ptr)
171 .  ptr - the application context for matrix data
172 
173 .keywords: matrix, destroy, shell, set
174 
175 .seealso: MatShellCreate()
176 @*/
177 int MatShellSetDestroy(Mat mat,int (*destroy)(void*))
178 {
179   MatShell *shell;
180   VALIDHEADER(mat,MAT_COOKIE);
181   shell = (MatShell *) mat->data;
182   shell->destroy = destroy;
183   return 0;
184 }
185 
186 
187