xref: /petsc/src/binding/petsc4py/src/petsc4py/PETSc/MatPartitioning.pyx (revision 552edb6364df478b294b3111f33a8f37ca096b20)
1# --------------------------------------------------------------------
2
3class MatPartitioningType(object):
4    """The partitioning types."""
5    PARTITIONINGCURRENT  = S_(MATPARTITIONINGCURRENT)
6    PARTITIONINGAVERAGE  = S_(MATPARTITIONINGAVERAGE)
7    PARTITIONINGSQUARE   = S_(MATPARTITIONINGSQUARE)
8    PARTITIONINGPARMETIS = S_(MATPARTITIONINGPARMETIS)
9    PARTITIONINGCHACO    = S_(MATPARTITIONINGCHACO)
10    PARTITIONINGPARTY    = S_(MATPARTITIONINGPARTY)
11    PARTITIONINGPTSCOTCH = S_(MATPARTITIONINGPTSCOTCH)
12    PARTITIONINGHIERARCH = S_(MATPARTITIONINGHIERARCH)
13
14# --------------------------------------------------------------------
15
16
17cdef class MatPartitioning(Object):
18    """Object for managing the partitioning of a matrix or graph."""
19
20    Type = MatPartitioningType
21
22    def __cinit__(self):
23        self.obj = <PetscObject*> &self.part
24        self.part = NULL
25
26    def __call__(self):
27        return self.getValue()
28
29    def view(self, Viewer viewer=None) -> None:
30        """View the partitioning data structure.
31
32        Collective.
33
34        Parameters
35        ----------
36        viewer
37            A `Viewer` to display the graph.
38
39        See Also
40        --------
41        petsc.MatPartitioningView
42
43        """
44        assert self.obj != NULL
45        cdef PetscViewer vwr = NULL
46        if viewer is not None: vwr = viewer.vwr
47        CHKERR(MatPartitioningView(self.part, vwr))
48
49    def destroy(self) -> Self:
50        """Destroy the partitioning context.
51
52        Collective.
53
54        See Also
55        --------
56        create, petsc.MatPartitioningDestroy
57
58        """
59        CHKERR(MatPartitioningDestroy(&self.part))
60        return self
61
62    def create(self, comm: Comm | None = None) -> Self:
63        """Create a partitioning context.
64
65        Collective.
66
67        Parameters
68        ----------
69        comm
70            MPI communicator, defaults to `Sys.getDefaultComm`.
71
72        See Also
73        --------
74        destroy, petsc.MatPartitioningCreate
75
76        """
77        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
78        CHKERR(MatPartitioningCreate(ccomm, &self.part))
79        return self
80
81    def setType(self, matpartitioning_type: Type | str) -> None:
82        """Set the type of the partitioner to use.
83
84        Collective.
85
86        Parameters
87        ----------
88        matpartitioning_type
89            The partitioner type.
90
91        See Also
92        --------
93        getType, petsc.MatPartitioningSetType
94
95        """
96        cdef PetscMatPartitioningType cval = NULL
97        matpartitioning_type = str2bytes(matpartitioning_type, &cval)
98        CHKERR(MatPartitioningSetType(self.part, cval))
99
100    def getType(self) -> str:
101        """Return the partitioning method.
102
103        Not collective.
104
105        See Also
106        --------
107        setType, petsc.MatPartitioningGetType
108
109        """
110        cdef PetscMatPartitioningType cval = NULL
111        CHKERR(MatPartitioningGetType(self.part, &cval))
112        return bytes2str(cval)
113
114    def setFromOptions(self) -> None:
115        """Set parameters in the partitioner from the options database.
116
117        Collective.
118
119        See Also
120        --------
121        petsc_options, petsc.MatPartitioningSetFromOptions
122
123        """
124        CHKERR(MatPartitioningSetFromOptions(self.part))
125
126    def setAdjacency(self, Mat adj) -> None:
127        """Set the adjacency graph (matrix) of the thing to be partitioned.
128
129        Collective.
130
131        Parameters
132        ----------
133        adj
134            The adjacency matrix, this can be any `Mat.Type` but the natural
135            representation is `Mat.Type.MPIADJ`.
136
137        See Also
138        --------
139        petsc.MatPartitioningSetAdjacency
140
141        """
142        CHKERR(MatPartitioningSetAdjacency(self.part, adj.mat))
143
144    def apply(self, IS partitioning) -> None:
145        """Return a partitioning for the graph represented by a sparse matrix.
146
147        Collective.
148
149        For each local node this tells the processor number that that node is
150        assigned to.
151
152        See Also
153        --------
154        petsc.MatPartitioningApply
155
156        """
157        CHKERR(MatPartitioningApply(self.part, &partitioning.iset))
158
159# --------------------------------------------------------------------
160
161del MatPartitioningType
162
163# --------------------------------------------------------------------
164