Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into depencencies
[simgrid.git] / teshsuite / smpi / mpich3-test / runtests
1 #! /usr/bin/env perl
2 # -*- Mode: perl; -*-
3 #
4 # This script is the beginnings of a script to run a sequence of test
5 # programs.  See the MPICH document for a description of the test
6 # strategy and requirements.
7 #
8 # Description
9 #   Tests are controlled by a file listing test programs; if the file is
10 #   a directory, then all of the programs in the directory and subdirectories
11 #   are run
12 #
13 #   To run a test, the following steps are executed
14 #   Build the executable:
15 #      make programname
16 #   Run the executable
17 #      mpiexec -n <np> ./programname >out 2>err
18 #   Check the return code (non zero is failure)
19 #   Check the stderr output (non empty is failure)
20 #   Check the stdout output (No Errors or Test passed are the only valid
21 #      output)
22 #   Remove executable, out, err files
23 #
24 # The format of a list file is
25 # programname number-of-processes
26 # If number-of-processes is missing, $np_default is used (this is 2 but can
27 # be overridden with -np=new-value)
28 #
29 # Special feature:
30 # Because these tests can take a long time to run, there is an
31 # option to cause the tests to stop is a "stopfile" is found.
32 # The stopfile can be created by a separate, watchdog process, to ensure that
33 # tests end at a certain time.
34 # The name of this file is (by default) .stoptest
35 # in the  top-level run directory.  The environment variable
36 #    MPITEST_STOPTEST
37 # can specify a different file name.
38 #
39 # Import the mkpath command
40 use File::Path;
41
42 # Global variables
43 $MPIMajorVersion = "3";
44 $MPIMinorVersion = "1";
45 $mpiexec = "smpirun";    # Name of mpiexec program (including path, if necessary)
46 $platformfile = "../../../../examples/platforms/small_platform_with_routers.xml";
47 $hostfile = "../../hostfile_mpich";
48 $testIsStrict = "true";
49 $MPIhasMPIX   = "no";
50 $np_arg  = "-np";         # Name of argument to specify the number of processes
51 $err_count = 0;          # Number of programs that failed.
52 $total_run = 0;          # Number of programs tested
53 $total_seen = 0;         # Number of programs considered for testing
54 $np_default = 2;         # Default number of processes to use
55 $np_max     = -1;        # Maximum number of processes to use (overrides any
56                          # value in the test list files.  -1 is Infinity
57 $defaultTimeLimit = 180; # default timeout
58
59 $srcdir = ".";           # Used to set the source dir for testlist files
60
61 $curdir = ".";           # used to track the relative current directory
62
63 # Output forms
64 $xmloutput = 0;          # Set to true to get xml output (also specify file)
65 $closeXMLOutput = 1;     # Set to false to leave XML output file open to
66                          # accept additional data
67 $verbose = 1;            # Set to true to get more output
68 $showProgress = 0;       # Set to true to get a "." with each run program.
69 $newline = "\r\n";       # Set to \r\n for Windows-friendly, \n for Unix only
70 $batchRun = 0;           # Set to true to batch the execution of the tests
71                          # (i.e., run them together, then test output,
72                          # rather than build/run/check for each test)
73 $testCount = 0;          # Used with batchRun to count tests.
74 $batrundir = ".";        # Set to the directory into which to run the examples
75
76 $execarg="";
77 $wrapparg="";
78
79 $enabled_privatization = 1; # disable tests that need SMPI privatization to run
80 # TAP (Test Anything Protocol) output
81 my $tapoutput = 0;
82 my $tapfile = '';
83 my $tapfullfile = '';
84
85 $debug = 1;
86
87 $depth = 0;              # This is used to manage multiple open list files
88
89 # Build flags
90 $remove_this_pgm = 0;
91 $clean_pgms      = 0;
92
93 my $program_wrapper = '';
94
95 #---------------------------------------------------------------------------
96 # Get some arguments from the environment
97 #   Currently, only the following are understood:
98 #   VERBOSE
99 #   RUNTESTS_VERBOSE  (an alias for VERBOSE in case you want to
100 #                      reserve VERBOSE)
101 #   RUNTESTS_SHOWPROGRESS
102 #   MPITEST_STOPTEST
103 #   MPITEST_TIMEOUT
104 #   MPITEST_PROGRAM_WRAPPER (Value is added after -np but before test
105 #                            executable.  Tools like valgrind may be inserted
106 #                            this way.)
107 #---------------------------------------------------------------------------
108 if ( defined($ENV{"VERBOSE"}) || defined($ENV{"V"}) || defined($ENV{"RUNTESTS_VERBOSE"}) ) {
109     $verbose = 1;
110 }
111 if ( defined($ENV{"RUNTESTS_SHOWPROGRESS"} ) ) {
112     $showProgress = 1;
113 }
114 if (defined($ENV{"MPITEST_STOPTEST"})) {
115     $stopfile = $ENV{"MPITEST_STOPTEST"};
116 }
117 else {
118     $stopfile = `pwd` . "/.stoptest";
119     $stopfile =~ s/\r*\n*//g;    # Remove any newlines (from pwd)
120 }
121
122 if (defined($ENV{"MPITEST_TIMEOUT"})) {
123     $defaultTimeLimit = $ENV{"MPITEST_TIMEOUT"};
124 }
125
126 # Define this to leave the XML output file open to receive additional data
127 if (defined($ENV{'NOXMLCLOSE'}) && $ENV{'NOXMLCLOSE'} eq 'YES') {
128     $closeXMLOutput = 0;
129 }
130
131 if (defined($ENV{'MPITEST_PROGRAM_WRAPPER'})) {
132     $program_wrapper = $ENV{'MPITEST_PROGRAM_WRAPPER'};
133 }
134
135 if (defined($ENV{'MPITEST_BATCH'})) {
136     if ($ENV{'MPITEST_BATCH'} eq 'YES' || $ENV{'MPITEST_BATCH'} eq 'yes') {
137         $batchRun = 1;
138     } elsif ($ENV{'MPITEST_BATCH'} eq 'NO' || $ENV{'MPITEST_BATCH'} eq 'no') {
139         $batchRun = 0;
140     }
141     else {
142         print STDERR "Unrecognized value for MPITEST_BATCH = $ENV{'MPITEST_BATCH'}\n";
143     }
144 }
145 if (defined($ENV{'MPITEST_BATCHDIR'})) {
146     $batrundir = $ENV{'MPITEST_BATCHDIR'};
147 }
148
149 #---------------------------------------------------------------------------
150 # Process arguments and override any defaults
151 #---------------------------------------------------------------------------
152 foreach $_ (@ARGV) {
153     if (/--?mpiexec=(.*)/) {
154         # Use mpiexec as given - it may be in the path, and
155         # we don't want to bother to try and find it.
156         $mpiexec = $1;
157     }
158     elsif (/--?np=(.*)/)   { $np_default = $1; }
159     elsif (/--?maxnp=(.*)/) { $np_max = $1; }
160     elsif (/--?tests=(.*)/) { $listfiles = $1; }
161     elsif (/--?platformfile=(.*)/) { $platformfile = $1; }
162     elsif (/--?hostfile=(.*)/) { $hostfile = $1; }
163     elsif (/--?srcdir=(.*)/) { $srcdir = $1;
164         $mpiexec="$mpiexec  -platform ${srcdir}/$platformfile -hostfile ${srcdir}/$hostfile --log=root.thr:critical --cfg=smpi/host-speed:1e9  --cfg=smpi/async-small-thresh:65536"; }
165     elsif (/--?verbose/) { $verbose = 1; }
166     elsif (/--?showprogress/) { $showProgress = 1; }
167     elsif (/--?debug/) { $debug = 1; }
168     elsif (/--?batch/) { $batchRun = 1; }
169     elsif (/--?batchdir=(.*)/) { $batrundir = $1; }
170     elsif (/--?timeoutarg=(.*)/) { $timeoutArgPattern = $1; }
171     elsif (/--?execarg=(.*)/) { $execarg = "$execarg $1"; }
172     elsif (/--?privatization=(.*)/) { 
173 print STDERR "privatization called\n";
174 $enabled_privatization = $1; }
175     elsif (/--?wrapper=(.*)/) {
176          $wrapparg = "-wrapper \"$1\"" if $1 ne "";
177     }
178     elsif (/--?xmlfile=(.*)/) {
179         $xmlfile   = $1;
180         if (! ($xmlfile =~ /^\//)) {
181             $thisdir = `pwd`;
182             chop $thisdir;
183             $xmlfullfile = $thisdir . "/" . $xmlfile ;
184         }
185         else {
186             $xmlfullfile = $xmlfile;
187         }
188         $xmloutput = 1;
189         open( XMLOUT, ">$xmlfile" ) || die "Cannot open $xmlfile\n";
190         my $date = `date "+%Y-%m-%d-%H-%M"`;
191         $date =~ s/\r?\n//;
192         # MPISOURCE can be used to describe the source of MPI for this
193         # test.
194         print XMLOUT "<?xml version='1.0' ?>$newline";
195         print XMLOUT "<?xml-stylesheet href=\"TestResults.xsl\" type=\"text/xsl\" ?>$newline";
196         print XMLOUT "<MPITESTRESULTS>$newline";
197         print XMLOUT "<DATE>$date</DATE>$newline";
198         print XMLOUT "<MPISOURCE></MPISOURCE>$newline";
199     }
200     elsif (/--?noxmlclose/) {
201         $closeXMLOutput = 0;
202     }
203     elsif (/--?tapfile=(.*)/) {
204         $tapfile = $1;
205         if ($tapfile !~ m|^/|) {
206             $thisdir = `pwd`;
207             chomp $thisdir;
208             $tapfullfile = $thisdir . "/" . $tapfile ;
209         }
210         else {
211             $tapfullfile = $tapfile;
212         }
213         $tapoutput = 1;
214         open( TAPOUT, ">$tapfile" ) || die "Cannot open $tapfile\n";
215         my $date = `date "+%Y-%m-%d-%H-%M"`;
216         $date =~ s/\r?\n//;
217         print TAPOUT "TAP version 13\n";
218         print TAPOUT "# MPICH test suite results (TAP format)\n";
219         print TAPOUT "# date ${date}\n";
220         # we do not know at this point how many tests will be run, so do
221         # not print a test plan line like "1..450" until the very end
222     }
223 }
224
225 # Perform any post argument processing
226 if ($batchRun) {
227     if (! -d $batrundir) {
228         mkpath $batrundir || die "Could not create $batrundir\n";
229     }
230     open( BATOUT, ">$batrundir/runtests.batch" ) || die "Could not open $batrundir/runtests.batch\n";
231 }
232 else {
233     # We must have mpiexec
234     if ("$mpiexec" eq "") {
235         print STDERR "No mpiexec found!\n";
236         exit(1);
237     }
238 }
239
240 #
241 # Process any files
242 if ($listfiles eq "") {
243     if ($batchRun) {
244         print STDERR "An implicit list of tests is not permitted in batch mode\n";
245         exit(1);
246     }
247     else {
248         &ProcessImplicitList;
249     }
250 }
251 elsif (-d $listfiles) {
252     print STDERR "Testing by directories not yet supported\n";
253 }
254 else {
255     &RunList( $listfiles );
256 }
257
258 if ($xmloutput && $closeXMLOutput) {
259     print XMLOUT "</MPITESTRESULTS>$newline";
260     close XMLOUT;
261 }
262
263 if ($tapoutput) {
264     print TAPOUT "1..$total_seen\n";
265     close TAPOUT;
266 }
267
268 # Output a summary:
269 if ($batchRun) {
270     print "Programs created along with a runtest.batch file in $batrundir\n";
271     print "Run that script and then use checktests to summarize the results\n";
272 }
273 else {
274     if ($err_count) {
275         print "$err_count tests failed out of $total_run\n";
276         print "Failing tests : $failed_tests\n";
277         if ($xmloutput) {
278             print "Details in $xmlfullfile\n";
279         }
280     }
281     else {
282         print " All $total_run tests passed!\n";
283     }
284     if ($tapoutput) {
285         print "TAP formatted results in $tapfullfile\n";
286     }
287 }
288 exit ($err_count > 0);
289 #\f
290 # ---------------------------------------------------------------------------
291 # Routines
292 #
293 # Enter a new directory and process a list file.
294 #  ProcessDir( directory-name, list-file-name )
295 sub ProcessDir {
296     my $dir = $_[0]; $dir =~ s/\/$//;
297     my $listfile = $_[1];
298     my $savedir = `pwd`;
299     my $savecurdir = $curdir;
300     my $savesrcdir = $srcdir;
301
302     chop $savedir;
303     if (substr($srcdir,0,3) eq "../") {
304       $srcdir = "../$srcdir";
305     }
306
307     print "Processing directory $dir\n" if ($verbose || $debug);
308     chdir $dir;
309     if ($dir =~ /\//) {
310         print STDERR "only direct subdirectories allowed in list files";
311     }
312     $curdir .= "/$dir";
313
314     &RunList( $listfile );
315     print "\n" if $showProgress; # Terminate line from progress output
316     chdir $savedir;
317     $curdir = $savecurdir;
318     $srcdir = $savesrcdir;
319 }
320 # ---------------------------------------------------------------------------
321 # Run the programs listed in the file given as the argument.
322 # This file describes the tests in the format
323 #  programname number-of-processes [ key=value ... ]
324 # If the second value is not given, the default value is used.
325 #
326 sub RunList {
327     my $LIST = "LIST$depth"; $depth++;
328     my $listfile = $_[0];
329     my $ResultTest = "";
330     my $InitForRun = "";
331     my $listfileSource = $listfile;
332
333     print "Looking in $curdir/$listfile\n" if $debug;
334     if (! -s "$listfile" && -s "$srcdir/$curdir/$listfile" ) {
335         $listfileSource = "$srcdir/$curdir/$listfile";
336     }
337     open( $LIST, "<$listfileSource" ) ||
338         die "Could not open $listfileSource\n";
339     while (<$LIST>) {
340         # Check for stop file
341         if (-s $stopfile) {
342             # Exit because we found a stopfile
343             print STDERR "Terminating test because stopfile $stopfile found\n";
344             last;
345         }
346         # Skip comments
347         s/#.*//g;
348         # Remove any trailing newlines/returns
349         s/\r?\n//;
350         # Remove any leading whitespace
351         s/^\s*//;
352         # Some tests require that support routines are built first
353         # This is specified with !<dir>:<target>
354         if (/^\s*\!([^:]*):(.*)/) {
355             # Hack: just execute in a subshell.  This discards any
356             # output.
357             `cd $1 && make $2`;
358             next;
359         }
360         # List file entries have the form:
361         # program [ np [ name=value ... ] ]
362         # See files errhan/testlist, init/testlist, and spawn/testlist
363         # for examples of using the key=value form
364         my @args = split(/\s+/,$_);
365         my $programname = $args[0];
366         my $np = "";
367         my $ResultTest = "";
368         my $InitForRun = "";
369         my $timeLimit  = "";
370         my $progArgs   = "";
371         my $mpiexecArgs = "$execarg";
372         my $requiresStrict = "";
373         my $requiresMPIX   = "";
374         my $progEnv    = "";
375         my $mpiVersion = "";
376         my $needs_privatization = 0;
377         my $xfail = "";
378         if ($#args >= 1) { $np = $args[1]; }
379         # Process the key=value arguments
380         for (my $i=2; $i <= $#args; $i++) {
381             if ($args[$i] =~ /([^=]+)=(.*)/) {
382                 my $key = $1;
383                 my $value = $2;
384                 if ($key eq "resultTest") {
385                     $ResultTest = $value;
386                 }
387                 elsif ($key eq "init") {
388                     $InitForRun = $value;
389                 }
390                 elsif ($key eq "timeLimit") {
391                     $timeLimit = $value;
392                 }
393                 elsif ($key eq "arg") {
394                     $progArgs = "$progArgs $value";
395                 }
396                 elsif ($key eq "mpiexecarg") {
397                     $mpiexecArgs = "$mpiexecArgs $value";
398                 }
399                 elsif ($key eq "env") {
400                     $progEnv = "$progEnv $value";
401                 }
402                 elsif ($key eq "mpiversion") {
403                     $mpiVersion = $value;
404                 }
405                 elsif ($key eq "needs_privatization") {
406                     $needs_privatization = $value;
407                 }
408                 elsif ($key eq "strict") {
409                     $requiresStrict = $value
410                 }
411                 elsif ($key eq "mpix") {
412                     $requiresMPIX = $value
413                 }
414                 elsif ($key eq "xfail") {
415                     if ($value eq "") {
416                         print STDERR "\"xfail=\" requires an argument\n";
417                     }
418                     $xfail = $value;
419                 }
420                 else {
421                     print STDERR "Unrecognized key $key in $listfileSource\n";
422                 }
423             }
424         }
425
426         # skip empty lines
427         if ($programname eq "") { next; }
428
429         # if privatization is disabled, and if the test needs it, ignore it
430         if ($needs_privatization == 1 &&
431                 $enabled_privatization != 1) {
432                 SkippedTest($programname, $np, $workdir, "requires SMPI privatization");
433                 next; 
434         }
435
436         if ($np eq "") { $np = $np_default; }
437         if ($np_max > 0 && $np > $np_max) { $np = $np_max; }
438
439         # allows us to accurately output TAP test numbers without disturbing the
440         # original totals that have traditionally been reported
441         #
442         # These "unless" blocks are ugly, but permit us to honor skipping
443         # criteria for directories as well without counting directories as tests
444         # in our XML/TAP output.
445         unless (-d $programname) {
446             $total_seen++;
447         }
448
449         # If a minimum MPI version is specified, check against the
450         # available MPI.  If the version is unknown, we ignore this
451         # test (thus, all tests will be run).
452         if ($mpiVersion ne "" && $MPIMajorVersion ne "unknown" &&
453             $MPIMinorVersion ne "unknown") {
454             my ($majorReq,$minorReq) = split(/\./,$mpiVersion);
455             if ($majorReq > $MPIMajorVersion or
456                 ($majorReq == $MPIMajorVersion && $minorReq > $MPIMinorVersion))
457             {
458                 unless (-d $programname) {
459                     SkippedTest($programname, $np, $workdir, "requires MPI version $mpiVersion");
460                 }
461                 next;
462             }
463         }
464         # Check whether strict is required by MPI but not by the
465         # test (use strict=false for tests that use non-standard extensions)
466         if (lc($requiresStrict) eq "false" && lc($testIsStrict) eq "true") {
467             unless (-d $programname) {
468                 SkippedTest($programname, $np, $workdir, "non-strict test, strict MPI mode requested");
469             }
470             next;
471         }
472
473         if (lc($testIsStrict) eq "true") {
474             # Strict MPI testing was requested, so assume that a non-MPICH MPI
475             # implementation is being tested and the "xfail" implementation
476             # assumptions do not hold.
477             $xfail = '';
478         }
479
480         if (lc($requiresMPIX) eq "true" && lc($MPIHasMPIX) eq "no") {
481             unless (-d $programname) {
482                 SkippedTest($programname, $np, $workdir, "tests MPIX extensions, MPIX testing disabled");
483             }
484             next;
485         }
486
487         if (-d $programname) {
488             # If a directory, go into the that directory and
489             # look for a new list file
490             &ProcessDir( $programname, $listfile );
491         }
492         else {
493             $total_run++;
494             if (&BuildMPIProgram( $programname, $xfail ) == 0) {
495                 if ($batchRun == 1) {
496                     &AddMPIProgram( $programname, $np, $ResultTest,
497                                     $InitForRun, $timeLimit, $progArgs,
498                                     $progEnv, $mpiexecArgs, $xfail );
499                 }
500                 else {
501                     &RunMPIProgram( $programname, $np, $ResultTest,
502                                     $InitForRun, $timeLimit, $progArgs,
503                                     $progEnv, $mpiexecArgs, $xfail );
504                 }
505             }
506             elsif ($xfail ne '') {
507                 # We expected to run this program, so failure to build
508                 # is an error
509                 $found_error = 1;
510                 $err_count++;
511             }
512             if ($batchRun == 0) {
513                 &CleanUpAfterRun( $programname );
514             }
515         }
516     }
517     close( $LIST );
518 }
519 #
520 # This routine tries to run all of the files in the current
521 # directory
522 sub ProcessImplicitList {
523     # The default is to run every file in the current directory.
524     # If there are no built programs, build and run every file
525     # WARNING: This assumes that anything executable should be run as
526     # an MPI test.
527     $found_exec = 0;
528     $found_src  = 0;
529     open (PGMS, "ls -1 |" ) || die "Cannot list directory\n";
530     while (<PGMS>) {
531         s/\r?\n//;
532         $programname = $_;
533         if (-d $programname) { next; }  # Ignore directories
534         if ($programname eq "runtests") { next; } # Ignore self
535         if ($programname eq "checktests") { next; } # Ignore helper
536         if ($programname eq "configure") { next; } # Ignore configure script
537         if ($programname eq "config.status") { next; } # Ignore configure helper
538         if (-x $programname) { $found_exec++; }
539         if ($programname =~ /\.[cf]$/) { $found_src++; }
540     }
541     close PGMS;
542
543     if ($found_exec) {
544         print "Found executables\n" if $debug;
545         open (PGMS, "ls -1 |" ) || die "Cannot list programs\n";
546         while (<PGMS>) {
547             # Check for stop file
548             if (-s $stopfile) {
549                 # Exit because we found a stopfile
550                 print STDERR "Terminating test because stopfile $stopfile found\n";
551                 last;
552             }
553             s/\r?\n//;
554             $programname = $_;
555             if (-d $programname) { next; }  # Ignore directories
556             if ($programname eq "runtests") { next; } # Ignore self
557             if (-x $programname) {
558                 $total_run++;
559                 &RunMPIProgram( $programname, $np_default, "", "", "", "", "", "", "" );
560             }
561         }
562         close PGMS;
563     }
564     elsif ($found_src) {
565         print "Found source files\n" if $debug;
566         open (PGMS, "ls -1 *.c |" ) || die "Cannot list programs\n";
567         while (<PGMS>) {
568             if (-s $stopfile) {
569                 # Exit because we found a stopfile
570                 print STDERR "Terminating test because stopfile $stopfile found\n";
571                 last;
572             }
573             s/\r?\n//;
574             $programname = $_;
575             # Skip messages from ls about no files
576             if (! -s $programname) { next; }
577             $programname =~ s/\.c//;
578             $total_run++;
579             if (&BuildMPIProgram( $programname, "") == 0) {
580                 &RunMPIProgram( $programname, $np_default, "", "", "", "", "", "", "" );
581             }
582             else {
583                 # We expected to run this program, so failure to build
584                 # is an error
585                 $found_error = 1;
586                 $err_count++;
587             }
588             &CleanUpAfterRun( $programname );
589         }
590         close PGMS;
591     }
592 }
593 # Run the program.
594 # ToDo: Add a way to limit the time that any particular program may run.
595 # The arguments are
596 #    name of program, number of processes, name of routine to check results
597 #    init for testing, timelimit, and any additional program arguments
598 # If the 3rd arg is not present, the a default that simply checks that the
599 # return status is 0 and that the output is " No Errors" is used.
600 sub RunMPIProgram {
601     my ($programname,$np,$ResultTest,$InitForTest,$timeLimit,$progArgs,$progEnv,$mpiexecArgs,$xfail) = @_;
602     my $found_error   = 0;
603     my $found_noerror = 0;
604     my $inline = "";
605
606     &RunPreMsg( $programname, $np, $curdir );
607
608     unlink "err";
609
610     # Set a default timeout on tests (3 minutes for now)
611     my $timeout = $defaultTimeLimit;
612     if (defined($timeLimit) && $timeLimit =~ /^\d+$/) {
613         $timeout = $timeLimit;
614     }
615     $ENV{"MPIEXEC_TIMEOUT"} = $timeout;
616
617     # Run the optional setup routine. For example, the timeout tests could
618     # be set to a shorter timeout.
619     if ($InitForTest ne "") {
620         &$InitForTest();
621     }
622     print STDOUT "Env includes $progEnv\n" if $verbose;
623     print STDOUT "$mpiexec $wrapparg $mpiexecArgs $np_arg $np $program_wrapper ./$programname $progArgs\n" if $verbose;
624     print STDOUT "." if $showProgress;
625     # Save and restore the environment if necessary before running mpiexec.
626     if ($progEnv ne "") {
627         %saveEnv = %ENV;
628         foreach $val (split(/\s+/, $progEnv)) {
629             if ($val =~ /([^=]+)=(.*)/) {
630                 $ENV{$1} = $2;
631             }
632             else {
633                 print STDERR "Environment variable/value $val not in a=b form\n";
634             }
635         }
636     }
637     open ( MPIOUT, "$mpiexec $wrapparg $np_arg $np $mpiexecArgs $program_wrapper ./$programname $progArgs 2>&1 |" ) ||
638         die "Could not run ./$programname\n";
639     if ($progEnv ne "") {
640         %ENV = %saveEnv;
641     }
642     if ($ResultTest ne "") {
643         # Read and process the output
644         ($found_error, $inline) = &$ResultTest( MPIOUT, $programname );
645     }
646     else {
647         if ($verbose) {
648             $inline = "$mpiexec $wrapparg $np_arg $np $program_wrapper ./$programname\n";
649         }
650         else {
651             $inline = "";
652         }
653         while (<MPIOUT>) {
654             print STDOUT $_ if $verbose;
655             # Skip FORTRAN STOP
656             if (/FORTRAN STOP/) { next; }
657             $inline .= $_;
658             if (/^==[0-9]+== ?WARNING: ASan doesn't fully support/) {
659                 next;
660             }
661             if (/^\s*No [Ee]rrors\s*$/ && $found_noerror == 0) {
662                 $found_noerror = 1;
663             }
664             if (! /^\s*No [Ee]rrors\s*$/ && !/^\s*Test Passed\s*$/) {
665                 print STDERR "Unexpected output in $programname: $_";
666                 if (!$found_error) {
667                     $found_error = 1;
668                     $failed_tests .= $programname;
669                     $failed_tests .= " ";
670                     $err_count ++;
671                 }
672             }
673         }
674         if ($found_noerror == 0) {
675             print STDERR "Program $programname exited without No Errors\n";
676             if (!$found_error) {
677                 $found_error = 1;
678                 $failed_tests .= $programname;
679                 $failed_tests .= " ";
680                 $err_count ++;
681             }
682         }
683         $rc = close ( MPIOUT );
684         if ($rc == 0) {
685             # Only generate a message if we think that the program
686             # passed the test.
687             if (!$found_error) {
688                 $run_status = $?;
689                 $signal_num = $run_status & 127;
690                 if ($run_status > 255) { $run_status >>= 8; }
691                 print STDERR "Program $programname exited with non-zero status $run_status\n";
692                 if ($signal_num != 0) {
693                     print STDERR "Program $programname exited with signal $signal_num\n";
694                 }
695                 $failed_tests .= $programname;
696                 $failed_tests .= " ";
697                 $found_error = 1;
698                 $err_count ++;
699             }
700         }
701     }
702     if ($found_error) {
703         &RunTestFailed( $programname, $np, $curdir, $inline, $xfail );
704     }
705     else {
706         &RunTestPassed( $programname, $np, $curdir, $xfail );
707     }
708     &RunPostMsg( $programname, $np, $curdir );
709 }
710
711 # This version simply writes the mpiexec command out, with the output going
712 # into a file, and recording the output status of the run.
713 sub AddMPIProgram {
714     my ($programname,$np,$ResultTest,$InitForTest,$timeLimit,$progArgs,$progEnv,$mpiexecArgs, $xfail) = @_;
715
716     if (! -x $programname) {
717         print STDERR "Could not find $programname!";
718         return;
719     }
720
721     if ($ResultTest ne "") {
722         # This test really needs to be run manually, with this test
723         # Eventually, we can update this to include handleing in checktests.
724         print STDERR "Run $curdir/$programname with $np processes and use $ResultTest to check the results\n";
725         return;
726     }
727
728     # Set a default timeout on tests (3 minutes for now)
729     my $timeout = $defaultTimeLimit;
730     if (defined($timeLimit) && $timeLimit =~ /^\d+$/) {
731         # On some systems, there is no effective time limit on
732         # individual mpi program runs.  In that case, we may
733         # want to treat these also as "run manually".
734         $timeout = $timeLimit;
735     }
736     print BATOUT "export MPIEXEC_TIMEOUT=$timeout\n";
737
738     # Run the optional setup routine. For example, the timeout tests could
739     # be set to a shorter timeout.
740     if ($InitForTest ne "") {
741         &$InitForTest();
742     }
743
744     # For non-MPICH versions of mpiexec, a timeout may require a different
745     # environment variable or command line option (e.g., for Cray aprun,
746     # the option -t <sec> must be given, there is no environment variable
747     # to set the timeout.
748     $extraArgs = "";
749     if (defined($timeoutArgPattern) && $timeoutArgPattern ne "") {
750         my $timeArg = $timeoutArgPattern;
751         $timeoutArg =~ s/<SEC>/$timeout/;
752         $extraArgs .= $timeoutArg
753     }
754
755     print STDOUT "Env includes $progEnv\n" if $verbose;
756     print STDOUT "$mpiexec $np_arg $np $extraArgs $program_wrapper ./$programname $progArgs\n" if $verbose;
757     print STDOUT "." if $showProgress;
758     # Save and restore the environment if necessary before running mpiexec.
759     if ($progEnv ne "") {
760         # Need to fix:
761         # save_NAME_is_set=is old name set
762         # save_NAME=oldValue
763         # export NAME=newvalue
764         # (run)
765         # export NAME=oldValue (if set!)
766         print STDERR "Batch output does not permit changes to environment\n";
767     }
768     # The approach here is to move the test codes to a single directory from
769     # which they can be run; this avoids complex code to change directories
770     # and ensure that the output goes "into the right place".
771     $testCount++;
772     rename $programname, "$batrundir/$programname";
773     print BATOUT "echo \"# $mpiexec $np_arg $np $extraArgs $mpiexecArgs $program_wrapper $curdir/$programname $progArgs\" > runtests.$testCount.out\n";
774     # Some programs expect to run in the same directory as the executable
775     print BATOUT "$mpiexec $np_arg $np $extraArgs $mpiexecArgs $program_wrapper ./$programname $progArgs >> runtests.$testCount.out 2>&1\n";
776     print BATOUT "echo \$? > runtests.$testCount.status\n";
777 }
778
779 #
780 # Return value is 0 on success, non zero on failure
781 sub BuildMPIProgram {
782     my $programname = shift;
783     if (! -x $programname) {
784         die "Could not find $programname. Aborting.\n";
785     }
786     return 0;
787     # THE FOLLOWING IS DISABLED.
788     my $xfail = shift;
789     my $rc = 0;
790     if ($verbose) { print STDERR "making $programname\n"; }
791     if (! -x $programname) { $remove_this_pgm = 1; }
792     else { $remove_this_pgm = 0; }
793     my $output = `make $programname 2>&1`;
794     $rc = $?;
795     if ($rc > 255) { $rc >>= 8; }
796     if (! -x $programname) {
797         print STDERR "Failed to build $programname; $output\n";
798         if ($rc == 0) {
799             $rc = 1;
800         }
801         # Add a line to the summary file describing the failure
802         # This will ensure that failures to build will end up
803         # in the summary file (which is otherwise written by the
804         # RunMPIProgram step)
805         &RunPreMsg( $programname, $np, $curdir );
806         &RunTestFailed( $programname, $np, $curdir, "Failed to build $programname; $output", $xfail );
807         &RunPostMsg( $programname, $np, $curdir );
808     }
809     return $rc;
810 }
811
812 sub CleanUpAfterRun {
813     my $programname = $_[0];
814
815     # Check for that this program has exited.  If it is still running,
816     # issue a warning and leave the application.  Of course, this
817     # check is complicated by the lack of a standard access to the
818     # running processes for this user in Unix.
819     @stillRunning = &FindRunning( $programname );
820
821     if ($#stillRunning > -1) {
822         print STDERR "Some programs ($programname) may still be running:\npids = ";
823         for (my $i=0; $i <= $#stillRunning; $i++ ) {
824             print STDERR $stillRunning[$i] . " ";
825         }
826         print STDERR "\n";
827         # Remind the user that the executable remains; we leave it around
828         # to allow the programmer to debug the running program, for which
829         # the executable is needed.
830         print STDERR "The executable ($programname) will not be removed.\n";
831     }
832     else {
833         if ($remove_this_pgm && $clean_pgms) {
834             unlink $programname, "$programname.o";
835         }
836         $remove_this_pgm = 0;
837     }
838 }
839 # ----------------------------------------------------------------------------
840 sub FindRunning {
841     my $programname = $_[0];
842     my @pids = ();
843
844     my $logname = $ENV{'USER'};
845     my $pidloc = 1;
846     my $rc = open PSFD, "ps auxw -U $logname 2>&1 |";
847
848     if ($rc == 0) {
849         $rc = open PSFD, "ps -fu $logname 2>&1 |";
850     }
851     if ($rc == 0) {
852         print STDERR "Could not execute ps command\n";
853         return @pids;
854     }
855
856     while (<PSFD>) {
857         if (/$programname/) {
858             @fields = split(/\s+/);
859             my $pid = $fields[$pidloc];
860             # Check that we've found a numeric pid
861             if ($pid =~ /^\d+$/) {
862                 $pids[$#pids + 1] = $pid;
863             }
864         }
865     }
866     close PSFD;
867
868     return @pids;
869 }
870 # ----------------------------------------------------------------------------
871 #
872 # TestStatus is a special test that reports success *only* when the
873 # status return is NONZERO
874 sub TestStatus {
875     my $MPIOUT = $_[0];
876     my $programname = $_[1];
877     my $found_error = 0;
878
879     my $inline = "";
880     while (<$MPIOUT>) {
881         #print STDOUT $_ if $verbose;
882         # Skip FORTRAN STOP
883         if (/FORTRAN STOP/) { next; }
884         $inline .= $_;
885         # ANY output is an error. We have the following output
886         # exception for the Hydra process manager.
887         if (/=*/) { last; }
888         if (! /^\s*$/) {
889             print STDERR "Unexpected output in $programname: $_";
890             if (!$found_error) {
891                 $found_error = 1;
892                 $err_count ++;
893             }
894         }
895     }
896     $rc = close ( MPIOUT );
897     if ($rc == 0) {
898         $run_status = $?;
899         $signal_num = $run_status & 127;
900         if ($run_status > 255) { $run_status >>= 8; }
901     }
902     else {
903         # This test *requires* non-zero return codes
904         if (!$found_error) {
905             $found_error = 1;
906             $err_count ++;
907         }
908         $inline .= "$mpiexec returned a zero status but the program returned a nonzero status\n";
909     }
910     return ($found_error,$inline);
911 }
912 #
913 # TestTimeout is a special test that reports success *only* when the
914 # status return is NONZERO and there are no processes left over.
915 # This test currently checks only for the return status.
916 sub TestTimeout {
917     my $MPIOUT = $_[0];
918     my $programname = $_[1];
919     my $found_error = 0;
920
921     my $inline = "";
922     while (<$MPIOUT>) {
923         #print STDOUT $_ if $verbose;
924         # Skip FORTRAN STOP
925         if (/FORTRAN STOP/) { next; }
926         $inline .= $_;
927         if (/[Tt]imeout/) { next; }
928         # Allow 'signaled with Interrupt' (see gforker mpiexec)
929         if (/signaled with Interrupt/) { next; }
930         # Allow 'job ending due to env var MPIEXEC_TIMEOUT' (mpd)
931         if (/job ending due to env var MPIEXEC_TIMEOUT/) { next; }
932         # Allow 'APPLICATION TIMED OUT' (hydra)
933         if (/\[mpiexec@.*\] APPLICATION TIMED OUT/) { last; }
934         # ANY output is an error (other than timeout)
935         if (! /^\s*$/) {
936             print STDERR "Unexpected output in $programname: $_";
937             if (!$found_error) {
938                 $found_error = 1;
939                 $err_count ++;
940             }
941         }
942     }
943     $rc = close ( MPIOUT );
944     if ($rc == 0) {
945         $run_status = $?;
946         $signal_num = $run_status & 127;
947         if ($run_status > 255) { $run_status >>= 8; }
948     }
949     else {
950         # This test *requires* non-zero return codes
951         if (!$found_error) {
952             $found_error = 1;
953             $err_count ++;
954         }
955         $inline .= "$mpiexec returned a zero status but the program returned a nonzero status\n";
956     }
957     #
958     # Here should go a check of the processes
959     # open( PFD, "ps -fu $LOGNAME | grep -v grep | grep $programname |" );
960     # while (<PFD>) {
961     #
962     # }
963     # close PFD;
964     return ($found_error,$inline);
965 }
966 #
967 # TestErrFatal is a special test that reports success *only* when the
968 # status return is NONZERO; it ignores error messages
969 sub TestErrFatal {
970     my $MPIOUT = $_[0];
971     my $programname = $_[1];
972     my $found_error = 0;
973
974     my $inline = "";
975     while (<$MPIOUT>) {
976         #print STDOUT $_ if $verbose;
977         # Skip FORTRAN STOP
978         if (/FORTRAN STOP/) { next; }
979         $inline .= $_;
980         # ALL output is allowed.
981     }
982     $rc = close ( MPIOUT );
983     if ($rc == 0) {
984         $run_status = $?;
985         $signal_num = $run_status & 127;
986         if ($run_status > 255) { $run_status >>= 8; }
987     }
988     else {
989         # This test *requires* non-zero return codes
990         if (!$found_error) {
991             $found_error = 1;
992             $err_count ++;
993         }
994         $inline .= "$mpiexec returned a zero status but the program returned a nonzero status\n";
995     }
996     return ($found_error,$inline);
997 }
998
999 # ----------------------------------------------------------------------------
1000 # Output routines:
1001 #  RunPreMsg( programname, np, workdir ) - Call before running a program
1002 #  RunTestFailed, RunTestPassed - Call after test
1003 #  RunPostMsg               - Call at end of each test
1004 #
1005 sub RunPreMsg {
1006     my ($programname,$np,$workdir) = @_;
1007     if ($xmloutput) {
1008         print XMLOUT "<MPITEST>$newline<NAME>$programname</NAME>$newline";
1009         print XMLOUT "<NP>$np</NP>$newline";
1010         print XMLOUT "<WORKDIR>$workdir</WORKDIR>$newline";
1011     }
1012 }
1013 sub RunPostMsg {
1014     my ($programname, $np, $workdir) = @_;
1015     if ($xmloutput) {
1016         print XMLOUT "</MPITEST>$newline";
1017     }
1018 }
1019 sub RunTestPassed {
1020     my ($programname, $np, $workdir, $xfail) = @_;
1021     if ($xmloutput) {
1022         print XMLOUT "<STATUS>pass</STATUS>$newline";
1023     }
1024     if ($tapoutput) {
1025         my $xfailstr = '';
1026         if ($xfail ne '') {
1027             $xfailstr = " # TODO $xfail";
1028         }
1029         print TAPOUT "ok ${total_run} - $workdir/$programname ${np}${xfailstr}\n";
1030     }
1031 }
1032 sub RunTestFailed {
1033     my $programname = shift;
1034     my $np = shift;
1035     my $workdir = shift;
1036     my $output = shift;
1037     my $xfail = shift;
1038
1039     if ($xmloutput) {
1040         my $xout = $output;
1041         # basic escapes that wreck the XML output
1042         $xout =~ s/</\*AMP\*lt;/g;
1043         $xout =~ s/>/\*AMP\*gt;/g;
1044         $xout =~ s/&/\*AMP\*amp;/g;
1045         $xout =~ s/\*AMP\*/&/g;
1046         # TODO: Also capture any non-printing characters (XML doesn't like them
1047         # either).
1048         print XMLOUT "<STATUS>fail</STATUS>$newline";
1049         print XMLOUT "<TESTDIFF>$newline$xout</TESTDIFF>$newline";
1050     }
1051
1052     if ($tapoutput) {
1053         my $xfailstr = '';
1054         if ($xfail ne '') {
1055             $xfailstr = " # TODO $xfail";
1056         }
1057         print TAPOUT "not ok ${total_run} - $workdir/$programname ${np}${xfailstr}\n";
1058         print TAPOUT "  ---\n";
1059         print TAPOUT "  Directory: $workdir\n";
1060         print TAPOUT "  File: $programname\n";
1061         print TAPOUT "  Num-procs: $np\n";
1062         print TAPOUT "  Date: \"" . localtime . "\"\n";
1063
1064         # The following would be nice, but it leads to unfortunate formatting in
1065         # the Jenkins web output for now.  Using comment lines instead, since
1066         # they are easier to read/find in a browser.
1067 ##        print TAPOUT "  Output: |\n";
1068 ##        # using block literal format, requires that all chars are printable
1069 ##        # UTF-8 (or UTF-16, but we won't encounter that)
1070 ##        foreach my $line (split m/\r?\n/, $output) {
1071 ##            chomp $line;
1072 ##            # 4 spaces, 2 for TAP indent, 2 more for YAML block indent
1073 ##            print TAPOUT "    $line\n";
1074 ##        }
1075
1076         print TAPOUT "  ...\n";
1077
1078         # Alternative to the "Output:" YAML block literal above.  Do not put any
1079         # spaces before the '#', this causes some TAP parsers (including Perl's
1080         # TAP::Parser) to treat the line as "unknown" instead of a proper
1081         # comment.
1082         print TAPOUT "## Test output (expected 'No Errors'):\n";
1083         foreach my $line (split m/\r?\n/, $output) {
1084             chomp $line;
1085             print TAPOUT "## $line\n";
1086         }
1087     }
1088 }
1089
1090 sub SkippedTest {
1091     my $programname = shift;
1092     my $np = shift;
1093     my $workdir = shift;
1094     my $reason = shift;
1095
1096     # simply omit from the XML output
1097
1098     if ($tapoutput) {
1099         print TAPOUT "ok ${total_seen} - $workdir/$programname $np  # SKIP $reason\n";
1100     }
1101 }
1102
1103 # ----------------------------------------------------------------------------
1104 # Alternate init routines
1105 sub InitQuickTimeout {
1106     $ENV{"MPIEXEC_TIMEOUT"} = 10;
1107 }