1.2. PyCOMPSs: Synchronization

In this example we will see how to synchronize with PyCOMPSs.

Import the PyCOMPSs library

[1]:
import pycompss.interactive as ipycompss

Start the runtime

Initialize COMPSs runtime Parameters indicates if the execution will generate task graph, tracefile, monitor interval and debug information.

[2]:
import os
if 'BINDER_SERVICE_HOST' in os.environ:
    ipycompss.start(graph=True, debug=False,
                    project_xml='../xml/project.xml',
                    resources_xml='../xml/resources.xml')
else:
    ipycompss.start(graph=True, monitor=1000, trace=False)
********************************************************
**************** PyCOMPSs Interactive ******************
********************************************************
*          .-~~-.--.           ______         ______   *
*         :         )         |____  \       |____  \  *
*   .~ ~ -.\       /.- ~~ .      __) |          __) |  *
*   >       `.   .'       <     |__  |         |__  |  *
*  (         .- -.         )   ____) |   _    ____) |  *
*   `- -.-~  `- -'  ~-.- -'   |______/  |_|  |______/  *
*     (        :        )           _ _ .-:            *
*      ~--.    :    .--~        .-~  .-~  }            *
*          ~-.-^-.-~ \_      .~  .-~   .~              *
*                   \ \ '     \ '_ _ -~                *
*                    \`.\`.    //                      *
*           . - ~ ~-.__\`.\`-.//                       *
*       .-~   . - ~  }~ ~ ~-.~-.                       *
*     .' .-~      .-~       :/~-.~-./:                 *
*    /_~_ _ . - ~                 ~-.~-._              *
*                                     ~-.<             *
********************************************************
* - Starting COMPSs runtime...                         *
* - Log path : /home/user/.COMPSs/Interactive_02/
* - PyCOMPSs Runtime started... Have fun!              *
********************************************************

Importing task and parameter modules

Import task module before annotating functions or methods

[3]:
from pycompss.api.task import task
from pycompss.api.parameter import *
from pycompss.api.api import compss_wait_on

Declaring tasks

Declare functions and decorate with @task those that should be tasks

[4]:
@task(returns=int)
def square(val1):
    return val1 * val1
[5]:
@task(returns=int)
def add(val2, val3):
    return val2 + val3
[6]:
@task(returns=int)
def multiply(val1, val2):
    return val1 * val2

Invoking tasks

[7]:
a = square(2)
Found task: square
[8]:
b = add(a, 4)
Found task: add
[9]:
c = multiply (b, 5)
Found task: multiply

Accessing data outside tasks requires synchronization

[10]:
c = compss_wait_on(c)
[11]:
c = c + 1
[12]:
print("a: %s" % a)
print("b: %s" % b)
print("c: %d" % c)
a: <pycompss.runtime.management.classes.Future object at 0x7efd908b9d20>
b: <pycompss.runtime.management.classes.Future object at 0x7efda7e01ff0>
c: 41
[13]:
a = compss_wait_on(a)
[14]:
print("a: %d" % a)
a: 4

Stop the runtime

[15]:
ipycompss.stop(sync=True)
********************************************************
***************** STOPPING PyCOMPSs ********************
********************************************************
Checking if any issue happened.
Synchronizing all future objects left on the user scope.
Found a future object: b
********************************************************
[16]:
print("Results after stopping PyCOMPSs: ")
print("a: %d" % a)
print("b: %d" % b)
print("c: %d" % c)
Results after stopping PyCOMPSs:
a: 4
b: 8
c: 41