Repository navigation
Expand file tree
/
Copy pathcs_case.py
More file actions
2073 lines (1551 loc) · 65.6 KB
/
Copy pathcs_case.py
File metadata and controls
2073 lines (1551 loc) · 65.6 KB
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/env python3
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# This file is part of code_saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2022 EDF S.A.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.
#-------------------------------------------------------------------------------
import configparser
import datetime
import os
import os.path
import platform
import sys
import stat
from code_saturne.base import cs_exec_environment, cs_run_conf
from code_saturne.base.cs_case_domain import *
homard_prefix = None
#===============================================================================
# Utility functions
#===============================================================================
def check_exec_dir_stamp(d):
"""
Return path to execution directory if present in a given directory.
Otherwise, return given directory path
"""
retval = d
p = os.path.join(d, 'run_status.exec_dir')
if os.path.isfile(p):
f = open(p, 'r')
l = f.readlines()
f.close()
d = l[0].rstrip()
if os.path.isdir(d):
retval = d
return retval
#-------------------------------------------------------------------------------
# Check if an execution directory is inside a standard case structure
#
# This may not be the case if a separate staging directory or destination
# directory has been specified.
#-------------------------------------------------------------------------------
def is_exec_dir_in_case(case_dir, exec_dir):
"""
Check if an execution directory is inside a standard case structure.
"""
in_case = False
if case_dir and exec_dir:
r_case_dir = os.path.normpath(case_dir)
r_exec_dir = os.path.normpath(exec_dir)
if not os.path.isabs(r_case_dir):
r_case_dir = os.path.abspath(r_case_dir)
if not os.path.isabs(r_exec_dir):
r_exec_dir = os.path.abspath(r_exec_dir)
if r_exec_dir.find(r_case_dir) == 0:
exec_parent_dir = os.path.split(r_exec_dir)[0]
if os.path.basename(exec_parent_dir) in ('RESU', 'RESU_COUPLING'):
if os.path.split(exec_parent_dir)[0] == case_dir:
in_case = True
return in_case
#-------------------------------------------------------------------------------
def get_case_dir(case=None, param=None, coupling=None, id=None):
"""
Check and return case path based on partial or sub-path.
"""
casedir = None
staging_dir = None
data = None
src = None
if coupling:
# Multiple domain case
if param:
err_str = 'Error: coupling and param options are incompatible.\n'
raise RunCaseError(err_str)
coupling = os.path.realpath(coupling)
if not os.path.isfile(coupling):
err_str = 'Error: coupling parameters: ' + coupling + '\n' \
'not found or not a file.\n'
raise RunCaseError(err_str)
if id:
cwd = os.path.split(coupling)[0]
if os.path.basename(cwd) == str(id):
d = os.path.split(cwd)[0]
if os.path.basename(d) == 'RESU_COUPLING':
staging_dir = cwd
if case:
casedir = os.path.realpath(case)
else:
casedir = os.path.split(coupling)[0]
if staging_dir:
casedir = os.path.split(os.path.split(staging_dir)[0])[0]
else:
cwd = os.getcwd()
# Single domain case
if param:
param = os.path.basename(param)
if param != param:
datadir = os.path.split(os.path.realpath(param))[0]
(casedir, data) = os.path.split(datadir)
if data != 'DATA': # inconsistent paramaters location.
casedir = None
if id:
if os.path.basename(cwd) == str(id):
d = os.path.split(cwd)[0]
if os.path.basename(d) == 'RESU':
staging_dir = cwd
if case:
if os.path.isabs(case):
casedir = os.path.realpath(case)
else:
casedir = os.path.realpath(os.path.join(cwd, case))
data = os.path.join(casedir, 'DATA')
src = os.path.join(casedir, 'SRC')
else:
testdir = cwd
while os.path.basename(testdir):
data = os.path.join(testdir, 'DATA')
src = os.path.join(testdir, 'SRC')
cfg = os.path.join(data, 'run.cfg')
if (os.path.isdir(data) and os.path.isdir(src)) \
or os.path.isfile(cfg):
casedir = testdir
break
testdir = os.path.split(testdir)[0]
if not (os.path.isdir(data)):
casedir = None
# Return casedir or None
return casedir, staging_dir
#===============================================================================
# Main class
#===============================================================================
class case:
"""Base class from which classes handling running case should inherit."""
#---------------------------------------------------------------------------
def __init__(self,
package, # main package
package_compute = None, # package for compute environment
case_dir = None,
dest_dir = None,
staging_dir = None,
domains = None,
syr_domains = None,
py_domains = None):
# Package specific information
self.package = package
if package_compute:
self.package_compute = package_compute
else:
self.package_compute = self.package
# Determine caller script path (which may contain batch directives)
self.parent_script = cs_exec_environment.get_parent_process_path()
# Set environment modules if present
cs_exec_environment.set_modules(self.package_compute)
cs_exec_environment.source_rcfile(self.package_compute)
# Re-clean os.environment as the above calls may have indirectly
# restored some unwanted variables (such as lmod macros).
cs_exec_environment.clean_os_environ_for_shell()
# Ensure we have tuples or lists to simplify later tests
if type(domains) == list:
self.domains = domains
elif type(domains) == tuple:
self.domains = list(domains)
else:
self.domains = [domains]
self.n_domains_native = self.domains # code_saturne based domains
if type(syr_domains) == tuple :
self.domains += list(syr_domains)
elif type(syr_domains) == list:
self.domains += syr_domains
elif syr_domains:
self.domains += [syr_domains]
n_domains = len(self.domains)
if type(py_domains) == tuple:
self.domains += list(py_domains)
elif type(py_domains) == list:
self.domains += py_domains
elif py_domains:
self.domains = [py_domains]
self.n_domains_python = len(self.domains) - n_domains
n_domains = len(self.domains)
# Check names in case of multiple domains (coupled by MPI)
if n_domains > 1:
err_str = 'In case of multiple domains (i.e. code coupling), ' \
+ 'each domain must have a name.\n'
for d in self.domains:
if d.name is None:
raise RunCaseError(err_str)
# Names, directories, and files in case structure:
# associate case domains and set case directory
self.case_dir = case_dir
self.dest_dir = dest_dir
self.__define_dest_dir__()
self.result_dir = None
# Determine parent (or coupling group) study directory
if (os.path.isdir(os.path.join(case_dir, 'DATA')) and n_domains == 1):
# Simple domain case from standard directory structure
(self.study_dir, self.name) = os.path.split(case_dir)
self.domains[0].name = None
else:
# Coupling or single domain run from coupling script
self.study_dir = case_dir
self.name = os.path.split(case_dir)[1]
if staging_dir:
staging_dir = check_exec_dir_stamp(staging_dir)
n_nc_solver = 0
for d in self.domains:
d.set_case_dir(self.case_dir, staging_dir)
try:
solver_name = os.path.basename(d.solver_path)
if solver_name == 'nc_solver':
n_nc_solver += 1
except Exception:
pass
self.module_name = 'code_saturne'
if n_nc_solver == self.n_domains_native:
self.module_name = 'neptune_cfd'
# Working directory
self.exec_dir = None
self.exec_prefix = None
self.exec_solver = True
n_exec_solver = 0
for d in self.domains:
if d.exec_solver:
n_exec_solver += 1
if n_exec_solver == 0:
self.exec_solver = False
elif n_exec_solver < n_domains:
err_str = 'In case of multiple domains (i.e. code coupling), ' \
+ 'all or no domains must execute their solver.\n'
raise RunCaseError(err_str)
# Script hooks
self.run_prologue = None
self.run_epilogue = None
self.compute_prologue = None
self.compute_epilogue = None
# Tool hooks
self.debug_args = None
self.tool_args = None
self.mpi_tool_args = None
# Date or other name
self.run_id = None
# Time limit
self.time_limit = None
# Error reporting
self.error = ''
self.error_long = ''
#---------------------------------------------------------------------------
def __define_dest_dir__(self):
if self.dest_dir != None:
if not os.path.isabs(self.dest_dir):
dest_dir = os.path.join(os.path.split(self.case_dir)[0],
self.dest_dir)
self.dest_dir = os.path.abspath(dest_dir)
#---------------------------------------------------------------------------
def print_procs_distribution(self):
"""
Print info on the processor distribution.
"""
# Print process info
name = self.module_name
for d in self.domains:
if len(self.domains) == 1:
if d.n_procs > 1:
msg = ' Parallel ' + d.code_name + ' on ' \
+ str(d.n_procs) + ' processes.\n'
else:
msg = ' Single processor ' + d.code_name + ' simulation.\n'
sys.stdout.write(msg)
else:
msg = ' ' + d.code_name + ' domain "' + d.name + '" on ' \
+ str(d.n_procs) + ' process(es).\n'
sys.stdout.write(msg)
sys.stdout.write('\n')
#---------------------------------------------------------------------------
def distribute_procs(self, n_procs=None):
"""
Distribute number of processors to domains,
and returns the total number of associated processes.
"""
# Initial count
np_list = []
for d in self.domains:
np_list.append(d.get_n_procs())
n_procs_tot = 0
n_procs_min = 0
for np in np_list:
n_procs_tot += np[0]
n_procs_min += np[1]
# If no process count is given or everything fits:
if n_procs is None or n_procs == n_procs_tot:
return n_procs_tot
# If process count is given and not sufficient, abort.
elif n_procs < n_procs_min:
err_str = ' Error:\n' \
' The current calculation scheme requires at least ' \
+ str(n_procs_min) + ' processes,\n' \
+ ' while the execution environment provides only ' \
+ str(n_procs) + '.\n' \
+ ' You may either allocate more processes or try to\n' \
+ ' oversubscribe by changing the number of processes\n' \
+ ' in the run.cfg configuration file.'
raise RunCaseError(err_str)
# Otherwise, rebalance process counts.
n_passes = 0
n_fixed_procs = 0
n_fixed_apps = 0
while ( n_procs_tot != n_procs and n_fixed_apps != len(np_list)
and n_passes < 5):
mult = (n_procs - n_fixed_procs) *1.0 \
// (n_procs_tot - n_fixed_procs)
n_procs_tot = 0
for np in np_list:
if np[2] is None:
np[2] = n_procs
if np[2] != 0: # marked as fixed here
np[0] = int(np[0]*mult)
if np[0] < np[1]:
np[0] = np[1]
np[2] = 0 # used as marker here
n_fixed_procs += np[0]
n_fixed_apps += 1
elif np[0] > np[2]:
np[0] = np[2]
np[2] = 0 # used as marker here
n_fixed_procs += np[0]
n_fixed_apps += 1
n_procs_tot += np[0]
n_passes += 1
# Minor adjustments may help in case of rounding
n_passes = 0
while ( n_procs_tot != n_procs and n_fixed_apps != len(np_list)
and n_passes < 5):
delta = int(round( (n_procs_tot - n_procs)*1.0
// (len(np_list) - n_fixed_apps)))
if delta == 0:
if n_procs_tot < n_procs:
delta = 1
else:
delta = -1
for np in np_list:
if np[2] != 0: # marked as fixed here
n_procs_prev = np[0]
np[0] -= delta
if np[0] < np[1]:
np[0] = np[1]
np[2] = 0 # used as marker here
n_fixed_procs += np[0]
n_fixed_apps += 1
elif np[0] > np[2]:
np[0] = np[2]
np[2] = 0 # used as marker here
n_fixed_procs += np[0]
n_fixed_apps += 1
n_procs_tot += (np[0] - n_procs_prev)
if n_procs_tot == n_procs:
break
n_passes += 1
# We are now are ready to set return values
app_id = 0
for d in self.domains:
d.set_n_procs(np_list[app_id][0])
app_id += 1
# Warn in case of over or under-subscription
if n_procs_tot != n_procs:
msg = \
'Warning:\n' \
+ ' total number of processes used (' + str(n_procs_tot) \
+ ') is different from the\n' \
+ ' number provided by the environment. (' + str(n_procs) \
+ ').\n\n'
sys.stderr.write(msg)
return n_procs_tot
#---------------------------------------------------------------------------
def define_exec_dir(self):
"""
Define execution directory.
"""
if self.exec_prefix != None:
if self.case_dir != self.study_dir:
study_name = os.path.split(self.study_dir)[1]
exec_dir_name = study_name + '.' + self.name
else:
exec_dir_name = self.name
exec_dir_name += '.' + self.run_id
self.exec_dir = os.path.join(self.exec_prefix, exec_dir_name)
else:
if not self.result_dir:
self.define_result_dir()
self.exec_dir = self.result_dir
#---------------------------------------------------------------------------
def set_exec_dir(self, force=False):
"""
Create execution directory.
"""
# Define execution directory name
self.define_exec_dir()
# Check that directory does not exist.
if os.path.isdir(self.exec_dir):
if not force and self.result_dir != self.exec_dir:
err_str = \
'\nWorking directory: ' + self.exec_dir \
+ ' already exists.\n' \
+ 'Calculation will not be run.\n'
raise RunCaseError(err_str)
else:
os.makedirs(self.exec_dir)
# Set execution directory
for d in self.domains:
d.set_exec_dir(self.exec_dir)
#---------------------------------------------------------------------------
def define_result_dir(self):
if self.dest_dir != None:
base_dir = os.path.join(self.dest_dir,
os.path.basename(self.case_dir))
else:
base_dir = self.case_dir
r = os.path.join(base_dir, 'RESU')
# Coupled case
if len(self.domains) > 1:
r += '_COUPLING'
if not os.path.isdir(r):
os.makedirs(r)
self.result_dir = os.path.join(r, self.run_id)
#---------------------------------------------------------------------------
def set_result_dir(self, force=True):
self.define_result_dir()
if os.path.isdir(self.result_dir):
if not force:
err_str = \
'\nResults directory: ' + self.result_dir \
+ ' already exists.\n' \
+ 'Calculation will not be run.\n'
raise RunCaseError(err_str)
else:
os.mkdir(self.result_dir)
dest_root_dir = None
if self.dest_dir != None:
dest_root_dir = os.path.join(self.dest_dir,
os.path.basename(self.case_dir))
for d in self.domains:
d.set_result_dir(self.run_id,
self.result_dir,
dest_root_dir=dest_root_dir)
#---------------------------------------------------------------------------
def summary_init(self, exec_env):
"""
Build summary start.
"""
s_path = os.path.join(self.exec_dir, 'summary')
s = open(s_path, 'w')
r = exec_env.resources
date = (datetime.datetime.now()).strftime("%A %B %d %H:%M:%S CEST %Y")
t_uname = platform.uname()
s_uname = ''
for t in t_uname:
s_uname = s_uname + t + ' '
n_procs = r.n_procs
if n_procs is None:
n_procs = 1
dhline = '========================================================\n'
hline = ' ----------------------------------------------------\n'
s.write(dhline)
s.write('Start time : ' + date + '\n')
s.write(dhline)
cmd = sys.argv[0]
for arg in sys.argv[1:]:
cmd += ' ' + arg
s.write(' Command : ' + cmd + '\n')
s.write(dhline)
s.write(' Package path : ' + self.package.get_dir('exec_prefix') + '\n')
s.write(hline)
if homard_prefix != None:
s.write(' HOMARD : ' + homard_prefix + '\n')
s.write(hline)
s.write(' MPI path : ' + self.package_compute.config.libs['mpi'].bindir + '\n')
if len(self.package_compute.config.libs['mpi'].variant) > 0:
s.write(' MPI type : ' + self.package_compute.config.libs['mpi'].variant + '\n')
s.write(hline)
s.write(' User : ' + exec_env.user + '\n')
s.write(hline)
s.write(' Machine : ' + s_uname + '\n')
s.write(' N Procs : ' + str(n_procs) + '\n')
if r.manager is None and r.hosts_list != None:
s.write(' Hosts :')
for p in r.hosts_list:
s.write(' ' + p)
s.write('\n')
s.write(hline)
if len(self.domains) > 1:
s.write(' Exec. dir. : ' + self.exec_dir + '\n')
s.write(hline)
s.write(' Environment variables\n')
ek = list(os.environ.keys())
ek.sort()
for k in ek:
s.write(' ' + k + '=' + os.environ[k] + '\n')
s.write(hline)
for d in self.domains:
d.summary_info(s)
s.write(hline)
s.close()
#---------------------------------------------------------------------------
def summary_finalize(self):
"""
Output summary body.
"""
date = (datetime.datetime.now()).strftime("%A %B %d %H:%M:%S CEST %Y")
dhline = '========================================================\n'
hline = ' ----------------------------------------------------\n'
s_path = os.path.join(self.exec_dir, 'summary')
s = open(s_path, 'a')
if self.error:
s.write(' ' + self.error + ' failed\n')
s.write(dhline)
s.write('Finish time : ' + date + '\n')
s.write(dhline)
s.close()
#---------------------------------------------------------------------------
def copy_log(self, name):
"""
Retrieve single log file from the execution directory
"""
if self.result_dir == self.exec_dir:
return
src = os.path.join(self.exec_dir, name)
dest = os.path.join(self.result_dir, name)
if os.path.isfile(src):
shutil.copy2(src, dest)
if self.error == '':
os.remove(src)
#---------------------------------------------------------------------------
def copy_script(self):
"""
Retrieve script (if present) from the execution directory
"""
src = sys.argv[0]
if os.path.basename(src) == self.package.name:
src = self.parent_script
if not src:
src = self.package.name
# Copy single file
dest = os.path.join(self.result_dir, os.path.basename(src))
if os.path.isfile(src) and os.path.abspath(src) != os.path.abspath(dest):
shutil.copy2(src, dest)
#---------------------------------------------------------------------------
def copy_top_run_conf(self):
"""
Retrieve top-level run.cfg (if present) from the top directory
"""
n = "run.cfg"
src = os.path.join(self.case_dir, n)
dest = os.path.join(self.result_dir, n)
if os.path.isfile(src) and src != dest:
shutil.copy2(src, dest)
#---------------------------------------------------------------------------
def solver_script_path(self):
"""
Determine name of solver script file.
"""
return os.path.join(self.exec_dir, self.package.runsolver)
#---------------------------------------------------------------------------
def debug_wrapper_args(self):
"""
Additional arguments to use debug wrapper
"""
debug_args = ''
python_exec = self.package.config.python
if os.path.isfile(python_exec) or os.path.islink(python_exec):
debug_args += python_exec + ' '
else:
debug_args += 'python '
cs_pkg_dir = self.package.get_dir('pkgpythondir')
if self.package.name != 'code_saturne':
cs_pkg_dir = os.path.join(cs_pkg_dir, '../code_saturne')
dbg_wrapper_path = os.path.join(cs_pkg_dir,
'base', 'cs_debug_wrapper.py')
debug_args += dbg_wrapper_path + ' '
return debug_args
#---------------------------------------------------------------------------
def generate_solver_mpmd_mpiexec(self, n_procs, mpi_env, tool_args=''):
"""
Generate MPMD mpiexec command.
"""
cmd = ''
app_id = 0
for d in self.domains:
s_args = d.solver_command()
if len(cmd) > 0:
cmd += ' : '
cmd += '-n ' + str(d.n_procs) \
+ ' -wdir ' + os.path.basename(s_args[0]) \
+ ' ' + tool_args + s_args[1] + s_args[2]
app_id += 1
return cmd
#---------------------------------------------------------------------------
def generate_solver_mpmd_configfile(self,
n_procs,
mpi_env,
tool_args=''):
"""
Generate MPMD mpiexec config file.
"""
e_path = os.path.join(self.exec_dir, 'mpmd_configfile')
e = open(e_path, 'w')
app_id = 0
for d in self.domains:
s_args = d.solver_command()
cmd = '-n ' + str(d.n_procs) \
+ ' -wdir ' + os.path.basename(s_args[0]) \
+ ' ' + tool_args + s_args[1] + s_args[2] + '\n'
e.write(cmd)
app_id += 1
e.close()
return e_path
#---------------------------------------------------------------------------
def generate_solver_mpmd_configfile_srun(self,
n_procs,
mpi_env,
tool_args=''):
"""
Generate MPMD mpiexec config file for SLURM's srun.
"""
e_path = os.path.join(self.exec_dir, 'mpmd_configfile')
e = open(e_path, 'w')
e.write('# srun multiple program configuration file\n#\n')
e.write('# > srun -n ' + str(n_procs) \
+ ' --multi-prog mpmd_configfile\n#\n')
rank_id = 0
for d in self.domains:
s_args = d.solver_command(need_abs_path=True)
cmd = '%d-%d\t' % (rank_id, rank_id + d.n_procs - 1) \
+ tool_args + s_args[1] + s_args[2] \
+ ' -wdir ' + os.path.basename(s_args[0]) + '\n'
e.write(cmd)
rank_id += d.n_procs
e.close()
return e_path
#---------------------------------------------------------------------------
def generate_solver_mpmd_configfile_ccc_mprun(self,
n_procs,
mpi_env,
tool_args=''):
"""
Generate MPMD mpiexec config file for CCRT's ccc_mprun.
"""
e_path = os.path.join(self.exec_dir, 'mpmd_configfile')
e = open(e_path, 'w')
app_id = 0
for d in self.domains:
s_args = d.solver_command()
cmd = str(d.n_procs) +' ' \
+ ' bash -c "' \
+ ' cd ' + os.path.basename(s_args[0]) + ' &&' \
+ ' ' + s_args[1] + s_args[2] + '"\n'
e.write(cmd)
app_id += 1
e.close()
return e_path
#---------------------------------------------------------------------------
def generate_solver_mpmd_script(self, n_procs,
mpi_env,
tool_args='',
use_mps=False):
"""
Generate MPMD dispatch file.
"""
e_path = os.path.join(self.exec_dir, 'mpmd_exec.sh')
e = open(e_path, 'w')
cs_exec_environment.write_shell_shebang(e)
e.write('# Make sure to transmit possible additional '
+ 'arguments assigned by mpirun.\n'
+ '# we use $@ to forward arguments passed to this script'
+ ' to the executable files.\n\n')
e.write('MPI_RANK=`'
+ self.package.get_pkgdatadir_script('runcase_mpi_rank')
+ ' $@`\n')
if use_mps:
cs_exec_environment.write_mps_start(e)
app_id = 0
nr = 0
test_pf = 'if [ $MPI_RANK -lt '
test_sf = ' ] ; then\n'
for d in self.domains:
nr += d.n_procs
e.write(test_pf + str(nr) + test_sf)
s_args = d.solver_command()
e.write(' cd ' + s_args[0] + '\n')
e.write(' ' + tool_args + s_args[1] + s_args[2] + ' $@\n')
if app_id == 0:
test_pf = 'el' + test_pf
app_id += 1
e.write('fi\n'
+ 'CS_RET=$?\n')
if use_mps:
cs_exec_environment.write_mps_stop(e)
e.write('exit $CS_RET\n')
e.close()
oldmode = (os.stat(e_path)).st_mode
newmode = oldmode | (stat.S_IXUSR)
os.chmod(e_path, newmode)
return e_path
#---------------------------------------------------------------------------
def solver_script_body(self, n_procs, mpi_env, s):
"""
Write commands to solver script.
"""
# Determine if an MPMD syntax (mpiexec variant) will be used
mpiexec_mpmd = False
if len(self.domains) > 1:
if mpi_env.mpmd & cs_exec_environment.MPI_MPMD_mpiexec:
mpiexec_mpmd = True
elif mpi_env.mpmd & cs_exec_environment.MPI_MPMD_configfile:
mpiexec_mpmd = True
use_srun = False
# Start assembling command
mpi_cmd = ''
mpi_cmd_exe = ''
mpi_cmd_args = ''
if self.mpi_tool_args:
mpi_cmd = self.mpi_tool_args + ' '
if mpi_env.mpiexec != None:
if os.path.basename(mpi_env.mpiexec)[:4] == 'srun':
use_srun = True
if (n_procs > 1 or use_srun):
mpi_cmd += mpi_env.mpiexec
if mpi_cmd:
if mpi_env.mpiexec_opts != None:
mpi_cmd += ' ' + mpi_env.mpiexec_opts
if mpiexec_mpmd == False:
if mpi_env.mpiexec_n != None and n_procs:
mpi_cmd += mpi_env.mpiexec_n + str(n_procs)
if mpi_env.mpiexec_n_per_node != None:
mpi_cmd += mpi_env.mpiexec_n_per_node
if mpi_env.mpiexec_separator != None:
mpi_cmd += ' ' + mpi_env.mpiexec_separator