-
Notifications
You must be signed in to change notification settings - Fork 2
/
gmxtest.pl
executable file
·1926 lines (1776 loc) · 68.9 KB
/
gmxtest.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
use strict;
use Cwd qw(getcwd);
use File::Basename;
use File::Copy qw(copy);
use List::Util qw[min max];
my $srcdir = dirname(__FILE__);
my $rundir = getcwd();
$srcdir = $rundir unless ($srcdir ne '.');
#change directory to script location
use lib dirname(__FILE__);
use gmxFile::Path qw(remove_tree);
use List::Util qw(sum);
#disable quotes as they could screw up pattern matching
$ENV{GMX_NO_QUOTES}='NO';
#disable backups because otherwise tests fail after being run 99x
if(!defined $ENV{GMX_MAXBACKUP}) {
$ENV{GMX_MAXBACKUP}=-1
}
my $tmpi_ranks = 0;
my $omp_threads = 0;
my $npme_ranks = -1;
my $mpi_ranks = 0;
my $double = 0;
my $crosscompiling = 0;
my $bluegene = 0;
my $keep_files = 0;
my $verbose = 5;
my $xml = 0;
# energy file comparision tolerance (potentials, not virials or pressure)
my $etol_rel = 0.001;
my $etol_abs = 0.05;
# topology comparision tolerance
my $ttol_rel = 0.0001;
my $ttol_abs = 0.001;
my $suffix = '';
my $autosuffix = '1';
my $prefix = '';
# virial - this tests the shifted force contribution.
# However, it is a sum of very many large terms, so it is
# often numerically imprecise.
my $virtol_rel = 0.01;
my $virtol_abs = 0.1;
# force tolerance is measured by calculating scalar products.
# tolerance 0.001 means the scalar product between the two
# compared forces should be at least 0.999.
my $ftol_rel = 0.001;
my $ftol_abs = 0.05;
my $ftol_sprod = 0.001;
# global variables to flag whether to explain some situations to the user
my $addversionnote = 0;
my $only_subdir = qr/.*/;
my $tolerance_factor = 1;
# trickery for program and reference file names, command line options, etc.
my $mdprefix = sub {''};
my $mdparams = '';
my $gpu_id = '';
my $ref = '';
my $mpirun = 'mpirun';
my $parse_cmd = '';
my $gmx_cmd = "gmx";
my $mdrun_cmd = "";
my %progs = ( 'grompp' => '',
'mdrun' => '',
'pdb2gmx' => '',
'check' => '',
'editconf' => '',
'make_edi' => '' );
# Names for filenames used by individual test cases to limit the
# amount of parallelism that mdrun can attempt
my $max_openmp_threads_filename = 'max-openmp-threads';
my $max_mpi_ranks_filename = "max-mpi-ranks";
# List of all the generic subdirectories of tests; pdb2gmx and essentialdynamics are treated
# separately.
my @all_dirs = ('complex', 'freeenergy', 'rotation', 'extra');
# Return the command-line option specifying the number of separate
# PME ranks to use, if appropriate (ie the .mdp file specifies a
# form of PME and the user specified the number of separate PME ranks,
# which can be zero). Otherwise, return an empty command-line string.
sub specify_number_of_pme_ranks {
my ($num_ranks, $npme_ranks, $grompp_mdp, $pp_ranks_ref) = @_;
# Only try -npme if using some kind of PME, with enough ranks and
# the user asked for it. Note that mdrun -npme > 0 is supported
# with at least 2 ranks.
if ((find_in_file("(coulomb[-_]?type|vdw[-_]?type)\\s*=\\s*(pme|PME)", $grompp_mdp) > 0) &&
(($npme_ranks == 0) ||
(($npme_ranks > 0) && ($num_ranks >= 2))))
{
# Specify number of PME-only ranks
$$pp_ranks_ref = $num_ranks - $npme_ranks;
return "-npme $npme_ranks";
}
else {
# Use default behaviour for MPMD with PME
$$pp_ranks_ref = $num_ranks;
return "";
}
}
# Return the number of GPUs detected by mdrun in the md.log file
sub find_number_of_gpus() {
my $number_of_gpus = 0;
# Note that tests for mdrun -cpi write a log file that has the
# second possible name.
my @possible_log_file_names = ("md.log", "md.part0002.log");
my $found_a_log_file = 0;
foreach my $logfile (@possible_log_file_names)
{
if (!open(FIN, "<", $logfile))
{
# Didn't find a log file with this name, try the next
# name.
next;
}
$found_a_log_file++;
while(my $line=<FIN>)
{
if ($line =~ /^\s+Number of GPUs detected: (.*)/) {
$number_of_gpus = $1;
last;
}
# If we have finished reading the header and haven't
# found a report of the number of GPUs, there's no
# point continuing to look.
if ($line =~ /^Started mdrun/)
{
last;
}
}
# No need to try any more log file names, one was already
# found.
last;
}
if ($found_a_log_file == 0)
{
print STDERR "Could not find any log files to open.\nAssuming 0 GPUs were detected.\n";
}
return $number_of_gpus;
}
sub setup_vars()
{
# We assume that the name of executables match the pattern
# ${prefix}mdrun[_mpi][_d]${suffix} where ${prefix} and ${suffix} are
# as defined above (or over-ridden on the command line), "_d" indicates
# a double-precision version, and (only in the case of mdrun) "_mpi"
# indicates a parallel version compiled with MPI.
if ( $mpi_ranks > 0 ) {
if ($autosuffix) {
$gmx_cmd .= "_mpi";
}
if ( $bluegene > 0 )
{
# edit the next line if you need to customize the call to mpirun
$mdprefix = sub { "$mpirun -n $_[0] --cwd " . getcwd() . " :" };
} elsif ( $mpirun =~ /(ap|s)run/ ) {
$mdprefix = sub { "$mpirun -n $_[0]" };
} else {
# edit the next line if you need to customize the call to mpirun
$mdprefix = sub { "$mpirun -np $_[0] -wdir " . getcwd() };
}
}
if ($autosuffix && ( $double > 0)) {
$gmx_cmd .= "_d";
}
foreach my $prog ( keys %progs ) {
$progs{$prog} = "$gmx_cmd$suffix $prog";
}
if ( "$mdrun_cmd" ne "" ) {
$progs{"mdrun"} = $mdrun_cmd;
}
$ref = 'reference_' . ($double > 0 ? 'd' : 's');
# now do -tight or -relaxed stuff
foreach my $var ( $etol_rel, $etol_abs, $ttol_rel, $ttol_abs, $virtol_rel, $virtol_abs, $ftol_rel, $ftol_abs, $ftol_sprod ) {
$var *= $tolerance_factor;
}
}
# Wrapper function to call system(), and then perhaps a callback based on the
# value of the return code. When no callback exists, a generic error is
# displayed
sub do_system
{
my $command = shift;
my $normalreturn = shift;
my $callback = shift;
$normalreturn = 0 unless(defined $normalreturn);
if ( $verbose > 2 ) {
print "$command\n";
}
my $returnvalue = system($command) >> 8;
if ($normalreturn != $returnvalue)
{
if (defined $callback)
{
$returnvalue = &$callback($returnvalue);
}
if($normalreturn != $returnvalue)
{
print "\nAbnormal return value for '$command' was $returnvalue\n";
}
}
return $returnvalue;
}
# Built-in replacement for some of grep
# Returns number of matches for pattern (1st arg) in file (2nd arg)
# If the file is grompp.mdp the search is case-insensitive.
# Optionally writes matching lines to file named in 3rd arg, which
# simulates "grep pattern file > redirectfile"
sub find_in_file {
my $pattern = shift;
my $filename_to_search = shift;
my $filename_for_redirect = shift;
my $return=0;
my $case_sensitive=1;
defined($filename_to_search) || die "find_in_file: Missing argument\n";
my $do_redirect = defined $filename_for_redirect;
if ($do_redirect) {
open(REDIRECT, ">$filename_for_redirect") || die "Could not open redirect file '$filename_for_redirect'\n";
}
# If the file doesn't exist, then the pattern doesn't
# match. Fortunately, the clients of find_in_file that care about
# the number of matches found use the name of a file that always
# exists.
open(FILE,$filename_to_search) || return 0;
if ($filename_to_search =~ "grompp.mdp\$") {
$case_sensitive=0;
}
while(<FILE>) {
if ($case_sensitive ? /$pattern/ : /$pattern/i) {
$return++;
if ($do_redirect) {
print REDIRECT $_;
}
}
}
close(FILE) || die "Could not close file '$filename_to_search'\n";
if ($do_redirect) {
close(REDIRECT) || die "Could not close file '$filename_for_redirect'\n";
}
return $return;
}
sub check_force($$)
{
my $traj = shift;
my $reftrr = shift;
my $cfor = "checkforce.out";
my $cfor2 = "checkforce.err";
my $nerr_force = 0;
do_system("$progs{'check'} -f $reftrr -f2 $traj -tol $ftol_rel -abstol $ftol_abs >$cfor 2>$cfor2", 0,
sub { print "\ngmx check failed on the .edr file while checking the forces\n"; $nerr_force = 1; });
open(FIN,"$cfor");
while(my $line=<FIN>)
{
if ($line =~ /^b([^ ]*) .*TRUE/) {
print("Existence of $1 doesn't match!\n");
$nerr_force++;
next;
}elsif ($line =~ /^End of file on/) {
print("Different number of frames!\n");
$nerr_force++;
next;
}elsif ($line =~ /^\r?$/ || $line =~ /^[xv]\[/ || $line =~ /Both files read correctly/ ) {
next;
}elsif (!($line =~ /^f\[/)) {
print("Unknown Error: $line!\n");
$nerr_force++;
next;
}
my @ll=split("[()]",$line);
my @f1=split(" ",$ll[1]);
my @f2=split(" ",$ll[3]);
my $l1 = sqrt($f1[0]*$f1[0]+$f1[1]*$f1[1]+$f1[2]*$f1[2]);
my $l2 = sqrt($f2[0]*$f2[0]+$f2[1]*$f2[1]+$f2[2]*$f2[2]);
my $denominator = $l1 * $l2;
if (0.0 == $denominator &&
$l1 != $l2) {
# Hopefully this means there was an error somewhere,
# rather than an atomic force that was (correctly) binary
# identical to zero...
$nerr_force++;
} else {
my $sprod = ($f1[0]*$f2[0]+$f1[1]*$f2[1]+$f1[2]*$f2[2]) / $denominator;
if( $sprod < (1.0-$ftol_sprod))
{
$nerr_force = $nerr_force + 1;
}
}
}
close(FIN);
if ($nerr_force == 0 && !$keep_files) {
unlink($cfor,$cfor2);
}
return $nerr_force;
}
sub check_virial($)
{
my $refedr = shift;
my $cvir = "checkvir.out";
my $cvir2 = "checkvir.err";
my $nerr_vir = 0;
do_system("$progs{'check'} -e $refedr -e2 ener -tol $virtol_rel -abstol $virtol_abs -lastener Vir-ZZ >$cvir 2>$cvir2", 0,
sub { print "\ngmx check failed on the .edr file while checking the virial\n"; $nerr_vir = 1; });
open(VIN,"$cvir");
while(my $line=<VIN>)
{
next unless $line =~ /Vir-/;
my @v1=split(" ",substr($line,26,14)); #TODO replace substr with split to make more reliable
my @v2=split(" ",substr($line,52,13)); #if again reactiving check_virial
my $diff = abs($v1[0]-$v2[0]);
my $norm = abs($v1[0])+abs($v2[0]);
if((2*$diff > $virtol_rel *$norm) && ($diff>$virtol_abs))
{
$nerr_vir = $nerr_vir + 1;
}
}
close(VIN);
if ($nerr_vir == 0 && !$keep_files) {
unlink($cvir,$cvir2);
}
return $nerr_vir;
}
sub check_xvg {
my $refx = shift;
my $kkk = shift;
my $ndx = shift;
my $nerr = 0;
if ((-f $refx) && (-f $kkk)) {
open(REF,"$refx") || die "Could not open file '$refx'\n";
open(KKK,"$kkk") || die "Could not open file '$kkk'\n";
my $n = 0;
my $header = 0;
while (my $line = <REF>) {
my $line2=<KKK>;
if (not defined($line2)){#REF has more lines
$nerr++;
next;
}
next if $line =~ /^[@#]/;
chomp($line);
chomp($line2);
my @tmp = split(' ',$line);
my @tmp2 = split(' ',$line2);
my $x1 = $tmp[$ndx];
my $x2 = $tmp2[$ndx];
my $error;
my $tol;
if ($x1+$x2==0) {
$error = abs($x1-$x2);
$tol = $etol_abs;
} else {
$error = abs(($x1-$x2)/($x1+$x2));
$tol = $etol_rel;
}
if ($error > $tol) {
$nerr++;
if (!$header) {
$header = 1;
print("Here follows a list of the lines in $refx and $kkk which did not\npass the comparison test within tolerance $etol_rel\nIndex Reference This test Error Description\n");
}
printf("%4d %10g %10g %10g %s\n",$n+1,$x1,$x2, $error, 'unknown');
}
$n++;
}
while (my $line2=<KKK>) {#KKK has more lines
$nerr++
}
close REF;
close KKK;
}
return $nerr;
}
# Callback used only when mdrun has returned an error, so we can work
# out how to try to call it so it works
sub how_should_we_rerun_mdrun {
my ($tmpi_ranks_ref, $omp_threads_ref, $mpi_ranks_ref, $npme_ranks_ref, $gpu_id_ref, $update_option_ref) = @_;
# Is it because we are using too many cores, or trying to use -nt
# with a reference build, or running a test that does not run in
# parallel?
open(MDOUT,"mdrun.out");
my(@lines) = <MDOUT>;
close(MDOUT);
my $rerun = -1;
foreach my $line (@lines) {
if ($line =~ /There is no domain decomposition for/) {
my $new_mpi_ranks = 0;
if ($$mpi_ranks_ref == 8 or $$tmpi_ranks_ref == 8) {
my $this_num_ranks = undef;
if ($$mpi_ranks_ref > 0) {
$this_num_ranks = $$mpi_ranks_ref;
} else {
$this_num_ranks = $$tmpi_ranks_ref;
}
my $this_pp_ranks = undef;
my $this_grompp_mdp = "grompp.mdp";
my $this_npme_opt = specify_number_of_pme_ranks($this_num_ranks, $$npme_ranks_ref, $this_grompp_mdp, \$this_pp_ranks);
if ($this_npme_opt eq "") {
$new_mpi_ranks = 2;
} else {
#we have at least one pme rank, so we need to have more total ranks to ensure
#that we use DD
$new_mpi_ranks = 2 + $$npme_ranks_ref;
}
}
else {
$new_mpi_ranks = 8;
}
if ($$mpi_ranks_ref > 0) {
$$mpi_ranks_ref = $new_mpi_ranks;
} else {
$$tmpi_ranks_ref = ${new_mpi_ranks};
}
print ("Mdrun cannot use the requested (or automatic) number of ranks, retrying with $new_mpi_ranks.\n");
$rerun = 1;
last;
}
elsif ($line =~ /Setting the number of thread-MPI .* is only supported/) {
printf ("Mdrun was compiled without thread-MPI or MPI support. Retrying with only 1 rank.\n");
$$tmpi_ranks_ref = 0;
$rerun = 1;
last;
}
elsif ($line =~ /OpenMP threads were requested/) {
$$omp_threads_ref = 8;
print ("Mdrun cannot use the requested (or automatic) number of OpenMP threads, retrying with $$omp_threads_ref.\n");
$rerun = 1;
last;
}
elsif ($line =~ /Domain decomposition does not support simple neighbor searching/) {
my $new_mpi_ranks = 1;
print ("Mdrun cannot use the requested (or automatic) number of MPI ranks, retrying with ${new_mpi_ranks}.\n");
if ($$mpi_ranks_ref > 0) {
$$mpi_ranks_ref = $new_mpi_ranks;
} else {
$$tmpi_ranks_ref = ${new_mpi_ranks};
}
$$gpu_id_ref = substr($$gpu_id_ref, 0, $new_mpi_ranks);
$rerun = 1;
last;
}
elsif ($line =~ "Your choice of .* MPI rank.* and the use of .* total threads .* leads to the use of .* OpenMP threads, whereas we expect the optimum to be with more MPI ranks" ||
$line =~ "Your choice of number of MPI ranks and amount of resources results in using" ) {
# On large nodes this error needs to be handled. It can
# be converted to a warning with setting the environment
# variable GMX_BYPASS_EFFICIENCY_CHECK, but we don't want
# anybody testing GROMACS to have to do that, whether with
# "make check" or stand-alone.
my $new_omp_threads = 6; # matches nthreads_omp_mpi_target_max in the code
if ($$omp_threads_ref > 6)
{
$$omp_threads_ref = $new_omp_threads;
}
else
{
# Do something that will work!
$$omp_threads_ref = 1;
}
$rerun = 1;
last;
}
elsif ($line =~ "Update task on the GPU was required") {
# With the update calculation on the GPU it may happen that a
# specific run can not be performed due to limitations of the current
# feature set. So we detect this and redirect mdrun to try the same
# but with the update forced to run on the CPU.
print ("Mdrun was unable to use the GPU for update calculations, defaulting to CPU update.\n");
$$update_option_ref = "-update cpu";
$rerun = 1;
last;
}
elsif ($line =~ "There were .* GPU tasks found on.*, but .* GPU.*were available") {
# It can happen that we want to run set-ups where the original intent is to test
# that rank separation works between multiple PP and PME ranks and multiple GPUs.
# This can cause issues when running inputs without PME support, as the task separation
# might not be possible for the same configuration.
# In such cases, we change the settings to get the maximum number of GPUs as the
# number of ranks to use.
print ("Mdrun was not able to distribute the requested non-bonded tasks to the available GPUs.\n");
my $num_ranks = undef;
if ($$mpi_ranks_ref > 0) {
$num_ranks = $$mpi_ranks_ref;
} else {
$num_ranks = $$tmpi_ranks_ref;
}
my $num_gpus = find_number_of_gpus();
my $pp_ranks = undef;
my $grompp_mdp = "grompp.mdp";
my $npme_opt = specify_number_of_pme_ranks($num_ranks, $$npme_ranks_ref, $grompp_mdp, \$pp_ranks);
if ($npme_opt eq "" && $$npme_ranks_ref > 0)
{
if ($$mpi_ranks_ref > 0) {
$$mpi_ranks_ref = $num_gpus;
} else {
$$tmpi_ranks_ref = ${num_gpus};
}
$$npme_ranks_ref = 0;
$rerun = 1;
print ("Will try again with $num_ranks different tasks and without dedicated PME task.\n");
}
else
{
# We need to also take into account cases where the auto assignment fails
# and we need to provide a manual task assignment. But if we got a user provided
# task assignment we just tell them that it doesn't work that way.
if ($$gpu_id_ref)
{
print ("Your gpu task assignment is not compatable, please adjust it!\n");
}
else
{
$$gpu_id_ref = substr($$gpu_id_ref, 0, $num_gpus);
print ("Will try again with following task assignment $$gpu_id_ref!\n");
}
}
last;
}
}
return $rerun;
}
sub run_mdrun {
# Copy all parameters by value, which is useful so we can modify
# them if we need to, and have the changes local to this test
my ($tmpi_ranks, $omp_threads, $mpi_ranks, $npme_ranks, $pme_option, $update_option, $gpu_id, $mdprefix, $mdparams, $grompp_mdp) = @_;
# Only one of tmpi_ranks or mpi_ranks may be greater than zero, but
# this is checked for sanity after parsing user input.
# Set up and enforce the maximum number of OpenMP threads to
# try for this test case
if ( -f $max_openmp_threads_filename )
{
open my $fh, '<', $max_openmp_threads_filename or die "error opening $max_openmp_threads_filename: $!";
$omp_threads = do { local $/; <$fh> };
chomp $omp_threads;
}
# Set up and enforce the maximum number of MPI ranks to try
# for this test case
if ( -f $max_mpi_ranks_filename ) {
open my $fh, '<', $max_mpi_ranks_filename or die "error opening $max_mpi_ranks_filename: $!";
my $max_ranks = do { local $/; <$fh> };
chomp $max_ranks;
if ($mpi_ranks > 0) {
$mpi_ranks = ($mpi_ranks < $max_ranks) ? $mpi_ranks : $max_ranks;
} elsif ($tmpi_ranks > 0) {
$tmpi_ranks = ($tmpi_ranks < $max_ranks) ? $tmpi_ranks : $max_ranks;
} else {
# The user specified nothing, but the default must
# still honour this maximum!
#
# If mdrun is serial, this code will trigger a second run
# where -ntmpi will not be set. This is not ideal, but
# only a few test cases specify the maximum number of
# ranks, and pretty much only Jenkins should compile
# the serial version of mdrun. In the absence of an
# explicit serial mode for this script, it should lead
# to a net improvement in Jenkins throughput.
$tmpi_ranks = $max_ranks;
}
}
# There is no general way to make all the tests pass by default on
# all possible hardware, because there's not yet a good way for
# the harness to find out about the hardware and mdrun
# configuration in time to moderate the user (or default) choice
# for various parallelism settings. Some tests can't run with more
# than a few domains, yet machines might have many cores and/or
# inconvenient ratios of cores to GPUs, or mdrun might be
# deliberately compiled without OpenMP, etc. So we're forced to
# inspect the output of mdrun when it fails, and try to react
# in an appropriate way.
#
# First, we try running with whatever number of whatever the
# user/default wants, then adapt to the error messages, for
# at most three attempts.
for my $attempt (0 .. 2)
{
my $ntmpi_opt = '';
my $ntomp_opt = '';
if (0 < $tmpi_ranks) {
$ntmpi_opt = "-ntmpi $tmpi_ranks";
}
if ($omp_threads > 0) {
$ntomp_opt = "-ntomp $omp_threads";
}
my $pp_ranks = undef;
my $num_ranks = $tmpi_ranks < $mpi_ranks ? $mpi_ranks : $tmpi_ranks;
my $npme_opt = specify_number_of_pme_ranks($num_ranks, $npme_ranks, $grompp_mdp, \$pp_ranks);
# If there's no GPUs, or no ability to use them then passing
# -gpu_id to mdrun results in an error. The caller of
# gmxtest.pl is responsible for using -gpu_id only when that
# makes sense for the build and the hardware it is running
# upon, but here we should only pass it to mdrun when that can
# succeed.
#
# Note that PME can technically also run on the GPU, but
# anyway it requires that NB can run on the GPU, so testing
# for the latter is enough for deciding whether to pass
# -gpu_id through from the gmxtest.pl command line.
#
# TODO It is not elegant to name the annotation file
# "no-nb-gpu-support" and then only use it in the negative
# sense, but removing that annotation file and instead adding
# "supports-nb-on-gpu" to all the other test cases is
# something that we should fix later.
my $test_supports_nb_on_gpu = ! -f "no-nb-gpu-support";
my $nb_task_assignment_opt;
if($test_supports_nb_on_gpu)
{
$nb_task_assignment_opt = $gpu_id ? "-gpu_id $gpu_id" : "";
}
else
{
$nb_task_assignment_opt = "-nb cpu";
}
my $command = $mdprefix->($mpi_ranks)
. " $progs{'mdrun'} $ntmpi_opt $npme_opt $pme_option $update_option $nb_task_assignment_opt $ntomp_opt $mdparams >mdrun.out 2>&1";
#do_system using the special callback will return:
#1 for known error: rerun
#0 exit value was 0 (and thus this callback wasn't called)
#-1 unknown error
my $nerror = do_system($command, 0,
sub { how_should_we_rerun_mdrun(\$tmpi_ranks, \$omp_threads, \$mpi_ranks, \$npme_ranks, \$gpu_id, \$update_option) });
if ($nerror > 0) {
print "Retrying mdrun with better settings...\n";
} else {
return $nerror;
}
}
# mdrun always failed, so pass the error upstream
return 1;
}
# Parameters:
# dir : The directory in which the test will run, and output files will be created/found
# input_dir : The relative path to the directory in which the input and reference files can be found (normally ".")
# test_name : The name of the test case to report to the user
#
# Returns : 1 for success, 0 for failure
sub test_case {
my ($dir, $input_dir, $test_name) = @_;
my $success = 0;
my $cwd = getcwd();
mkdir($dir) unless (-d $dir);
chdir($dir);
if ($verbose > 1) {
print "Testing $test_name . . . ";
}
my $nerror = 0;
my $ndx = "";
if ( -f "$input_dir/index.ndx" ) {
$ndx = "-n $input_dir/index";
}
my $grompp_mdp = "$input_dir/grompp.mdp";
my $grompp_out = "grompp.out";
my $grompp_err = "grompp.err";
$nerror = do_system("$progs{'grompp'} -f $grompp_mdp -c $input_dir/conf -r $input_dir/conf -p $input_dir/topol -ref $input_dir/rotref -maxwarn 10 $ndx >$grompp_out 2>$grompp_err");
my @error_detail;
if (! -f "topol.tpr") {
print ("No topol.tpr file in $dir. grompp failed\n");
$nerror = 1;
}
if ($nerror == 0) {
my $reftpr = "$input_dir/${ref}.tpr";
if (! -f $reftpr) {
print ("No $reftpr file for $test_name\n");
print ("This means you are not really testing $test_name\n");
copy('topol.tpr', $reftpr);
} else {
my $tprout="checktpr.out";
my $tprerr="checktpr.err";
do_system("$progs{'check'} -s1 $reftpr -s2 topol.tpr -tol $ttol_rel -abstol $ttol_abs >$tprout 2>$tprerr", 0,
sub { print "Comparison of input .tpr files failed!\n"; $nerror = 1; });
$nerror |= find_in_file("^(?!comparing|WARNING)","$tprout");
if ($nerror > 0) {
push(@error_detail, ("checktpr.out", "checktpr.err"));
print "topol.tpr file different from $reftpr. Check files in $dir for $test_name\n";
}
if (find_in_file ('reading tpx file (reference_[sd].tpr) version .* with version .* program',"$tprout") > 0) {
print "\nThe GROMACS version being tested may be older than the reference version.\nPlease see the note at end of this output.\n";
$addversionnote = 1;
}
if ($nerror == 0 && !$keep_files) {
unlink($tprout,$tprerr);
}
}
} else {
push(@error_detail, ($grompp_err, $grompp_out));
}
if ($nerror == 0) {
my $grompp_warn = "grompp.warn";
open(GROMPP,$grompp_err) || die "Could not open file '$grompp_err'\n";
open(WARN,"> $grompp_warn") || die "Could not open file '$grompp_warn'\n";
my $p=0;
while(<GROMPP>) {
$p=1 if /^WARNING/;
print WARN if ($p);
$p=0 if /^\r?$/; # match a blank line, even with Windows line ending
}
close(GROMPP) || die "Could not close file '$grompp_err'\n";
close(WARN) || die "Could not close file '$grompp_warn'\n";
my $ref_warn = "$input_dir/${ref}.warn";
if (! -f $ref_warn) {
print("No $ref_warn file for $test_name\n");
print ("This means you are not really testing $test_name\n");
copy($grompp_warn, $ref_warn);
} else {
open(WARN1,$grompp_warn) || die "Could not open file '$grompp_warn'\n";
open(WARN2,"$ref_warn") || die "Could not open file '$grompp_warn'\n";
while (my $line1=<WARN1>) {
my $line2=<WARN2>;
if (not defined($line2)){#FILE1 has more lines
print("$grompp_warn has more lines than $ref_warn\n");
$nerror++;
next;
}
$line1 =~ s/(e[-+])0([0-9][0-9])/$1$2/g; #hack on windows X.Xe-00X -> X.Xe-0X (posix)
# Hack to avoid issues based only on a difference in
# relative path to grompp.mdp, which might happen in a
# non-problematic way when the same test case is run
# in a different output directory
$line1 =~ s/file .*grompp.mdp/file grompp.mdp/;
$line2 =~ s/file .*grompp.mdp/file grompp.mdp/;
if ("$line2" ne "$line1") {
$nerror++;
# Note that the next line uses the fact that
# $line1 and $line2 still have their end-of-line
# termination characters
print("$grompp_warn had line\n$line1 which did not match line\n$line2 from $ref_warn\n");
}
}
while (my $line2=<WARN2>) {#FILE2 has more lines
print("$grompp_warn has fewer lines than $ref_warn\n");
$nerror++
}
if ($nerror>0) {
print("Different warnings in $ref_warn and $grompp_warn\n");
push(@error_detail, ($grompp_err, $grompp_out));
} elsif (!$keep_files) {
unlink($grompp_warn);
}
}
}
if ($nerror == 0) {
# Do the mdrun at last!
my $local_gpu_id = $gpu_id;
# With tunepme Coul-Sr/Recip isn't reproducible
my $local_mdparams = $mdparams . " -notunepme";
my $part = "";
if ( -f "$input_dir/continue.cpt" ) {
$local_mdparams .= " -cpi $input_dir/continue -noappend";
$part = ".part0002";
}
if ($test_name =~ /-nb-cpu/) {
$local_mdparams .= " -nb cpu";
# Even if the user specified or permited a GPU run with
# -mdparam, the purpose of doing a CPU rerun is to run
# without GPUs. Clearing $local_gpu_id ensures it is so.
$local_gpu_id = "";
}
my $pme_option = "";
if ($test_name =~ /-pme-cpu/) {
$pme_option = "-pme cpu";
}
my $update_option = "";
if ($test_name =~ /-update-cpu/) {
$update_option = "-update cpu";
}
$nerror = run_mdrun($tmpi_ranks, $omp_threads, $mpi_ranks, $npme_ranks, $pme_option, $update_option, $local_gpu_id, $mdprefix, $local_mdparams, $grompp_mdp);
if ($nerror != 0) {
if ($parse_cmd eq '') {
push(@error_detail, ("mdrun.out", "md.log"));
} else {
do_system("$parse_cmd <mdrun.out >mdrun_parsed.out");
push(@error_detail, ("mdrun_parsed.out", "md.log"));
}
}
my $ener = "ener${part}.edr";
my $traj = "traj${part}.trr";
my $log = "md${part}.log";
if ($nerror!=0) {
$nerror=1;
} elsif ((-f "$ener" ) && (-f "$traj")) { # Check whether we have any output
# Now check whether we have any reference files
my $refedr = "$input_dir/${ref}.edr";
if (! -f $refedr) {
print ("No $refedr file for $test_name.\n");
print ("This means you are not really testing $test_name\n");
copy("$ener", $refedr);
} else {
my $potout="checkpot.out";
my $poterr="checkpot.err";
# Now do the real tests
do_system("$progs{'check'} -e $refedr -e2 $ener -tol $etol_rel -abstol $etol_abs -lastener Potential >$potout 2>$poterr", 0,
sub {
if($nerror != 0) {
print "\ngmx check failed on the .edr file, probably because mdrun also failed";
} else {
print "\ngmx check FAILED on the .edr file";
$nerror = 1;
}
});
my $nerr_pot = find_in_file("step","$potout");
push(@error_detail, "$potout ($nerr_pot errors)") if ($nerr_pot > 0);
my $nerr_vir = 0; #TODO: check_virial();
push(@error_detail, "checkvir.out ($nerr_vir errors)") if ($nerr_vir > 0);
$nerror |= $nerr_pot | $nerr_vir;
unlink($potout,$poterr) unless ($nerr_pot || $keep_files);
}
my $reftrr = "$input_dir/${ref}.trr";
if (! -f $reftrr ) {
print ("No $reftrr file for $test_name.\n");
print ("This means you are not really testing $test_name\n");
copy("$traj", $reftrr);
} else {
# Now do the real tests
my $nerr_force = check_force($traj, $reftrr);
push(@error_detail, "checkforce.out ($nerr_force errors)") if ($nerr_force > 0);
$nerror |= $nerr_force;
}
my $reflog = "${ref}.log";
if (! -f $reflog ) {
copy($log, $reflog);
}
# This bit below is only relevant for free energy tests
my $refxvg = "$input_dir/${ref}.xvg";
my $nerr_xvg = check_xvg($refxvg,'dgdl.xvg',1);
push(@error_detail, "$refxvg ($nerr_xvg errors)") if ($nerr_xvg > 0);
$nerror |= $nerr_xvg;
}
else {
print "mdrun output files ${ener} and/or ${traj} were not found.\n";
$nerror = 1;
}
}
my $error_detail = join(', ', @error_detail) . ' ';
print XML "<testcase name=\"$test_name\">\n" if ($xml);
if ($nerror > 0) {
my $error_detail = join(', ', @error_detail) . ' ';
print "FAILED. Check ${error_detail}file(s) in $dir for $test_name\n";
if ($xml) {
print XML "<error message=\"Errors in ${error_detail}\">\n";
print XML "<![CDATA[\n";
foreach my $err (@error_detail) {
my @err = split(/ /, $err);
my $errfn = $err[0];
print XML "$errfn:\n";
if (!open FH, $errfn) {
print XML "failed to open $errfn";
} else {
while(my $line=<FH>) {
$line=~s/\x00//g; #remove invalid XML characters
print XML $line;
}
}
print XML "\n--------------------------------\n";
close FH;
}
print XML "]]>\n";
print XML "</error>";
}
}
else {
if ($verbose > 0) {
if (find_in_file(".", $grompp_mdp) < 50) {
# if the input .mdp file is trivially short, then
# the diff test below will always fail, however this
# is normal and expected for any usefully-short
# test .mdp files, so we don't compare the
# .mdp files for those cases
print "PASSED\n";
}
else {
my $mdp_result = 0;
foreach my $reference_mdp ( $grompp_mdp ) {
if (-f $reference_mdp) {
open(FILE1,$reference_mdp) || die "Could not open file '$reference_mdp'\n";
open(FILE2,"mdout.mdp") || die "Could not open file 'mdout.mdp'\n";
my $diff=0;
while (my $line1=<FILE1>) {
my $line2=<FILE2>;
next if defined($line1) && $line1 =~ /(data|host|user|generated)/;
next if defined($line2) && $line2 =~ /(data|host|user|generated)/;
if (not defined($line2)){#FILE1 has more lines
$diff++;
next;
}
$diff++ unless ("$line2" eq "$line1");
}
while (my $line2=<FILE2>) {#FILE2 has more lines
$diff++
}
$mdp_result++ if $diff > 2;
close(FILE1) || die "Could not close file '$reference_mdp'\n";
close(FILE2) || die "Could not close file 'mdout.mdp'\n";
}
}
if ($mdp_result > 0) {
print("PASSED but check mdp file differences\n");
}
else {
print "PASSED\n";
if (! $keep_files) {
unlink("mdout.mdp");
}
}
}
}
$success = 1;
}
print XML "</testcase>\n" if ($xml);
chdir($cwd);
return $success;
}
sub prepare_run_with_different_task_decomposition {
my ($test_name, $suffix, $new_dir_ref, $new_input_dir_ref, $new_test_name_ref) = @_;
# Set up new variables that control where the test
# case does its I/O
$$new_dir_ref = "${test_name}/$suffix";
$$new_input_dir_ref = "..";
$$new_test_name_ref = "${test_name}-${suffix}";
mkdir $$new_dir_ref;
# Copy the files that limit parallelism
foreach my $limiter_file ($max_openmp_threads_filename, $max_mpi_ranks_filename) {
if (-f "$test_name/$limiter_file") {
copy("$test_name/$limiter_file", $$new_dir_ref);
}
}
}
sub test_systems {
my ($npassed, $nn, $dirs, @subdirs) = @_;
$$npassed = 0;
$$nn = 0;
foreach my $dir ( @subdirs ) {
my $test_name = $dir;
my $input_dir = "$srcdir/$dirs/$dir";
$$nn++;
# Run the test case
$$npassed += test_case $dir, $input_dir, $test_name;
# The nbnxn test cases are most of the cases that can execute GPU-based non-bonded
# kernels. If GPU kernels were used to run this test case
# (whether natively or in emulation), run it again in CPU-only
# mode. This relies on test_case() not clearing up md.log.
my $mdrun_ran_nb_on_gpu = ($test_name =~ /nbnxn/ && 0 < find_in_file("^Using .* 8x8 non-bonded kernels", "$dir/md.log"));
my $specified_nb_option = ($mdparams =~ /-nb/);
if ($mdrun_ran_nb_on_gpu) {
if ($specified_nb_option) {
print("Test case $test_name has been run using GPU non-bonded kernels,\n" .