cea-hpc / wi4mpi

Wrapper interface for MPI
BSD 3-Clause "New" or "Revised" License
80 stars 15 forks source link

compiling a simple c program with wi4mpi mpicc wrapper #45

Closed Clement-Fontenaille closed 9 months ago

Clement-Fontenaille commented 1 year ago

Hi, I am

While if would be equally ok in this case if hdf5 configure accepted passing a path to an mpi compiler for compiling mpi programs, or path to mpi libraries, it would make life easier if compiling a non-mpi program with wi4mpi interface mode was more transparent.

adrien-cotte commented 1 year ago

Hi,

I have the same issue with OMB:

$ curl -O https://mvapich.cse.ohio-state.edu/download/mvapich/osu-micro-benchmarks-5.9.tar.gz
$ tar xf osu-micro-benchmarks-5.9.tar.gz
$ cd osu-micro-benchmarks-5.9/
$ ./configure CC=mpicc CXX=mpicxx
[...]
configure: error: cannot run C compiled programs.
[...]

Here is my workaround (create libmpi.so inside fake libs):

$ ln -s $WI4MPI_ROOT/libexec/wi4mpi/fakelibMPICH/libmpi.so.12 $WI4MPI_ROOT/libexec/wi4mpi/fakelibMPICH/libmpi.so
$ LD_LIBRARY_PATH=$WI4MPI_ROOT/libexec/wi4mpi/fakelibMPICH/:$LD_LIBRARY_PATH ./configure CC=mpicc CXX=mpicxx

Basically, Wi4MPI should have libmpi.so inside fake libs to trick configure. I don't know why we just have libmpi.so.X ones. Edit: Because libmpi.so is Wi4MPI interface lib (wi4mpi/lib/libmpi.so)...

But for a long time behavior, I agree with you. If a program is not MPI based, mpicc should not link it with libmpi.so. Unfortunately, Open MPI and MPICH do the same so it seems to be a Wi4MPI dedicated problem.

Adrien,

adrien-cotte commented 1 year ago

Great news @Clement-Fontenaille!

We are so lucky, ld has an option specificly for our issue. Modifying mpicc with LD_FLAGS option -Wl,--as-needed -lmpi is enough. If the binary does not need libmpi.so, then ld will not use it.

--- mpicc.ori   2022-12-26 16:50:38.000000000 +0100
+++ mpicc   2022-12-26 16:50:29.000000000 +0100
@@ -29,7 +29,12 @@
 WI4MPI_CFLAGS="-I${WI4MPI_ROOT}/include"
 WI4MPI_CXXFLAGS="-I${WI4MPI_ROOT}/include"
 WI4MPI_FCFLAGS="-I${WI4MPI_ROOT}/include -cpp"
-WI4MPI_LDFLAGS="-L${WI4MPI_ROOT}/lib -lmpi"
+
+## LD_FLAGS - LD Hack - required for "./configure" simple C program ##
+# The ld "--as-needed -lmpi" only link libmpi.so if really needed.
+# "--push-state" and "--pop-state" is a trick to anly apply "--as-needed" to "-lmpi" option.
+# and not affect other libs.
+WI4MPI_LDFLAGS="-L${WI4MPI_ROOT}/lib -Wl,--push-state,--as-needed -lmpi -Wl,--pop-state"

 case $progname in
     mpicc)