def recvUsrData(self, buf_size): """Receive data from the front-end.""" udata = create_string_buffer(buf_size) lmon.call(self.lib.LMON_be_recvUsrData, cast(udata, c_void_p)) if self.amIMaster(): return lmon.udata_unserialize(udata.raw) return None
def getProctable(self, session, maxsize): """Return the process table and its size with LMON_fe_getProctable.""" proctab_type = lmon.MPIR_PROCDESC_EXT * maxsize proctab = proctab_type() size = c_uint() lmon.call(self.lib.LMON_fe_getProctable, session, byref(proctab), byref(size), c_uint(maxsize)) return proctab, size.value
def attachAndSpawnDaemons(self, session, hostname, pid, daemon, d_argv, febe_data, befe_data): """Invoke LMON_fe_attachAndSpawnDaemons. See the manpages. d_argv is a list. befe_data is the size of the desired buffer or None. """ # Need a trailing null entry on the array. d_argv += [None] _d_argv = lmon.create_array(c_char_p, d_argv) if febe_data is not None: _febe_data = lmon.udata_serialize(febe_data) else: _febe_data = cast(None, c_void_p) buf = None if befe_data is not None: buf = create_string_buffer(befe_data) _befe_data = cast(buf, c_void_p) else: _befe_data = cast(None, c_void_p) lmon.call(self.lib.LMON_fe_attachAndSpawnDaemons, session, hostname, pid, daemon, _d_argv, _febe_data, _befe_data) if befe_data: return lmon.udata_unserialize(buf.value) else: return None
def getMyProctab(self, maxsize): """Return the process table and size for this process.""" proctab_type = lmon.MPIR_PROCDESC_EXT * maxsize proctab = proctab_type() size = c_int() lmon.call(self.lib.LMON_be_getMyProctab, byref(proctab), byref(size), maxsize) return proctab, size.value
def getProctable(self, session, maxsize): """Return the process table and its size.""" proctab_type = lmon.MPIR_PROCDESC_EXT * maxsize proctab = proctab_type() size = c_uint() lmon.call(self.lib.LMON_fe_getProctable, session, byref(proctab), byref(size), c_uint(maxsize)) return proctab, size.value
def handshake(self, udata): """Do the LaunchMON handshake. If udata is not None, it should be the length of the buffer to unserialize front-end data to. """ if udata is None: lmon.call(self.lib.LMON_be_handshake, cast(None, c_void_p)) else: buf = create_string_buffer(udata) lmon.call(self.lib.LMON_be_handshake, cast(buf, c_void_p)) # Check if we actually received data. if buf.raw[0] != "\0": return lmon.udata_unserialize(buf.value) return None
def putToBeDaemonEnv(self, session, environ): """Set up the backend environment with LMON_fe_putToBeDaemonEnv. Environ is a list of tuples of keys and values. """ env_list_type = lmon.lmon_daemon_env_t * len(environ) env_list = env_list_type() for k, env_item in enumerate(environ): env_list[k].envName = env_item[0] env_list[k].envValue = env_item[1] if k < (len(environ) - 1): env_list[k].next = pointer(env_list[k + 1]) else: env_list[k].next = None lmon.call(self.lib.LMON_fe_putToBeDaemonEnv, session, env_list, len(environ))
def init(self, argc, argv): """Invoke LMON_be_init. argc is the number of arguments in argv. argv is a list of arguments to pass as argv. """ _argc = c_int(argc) # This is horribly ugly code to properly reconstruct argv for LaunchMON. # We stuff the arguments in string buffers (since they can be modified). # We stuff those into an array. # We add an entry at the end with a bunch of null bytes, since the last # argv entry is supposed to be a null and otherwise LaunchMON will make # malloc *very* unhappy. # We create a pointer to this array. # We pass that pointer by reference (another pointer). tmp_argv = [cast(create_string_buffer(x), c_char_p) for x in argv] tmp_argv.append(cast(create_string_buffer(32), c_char_p)) _argv = lmon.create_array(c_char_p, tmp_argv) argv_ref = c_void_p(addressof(_argv)) lmon.call(self.lib.LMON_be_init, lmon.LMON_VERSION, byref(_argc), byref(argv_ref))
def broadcast(self, udata, size): """Broadcast data. The master provides data and the size of it, and this returns None. The slaves can provide None for the data, and size is the size of the buffer, which is returned after unserialization. """ if self.amIMaster(): # Master sends the data, returns None. lmon.call(self.lib.LMON_be_broadcast, lmon.udata_serialize(udata), size) return None else: # Slave returns the data. buf = create_string_buffer(size) lmon.call(self.lib.LMON_be_broadcast, cast(buf, c_void_p), size) # Depending on the application, there appears to be a spurious null # byte at the start of the data. I do not know why. This avoids it. if buf.raw[0] == "\0" and buf.raw[1] != "\0": buf = string_at(addressof(buf) + 1) else: buf = buf.value return lmon.udata_unserialize(buf)
def scatter(self, udata_array, elem_size): """Scatter data. The master provides udata_array, which is an array of data to scatter. The slaves may provide None for the data. elem_size is the size of each element. Due to serialization of data, this should be the maximum size of the data, and the elements are padded to this length. All callers return their respective data (including the master). """ buf = create_string_buffer(elem_size) if self.amIMaster(): # Master pads data if needed and sends. total_size = len(udata_array) * elem_size send_buf = create_string_buffer(total_size) buf_addr = addressof(send_buf) idx = 0 for elem in udata_array: tmp_buf = create_string_buffer( string_at(lmon.udata_serialize(elem)), elem_size) memmove(buf_addr + (idx * elem_size), tmp_buf, elem_size) idx += 1 lmon.call(self.lib.LMON_be_scatter, cast(send_buf, c_void_p), elem_size, cast(buf, c_void_p)) else: lmon.call(self.lib.LMON_be_scatter, cast(None, c_void_p), elem_size, cast(buf, c_void_p)) # This is here for the same reason as in broadcast. # Note that it appears that the null byte is only present for children. if buf.raw[0] == "\0" and buf.raw[1] != "\0": buf = string_at(addressof(buf) + 1) else: buf = buf.value return lmon.udata_unserialize(buf)
def launchAndSpawnDaemons(self, session, hostname, launcher, l_argv, daemon, d_argv, febe_data, befe_data): """Invoke LMON_fe_launchAndSpawnDaemons.""" # Need trailing null entries on the arrays. l_argv += [None] d_argv += [None] _l_argv = lmon.create_array(c_char_p, l_argv) _d_argv = lmon.create_array(c_char_p, d_argv) if febe_data is not None: _febe_data = lmon.udata_serialize(febe_data) else: _febe_data = cast(None, c_void_p) buf = None if befe_data is not None: buf = create_string_buffer(befe_data) _befe_data = cast(buf, c_void_p) else: _befe_data = cast(None, c_void_p) lmon.call(self.lib.LMON_fe_launchAndSpawnDaemons, session, hostname, launcher, _l_argv, daemon, _d_argv, _febe_data, _befe_data) if befe_data: return lmon.udata_unserialize(buf.value) else: return None
def sendUsrData(self, udata): """Send data to the front-end.""" lmon.call(self.lib.LMON_be_sendUsrData, lmon.udata_serialize(udata))
def regPackForBeToFe(self, callback): """Register a pack function.""" self.pack_cb = self.pack_type(callback) lmon.call(self.lib.LMON_be_regPackForBeToFe, self.pack_cb)
def amIMaster(self): """Return whether this process is the master.""" rc = lmon.call(self.lib.LMON_be_amIMaster) if rc == lmon.LMON_YES: return True return False
def createSession(self): """Create and return a session handle with LMON_fe_createSession.""" session = c_int() lmon.call(self.lib.LMON_fe_createSession, byref(session)) return session.value
def kill(self, session): """Destroy all resources associated with the session.""" lmon.call(self.lib.LMON_fe_kill, session)
def detach(self, session): """Detach from the resource manager but leave daemons running.""" lmon.call(self.lib.LMON_fe_detach, session)
def getProctableSize(self, session): """Return the size of the process table with LMON_fe_getProctableSize.""" size = c_uint() lmon.call(self.lib.LMON_fe_getProctableSize, session, byref(size)) return size.value
def init(self): """Invoke LMON_fe_init.""" lmon.call(self.lib.LMON_fe_init, lmon.LMON_VERSION)
def shutdownDaemons(self, session): """Detach from the resource manager and shut down daemons.""" lmon.call(self.lib.LMON_fe_shutdownDaemons, session)
def barrier(self): """Make a barrier.""" lmon.call(self.lib.LMON_be_barrier)
def getProctableSize(self, session): """Return the size of the process table.""" size = c_uint() lmon.call(self.lib.LMON_fe_getProctableSize, session, byref(size)) return size.value
def sendUsrDataBe(self, session, febe_data): """Send user data to the backend.""" lmon.call(self.lib.LMON_fe_sendUsrDataBe, session, lmon.udata_serialize(febe_data))
def regUnpackForFeToBe(self, callback): """Register an unpack function.""" self.unpack_cb = self.unpack_type(callback) lmon.call(self.lib.LMON_be_regUnpackForFeToBe, self.unpack_cb)
def ready(self, udata): """Inform the front-end that we are ready.""" if udata is None: lmon.call(self.lib.LMON_be_ready, cast(None, c_void_p)) else: lmon.call(self.lib.LMON_be_ready, lmon.udata_serialize(udata))
def getMyProctabSize(self): """Return the size of the process table.""" size = c_int() lmon.call(self.lib.LMON_be_getMyProctabSize, byref(size)) return size.value
def recvUsrDataBe(self, session, buf_size): """Receive user data from the backend.""" befe_data = create_string_buffer(buf_size) lmon.call(self.lib.LMON_fe_recvUsrDataBe, session, cast(befe_data, c_void_p)) return lmon.udata_unserialize(befe_data.raw)
def regPackForFeToBe(self, session, callback): """Register a pack function with LMON_fe_regPackForFeToBe.""" cb = self.pack_type(callback) self.pack_cbs[session] = cb lmon.call(self.lib.LMON_fe_regPackForFeToBe, session, cb)
def finalize(self): """Finalize this session.""" lmon.call(self.lib.LMON_be_finalize)
def getMyRank(self): """Return the rank of this process.""" rank = c_int() lmon.call(self.lib.LMON_be_getMyRank, byref(rank)) return rank.value
def getSize(self): """Return the number of LaunchMON processes.""" size = c_int() lmon.call(self.lib.LMON_be_getSize, byref(size)) return size.value
def regUnpackForBeToFe(self, session, callback): """Register an unpack function with LMON_fe_regUnpackForBeToFe.""" cb = self.unpack_type(callback) self.unpack_cbs[session] = cb lmon.call(self.lib.LMON_fe_regUnpackForBeToFe, session, cb)
def sendUsrDataBe(self, session, febe_data): """Send user data to the backend with LMON_fe_sendUsrDataBe (it is serialized).""" lmon.call(self.lib.LMON_fe_sendUsrDataBe, session, lmon.udata_serialize(febe_data))
def recvUsrDataBe(self, session, buf_size): """Receive user data from the backend with LMON_fe_recvUsrDataBe (it is unserialized).""" befe_data = create_string_buffer(buf_size) lmon.call(self.lib.LMON_fe_recvUsrDataBe, session, cast(befe_data, c_void_p)) return lmon.udata_unserialize(befe_data.raw)