darshan-gen-fortran.pl.in 9.74 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
Shane Snyder's avatar
Shane Snyder committed
3 4
# Copyright (C) 2015 University of Chicago.
# See COPYRIGHT notice in top-level directory.
5 6 7 8 9 10 11 12
#

# takes a standard mpif* script as an argument and tried to generate a
# darshan-enabled mpif* script to mimic it's behavior

use Getopt::Long;
use English;

13
my $PREFIX="@prefix@";
14 15 16

my $input_file = "";
my $output_file = "";
17
my $trim_exe_path=0;
18
my $xl_check = 0;
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

process_args();

# run original mpicc with -show argument to capture command line for
# compilation and linking
my $compile_cmd = `$input_file -show -c foo.c`;
if (!($compile_cmd))
{
    printf STDERR "Error: failed to invoke $input_file with -show\n";
    exit(1);
}

my $link_cmd = `$input_file -show foo.o -o foo`;
if (!($link_cmd))
{
    printf STDERR "Error: failed to invoke $input_file with -show\n";
    exit(1);
}

38 39 40 41 42 43 44 45 46
# we just want the first line of output from the mpi script's -v argument;
# the rest we assume will be generated by the underlying compiler
my $version_out = `$input_file -v 2>/dev/null |head -n 1`;
if (!($version_out))
{
    printf STDERR "Error: failed to invoke $input_file with -v\n";
    exit(1);
}

47 48 49 50 51 52 53 54
# check command lines for accuracy
if(!($compile_cmd =~ /-c foo.c/) || !($link_cmd =~ /foo.o -o foo/))
{
    printf STDERR "Error: faulty output from $input_file with -show.\n";
    exit(1);
}
chomp($compile_cmd);
chomp($link_cmd);
55
chomp($version_out);
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70
#
# Incomprehensible re to eat the leading path of the compiler command
# and only give the basename.
#
# /cmd -> cmd
# cmd -> cmd
# /x/y/x/z/cmd -> cmd
#
if($trim_exe_path)
{
    $compile_cmd =~ s/\/*([^\/ ]+\/)*//;
    $link_cmd =~ s/\/*([^\/ ]+\/)*//;
}

71 72
open (OUTPUT, ">$output_file") || die("Error opening output file: $!");

73 74 75 76
# split the link command from the original mpicc script so that we have the
# part before the "foo.o -o foo", which is assumed to be FC, and the part
# that comes afterwards, which is assumed to be the required link flags and
# libraries.
77
if(!($link_cmd =~ /(\S+)(.+)(foo.o -o foo)\s+(.*)/))
78 79 80 81 82
{
    printf STDERR "Error: improperly formatted link cmd: $link_cmd\n";
    exit(1);
}
$FC_from_link=$1;
83 84 85
$link_cmd_prefix=$2;
$base_link_cmd_suffix="\"\$\{allargs\[\@\]\}\" $4";
$link_cmd_suffix="\"\$\{newallargs\[\@\]\}\" \$FMPICH `$PREFIX/bin/darshan-config --pre-ld-flags` $4 `$PREFIX/bin/darshan-config --post-ld-flags`";
86 87

# repeat the above step for the compilation command line
88
if(!($compile_cmd =~ /(\S+)(.+)(-c foo.c)\s+(.*)/))
89 90 91 92 93
{
    printf STDERR "Error: improperly formatted compile cmd: $compile_cmd\n";
    exit(1);
}
$FC_from_compile=$1;
94 95
$compile_cmd_prefix=$2;
$compile_cmd_suffix="\"\$\{allargs\[\@\]\}\" $4";
96 97 98 99 100 101 102 103 104

# we expect to see the same FC command in both the link and compile steps
if($FC_from_link ne $FC_from_compile)
{
    printf STDERR "Error: cannot find matching FC from: $compile_cmd\n";
    printf STDERR "and: $link_cmd\n";
    exit(1);
}
$FC=$FC_from_compile;
105

106
# create link cmd with noshrargs variable
107 108
$link_cmd_no_shared_suffix = $base_link_cmd_suffix;
$link_cmd_no_shared_suffix =~ s/allargs/noshrargs/;
109

110
print OUTPUT<<"EOF";
111 112 113 114 115 116 117 118 119
#!/bin/bash
#
# Auto-generated mpicc script from darshan-gen-cc.pl
#
#
# Internal variables
# Show is set to echo to cause the compilation command to be echoed instead 
# of executed.
Show=
120
FC=\${MPICH_F77:-"$FC"}
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
EOF

