xref: /petsc/src/mat/impls/python/pythonmat.c (revision 83c9f7f220f3448cfcd2a76a64462ac242b37309)
1 #include <petsc/private/matimpl.h> /*I "petscmat.h" I*/
2 
3 /*@C
4    MatPythonSetType - Initialize a `Mat` object implemented in Python.
5 
6    Collective on Mat
7 
8    Input Parameters:
9 +  mat - the matrix object.
10 -  pyname - full dotted Python name [package].module[.{class|function}]
11 
12    Options Database Key:
13 .  -mat_python_type <pyname> - python class
14 
15    Level: intermediate
16 
17 .seealso: `Mat`, `MatType`, `MatCreate()`, `MatSetType()`, `MATPYTHON`, `PetscPythonInitialize()`
18 @*/
19 PetscErrorCode MatPythonSetType(Mat mat, const char pyname[]) {
20   PetscFunctionBegin;
21   PetscValidHeaderSpecific(mat, MAT_CLASSID, 1);
22   PetscValidCharPointer(pyname, 2);
23   PetscTryMethod(mat, "MatPythonSetType_C", (Mat, const char[]), (mat, pyname));
24   PetscFunctionReturn(0);
25 }
26 
27 /*@C
28    MatPythonGetType - Get the type of a `Mat` object implemented in Python.
29 
30    Not collective
31 
32    Input Parameter:
33 .  mat - the matrix
34 
35    Output Parameter:
36 .  pyname - full dotted Python name [package].module[.{class|function}]
37 
38    Level: intermediate
39 
40 .seealso: `Mat`, `MatType`, `MatCreate()`, `MatSetType()`, `MATPYTHON`, `PetscPythonInitialize()`, `MatPythonSetType()`
41 @*/
42 PetscErrorCode MatPythonGetType(Mat mat, const char *pyname[]) {
43   PetscFunctionBegin;
44   PetscValidHeaderSpecific(mat, MAT_CLASSID, 1);
45   PetscValidPointer(pyname, 2);
46   PetscUseMethod(mat, "MatPythonGetType_C", (Mat, const char *[]), (mat, pyname));
47   PetscFunctionReturn(0);
48 }
49 
50 /*@C
51    MatPythonCreate - Create a `Mat` object implemented in Python.
52 
53    Collective on Mat
54 
55    Input Parameters:
56 +  comm - MPI communicator
57 .  m - number of local rows (or `PETSC_DECIDE` to have calculated if M is given)
58 .  n - number of local columns (or `PETSC_DECIDE` to have calculated if N is given)
59 .  M - number of global rows (or `PETSC_DECIDE` to have calculated if m is given)
60 .  N - number of global columns (or `PETSC_DECIDE` to have calculated if n is given)
61 -  pyname - full dotted Python name [package].module[.{class|function}]
62 
63    Output Parameter:
64 .  A - the matrix
65 
66    Level: intermediate
67 
68 .seealso: `Mat`, `MatType`, `MATPYTHON`, `MatPythonSetType()`, `PetscPythonInitialize()`
69 @*/
70 PetscErrorCode MatPythonCreate(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt M, PetscInt N, const char pyname[], Mat *A) {
71   PetscFunctionBegin;
72   PetscValidCharPointer(pyname, 6);
73   PetscValidPointer(A, 6);
74   PetscCall(MatCreate(comm, A));
75   PetscCall(MatSetSizes(*A, m, n, M, N));
76   PetscCall(MatSetType(*A, MATPYTHON));
77   PetscCall(MatPythonSetType(*A, pyname));
78   PetscCall(MatBindToCPU(*A, PETSC_FALSE));
79   PetscFunctionReturn(0);
80 }
81