xref: /petsc/src/binding/petsc4py/src/petsc4py/PETSc/Partitioner.pyx (revision 237137dc2c36cd704ae82eca1d03b531ce3d02f9)
1# --------------------------------------------------------------------
2
3class PartitionerType(object):
4    """The partitioner types."""
5    PARMETIS        = S_(PETSCPARTITIONERPARMETIS)
6    PTSCOTCH        = S_(PETSCPARTITIONERPTSCOTCH)
7    CHACO           = S_(PETSCPARTITIONERCHACO)
8    SIMPLE          = S_(PETSCPARTITIONERSIMPLE)
9    SHELL           = S_(PETSCPARTITIONERSHELL)
10    GATHER          = S_(PETSCPARTITIONERGATHER)
11    MATPARTITIONING = S_(PETSCPARTITIONERMATPARTITIONING)
12    MULTISTAGE      = S_(PETSCPARTITIONERMULTISTAGE)
13
14# --------------------------------------------------------------------
15
16
17cdef class Partitioner(Object):
18    """A graph partitioner."""
19
20    Type = PartitionerType
21
22    def __cinit__(self):
23        self.obj = <PetscObject*> &self.part
24        self.part = NULL
25
26    def view(self, Viewer viewer=None) -> None:
27        """View the partitioner.
28
29        Collective.
30
31        Parameters
32        ----------
33        viewer
34            A `Viewer` to display the graph.
35
36        See Also
37        --------
38        petsc.PetscPartitionerView
39
40        """
41        cdef PetscViewer vwr = NULL
42        if viewer is not None: vwr = viewer.vwr
43        CHKERR(PetscPartitionerView(self.part, vwr))
44
45    def destroy(self) -> Self:
46        """Destroy the partitioner object.
47
48        Collective.
49
50        See Also
51        --------
52        petsc.PetscPartitionerDestroy
53
54        """
55        CHKERR(PetscPartitionerDestroy(&self.part))
56        return self
57
58    def create(self, comm: Comm | None = None) -> Self:
59        """Create an empty partitioner object.
60
61        Collective.
62
63        The type can be set with `setType`.
64
65        Parameters
66        ----------
67        comm
68            MPI communicator, defaults to `Sys.getDefaultComm`.
69
70        See Also
71        --------
72        setType, petsc.PetscPartitionerCreate
73
74        """
75        cdef MPI_Comm ccomm = def_Comm(comm, PETSC_COMM_DEFAULT)
76        cdef PetscPartitioner newpart = NULL
77        CHKERR(PetscPartitionerCreate(ccomm, &newpart))
78        CHKERR(PetscCLEAR(self.obj)); self.part = newpart
79        return self
80
81    def setType(self, part_type: Type | str) -> None:
82        """Build a particular type of the partitioner.
83
84        Collective.
85
86        Parameters
87        ----------
88        part_type
89            The kind of partitioner.
90
91        See Also
92        --------
93        getType, petsc.PetscPartitionerSetType
94
95        """
96        cdef PetscPartitionerType cval = NULL
97        part_type = str2bytes(part_type, &cval)
98        CHKERR(PetscPartitionerSetType(self.part, cval))
99
100    def getType(self) -> str:
101        """Return the partitioner type.
102
103        Not collective.
104
105        See Also
106        --------
107        setType, petsc.PetscPartitionerGetType
108
109        """
110        cdef PetscPartitionerType cval = NULL
111        CHKERR(PetscPartitionerGetType(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.PetscPartitionerSetFromOptions
122
123        """
124        CHKERR(PetscPartitionerSetFromOptions(self.part))
125
126    def setUp(self) -> None:
127        """Construct data structures for the partitioner.
128
129        Collective.
130
131        See Also
132        --------
133        petsc.PetscPartitionerSetUp
134
135        """
136        CHKERR(PetscPartitionerSetUp(self.part))
137
138    def reset(self) -> None:
139        """Reset data structures of the partitioner.
140
141        Collective.
142
143        See Also
144        --------
145        petsc.PetscPartitionerReset
146
147        """
148        CHKERR(PetscPartitionerReset(self.part))
149
150    def setShellPartition(
151        self,
152        numProcs: int,
153        sizes: Sequence[int] | None = None,
154        points: Sequence[int] | None = None) -> None:
155        """Set a custom partition for a mesh.
156
157        Collective.
158
159        Parameters
160        ----------
161        sizes
162            The number of points in each partition.
163        points
164            A permutation of the points that groups those assigned to each
165            partition in order (i.e., partition 0 first, partition 1 next,
166            etc.).
167
168        See Also
169        --------
170        petsc.PetscPartitionerShellSetPartition
171
172        """
173        cdef PetscInt cnumProcs = asInt(numProcs)
174        cdef PetscInt *csizes = NULL
175        cdef PetscInt *cpoints = NULL
176        cdef PetscInt nsize = 0
177        if sizes is not None:
178            sizes = iarray_i(sizes, &nsize, &csizes)
179            if nsize != cnumProcs:
180                raise ValueError("sizes array should have %d entries (has %d)" %
181                                 numProcs, toInt(nsize))
182            if points is None:
183                raise ValueError("Must provide both sizes and points arrays")
184        if points is not None:
185            points = iarray_i(points, NULL, &cpoints)
186        CHKERR(PetscPartitionerShellSetPartition(self.part, cnumProcs,
187                                                 csizes, cpoints))
188
189# --------------------------------------------------------------------
190
191del PartitionerType
192
193# --------------------------------------------------------------------
194