Date: 2025-03-29T05:58:35+00:00

Actual source code: pythonmat.c

      1: #include <petsc/private/matimpl.h>

      3: /*@
      4:    MatPythonSetType - Initialize a `Mat` object implemented in Python.

      6:    Collective

      8:    Input Parameters:
      9: +  mat - the matrix object.
     10: -  pyname - full dotted Python name [package].module[.{class|function}]

     12:    Options Database Key:
     13: .  -mat_python_type <pyname> - python class

     15:    Level: intermediate

     17: .seealso: [](ch_matrices), `Mat`, `MatType`, `MatCreate()`, `MatSetType()`, `MATPYTHON`, `PetscPythonInitialize()`
     18: @*/
     19: PetscErrorCode MatPythonSetType(Mat mat, const char pyname[])
     20: {
     21:   PetscFunctionBegin;
     23:   PetscAssertPointer(pyname, 2);
     24:   PetscTryMethod(mat, "MatPythonSetType_C", (Mat, const char[]), (mat, pyname));
     25:   PetscFunctionReturn(PETSC_SUCCESS);
     26: }

     28: /*@
     29:    MatPythonGetType - Get the Python name of a `Mat` object implemented in Python.

     31:    Not Collective

     33:    Input Parameter:
     34: .  mat - the matrix

     36:    Output Parameter:
     37: .  pyname - full dotted Python name [package].module[.{class|function}]

     39:    Level: intermediate

     41: .seealso: [](ch_matrices), `Mat`, `MatType`, `MatCreate()`, `MatSetType()`, `MATPYTHON`, `PetscPythonInitialize()`, `MatPythonSetType()`
     42: @*/
     43: PetscErrorCode MatPythonGetType(Mat mat, const char *pyname[])
     44: {
     45:   PetscFunctionBegin;
     47:   PetscAssertPointer(pyname, 2);
     48:   PetscUseMethod(mat, "MatPythonGetType_C", (Mat, const char *[]), (mat, pyname));
     49:   PetscFunctionReturn(PETSC_SUCCESS);
     50: }

     52: /*@
     53:    MatPythonCreate - Create a `Mat` object implemented in Python.

     55:    Collective

     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}]

     65:    Output Parameter:
     66: .  A - the matrix

     68:    Level: intermediate

     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: }