module module

class module.Module[source]

Bases: object

Cortix module super class.

This class provides facilities for creating modules within the Cortix network. Cortix will map one object of this class to either a Multiprocessing or MPI process depending on the user’s configuration.

Note

This class is to be inherited by every Cortix module. In order to execute, modules must override the run method, which will be executed during the simulation.

__init__()[source]

Module super class constructor.

Note

This constructor must be called explicitly in the constructor of every Cortix module like so:

super().__init__()

name

A name given to the instance. Default is the derived class name.

Type

str

port_names_expected

A list of names of ports expected in the module. This will be compared to port names during runtime to check against the intended use of the module.

Type

list(str), None

state

Any pickle-able data structure to be passed in a multiprocessing.Queue to the parent process or to be gathered in the root MPI process. Default is None.

Type

any

use_mpi

True for MPI, False for Multiprocessing

Type

bool

use_multiprocessing

False for MPI, True for Multiprocessing

Type

bool

ports

A list of ports contained by the module

Type

list(Port)

id

An integer set by the external network once a module is added to it. The id is the position of the module in the network list. Default: None.

Type

int

__network

An internal network inherited by the derived module for nested networks.

Type

Network

get_port(name)[source]

Get port by name; if it does not exist, create one.

Parameters

name (str) – The name of the port to get

Returns

port – The port object with the corresponding name

Return type

Port

network
recv(port)[source]

Receive data from a given port

Warning

This function will block until data is available

Parameters

port (Port, str) – A Port object to send the data through, or its string name

Returns

data – The data received through the port

Return type

any

run(*args)[source]

Module run function

Run method with an option to pass data back to the parent process when running in Python multiprocessing mode. If the user does not want to share data with the parent process, this function can be overriden with run(self) or run(self, *args) as long as self.state = None. If self.state points to anything but None, the user must use `run(self, *args).

Notes

When in multiprocessing, *args has two elements: comm_idx and comm_state. To pass back the state of the module, the user should insert the provided index comm_idx and the state into the queue as follows:

if self.use_multiprocessing:
try:

pickle.dumps(self.state)

except pickle.PicklingError:

args[1].put((arg[0],None))

else:

args[1].put((arg[0],self.state))

at the bottom of the user defined run() function.

Warning

This function must be overridden by all Cortix modules

Parameters
  • arg[0] (int) – Index of the state in the communication queue.

  • arg[1] (multiprocessing.Queue) – When using the Python multiprocessing library state_comm must have the module’s self.state in it. That is, state_comm.put((idx_comm,self.state)) must be the last command in the method before return. In addition, self.state must be pickle-able.

run_and_save()[source]
send(data, port)[source]

Send data through a given port.

Parameters
  • data (any) – The data being sent out - must be pickleable

  • port (Port, str) – A Port object to send the data through, or its string name