Logo AND Algorithmique Numérique Distribuée

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