Documentation

Functions

BSPy.run(function, cores=0, *args)[source]

Execute function on number of cores specified.

This is how a BSP program is launched. Function needs to be of the form func(BSP:BSPObject). run() needs to be nested in an if __name__ = ‘__main__’ statement.

Parameters:
  • function (callable) – A callable function to run in BSP.
  • cores (int, optional) – The amount of cores you wish to use Defaults to all available cores
  • *args (optional) – Any arguments you wish to pass on to your function.
Returns:

out

Return type:

None

The function you pass to run() is required to accept a BSPy.BSPObject as its first argument. All BSP functions will be methods on this object.

import BSPy

def hello_world(bsp: BSPy.BSPObject):
    cores = bsp.cores
    pid = bsp.pid
    print("Hello World (from processor %d / %d)" % (pid+1, cores))

if __name__ == '__main__':
    BSPy.run(hello_world, 4)

>> Hello World (from processor 1 / 4)
>> Hello World (from processor 2 / 4)
>> Hello World (from processor 3 / 4)
>> Hello World (from processor 4 / 4)

In contrary to other BSP implementations, it is possible pass additional arguments to this function. The code below is an equivalent implementation of hello world:

import BSPy

def hello_world(bsp: BSPy.BSPObject, text):
    cores = bsp.cores
    pid = bsp.pid
    print(text, "(from processor %d / %d)" % (pid+1, cores))

if __name__ == '__main__':
    BSPy.run(hello_world, 4, "Hello World")

Warning

BSPy.run must be nested inside an if __name__ == '__main__': statement.

BSPy.max_cores()[source]

Finds number of available processors of the system.

Returns:out – Maximum number of available processors.
Return type:int

BSPObject

class BSPy.BSPObject(cores, pid, pipe_dict, barrier)[source]

The main BSP object that enables use of BSP

This class keeps track of communication lines, its pid and the total cores it also enables the use of BSP functions through these variables. This is a low level object which the end user should not need to call directly.

Parameters:
  • cores (int) – The total number of cores in this BSP instance.
  • pid (int) – This processor’s id. It always holds that 0 <= pid < cores.
  • pipe_dict (dict of Pipeconnection) – A connection to every other processor. Should be given through the run() function.
  • barrier (barrier) – The barrier object all processors adhere to.
cores

The total number of cores in this BSP instance (run).

Type:int
pid

This processor’s id. It always holds that 0 <= pid < cores

Type:int
send(message, pid)[source]

Sends a message to the processor identified with pid.

Parameters:
  • message (literally anything) – Whatever data you wish to transfer between processors
  • pid (int) – The processor id to which you wish to send a message.
Returns:

out

Return type:

None

The send() function queues a message to be sent at the next synchronisation.

The following code snippet would send "Hello World" to processor 0.

bsp.send(message="Hello World", pid=0)
sync()[source]

Starts the synchronisation defined in the BSP model. A synchronisation consists of 3 distinct steps: 1) Wait for all processing nodes to finish computing 2) Send all messages that have queued up since the last sync 3) Receive all messages that have been sent in the previous step. After sync() has completed, every processor will have access to the data that has been sent to it.

Returns:out
Return type:None

Synchronisation is perhaps the main construct of the BSP model. During synchronisation, all communication happens, and every processor is lined up again for the next step.

move()[source]

Acquires the first message stored in the receiving queue.

Used to grab data from the receiving queue. This is a pop-like function and previously returned data cannot be returned a second time. returns None if queue is empty.

Returns:out – The first element in the processor’s “receive” queue.
Return type:queue data

The move function combines with send to form our primary communication protocol.

With this knowledge of the communication protocol, we’re able to build a slightly more complicated version of the hello world program; before turning our heads to some more interesting and complicated examples.

import BSPy

def hello_proc(bsp: BSPy.BSPObject):
    # Get the data we need about the BSP instance
    cores = bsp.cores
    pid = bsp.pid

    # Queue all messages for sending
    for destination in range(cores):
        bsp.send("Hello from proc %d to proc %d"%(pid, destination), pid=destination)

    # Synchronise the system
    bsp.sync()

    # Print the received messages
    for _ in range(cores):
        message = bsp.move()
        print(message)

# Run the hello_proc function
if __name__ == '__main__':
    BSPy.run(hello_proc, 2)

>> Hello from proc 0 to proc 0
>> Hello from proc 1 to proc 1
>> Hello from proc 1 to proc 0
>> Hello from proc 0 to proc 1
nprocs()[source]

Finds number of available processors in BSP instance.

Returns:out – Number of processors participating in the current BSP instance.
Return type:int
static time()[source]

Records the current Unix time in seconds of the system as a float.

Returns:out – Current Unix time in seconds
Return type:float