-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
execute_task()
to a dedicated module
Multiple executors use the `execute_task()` function, so moving it to its own module improves code organization and reusability. Also removed MPI-related code from `execute_task()`, as it's specific to the HTEX.
- Loading branch information
Showing
5 changed files
with
80 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os | ||
|
||
from parsl.serialize import unpack_res_spec_apply_message | ||
|
||
|
||
def execute_task(bufs: bytes): | ||
"""Deserialize the buffer and execute the task. | ||
Returns the result or throws exception. | ||
""" | ||
user_ns = locals() | ||
user_ns.update({'__builtins__': __builtins__}) | ||
|
||
f, args, kwargs, resource_spec = unpack_res_spec_apply_message(bufs, copy=False) | ||
|
||
for varname in resource_spec: | ||
envname = "PARSL_" + str(varname).upper() | ||
os.environ[envname] = str(resource_spec[varname]) | ||
|
||
# We might need to look into callability of the function from itself | ||
# since we change it's name in the new namespace | ||
prefix = "parsl_" | ||
fname = prefix + "f" | ||
argname = prefix + "args" | ||
kwargname = prefix + "kwargs" | ||
resultname = prefix + "result" | ||
|
||
user_ns.update({fname: f, | ||
argname: args, | ||
kwargname: kwargs, | ||
resultname: resultname}) | ||
|
||
code = "{0} = {1}(*{2}, **{3})".format(resultname, fname, | ||
argname, kwargname) | ||
exec(code, user_ns, user_ns) | ||
return user_ns.get(resultname) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
from parsl.executors.execute_task import execute_task | ||
from parsl.serialize.facade import pack_res_spec_apply_message | ||
|
||
|
||
def addemup(*args: int, name: str = "apples"): | ||
total = sum(args) | ||
return f"{total} {name}" | ||
|
||
|
||
@pytest.mark.local | ||
def test_execute_task(): | ||
args = (1, 2, 3) | ||
kwargs = {"name": "boots"} | ||
buff = pack_res_spec_apply_message(addemup, args, kwargs, {}) | ||
res = execute_task(buff) | ||
assert res == addemup(*args, **kwargs) | ||
|
||
|
||
@pytest.mark.local | ||
def test_execute_task_resource_spec(): | ||
resource_spec = {"num_nodes": 2, "ranks_per_node": 2, "num_ranks": 4} | ||
buff = pack_res_spec_apply_message(addemup, (1, 2), {}, resource_spec) | ||
execute_task(buff) | ||
for key, val in resource_spec.items(): | ||
assert os.environ[f"PARSL_{key.upper()}"] == str(val) |