xref: /petsc/src/mat/impls/shell/shell.c (revision 1a9411475029bd0c092ff088e6540bae5153ea53)
1 #ifndef lint
2 static char vcid[] = "$Id: shell.c,v 1.13 1995/08/04 01:52:12 bsmith 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 } Mat_Shell;
22 
23 static int MatMult_Shell(Mat mat,Vec x,Vec y)
24 {
25   Mat_Shell *shell;
26   shell = (Mat_Shell *) mat->data;
27   return (*shell->mult)(shell->ctx,x,y);
28 }
29 static int MatMultTransAdd_Shell(Mat mat,Vec x,Vec y,Vec z)
30 {
31   Mat_Shell *shell;
32   shell = (Mat_Shell *) mat->data;
33   return (*shell->multtransadd)(shell->ctx,x,y,z);
34 }
35 static int MatDestroy_Shell(PetscObject obj)
36 {
37   int      ierr;
38   Mat      mat = (Mat) obj;
39   Mat_Shell *shell;
40   shell = (Mat_Shell *) 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        MatMult_Shell,0,0,MatMultTransAdd_Shell,
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    Mat_ShellCreate - 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   Mat_Shell *shell;
93   PETSCHEADERCREATE(newmat,_Mat,MAT_COOKIE,MATSHELL,comm);
94   PLogObjectCreate(newmat);
95   *mat           = newmat;
96   newmat->factor = 0;
97   newmat->destroy= MatDestroy_Shell;
98   newmat->ops    = &MatOps;
99   shell          = PETSCNEW(Mat_Shell); CHKPTRQ(shell);
100   PETSCMEMSET(shell,0,sizeof(Mat_Shell));
101   newmat->data   = (void *) shell;
102   shell->mult    = 0;
103   shell->m       = m;
104   shell->n       = n;
105   shell->ctx     = ctx;
106   return 0;
107 }
108 
109 /*@
110    MatShellSetMult - Sets the routine for computing the matrix-vector product.
111 
112    Input Parameters:
113 .  mat - the matrix associated with this operation, created
114          with MatShellCreate()
115 .  mult - the user-defined routine
116 
117    Calling sequence of mult:
118    int mult (void *ptr,Vec xin,Vec xout)
119 .  ptr - the application context for matrix data
120 .  xin - input vector
121 .  xout - output vector
122 
123 .keywords: matrix, multiply, shell, set
124 
125 .seealso: MatShellCreate(), MatShellSetMultTransAdd()
126 @*/
127 int MatShellSetMult(Mat mat,int (*mult)(void*,Vec,Vec))
128 {
129   Mat_Shell *shell;
130   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
131   shell = (Mat_Shell *) mat->data;
132   shell->mult = mult;
133   return 0;
134 }
135 /*@
136    MatShellSetMultTransAdd - Sets the routine for computing v3 = v2 + A' * v1.
137 
138    Input Parameters:
139 .  mat - the matrix associated with this operation, created
140          with MatShellCreate()
141 .  mult - the user-defined routine
142 
143    Calling sequence of mult:
144    int mult (void *ptr,Vec v1,Vec v2,Vec v3)
145 .  ptr - the application context for matrix data
146 .  v1, v2 - the input vectors
147 .  v3 - the result
148 
149 .keywords: matrix, multiply, transpose
150 
151 .seealso: MatShellCreate(), MatShellSetMult()
152 @*/
153 int MatShellSetMultTransAdd(Mat mat,int (*mult)(void*,Vec,Vec,Vec))
154 {
155   Mat_Shell *shell;
156   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
157   shell               = (Mat_Shell *) mat->data;
158   shell->multtransadd = mult;
159   return 0;
160 }
161 /*@
162    MatShellSetDestroy - Set the routine to use to destroy the
163         private contents of your MatShell.
164 
165    Input Parameters:
166 .  mat - the matrix associated with this operation, created
167          with MatShellCreate()
168 .  destroy - the user-defined routine
169 
170    Calling sequence of mult:
171    int destroy (void *ptr)
172 .  ptr - the application context for matrix data
173 
174 .keywords: matrix, destroy, shell, set
175 
176 .seealso: MatShellCreate()
177 @*/
178 int MatShellSetDestroy(Mat mat,int (*destroy)(void*))
179 {
180   Mat_Shell *shell;
181   PETSCVALIDHEADERSPECIFIC(mat,MAT_COOKIE);
182   shell = (Mat_Shell *) mat->data;
183   shell->destroy = destroy;
184   return 0;
185 }
186 
187 
188