1.1. Basics of programming with PyCOMPSs

In this example we will see basics of programming with PyCOMPSs: - Runtime start - Task definition - Task invocation - Runtime stop

Let’s get started with a simple example

First step

  • Import the PyCOMPSs library
[1]:
import pycompss.interactive as ipycompss

Second step

  • 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,
                    project_xml='../xml/project.xml',
                    resources_xml='../xml/resources.xml')
else:
    ipycompss.start(graph=True, monitor=1000)  # debug=True, trace=True
******************************************************
*************** PyCOMPSs Interactive *****************
******************************************************
*          .-~~-.--.           _____       ________  *
*         :         )         |____ \     |____   /  *
*   .~ ~ -.\       /.- ~~ .     ___) |        /  /   *
*   >       `.   .'       <    / ___/        /  /    *
*  (         .- -.         )  | |___   _    /  /     *
*   `- -.-~  `- -'  ~-.- -'   |_____| |_|  /__/      *
*     (        :        )           _ _ .-:          *
*      ~--.    :    .--~        .-~  .-~  }          *
*          ~-.-^-.-~ \_      .~  .-~   .~            *
*                   \ \ '     \ '_ _ -~              *
*                    \`.\`.    //                    *
*           . - ~ ~-.__\`.\`-.//                     *
*       .-~   . - ~  }~ ~ ~-.~-.                     *
*     .' .-~      .-~       :/~-.~-./:               *
*    /_~_ _ . - ~                 ~-.~-._            *
*                                     ~-.<           *
******************************************************
* - Starting COMPSs runtime...                       *
* - Log path : /home/user/.COMPSs/Interactive_01/
* - PyCOMPSs Runtime started... Have fun!            *
******************************************************

Third step

  • Import task module before annotating functions or methods
[3]:
from pycompss.api.task import task

Fourth step

  • Declare functions and decorate with @task those that should be tasks
[4]:
@task(returns=int)
def square(val1):
    return val1 * val1
Found task: square
[5]:
@task(returns=int)
def add(val2, val3):
    return val2 + val3
Found task: add
[6]:
@task(returns=int)
def multiply(val1, val2):
    return val1 * val2
Found task: multiply

Fifth step

  • Invoke tasks
[7]:
a = square(2)
[8]:
b = add(a, 4)
[9]:
c = multiply(b, 5)

Sixth step (last)

  • Stop COMPSs runtime. All data can be synchronized in the main program .
[10]:
ipycompss.stop(sync=True)
****************************************************
*************** STOPPING PyCOMPSs ******************
****************************************************
Synchronizing all future objects left on the user scope.
Found a future object: b
Found a future object: a
Found a future object: c
****************************************************
[11]:
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: 40