-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for passing mpi4py comm through cython. #818
Conversation
@hmcezar thanks for your contribution! Could you please check if it's possible to add a regression test? You could just add one extra test in this directory. I think that in our master branch they are run with pytest, so you could be able to just do something like
Then, for the test to work on GitHub actions, you should install the A possible strategy would be to make sure that the test is skipped when the package is not available (by properly adding a try import in the new test you are going to add) and then install the
|
@GiovanniBussi I know the test is too simple, but I'm not sure what could be done to really test the implementation. About the releases, maybe it should be included in 2.7 and 2.8? |
@hmcezar the test is fine for start. I am a bit skeptical in including mpi4py as a dependency in conda. The more dependencies you add, the more you have problems with upgrades on conda. An ideal solution would be one where:
For this to work, at least the cython compilation should work without mpi4py. Notice that we have type checkings in the python interface (that's why points are set to, e.g. |
This reverts commit 4fd8c3f.
@GiovanniBussi No problem, I reverted the commit. The CI problem seems to be in this line when I check if the object is MPI.Comm. |
Indeed, I think using |
Well, after implementing the suggested changes I still get the CI errors below. The [1/1] Cythonizing plumed.pyx
Error compiling Cython file:
------------------------------------------------------------
...
except ImportError:
HAS_NUMPY=False
try:
import mpi4py.MPI as MPI
cimport mpi4py.libmpi as libmpi
^
------------------------------------------------------------
plumed.pyx:52:13: 'mpi4py/libmpi.pxd' not found
Error compiling Cython file:
------------------------------------------------------------
...
self.c_plumed.cmd( ckey, <long*>&abuffer[0], len(abuffer))
cdef cmd_float(self, ckey, double val ):
self.c_plumed.cmd_float( ckey, val)
cdef cmd_int(self, ckey, int val):
self.c_plumed.cmd_int( ckey, val)
cdef cmd_mpi(self, ckey, MPI.Comm val):
^
------------------------------------------------------------
plumed.pyx:136:30: 'MPI' is not a cimported module
Error compiling Cython file:
------------------------------------------------------------
...
cdef cmd_float(self, ckey, double val ):
self.c_plumed.cmd_float( ckey, val)
cdef cmd_int(self, ckey, int val):
self.c_plumed.cmd_int( ckey, val)
cdef cmd_mpi(self, ckey, MPI.Comm val):
cdef libmpi.MPI_Comm buffer = val.ob_mpi
^
------------------------------------------------------------
plumed.pyx:137:14: 'MPI_Comm' is not a type identifier |
Ok, so digging deeper into this issue, it looks like cython supports conditional compilation. The solution probably should go in this direction. Detect mpi4py prior to the actual cython compilation and |
@hmcezar this is a possible solution, but I think it would be suboptimal. In practice, if you do so, one should compile the python extension with a MPI compiler. This is not done by default when you install plumed, and it is not done in the py-plumed conda and Macports packages. Notice that at the level of the python extension there should be no need to include the |
This pull request introduces 1 alert when merging feb1795 into 99bcb04 - view on LGTM.com new alerts:
|
I am not sure how portable this would be, but you might use |
@GiovanniBussi The problem is that even though I understand that and it should be possible in the sense that we are just passing pointers, we still have to make some checks before knowing which Error compiling Cython file:
------------------------------------------------------------
...
HAS_NUMPY=True
except ImportError:
HAS_NUMPY=False
try:
cimport mpi4py.MPI as MPI
^
------------------------------------------------------------
plumed.pyx:51:13: 'mpi4py/MPI.pxd' not found
Error compiling Cython file:
------------------------------------------------------------
...
raise ValueError("ndarrays should be double (size=8), int, or long")
elif isinstance(val, str ) :
py_bytes = val.encode()
cval = py_bytes
self.c_plumed.cmd( ckey, <const char*>cval )
elif HAS_MPI4PY and isinstance(val, MPI.Comm):
^
------------------------------------------------------------
plumed.pyx:171:48: cimported module has no attribute 'Comm'
|
I think you can use a plain
The only problem that I've seen is that the resulting One thing that I don't like very much is that it looks like just importing mpi4py is slowing down things by initializing MPI. We could thing about options such as only importing that only when reaching a failure in the Another option could be to have a specific python method such as Finally, we could try to import mpi4py when the cmd string contains an In any case, this can be solved if you find a way to pass a pointer to the |
So, I found this issue where the main I've implemented it, and things seem to work:
I reverted the conditional compilation changes. About importing |
Codecov Report
@@ Coverage Diff @@
## master #818 +/- ##
==========================================
+ Coverage 85.03% 85.82% +0.78%
==========================================
Files 599 599
Lines 49223 49268 +45
==========================================
+ Hits 41857 42282 +425
+ Misses 7366 6986 -380
Continue to review full report at Codecov.
|
Thanks! Regarding slowdown: the problematic case is a user with I tried to fix it here: 19cdea3 This is the same thing that we do with In this branch https://github.com/plumed/plumed2/tree/python-mpi I also added a couple more little change. If you are ok with this, I would merge it to master. I think it's better not to merge this in v2.7 or v2.8 since it's to be considered a new feature. |
I just checked and everything seems to work in your branch. I cythonized with and without About merging it to master, in the end, it is up to you guys, but if possible, since I'm using this implementation in HyMD it would be nice to have it in 2.7 and 2.8, so the code would be compatible with more versions of Plumed. Thanks for your help in implementing this! |
Notice that you can install the plumed python wrappers from the master branch and then load (setting Anyway, I will check if this can be backported easily to 2.8. v2.7 is more difficult since the plumed.pyx file was extensively changed. |
Thanks to you, you actually made most of the job! |
Great, just let me know if you need anything else for merging. |
@hmcezar FYI I just backported this fix to the v2.8 branch |
Thanks! |
Description
As mentioned in #817, currently it's not possible to
setMPIComm
using the Python interface.Target release
I would like my code to appear in the next point releases.
Type of contribution
Copyright
COPYRIGHT
file with the correct license information. Code should be released under an open source license. I also used the commandcd src && ./header.sh mymodulename
in order to make sure the headers of the module are correct.Tests