if ($xl_check == 1)
{
print OUTPUT<<'EOF';
#
# Check for IBM XL Linker Parameters
#
# If IPA linker is used, we must disable darshan
# because the IPA linker doesn't support -wrap.
#
function check_XL()
{
    arg=$1;
    ipa_enabled=0;
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    if [ "$arg" = "-O5" ]; then
        ipa_enabled=1;
    fi
    if [ "$arg" = "-O4" ]; then
        ipa_enabled=1;
    fi
    if [ "$arg" = "-qipa" ]; then
        ipa_enabled=1;
    fi

    return $ipa_enabled;
}
EOF
}

print OUTPUT<<'EOF';
154 155 156 157 158 159 160 161 162 163 164 165 166
linking=yes
allargs=("$@")
argno=0
for arg in "$@" ; do
    # Set addarg to no if this arg should be ignored by the C compiler
    addarg=yes
    case "$arg" in 
 	# ----------------------------------------------------------------
	# Compiler options that affect whether we are linking or no
    -c|-S|-E|-M|-MM)
    # The compiler links by default
    linking=no
    ;;
167 168 169 170 171 172 173 174 175 176 177 178
    -v)
    # Pass this argument to the compiler as well.
EOF
print OUTPUT<<"EOF";
    echo "$version_out"
EOF
print OUTPUT<<'EOF';
    # if there is only 1 argument, it must be -v.
    if [ "$#" -eq "1" ] ; then
        linking=no
    fi
    ;;
179 180 181 182 183 184 185 186 187 188 189 190
    -f77=*)
    FC=`echo A$arg | sed -e 's/A-f77=//g'`
    addarg=no
    ;;
    -f90=*)
    FC=`echo A$arg | sed -e 's/A-f90=//g'`
    addarg=no
    ;;
    -fc=*)
    FC=`echo A$arg | sed -e 's/A-fc=//g'`
    addarg=no
    ;;
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    -show)
    addarg=no
    Show=echo
    ;;
    esac
    if [ $addarg = no ] ; then
	unset allargs[$argno]
    fi
    # Some versions of bash do not accept ((argno++))
    argno=`expr $argno + 1`
done
if [ "$linking" = yes ] ; then
    if [ -n "$C_LINKPATH_SHL" ] ; then
	# prepend the path for the shared libraries to the library list
	mpilibs="$C_LINKPATH_SHL$libdir $mpilibs"
    fi
EOF
print OUTPUT<<"EOF";
    # Trial run to generate a symbol listing.  We only enable Darshan if:
    # a) MPI is used
    # b) PMPI is _not_ used
    #
    # We want Darshan to get out of the way if the user is doing a runtime
    # test in configure (in which case MPI objects break the executable) or
    # if the user is using another PMPI based tool

    # if allargs includes any libraries, then we need to get 
218
    # -ldarshan in there first
219 220
    argno=0;
    once=0;
221
    compiler_check=0;
222 223
    newallargs=\("\$\{allargs\[\@\]\}"\);
    for arg in "\$\{newallargs\[\@\]\}"; do
224
        res=`expr match "\$arg" "-l"`;
225
        if [ \$res -gt 0 -a \$once -lt 1 ]; then
226
            newallargs[\$argno]=-ldarshan
227 228 229 230 231 232
            argno=`expr \$argno + 1`
            newallargs[\$argno]=\$arg;
            once=1;
        else
            newallargs[\$argno]=\$arg;
        fi
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
EOF

#
# Add any functional tests for linker parameters
#
       if ($xl_check)
       {
print OUTPUT<<"EOF";
           check_XL "\$arg";
           if [ \$? -ne 0 ]; then
               compiler_check=1;
           fi
EOF
       }

print OUTPUT<<"EOF";

250 251
        argno=`expr \$argno + 1`
    done
252 253 254 255 256

    used_darshan=0

    # create a temporary file
    tmpfile=`mktemp`
257
    binfile=`mktemp`
258 259
    # generate a map of the symbol names
    # don't use -shared for this step
260 261 262 263 264 265 266 267
    argno=0
    noshrargs=\("\$\{allargs\[\@\]\}");
    for arg in "\$\{noshrargs\[\@\]\}"; do
        if [ "\$arg" = "-shared" ]; then
            unset noshrarg[\$argno];
        fi
        argno=`expr \$argno + 1`
    done
268
    \$FC $link_cmd_prefix $link_cmd_no_shared_suffix -Wl,-Map,\$tmpfile \$LDFLAGS -o \$binfile >& /dev/null
