1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
cdef class Request:
"""
Request
"""
def __cinit__(self):
self.ob_mpi = MPI_REQUEST_NULL
def __dealloc__(self):
if not (self.flags & PyMPI_OWNED): return
CHKERR( _del_Request(&self.ob_mpi) )
def __richcmp__(self, other, int op):
if not isinstance(self, Request): return NotImplemented
if not isinstance(other, Request): return NotImplemented
cdef Request s = self, o = other
if op == 2: return (s.ob_mpi == o.ob_mpi)
elif op == 3: return (s.ob_mpi != o.ob_mpi)
else: raise TypeError(mpistr("only '==' and '!='"))
def __nonzero__(self):
return self.ob_mpi != MPI_REQUEST_NULL
# Completion Operations
# ---------------------
def Wait(self, Status status=None):
"""
Wait for an MPI send or receive to complete.
"""
cdef MPI_Status *statusp = _arg_Status(status)
with nogil: CHKERR( MPI_Wait(&self.ob_mpi, statusp) )
def Test(self, Status status=None):
"""
Test for the completion of a send or receive.
"""
cdef bint flag = 0
cdef MPI_Status *statusp = _arg_Status(status)
with nogil: CHKERR( MPI_Test(&self.ob_mpi, &flag, statusp) )
return flag
def Free(self):
"""
Free a communication request
"""
with nogil: CHKERR( MPI_Request_free(&self.ob_mpi) )
def Get_status(self, Status status=None):
"""
Non-destructive test for the
completion of a request
"""
cdef bint flag = 0
cdef MPI_Status *statusp = _arg_Status(status)
with nogil: CHKERR( MPI_Request_get_status(self.ob_mpi, &flag, statusp) )
return flag
# Multiple Completions
# --------------------
@classmethod
def Waitany(cls, requests, Status status=None):
"""
Wait for any previously initiated request to complete
"""
cdef int count = len(requests)
cdef MPI_Request *irequests = NULL
cdef tmp1 = asarray_Request(requests, &irequests, count)
cdef MPI_Status *statusp = _arg_Status(status)
cdef int index = MPI_UNDEFINED
#
try:
with nogil: CHKERR( MPI_Waitany(count, irequests, &index, statusp) )
finally:
restore_Request(requests, &irequests, count);
#
return index
@classmethod
def Testany(cls, requests, Status status=None):
"""
Test for completion of any previously initiated request
"""
cdef int count = len(requests)
cdef MPI_Request *irequests = NULL
cdef tmp1 = asarray_Request(requests, &irequests, count)
cdef int index = MPI_UNDEFINED
cdef bint flag = 0
cdef MPI_Status *statusp = _arg_Status(status)
#
try:
with nogil: CHKERR( MPI_Testany(count, irequests, &index, &flag, statusp) )
finally:
restore_Request(requests, &irequests, count)
#
return (index, flag)
@classmethod
def Waitall(cls, requests, statuses=None):
"""
Wait for all previously initiated requests to complete
"""
cdef int count = len(requests)
cdef MPI_Request *irequests = NULL
cdef tmp1 = asarray_Request(requests, &irequests, count)
cdef MPI_Status *istatuses = MPI_STATUSES_IGNORE
cdef tmp2 = asarray_Status(statuses, &istatuses, count)
#
try:
with nogil: CHKERR( MPI_Waitall(count, irequests, istatuses) )
finally:
restore_Request(requests, &irequests, count)
restore_Status(statuses, &istatuses, count)
#
return None
@classmethod
def Testall(cls, requests, statuses=None):
"""
Test for completion of all previously initiated requests
"""
cdef int count = len(requests)
cdef MPI_Request *irequests = NULL
cdef tmp1 = asarray_Request(requests, &irequests, count)
cdef MPI_Status *istatuses = MPI_STATUSES_IGNORE
cdef tmp2 = asarray_Status(statuses, &istatuses, count)
cdef bint flag = 0
#
try:
with nogil: CHKERR( MPI_Testall(count, irequests, &flag, istatuses) )
finally:
restore_Request(requests, &irequests, count)
restore_Status(statuses, &istatuses, count)
#
return flag
@classmethod
def Waitsome(cls, requests, statuses=None):
"""
Wait for some previously initiated requests to complete
"""
cdef int incount = len(requests)
cdef MPI_Request *irequests = NULL
cdef tmp1 = asarray_Request(requests, &irequests, incount)
cdef MPI_Status *istatuses = MPI_STATUSES_IGNORE
cdef tmp2 = asarray_Status(statuses, &istatuses, incount)
cdef int outcount = MPI_UNDEFINED
cdef int *iindices = NULL
cdef tmp3 = newarray_int(incount, &iindices)
#
try:
with nogil: CHKERR( MPI_Waitsome(incount, irequests, &outcount, iindices, istatuses) )
finally:
restore_Request(requests, &irequests, incount)
restore_Status(statuses, &istatuses, incount)
#
cdef int i = 0
indices = []
if outcount != 0 and outcount != MPI_UNDEFINED:
indices = [iindices[i] for i from 0 <= i < outcount]
return (outcount, indices)
@classmethod
def Testsome(cls, requests, statuses=None):
"""
Test for completion of some previously initiated requests
"""
cdef int incount = len(requests)
cdef MPI_Request *irequests = NULL
cdef tmp1 = asarray_Request(requests, &irequests, incount)
cdef MPI_Status *istatuses = MPI_STATUSES_IGNORE
cdef tmp2 = asarray_Status(statuses, &istatuses, incount)
cdef int outcount = MPI_UNDEFINED
cdef int *iindices = NULL
cdef tmp3 = newarray_int(incount, &iindices)
#
try:
with nogil: CHKERR( MPI_Waitsome(incount, irequests, &outcount, iindices, istatuses) )
finally:
restore_Request(requests, &irequests, incount)
restore_Status(statuses, &istatuses, incount)
#
cdef int i = 0
indices = []
if outcount != 0 and outcount != MPI_UNDEFINED:
indices = [iindices[i] for i from 0 <= i < outcount]
return (outcount, indices)
# Cancel
# ------
def Cancel(self):
"""
Cancel a communication request
"""
with nogil: CHKERR( MPI_Cancel(&self.ob_mpi) )
# Fortran Handle
# --------------
def py2f(self):
"""
"""
return MPI_Request_c2f(self.ob_mpi)
@classmethod
def f2py(cls, arg):
"""
"""
cdef Request request = cls()
request.ob_mpi = MPI_Request_f2c(arg)
return request
cdef class Prequest(Request):
"""
Persistent request
"""
def Start(self):
"""
Initiate a communication with a persistent request
"""
with nogil: CHKERR( MPI_Start(&self.ob_mpi) )
@classmethod
def Startall(cls, requests):
"""
Start a collection of persistent requests
"""
cdef int count = len(requests)
cdef MPI_Request *irequests = NULL
cdef tmp = asarray_Request(requests, &irequests, count)
#
try:
with nogil: CHKERR( MPI_Startall(count, irequests) )
finally:
restore_Request(requests, &irequests, count)
cdef class Grequest(Request):
"""
Generalized request
"""
def __cinit__(self):
self.ob_grequest = MPI_REQUEST_NULL
@classmethod
def Start(cls, query_fn, free_fn, cancel_fn,
args=None, kargs=None):
"""
Create and return a user-defined request
"""
#
cdef Grequest request = cls()
cdef _p_greq state = \
_p_greq(query_fn, free_fn, cancel_fn,
args, kargs)
with nogil: CHKERR( MPI_Grequest_start(
greq_query_fn, greq_free_fn, greq_cancel_fn,
<void*>state, &request.ob_mpi) )
Py_INCREF(state)
request.ob_grequest = request.ob_mpi
return request
def Complete(self):
"""
Notify that a user-defined request is complete
"""
if self.ob_mpi != MPI_REQUEST_NULL:
if self.ob_mpi != self.ob_grequest:
raise Exception(MPI_ERR_REQUEST)
cdef MPI_Request grequest = self.ob_grequest
self.ob_grequest = self.ob_mpi # sync handles
with nogil: CHKERR( MPI_Grequest_complete(grequest) )
cdef Request __REQUEST_NULL__ = _new_Request(MPI_REQUEST_NULL)
# Predefined request handles
# --------------------------
REQUEST_NULL = __REQUEST_NULL__ #: Null request handle
|