[Scons-users] OpenMP and MPI

Florian Lindner mailinglists at xgm.de
Tue Jan 27 03:12:19 EST 2015


Russel Winder wrote:

> On Mon, 2015-01-26 at 10:22 +0100, Florian Lindner wrote:
>> Hello,
>> 
>> I'm working on a program that uses both MPI and OpenMP. gcc and icc
>> require different compiler flags -openmp / -fopenmp.
>> 
>> Furthermore when using MPI you usually use a compiler wrapper like mpicc
>> / mpicxx / mpic++ where you don't see the compiler behind.
>> 
>> What's the best scons way to deal with that? Current idea is
>> 
>> if compiler = "g++":
>>   env.Append(CCFLAGS = ['-fopenmp'])
>>   env.Append(LINKFLAGS = ['-fopenmp'])
>> elif compiler = "icc"
>>   # likewise
>> elif compiler.startswith("mpi"):
>>   # execute $compiler -show with gives output like that
>>   # g++ -pthread -Wl,-rpath -Wl,/usr/lib/openmpi [...]
>>   # and parse that output
> 
> Are you using specific SCons tools or just the default tool set? I am
> guessing the latter.

Just vanilla scons.

> For MPI, I always explicitly set compiler to mpic++.

That's what I'm doing. Problem is that I don't know which openmp flag 
(fopenmp for g++, openmp for icc) to pass, because I don't know which 
compiler mpic++ calls.
 
> Put this together and I am not sure I would advocate the if you have.

Sorry, I don't understand that sentence.

> 
> Is there a small project that we could bandy about to iterate to a "best
> practice"?
> 
>> 
>> Is using subprocess to execute external processes ok? Any better way?
> 
> It is, but it shouldn't be needed.

My current, actual working code looks like that:

def guess_omp_flag(compiler):
    """ Guess the right flag to enable OpenMP, depending on the compiler / 
MPI wrapper. """
    if compiler.startswith("g++"):
        return "-fopenmp"
    elif compiler.startswith("icc"):
        return "-openmp"
    elif compiler.startswith("clang"):
        print "Clang does not support OpenMP."
        return ""
    elif compiler.startswith("mpi"):
        try:
            output = subprocess.check_output("%s -show" % compiler, 
shell=True)
        except OSError as e:
            print "Error checking for MPI compiler"
            print e
        except subprocess.CalledProcessError as e:
            print "Error testing for OpenMP flag."
            print "Command was:", e.cmd, "Output was:", e.output
        else:
            return guess_omp_flag(output.split()[0])

def CheckOpenMP(context):
    """ Encapsulate guess_omp_flag into a scons test. """
    context.Message("Checking for OpenMP flag... ")
    result = guess_omp_flag(context.env["compiler"])
    context.Result(result)
    context.env.Append(CCFLAGS = [result])
    context.env.Append(LINKFLAGS = [result])


env = Environment(ENV = os.environ)   
conf = Configure(env, custom_tests= { "CheckOpenMP" : CheckOpenMP})

conf.CheckOpenMP()


Regards,
Florian




More information about the Scons-users mailing list