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