Adding Constraints to Tasks and Workflows#
Add constraints and execution directives to electrons by specifying them as arguments to the Covalent @electron
decorator.
The most-used constraint is to explicitly name an executor for an electron. Other constraints are described in the API Reference.
Prerequisites#
[1]:
import covalent as ct
@ct.electron
def sum(x, y):
return x + y
@ct.electron
def square(x):
return x * x
@ct.lattice
def sum_of_squares_wf(a, b):
x2 = square(a)
y2 = square(b)
return sum(x=x2, y=y2)
Procedure#
To specify a constraint on a single electron, add the constraint as a keyword argument to its Covalent
@electron
decorator.
[2]:
@ct.electron(executor = "local")
def sum(x, y):
return x + y
To specify a constraint on all electrons in a lattice, add the constraint as a keyword argument to the
@lattice
decorator. The constraint is inherited by all electrons in the lattice.
[3]:
@ct.lattice(executor = "local")
def sum_of_squares_wf(a, b):
x2 = square(a)
y2 = square(b)
return sum(x=x2, y=y2)
To override a constraint inherited from a lattice, specify the overriding constraint on the
@electron
decorator.
[4]:
import covalent as ct
@ct.electron(executor = "dask")
def sum(x, y):
return x + y
@ct.electron
def square(x):
return x * x
@ct.lattice(executor = "local")
def sum_of_squares_wf(a, b):
x2 = square(a)
y2 = square(b)
return sum(x=x2, y=y2)
In the example above, the square
electron inherits the local
executor from the lattice. The sum
electron overrides the inherited value and uses the dask
executor instead.
See Also#
Adding Pip Dependencies to an Electron