xref: /petsc/src/binding/petsc4py/src/petsc4py/PETSc/petscis.pxi (revision 89a64d68d2f8dc8c4ae19e87b21a1e0b1231e51b)
1cdef extern from * nogil:
2
3    ctypedef const char* PetscISType "ISType"
4    PetscISType ISGENERAL
5    PetscISType ISSTRIDE
6    PetscISType ISBLOCK
7
8    PetscErrorCode ISView(PetscIS, PetscViewer)
9    PetscErrorCode ISDestroy(PetscIS*)
10    PetscErrorCode ISCreate(MPI_Comm, PetscIS*)
11    PetscErrorCode ISSetType(PetscIS, PetscISType)
12    PetscErrorCode ISGetType(PetscIS, PetscISType*)
13
14    PetscErrorCode ISCreateGeneral(MPI_Comm, PetscInt, PetscInt[], PetscCopyMode, PetscIS*)
15    PetscErrorCode ISCreateBlock(MPI_Comm, PetscInt, PetscInt, PetscInt[], PetscCopyMode, PetscIS*)
16    PetscErrorCode ISCreateStride(MPI_Comm, PetscInt, PetscInt, PetscInt, PetscIS*)
17
18    PetscErrorCode ISLoad(PetscIS, PetscViewer)
19    PetscErrorCode ISDuplicate(PetscIS, PetscIS*)
20    PetscErrorCode ISCopy(PetscIS, PetscIS)
21    PetscErrorCode ISAllGather(PetscIS, PetscIS*)
22    PetscErrorCode ISInvertPermutation(PetscIS, PetscInt, PetscIS*)
23    PetscErrorCode ISPartitioningToNumbering(PetscIS, PetscIS*)
24    PetscErrorCode ISPartitioningCount(PetscIS, PetscInt, PetscInt*)
25
26    PetscErrorCode ISGetSize(PetscIS, PetscInt*)
27    PetscErrorCode ISGetLocalSize(PetscIS, PetscInt*)
28    PetscErrorCode ISGetBlockSize(PetscIS, PetscInt*)
29    PetscErrorCode ISSetBlockSize(PetscIS, PetscInt)
30    PetscErrorCode ISGetIndices(PetscIS, const PetscInt*[])
31    PetscErrorCode ISRestoreIndices(PetscIS, const PetscInt*[])
32
33    PetscErrorCode ISEqual(PetscIS, PetscIS, PetscBool*)
34
35    PetscErrorCode ISSetPermutation(PetscIS)
36    PetscErrorCode ISPermutation(PetscIS, PetscBool*)
37    PetscErrorCode ISSetIdentity(PetscIS)
38    PetscErrorCode ISIdentity(PetscIS, PetscBool*)
39
40    PetscErrorCode ISSort(PetscIS)
41    PetscErrorCode ISSorted(PetscIS, PetscBool*)
42
43    PetscErrorCode ISSum(PetscIS, PetscIS, PetscIS*)
44    PetscErrorCode ISExpand(PetscIS, PetscIS, PetscIS*)
45    PetscErrorCode ISDifference(PetscIS, PetscIS, PetscIS*)
46    PetscErrorCode ISComplement(PetscIS, PetscInt, PetscInt, PetscIS*)
47    PetscErrorCode ISEmbed(PetscIS, PetscIS, PetscBool, PetscIS*)
48    PetscErrorCode ISRenumber(PetscIS, PetscIS, PetscInt*, PetscIS*)
49
50    PetscErrorCode ISGeneralSetIndices(PetscIS, PetscInt, PetscInt[], PetscCopyMode)
51
52    PetscErrorCode ISBlockSetIndices(PetscIS, PetscInt, PetscInt, PetscInt[], PetscCopyMode)
53    PetscErrorCode ISBlockGetIndices(PetscIS, const PetscInt*[])
54    PetscErrorCode ISBlockRestoreIndices(PetscIS, const PetscInt*[])
55
56    PetscErrorCode ISStrideSetStride(PetscIS, PetscInt, PetscInt, PetscInt)
57    PetscErrorCode ISStrideGetInfo(PetscIS, PetscInt*, PetscInt*)
58
59    PetscErrorCode ISToGeneral(PetscIS)
60    PetscErrorCode ISBuildTwoSided(PetscIS, PetscIS, PetscIS*)
61
62
63cdef extern from * nogil:
64
65    ctypedef const char* PetscISLocalToGlobalMappingType "ISLocalToGlobalMappingType"
66    PetscISLocalToGlobalMappingType ISLOCALTOGLOBALMAPPINGBASIC
67    PetscISLocalToGlobalMappingType ISLOCALTOGLOBALMAPPINGHASH
68
69    ctypedef enum PetscGLMapMode "ISGlobalToLocalMappingMode":
70        PETSC_IS_GTOLM_MASK "IS_GTOLM_MASK"
71        PETSC_IS_GTOLM_DROP "IS_GTOLM_DROP"
72
73    PetscErrorCode ISLocalToGlobalMappingCreate(MPI_Comm, PetscInt, PetscInt, PetscInt[], PetscCopyMode, PetscLGMap*)
74    PetscErrorCode ISLocalToGlobalMappingCreateIS(PetscIS, PetscLGMap*)
75    PetscErrorCode ISLocalToGlobalMappingCreateSF(PetscSF, PetscInt, PetscLGMap*)
76    PetscErrorCode ISLocalToGlobalMappingSetType(PetscLGMap, PetscISLocalToGlobalMappingType)
77    PetscErrorCode ISLocalToGlobalMappingSetFromOptions(PetscLGMap)
78    PetscErrorCode ISLocalToGlobalMappingView(PetscLGMap, PetscViewer)
79    PetscErrorCode ISLocalToGlobalMappingLoad(PetscLGMap, PetscViewer)
80    PetscErrorCode ISLocalToGlobalMappingDestroy(PetscLGMap*)
81    PetscErrorCode ISLocalToGlobalMappingGetSize(PetscLGMap, PetscInt*)
82    PetscErrorCode ISLocalToGlobalMappingGetBlockSize(PetscLGMap, PetscInt*)
83    PetscErrorCode ISLocalToGlobalMappingGetIndices(PetscLGMap, const PetscInt*[])
84    PetscErrorCode ISLocalToGlobalMappingRestoreIndices(PetscLGMap, const PetscInt*[])
85    PetscErrorCode ISLocalToGlobalMappingGetBlockIndices(PetscLGMap, const PetscInt*[])
86    PetscErrorCode ISLocalToGlobalMappingRestoreBlockIndices(PetscLGMap, const PetscInt*[])
87    PetscErrorCode ISLocalToGlobalMappingGetInfo(PetscLGMap, PetscInt*, PetscInt*[], PetscInt*[], PetscInt**[])
88    PetscErrorCode ISLocalToGlobalMappingRestoreInfo(PetscLGMap, PetscInt*, PetscInt*[], PetscInt*[], PetscInt**[])
89    PetscErrorCode ISLocalToGlobalMappingGetBlockInfo(PetscLGMap, PetscInt*, PetscInt*[], PetscInt*[], PetscInt**[])
90    PetscErrorCode ISLocalToGlobalMappingRestoreBlockInfo(PetscLGMap, PetscInt*, PetscInt*[], PetscInt*[], PetscInt**[])
91    PetscErrorCode ISLocalToGlobalMappingApply(PetscLGMap, PetscInt, PetscInt[], PetscInt[])
92    PetscErrorCode ISLocalToGlobalMappingApplyBlock(PetscLGMap, PetscInt, PetscInt[], PetscInt[])
93    PetscErrorCode ISLocalToGlobalMappingApplyIS(PetscLGMap, PetscIS, PetscIS*)
94    PetscErrorCode ISGlobalToLocalMappingApply(PetscLGMap, PetscGLMapMode, PetscInt, PetscInt[], PetscInt*, PetscInt[])
95    PetscErrorCode ISGlobalToLocalMappingApplyBlock(PetscLGMap, PetscGLMapMode, PetscInt, PetscInt[], PetscInt*, PetscInt[])
96
97
98# --------------------------------------------------------------------
99
100cdef inline IS ref_IS(PetscIS iset):
101    cdef IS ob = <IS> IS()
102    ob.iset = iset
103    CHKERR(PetscINCREF(ob.obj))
104    return ob
105
106cdef inline LGMap ref_LGMap(PetscLGMap lgm):
107    cdef LGMap ob = <LGMap> LGMap()
108    ob.lgm = lgm
109    CHKERR(PetscINCREF(ob.obj))
110    return ob
111
112# --------------------------------------------------------------------
113
114cdef class _IS_buffer:
115
116    cdef PetscIS iset
117    cdef PetscInt size
118    cdef const PetscInt *data
119    cdef bint hasarray
120
121    def __cinit__(self, IS iset):
122        cdef PetscIS i = iset.iset
123        CHKERR(PetscINCREF(<PetscObject*>&i))
124        self.iset = i
125        self.size = 0
126        self.data = NULL
127        self.hasarray = 0
128
129    def __dealloc__(self):
130        if self.hasarray and self.iset != NULL:
131            CHKERR(ISRestoreIndices(self.iset, &self.data))
132        CHKERR(ISDestroy(&self.iset))
133
134    #
135
136    cdef int acquire(self) except -1:
137        if not self.hasarray and self.iset != NULL:
138            CHKERR(ISGetLocalSize(self.iset, &self.size))
139            CHKERR(ISGetIndices(self.iset, &self.data))
140            self.hasarray = 1
141        return 0
142
143    cdef int release(self) except -1:
144        if self.hasarray and self.iset != NULL:
145            self.size = 0
146            CHKERR(ISRestoreIndices(self.iset, &self.data))
147            self.hasarray = 0
148            self.data = NULL
149        return 0
150
151    # buffer interface (PEP 3118)
152
153    cdef int acquirebuffer(self, Py_buffer *view, int flags) except -1:
154        self.acquire()
155        PyPetscBuffer_FillInfo(view, <void*>self.data, self.size,
156                               c'i', 1, flags)
157        view.obj = self
158        return 0
159
160    cdef int releasebuffer(self, Py_buffer *view) except -1:
161        PyPetscBuffer_Release(view)
162        self.release()
163        return 0
164
165    def __getbuffer__(self, Py_buffer *view, int flags):
166        self.acquirebuffer(view, flags)
167
168    def __releasebuffer__(self, Py_buffer *view):
169        self.releasebuffer(view)
170
171    # 'with' statement (PEP 343)
172
173    cdef object enter(self):
174        self.acquire()
175        return asarray(self)
176
177    cdef object exit(self):
178        self.release()
179        return None
180
181    def __enter__(self):
182        return self.enter()
183
184    def __exit__(self, *exc):
185        return self.exit()
186
187    # NumPy array interface (legacy)
188
189    property __array_interface__:
190        def __get__(self):
191            cdef PetscInt n = 0
192            if self.iset != NULL:
193                CHKERR(ISGetLocalSize(self.iset, &n))
194            cdef object size = toInt(n)
195            cdef dtype descr = PyArray_DescrFromType(NPY_PETSC_INT)
196            cdef str typestr = "=%c%d" % (descr.kind, descr.itemsize)
197            return dict(version=3,
198                        data=self,
199                        shape=(size,),
200                        typestr=typestr)
201
202# --------------------------------------------------------------------
203