1.3. PyCOMPSs: Using objects, lists, and synchronization
In this example we will see how classes and objects can be used from PyCOMPSs, and that class methods can become tasks.
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=True,
project_xml='../xml/project.xml',
resources_xml='../xml/resources.xml')
else:
ipycompss.start(graph=True, monitor=1000, debug=True)
********************************************************
**************** PyCOMPSs Interactive ******************
********************************************************
* .-~~-.--. ______ ______ *
* : ) |____ \ |____ \ *
* .~ ~ -.\ /.- ~~ . __) | __) | *
* > `. .' < |__ | |__ | *
* ( .- -. ) ____) | _ ____) | *
* `- -.-~ `- -' ~-.- -' |______/ |_| |______/ *
* ( : ) _ _ .-: *
* ~--. : .--~ .-~ .-~ } *
* ~-.-^-.-~ \_ .~ .-~ .~ *
* \ \ ' \ '_ _ -~ *
* \`.\`. // *
* . - ~ ~-.__\`.\`-.// *
* .-~ . - ~ }~ ~ ~-.~-. *
* .' .-~ .-~ :/~-.~-./: *
* /_~_ _ . - ~ ~-.~-._ *
* ~-.< *
********************************************************
* - Starting COMPSs runtime... *
* - Log path : /home/user/.COMPSs/Interactive_03/
* - PyCOMPSs Runtime started... Have fun! *
********************************************************
Importing task and arguments directionality modules
Import task module before annotating functions or methods
[3]:
from pycompss.api.api import compss_barrier
from pycompss.api.api import compss_wait_on
Declaring a class
[4]:
%%writefile my_shaper.py
from pycompss.api.task import task
from pycompss.api.parameter import IN
class Shape(object):
def __init__(self,x,y):
self.x = x
self.y = y
@task(returns=int)
def area(self):
return self.x * self.y
@task(returns=int)
def perimeter(self):
return 2 * self.x + 2 * self.y
def describe(self,text):
self.description = text
@task()
def scaleSize(self,scale):
self.x = self.x * scale
self.y = self.y * scale
@task(target_direction=IN)
def infoShape(self):
print('Shape x=', self.x, 'y= ', self.y)
Writing my_shaper.py
Invoking tasks
[5]:
from my_shaper import Shape
[6]:
my_shapes = []
my_shapes.append(Shape(100,45))
my_shapes.append(Shape(50,50))
[7]:
all_areas = []
[8]:
for this_shape in my_shapes:
all_areas.append(this_shape.area())
[9]:
# Need it if we want to synchonize nested objects
all_areas = compss_wait_on(all_areas)
print(all_areas)
[4500, 2500]
[10]:
rectangle = Shape(200,25)
rectangle.scaleSize(5)
area_rectangle = rectangle.area()
rectangle = compss_wait_on(rectangle)
print('X = %d' % rectangle.x)
area_rectangle = compss_wait_on(area_rectangle)
print('Area = %d' % area_rectangle)
X = 1000
Area = 125000
[11]:
all_perimeters=[]
my_shapes.append(rectangle)
for this_shape in my_shapes:
this_shape.infoShape()
all_perimeters.append(this_shape.perimeter())
[12]:
all_perimeters = compss_wait_on(all_perimeters)
print(all_perimeters)
[290, 200, 2250]
Stop the runtime
[13]:
ipycompss.stop(sync=True)
********************************************************
***************** STOPPING PyCOMPSs ********************
********************************************************
Checking if any issue happened.
Synchronizing all future objects left on the user scope.
Found a list to synchronize: my_shapes
Found a list to synchronize: all_areas
Found a list to synchronize: all_perimeters
********************************************************