xref: /petsc/src/mat/impls/shell/shell.c (revision ca161407697b5d73508d887e7f1927b4808f5f9d)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*ca161407SBarry Smith static char vcid[] = "$Id: shell.c,v 1.51 1997/10/19 03:25:34 bsmith Exp bsmith $";
3357feee3SLois Curfman McInnes #endif
4e51e0e81SBarry Smith 
5e51e0e81SBarry Smith /*
620563c6bSBarry Smith    This provides a simple shell for Fortran (and C programmers) to
720563c6bSBarry Smith   create a very simple matrix class for use with KSP without coding
8ed3cc1f0SBarry Smith   much of anything.
9e51e0e81SBarry Smith */
10e51e0e81SBarry Smith 
11e51e0e81SBarry Smith #include "petsc.h"
1270f55243SBarry Smith #include "src/mat/matimpl.h"        /*I "mat.h" I*/
13f5eb4b81SSatish Balay #include "src/vec/vecimpl.h"
14e51e0e81SBarry Smith 
1520563c6bSBarry Smith typedef struct {
16f39d1f56SLois Curfman McInnes   int  M, N;                  /* number of global rows, columns */
17f39d1f56SLois Curfman McInnes   int  m, n;                  /* number of local rows, columns */
183a3eedf2SBarry Smith   int  (*destroy)(Mat);
1920563c6bSBarry Smith   void *ctx;
2088cf3e7dSBarry Smith } Mat_Shell;
21e51e0e81SBarry Smith 
225615d1e5SSatish Balay #undef __FUNC__
23d4bb536fSBarry Smith #define __FUNC__ "MatShellGetContext"
24b4fd4287SBarry Smith /*@
25a62d957aSLois Curfman McInnes     MatShellGetContext - Returns the user-provided context associated with a shell matrix.
26b4fd4287SBarry Smith 
27b4fd4287SBarry Smith     Input Parameter:
28b4fd4287SBarry Smith .   mat - the matrix, should have been created with MatCreateShell()
29b4fd4287SBarry Smith 
30b4fd4287SBarry Smith     Output Parameter:
31b4fd4287SBarry Smith .   ctx - the user provided context
32b4fd4287SBarry Smith 
33a62d957aSLois Curfman McInnes     Notes:
34a62d957aSLois Curfman McInnes     This routine is intended for use within various shell matrix routines,
35a62d957aSLois Curfman McInnes     as set with MatShellSetOperation().
36a62d957aSLois Curfman McInnes 
37a62d957aSLois Curfman McInnes .keywords: matrix, shell, get, context
38a62d957aSLois Curfman McInnes 
39a62d957aSLois Curfman McInnes .seealso: MatCreateShell(), MatShellSetOperation()
40b4fd4287SBarry Smith @*/
41b4fd4287SBarry Smith int MatShellGetContext(Mat mat,void **ctx)
42b4fd4287SBarry Smith {
433a40ed3dSBarry Smith   PetscFunctionBegin;
4477c4ece6SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
45b4fd4287SBarry Smith   if (mat->type != MATSHELL) *ctx = 0;
46b4fd4287SBarry Smith   else                       *ctx = ((Mat_Shell *) (mat->data))->ctx;
473a40ed3dSBarry Smith   PetscFunctionReturn(0);
48b4fd4287SBarry Smith }
49b4fd4287SBarry Smith 
505615d1e5SSatish Balay #undef __FUNC__
51d4bb536fSBarry Smith #define __FUNC__ "MatGetSize_Shell"
528f6be9afSLois Curfman McInnes int MatGetSize_Shell(Mat mat,int *M,int *N)
5371b459e3SLois Curfman McInnes {
5471b459e3SLois Curfman McInnes   Mat_Shell *shell = (Mat_Shell *) mat->data;
553a40ed3dSBarry Smith 
563a40ed3dSBarry Smith   PetscFunctionBegin;
57f39d1f56SLois Curfman McInnes   *M = shell->M; *N = shell->N;
583a40ed3dSBarry Smith   PetscFunctionReturn(0);
59f39d1f56SLois Curfman McInnes }
60f39d1f56SLois Curfman McInnes 
615615d1e5SSatish Balay #undef __FUNC__
62d4bb536fSBarry Smith #define __FUNC__ "MatGetLocalSize_Shell"
638f6be9afSLois Curfman McInnes int MatGetLocalSize_Shell(Mat mat,int *m,int *n)
64f39d1f56SLois Curfman McInnes {
65f39d1f56SLois Curfman McInnes   Mat_Shell *shell = (Mat_Shell *) mat->data;
663a40ed3dSBarry Smith 
673a40ed3dSBarry Smith   PetscFunctionBegin;
68f39d1f56SLois Curfman McInnes   *m = shell->n; *n = shell->n;
693a40ed3dSBarry Smith   PetscFunctionReturn(0);
7071b459e3SLois Curfman McInnes }
7171b459e3SLois Curfman McInnes 
725615d1e5SSatish Balay #undef __FUNC__
73d4bb536fSBarry Smith #define __FUNC__ "MatDestroy_Shell"
748f6be9afSLois Curfman McInnes int MatDestroy_Shell(PetscObject obj)
75e51e0e81SBarry Smith {
76b9fa9cd0SBarry Smith   int       ierr;
7720563c6bSBarry Smith   Mat       mat = (Mat) obj;
7888cf3e7dSBarry Smith   Mat_Shell *shell;
79ed3cc1f0SBarry Smith 
803a40ed3dSBarry Smith   PetscFunctionBegin;
8188cf3e7dSBarry Smith   shell = (Mat_Shell *) mat->data;
823a3eedf2SBarry Smith   if (shell->destroy) {ierr = (*shell->destroy)(mat);CHKERRQ(ierr);}
830452661fSBarry Smith   PetscFree(shell);
843a3eedf2SBarry Smith   PLogObjectDestroy(mat);
853a3eedf2SBarry Smith   PetscHeaderDestroy(mat);
863a40ed3dSBarry Smith   PetscFunctionReturn(0);
87e51e0e81SBarry Smith }
88e51e0e81SBarry Smith 
898f6be9afSLois Curfman McInnes int MatGetOwnershipRange_Shell(Mat mat, int *rstart,int *rend)
90b951964fSBarry Smith {
91*ca161407SBarry Smith   int ierr;
92*ca161407SBarry Smith 
93*ca161407SBarry Smith   PetscFunctionBegin;
94*ca161407SBarry Smith   ierr = MPI_Scan(&mat->m,rend,1,MPI_INT,MPI_SUM,mat->comm);CHKERRQ(ierr);
95b951964fSBarry Smith   *rstart = *rend - mat->m;
963a40ed3dSBarry Smith   PetscFunctionReturn(0);
97b951964fSBarry Smith }
98b951964fSBarry Smith 
99b951964fSBarry Smith 
100b951964fSBarry Smith 
101b951964fSBarry Smith 
102b951964fSBarry Smith static struct _MatOps MatOps = {0,
10320563c6bSBarry Smith        0,
10420563c6bSBarry Smith        0,
10520563c6bSBarry Smith        0,
10620563c6bSBarry Smith        0,
107b951964fSBarry Smith        0,
108b951964fSBarry Smith        0,
109b951964fSBarry Smith        0,
110b951964fSBarry Smith        0,
111b951964fSBarry Smith        0,
112b951964fSBarry Smith        0,
113b951964fSBarry Smith        0,
114b951964fSBarry Smith        0,
115b951964fSBarry Smith        0,
116b951964fSBarry Smith        0,
117b951964fSBarry Smith        0,
118b951964fSBarry Smith        0,
119b951964fSBarry Smith        0,
120b951964fSBarry Smith        0,
121b951964fSBarry Smith        0,
122b951964fSBarry Smith        0,
123b951964fSBarry Smith        0,
124b951964fSBarry Smith        0,
125b951964fSBarry Smith        0,
126b951964fSBarry Smith        0,
127b951964fSBarry Smith        0,
128b951964fSBarry Smith        0,
129b951964fSBarry Smith        0,
130b951964fSBarry Smith        0,
131b951964fSBarry Smith        0,
132b951964fSBarry Smith        MatGetSize_Shell,
133b951964fSBarry Smith        MatGetLocalSize_Shell,
134b951964fSBarry Smith        MatGetOwnershipRange_Shell,
135b951964fSBarry Smith        0,
136b951964fSBarry Smith        0,
137b951964fSBarry Smith        0,
138b951964fSBarry Smith        0,
139b951964fSBarry Smith        0 };
140e51e0e81SBarry Smith 
1415615d1e5SSatish Balay #undef __FUNC__
142d4bb536fSBarry Smith #define __FUNC__ "MatCreateShell"
1434b828684SBarry Smith /*@C
144052efed2SBarry Smith    MatCreateShell - Creates a new matrix class for use with a user-defined
145ff756334SLois Curfman McInnes    private data storage format.
146e51e0e81SBarry Smith 
147e51e0e81SBarry Smith    Input Parameters:
1486b5873e3SBarry Smith .  comm - MPI communicator
149f39d1f56SLois Curfman McInnes .  m - number of local rows
150f39d1f56SLois Curfman McInnes .  n - number of local columns
151f39d1f56SLois Curfman McInnes .  M - number of global rows
152f39d1f56SLois Curfman McInnes .  N - number of global columns
153deebb3c3SLois Curfman McInnes .  ctx - pointer to data needed by the shell matrix routines
154e51e0e81SBarry Smith 
155ff756334SLois Curfman McInnes    Output Parameter:
15644cd7ae7SLois Curfman McInnes .  A - the matrix
157e51e0e81SBarry Smith 
158f39d1f56SLois Curfman McInnes    Usage:
159f39d1f56SLois Curfman McInnes $    MatCreateShell(comm,m,n,M,N,ctx,&mat);
1601c1c02c0SLois Curfman McInnes $    MatShellSetOperation(mat,MATOP_MULT,mult);
161f39d1f56SLois Curfman McInnes $    [ Use matrix for operations that have been set ]
162f39d1f56SLois Curfman McInnes $    MatDestroy(mat);
163f39d1f56SLois Curfman McInnes 
164ff756334SLois Curfman McInnes    Notes:
165ff756334SLois Curfman McInnes    The shell matrix type is intended to provide a simple class to use
166ff756334SLois Curfman McInnes    with KSP (such as, for use with matrix-free methods). You should not
167ff756334SLois Curfman McInnes    use the shell type if you plan to define a complete matrix class.
168e51e0e81SBarry Smith 
169f39d1f56SLois Curfman McInnes    PETSc requires that matrices and vectors being used for certain
170f39d1f56SLois Curfman McInnes    operations are partitioned accordingly.  For example, when
171645985a0SLois Curfman McInnes    creating a shell matrix, A, that supports parallel matrix-vector
172645985a0SLois Curfman McInnes    products using MatMult(A,x,y) the user should set the number
173645985a0SLois Curfman McInnes    of local matrix rows to be the number of local elements of the
174645985a0SLois Curfman McInnes    corresponding result vector, y. Note that this is information is
175645985a0SLois Curfman McInnes    required for use of the matrix interface routines, even though
176645985a0SLois Curfman McInnes    the shell matrix may not actually be physically partitioned.
177645985a0SLois Curfman McInnes    For example,
178f39d1f56SLois Curfman McInnes 
179f39d1f56SLois Curfman McInnes $
180f39d1f56SLois Curfman McInnes $     Vec x, y
181645985a0SLois Curfman McInnes $     Mat A
182f39d1f56SLois Curfman McInnes $
183f39d1f56SLois Curfman McInnes $     VecCreate(comm,M,&y);
184f39d1f56SLois Curfman McInnes $     VecCreate(comm,N,&x);
185f39d1f56SLois Curfman McInnes $     VecGetLocalSize(y,&m);
186645985a0SLois Curfman McInnes $     MatCreateShell(comm,m,N,M,N,ctx,&A);
1871c1c02c0SLois Curfman McInnes $     MatShellSetOperation(mat,MATOP_MULT,mult);
188645985a0SLois Curfman McInnes $     MatMult(A,x,y);
189645985a0SLois Curfman McInnes $     MatDestroy(A);
190f39d1f56SLois Curfman McInnes $     VecDestroy(y); VecDestroy(x);
191645985a0SLois Curfman McInnes $
192e51e0e81SBarry Smith 
1930b627109SLois Curfman McInnes .keywords: matrix, shell, create
1940b627109SLois Curfman McInnes 
1953a3eedf2SBarry Smith .seealso: MatShellSetOperation(), MatHasOperation(), MatShellGetContext()
196e51e0e81SBarry Smith @*/
197f39d1f56SLois Curfman McInnes int MatCreateShell(MPI_Comm comm,int m,int n,int M,int N,void *ctx,Mat *A)
198e51e0e81SBarry Smith {
19944cd7ae7SLois Curfman McInnes   Mat       B;
20044cd7ae7SLois Curfman McInnes   Mat_Shell *b;
201ed3cc1f0SBarry Smith 
2023a40ed3dSBarry Smith   PetscFunctionBegin;
203d4bb536fSBarry Smith   PetscHeaderCreate(B,_p_Mat,MAT_COOKIE,MATSHELL,comm,MatDestroy,MatView);
20444cd7ae7SLois Curfman McInnes   PLogObjectCreate(B);
20544cd7ae7SLois Curfman McInnes   B->factor    = 0;
20644cd7ae7SLois Curfman McInnes   B->destroy   = MatDestroy_Shell;
20744cd7ae7SLois Curfman McInnes   B->assembled = PETSC_TRUE;
20844cd7ae7SLois Curfman McInnes   PetscMemcpy(&B->ops,&MatOps,sizeof(struct _MatOps));
209227d817aSBarry Smith 
21044cd7ae7SLois Curfman McInnes   b          = PetscNew(Mat_Shell); CHKPTRQ(b);
211eed86810SBarry Smith   PLogObjectMemory(B,sizeof(struct _p_Mat)+sizeof(Mat_Shell));
21244cd7ae7SLois Curfman McInnes   PetscMemzero(b,sizeof(Mat_Shell));
21344cd7ae7SLois Curfman McInnes   B->data   = (void *) b;
214f39d1f56SLois Curfman McInnes   b->M = M; B->M = M;
215f39d1f56SLois Curfman McInnes   b->N = N; B->N = N;
216f39d1f56SLois Curfman McInnes   b->m = m; B->m = m;
217f39d1f56SLois Curfman McInnes   b->n = n; B->n = n;
21844cd7ae7SLois Curfman McInnes   b->ctx     = ctx;
21944cd7ae7SLois Curfman McInnes   *A = B;
2203a40ed3dSBarry Smith   PetscFunctionReturn(0);
221e51e0e81SBarry Smith }
222e51e0e81SBarry Smith 
2235615d1e5SSatish Balay #undef __FUNC__
224d4bb536fSBarry Smith #define __FUNC__ "MatShellSetOperation"
225c16cb8f2SBarry Smith /*@C
2263a3eedf2SBarry Smith     MatShellSetOperation - Allows user to set a matrix operation for
2273a3eedf2SBarry Smith                            a shell matrix.
228e51e0e81SBarry Smith 
229e51e0e81SBarry Smith     Input Parameters:
230fae171e0SBarry Smith .   mat - the shell matrix
231fae171e0SBarry Smith .   op - the name of the operation
232fae171e0SBarry Smith .   f - the function that provides the operation.
233e51e0e81SBarry Smith 
234fae171e0SBarry Smith     Usage:
235a62d957aSLois Curfman McInnes $      extern int usermult(Mat,Vec,Vec);
236f39d1f56SLois Curfman McInnes $      ierr = MatCreateShell(comm,m,n,M,N,ctx,&A);
2371c1c02c0SLois Curfman McInnes $      ierr = MatShellSetOperation(A,MATOP_MULT,usermult);
2380b627109SLois Curfman McInnes 
239a62d957aSLois Curfman McInnes     Notes:
240a62d957aSLois Curfman McInnes     See the file petsc/include/mat.h for a complete list of matrix
2411c1c02c0SLois Curfman McInnes     operations, which all have the form MATOP_<OPERATION>, where
242a62d957aSLois Curfman McInnes     <OPERATION> is the name (in all capital letters) of the
2431c1c02c0SLois Curfman McInnes     user interface routine (e.g., MatMult() -> MATOP_MULT).
244a62d957aSLois Curfman McInnes 
245a62d957aSLois Curfman McInnes     All user-provided functions should have the same calling
246deebb3c3SLois Curfman McInnes     sequence as the usual matrix interface routines, since they
247deebb3c3SLois Curfman McInnes     are intended to be accessed via the usual matrix interface
248deebb3c3SLois Curfman McInnes     routines, e.g.,
249a62d957aSLois Curfman McInnes $       MatMult(Mat,Vec,Vec) -> usermult(Mat,Vec,Vec)
250a62d957aSLois Curfman McInnes 
251a62d957aSLois Curfman McInnes     Within each user-defined routine, the user should call
252a62d957aSLois Curfman McInnes     MatShellGetContext() to obtain the user-defined context that was
253a62d957aSLois Curfman McInnes     set by MatCreateShell().
254a62d957aSLois Curfman McInnes 
255a62d957aSLois Curfman McInnes .keywords: matrix, shell, set, operation
256a62d957aSLois Curfman McInnes 
257d4bb536fSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation()
258e51e0e81SBarry Smith @*/
259fae171e0SBarry Smith int MatShellSetOperation(Mat mat,MatOperation op, void *f)
260e51e0e81SBarry Smith {
2613a40ed3dSBarry Smith   PetscFunctionBegin;
26277c4ece6SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
263fae171e0SBarry Smith 
2641c1c02c0SLois Curfman McInnes   if (op == MATOP_DESTROY) {
265a62d957aSLois Curfman McInnes     if (mat->type == MATSHELL) {
266a62d957aSLois Curfman McInnes        Mat_Shell *shell = (Mat_Shell *) mat->data;
2673a3eedf2SBarry Smith        shell->destroy                 = (int (*)(Mat)) f;
268a62d957aSLois Curfman McInnes     }
269a62d957aSLois Curfman McInnes     else mat->destroy                 = (int (*)(PetscObject)) f;
270a62d957aSLois Curfman McInnes   }
2711c1c02c0SLois Curfman McInnes   else if (op == MATOP_VIEW) mat->view  = (int (*)(PetscObject,Viewer)) f;
272fae171e0SBarry Smith   else      (((void**)&mat->ops)[op]) = f;
273a62d957aSLois Curfman McInnes 
2743a40ed3dSBarry Smith   PetscFunctionReturn(0);
275e51e0e81SBarry Smith }
276f0479e8cSBarry Smith 
277d4bb536fSBarry Smith #undef __FUNC__
278d4bb536fSBarry Smith #define __FUNC__ "MatShellGetOperation"
279d4bb536fSBarry Smith /*@C
280d4bb536fSBarry Smith     MatShellGetOperation - Gets a matrix function for a shell matrix.
281d4bb536fSBarry Smith 
282d4bb536fSBarry Smith     Input Parameters:
283d4bb536fSBarry Smith .   mat - the shell matrix
284d4bb536fSBarry Smith .   op - the name of the operation
285d4bb536fSBarry Smith 
286d4bb536fSBarry Smith     Output Parameter:
287d4bb536fSBarry Smith .   f - the function that provides the operation.
288d4bb536fSBarry Smith 
289d4bb536fSBarry Smith     Notes:
290d4bb536fSBarry Smith     See the file petsc/include/mat.h for a complete list of matrix
291d4bb536fSBarry Smith     operations, which all have the form MATOP_<OPERATION>, where
292d4bb536fSBarry Smith     <OPERATION> is the name (in all capital letters) of the
293d4bb536fSBarry Smith     user interface routine (e.g., MatMult() -> MATOP_MULT).
294d4bb536fSBarry Smith 
295d4bb536fSBarry Smith     All user-provided functions have the same calling
296d4bb536fSBarry Smith     sequence as the usual matrix interface routines, since they
297d4bb536fSBarry Smith     are intended to be accessed via the usual matrix interface
298d4bb536fSBarry Smith     routines, e.g.,
299d4bb536fSBarry Smith $       MatMult(Mat,Vec,Vec) -> usermult(Mat,Vec,Vec)
300d4bb536fSBarry Smith 
301d4bb536fSBarry Smith     Within each user-defined routine, the user should call
302d4bb536fSBarry Smith     MatShellGetContext() to obtain the user-defined context that was
303d4bb536fSBarry Smith     set by MatCreateShell().
304d4bb536fSBarry Smith 
305d4bb536fSBarry Smith .keywords: matrix, shell, set, operation
306d4bb536fSBarry Smith 
307d4bb536fSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellSetOperation()
308d4bb536fSBarry Smith @*/
309d4bb536fSBarry Smith int MatShellGetOperation(Mat mat,MatOperation op, void **f)
310d4bb536fSBarry Smith {
3113a40ed3dSBarry Smith   PetscFunctionBegin;
312d4bb536fSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
313d4bb536fSBarry Smith 
314d4bb536fSBarry Smith   if (op == MATOP_DESTROY) {
315d4bb536fSBarry Smith     if (mat->type == MATSHELL) {
316d4bb536fSBarry Smith        Mat_Shell *shell = (Mat_Shell *) mat->data;
317d4bb536fSBarry Smith        *f = (void *) shell->destroy;
318d4bb536fSBarry Smith     }
319d4bb536fSBarry Smith     else *f = (void *) mat->destroy;
320d4bb536fSBarry Smith   }
321d4bb536fSBarry Smith   else if (op == MATOP_VIEW) *f = (void *) mat->view;
322d4bb536fSBarry Smith   else      *f = (((void**)&mat->ops)[op]);
323d4bb536fSBarry Smith 
3243a40ed3dSBarry Smith   PetscFunctionReturn(0);
325d4bb536fSBarry Smith }
326d4bb536fSBarry Smith 
327