xref: /petsc/src/mat/impls/python/pythonmat.c (revision a69119a591a03a9d906b29c0a4e9802e4d7c9795)
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 (Mat) 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: `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 object
34 
35    Output Parameter:
36 .  pyname - full dotted Python name [package].module[.{class|function}]
37 
38    Level: intermediate
39 
40 .seealso: `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: `MATPYTHON`, `MatPythonSetType()`, `PetscPythonInitialize()`
69 
70 @*/
71 PetscErrorCode MatPythonCreate(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt M, PetscInt N, const char pyname[], Mat *A) {
72   PetscFunctionBegin;
73   PetscValidCharPointer(pyname, 6);
74   PetscValidPointer(A, 6);
75   PetscCall(MatCreate(comm, A));
76   PetscCall(MatSetSizes(*A, m, n, M, N));
77   PetscCall(MatSetType(*A, MATPYTHON));
78   PetscCall(MatPythonSetType(*A, pyname));
79   PetscCall(MatBindToCPU(*A, PETSC_FALSE));
80   PetscFunctionReturn(0);
81 }
82