269

270
    # is MPI in there?
271
    grep -i MPI \$tmpfile >& /dev/null
272 273
    rc_mpi=\$?

Philip Carns's avatar
Philip Carns committed
274 275
    # is PMPI being used for PMPI_File_open?
    grep -E PMPI_File_open \$tmpfile | grep -v -E \\(mpich.*\\.a\\) |grep \\(PMPI >& /dev/null
276 277
    rc_pmpi=\$?

278 279 280

    # find appropriate fortran library name for profiling
    grep -E libmpifort \$tmpfile >& /dev/null
281 282
    rc_cnk_check=\$?
    if [ \$rc_cnk_check -eq 0 ] ; then
283
        FMPICH=-lmpifort
284
    else
285 286 287 288 289
        grep -E mpich\\.cnk \$tmpfile >& /dev/null
        rc_cnk_check=\$?
        if [ \$rc_cnk_check -eq 0 ] ; then
            FMPICH=-lfmpich.cnk
        else
290
            bgqlib_out=`grep -m 1 -Po "libmpichf\\S\+\-\\S\+" \$tmpfile 2>/dev/null`
291 292
            rc_bgqlib_check=\$?
            if [ \$rc_bgqlib_check -eq 0 ] ; then
293
                FMPICH=`echo \$bgqlib_out | sed 's/libmpich\\(.*\\)-\\(.*\\)\\.a.*/-lmpich\\1-\\2/'`
294 295 296
            else
                FMPICH=-lfmpich
            fi
297
        fi
298 299 300 301
    fi

    rm \$tmpfile >& /dev/null

302 303 304 305 306 307 308 309 310 311 312
    # is MPI_Init a weak symbol?
    # disable darshan if it is not
    if [ -e \$binfile ];
    then
        nm \$binfile | grep -i "mpi_init" | grep -i "w" >& /dev/null
        rc_weak=\$?
    else
        rc_weak=1
    fi
    rm \$binfile >& /dev/null

313 314 315 316 317
    # disable darshan if the executable is not an MPI program (this feature
    # mainly allows runtime configure tests to work
    if [ \$rc_mpi -eq 0 ] ; then
        # disable darshan if something else is already using PMPI; we don't
        # want to cause any symbol conflicts
318
        if [ \$rc_pmpi -ne 0 ] && [ \$compiler_check -eq 0 ] && [ \$rc_weak -eq 0 ]; then
319
            \$Show \$FC $link_cmd_prefix $link_cmd_suffix
320 321 322 323 324 325
            used_darshan=1
        fi
    fi

    # otherwise use the normal command line
    if [ "\$used_darshan" = "0" ] ; then
326
        \$Show \$FC $link_cmd_prefix $base_link_cmd_suffix
327 328 329
    fi
    rc=\$?
else
330
    \$Show \$FC $compile_cmd_prefix $compile_cmd_suffix
331 332 333 334 335 336 337 338 339 340 341 342 343 344
    rc=\$?
fi

exit \$rc
EOF

close(OUTPUT);

chmod(0755, $output_file); 

exit(0);

sub process_args
{
345
    use vars qw( $opt_help $opt_output $opt_trim $opt_xl );
346 347 348

    Getopt::Long::Configure("no_ignore_case", "bundling");
    GetOptions( "help",
349
        "output=s",
350 351
        "trim",
        "xl");
352 353 354 355 356 357 358

    if($opt_help)
    {
        print_help();
        exit(0);
    }

359 360 361 362 363
    if($opt_trim)
    {
        $trim_exe_path=1;
    }

364 365 366 367 368
    if($opt_xl)
    {
        $xl_check=1;
    }

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    if($opt_output)
    {
        $output_file = $opt_output;
    }
    else
    {
        print_help();
        exit(1);
    }

    # there should only be one remaining argument: the input file 
    if($#ARGV != 0)
    {
        print "Error: invalid arguments.\n";
        print_help();
        exit(1);
    }
    $input_file = $ARGV[0];

    return;
}

sub print_help
{
    print<<"EOF";

Usage: $PROGRAM_NAME <stock mpi fortran script> --output <modified mpi fortran script>

    --help          Prints this help message
    --output        Specifies name of output script
399
    --trim          Trim the compilers path
400 401
    --xl            Disables darshan if certain linker parameters are used
                    (Intended for IBM XL Compilers)
402 403 404 405 406 407 408 409 410 411

Purpose:

    This script takes an existing mpif* script as input and generates a
    modified version that includes Darshan support.

EOF
    return;
}