Interface Python with Legion C++ tasks

50 views
Skip to first unread message

Eric Raut

unread,
Dec 8, 2021, 3:42:47 PM12/8/21
to Legion Users

Hi all,


I have a Legion program targeting the Legion C++ API directly, with a top-level task written in C++ and subtasks also written in C++. I would like to partially replace the top-level task with one written in Python, while keeping the C++ subtasks. See minimal example below.


What is the best way to accomplish this? I can use the Python API to call “call_task_1” in the code below.  But will the Legion tasks conflict with Pygion, which might expect that the top level task main() is the only task in the application? Or, since the Python code doesn't have anything specific to Legion, is there a better way to accomplish this than using Pygion?


I’d like to have something similar to this:


C++ code:

enum {

  TASK_1_ID,

  TASK_2_ID,

  //...

};


void task_1(const Task *task,

            const std::vector<PhysicalRegion> &regions,

            Context ctx, Runtime *runtime) {

  // ...

}


void call_task_1(...) {

  // ... Setup code ...

  IndexTaskLauncher task_1_launcher(...);

  task_1_launcher.execute_index_space();

}


void preregister_tasks() {

  Runtime::preregister_task_variant<task_1>(...);

}


Python code:

@task

def main():

    # application high-level logic ...

    c_interface.call_task_1(...)

    # ...


if __name__ == '__main__':

    c_interface.preregister_tasks()

    main()


Thanks for any insights,

Eric


Elliott Slaughter

unread,
Dec 8, 2021, 4:29:38 PM12/8/21
to Eric Raut, Legion Users
Hi Eric,

Pygion uses dynamic task IDs, so the task IDs won't conflict:


The style you're using (a C API exposed to Python, with task calls in the interior) is fine. But if you want, you can also make Python do the calls directly via the Regent calling convention (which despite the name, does not require Regent).

Here's an example of an extern task declaration:

https://github.com/StanfordLegion/legion/blob/master/apps/circuit/python/circuit_sparse.py#L91

And an example of calling it:


On the C++ side, you'd need to follow the Regent convention for packing task arguments and regions, but otherwise would be able to write your code as normal. I don't have an example, unfortunately, but it's not too hard to do.

--
You received this message because you are subscribed to the Google Groups "Legion Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to legionusers...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/legionusers/CAPS9aMfyfxB6eAg1TesTOAUhLHLZseL7aQyu-8L70geMK_VNXg%40mail.gmail.com.


--
Elliott Slaughter

"Don't worry about what anybody else is going to do. The best way to predict the future is to invent it." - Alan Kay

Eric Raut

unread,
Dec 9, 2021, 5:58:45 PM12/9/21
to Elliott Slaughter, Legion Users
Hi Elliott

Thank you for your response.

Do you have any examples of how to run a Pygion program? I tried one of the examples, but got this error:

$ python $install/share/Legion/python/examples/hello.py
Traceback (most recent call last):
  File "/data/eraut/legion/build_cugcc8_python/install/share/Legion/python/examples/hello.py", line 20, in <module>
    from pygion import task
  File "/data/eraut/legion/build_cugcc8_python/install/lib/python3.10/site-packages/pygion.py", line 75, in <module>
    assert _max_dim is not None, 'Unable to detect LEGION_MAX_DIM'
AssertionError: Unable to detect LEGION_MAX_DIM

I am using CMake to build Legion, and setting Legion_USE_Python=ON and Legion_BUILD_BINDINGS=ON. I also saw an exe called legion_python, but running that fails as well with a different error:
$ $install/bin/legion_python -ll:py 1
[0 - 7f03e0752800]    0.542676 {6}{python}: python exception occurred within task:
Traceback (most recent call last):
  File "/data/eraut/legion/build_cugcc8_python/install/lib/python3.10/site-packages/legion_top.py", line 311, in legion_python_main
    c.legion_task_preamble(
  File "/usr/lib64/python3.10/site-packages/cffi/api.py", line 912, in __getattr__
    make_accessor(name)
  File "/usr/lib64/python3.10/site-packages/cffi/api.py", line 908, in make_accessor
    accessors[name](name)
  File "/usr/lib64/python3.10/site-packages/cffi/api.py", line 838, in accessor_function
    value = backendlib.load_function(BType, name)
AttributeError: function/symbol 'legion_task_preamble' not found in library '<None>': ./legion_python: undefined symbol: legion_task_preamble
legion_python: /data/eraut/legion/runtime/realm/python/python_module.cc:978: virtual void Realm::LocalPythonProcessor::execute_task(Realm::Processor::TaskFuncID, const Realm::ByteArrayRef&): Assertion `0' failed.
Aborted (core dumped)

Any input would be appreciated. Thanks again!

Eric

Elliott Slaughter

unread,
Dec 9, 2021, 6:01:37 PM12/9/21
to Eric Raut, Legion Users
Hi Eric,

I think your second command line is correct, but you need to build Legion with -DBUILD_SHARED_LIBS=ON (in addition to the flags you found).

Daniel Guilder

unread,
Dec 27, 2022, 4:19:06 PM12/27/22
to Legion Users
Hello,

I'm also running into similar problems. From the looks of things I was able to build legion and install pygion after running "sudo python3 setup.py install" without errors. But when I try running "hello.py" I also get an error importing pygion  " assert _max_dim is not None, 'Unable to detect LEGION_MAX_DIM'" same as Eric.  I tried modifying pygion.py to hardcode the number to 3but that creates different errors. 

I have not been able to find any documentation describing how to run the pygion examples.  My current impression is I should be able to just run these examples with python3.  It would be nice if some was created and in the legion/bindings/python/examples.  It would be nice if someone tried using an VM to install legion and run the pygion examples and describe what is required.  I was able to run the legion C++ and regent examples without problems but pygion has been a struggle. 

Thanks,

Daniel Guilder

Daniel Guilder

unread,
Dec 27, 2022, 4:55:11 PM12/27/22
to Legion Users
Hi,

I was able to get the examples to work with using the legion_python.exe to call the hello.py example.  Is this the only way to get pygion to work? I was originally leaning towards python since I thought it would just be a import into existing python code so things like the ide/debuggers would be the same. But I don't know if those are available when using a custom python exe. 

Thanks,

Daniel Guilder 

Elliott Slaughter

unread,
Jan 9, 2023, 3:06:25 PM1/9/23
to Daniel Guilder, Legion Users
There is now a MR for loading Legion as a library, thanks to Wei Wu. I have not tested it, but you may be interested.


Having said that, while legion_python doesn't necessarily support all flags of the normal Python executable, we're pretty close. A "debugger" in Python is just a module you load. I think you could load it in legion_python just as well as anything else.

Reply all
Reply to author
Forward
0 new messages