2

I need to run a code using mpicc(parallel gcc). A server has two packages of mpicc installed namely OPENMPI and MPICH at /usr/local/OPENMPI and /usr/local/MPICH respectively. By default mpicc of OPENMPI is being used

which mpicc

shows /usr/local/OPENMPI/bin/mpicc

My code is built for MPICH. Instead of calling mpicc of MPICH using full path every time. Is there an easy way of making /usr/local/MPICH/bin/mpicc as default until I complete my work and later change it back to /usr/local/OPENMPI/bin/mpicc

jesse_b
  • 37,005
Jithu
  • 23
  • ln -sf /usr/local/OPENMPI/bin/mpicc /usr/local/bin if you do that it works? – Luciano Andress Martini Dec 26 '17 at 12:18
  • In some systems (like Fedora) there are modules that allow you to change the enviroment for certain libraries/compilers. For example module load mpich/module swap open-mpi. – alfC Mar 18 '20 at 01:45

1 Answers1

1

I think an alias would work perfectly for your situation. You can add the following to your .profile or .bash_profile:

alias mympicc='/usr/local/MPICH/bin/mpicc'

A more permanent (but still reversible) solution is to add the MPICH location to your path BEFORE the OPENMPI location. Easily done by adding the following your .profile or .bash_profile:

export PATH="/usr/local/MPICH/bin:$PATH"

When you type a command like mpicc, if it's not in your current directory, your shell will search the PATH for it, in order, so it is important which position you put it to set precedence.

If you want to have some sort of a switch that you can use to easily "flip" between the two, you could make some sort of function and add it to your .bashrc (or .profile/.bash_profile ?):

use_mpicc () {
    shopt -s nocasematch
    case "$1" in
        mpich) export PATH="...:/usr/local/MPICH/bin:/usr/local/OPENMPI/bin:..." ;;
        openmpi) export PATH="...:/usr/local/OPENMPI/bin:/usr/local/MPICH/bin:..." ;;
        *) return 1 ;;
    esac
    shopt -u nocasematch
}

In this example I have used ellipsis in place of your actual path. I recommend actually specifying the full path in this function (if used) rather than using the: PATH="Stuff_I_Need_added:$PATH" method as that will just keep adding to your PATH every time you call the function, potentially causing it to become obnoxiously long.

You would call this like:

$ use_mpicc mpich
$ # OR
$ use_mpicc openmpi

More reading on setting your path

jesse_b
  • 37,005