-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInterface.py
More file actions
executable file
·658 lines (568 loc) · 21.6 KB
/
Copy pathInterface.py
File metadata and controls
executable file
·658 lines (568 loc) · 21.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
"""
The overall interface to the RynLib CLI
"""
import os, shutil
from .RynUtils import Config, ConfigSerializer
from .PlzNumbers import PotentialManager
from .DoMyCode import SimulationManager, ImportanceSamplerManager
__all__ = [
"SimulationInterface",
"PotentialInterface",
"RynLib"
]
class SimulationInterface:
"""
Defines all of the CLI options for working with simulations
"""
@classmethod
def list_simulations(cls):
print("\n".join(SimulationManager().list_simulations()))
@classmethod
def add_simulation(self, name=None, src=None, config_file=None):
data=src
no_config = config_file is None
if config_file is None:
if os.path.exists(os.path.join(data, "config.py")):
config_file = os.path.join(data, "config.py")
if no_config:
if os.path.exists(os.path.join(data, "data")):
data = os.path.join(data, "data")
SimulationManager().add_simulation(name, data=data, config_file=config_file)
print("Added simulation {}".format(name))
@classmethod
def remove_simulation(self, name=None):
SimulationManager().remove_simulation(name)
print("Removed simulation {}".format(name))
@classmethod
def copy_simulation(self, name=None, new_name=None):
SimulationManager().copy_simulation(name, new_name)
print("Copied simulation {} into {}".format(name, new_name))
@classmethod
def simulation_status(self, name=None):
status = SimulationManager().simulation_ran(name)
config = SimulationManager().simulation_config(name)
print(
"Has Run: {}".format(status),
*(" {}: {}".format(k, v) for k, v in config.opt_dict.items()),
sep="\n"
)
@classmethod
def edit_simulation(self, name=None, opts=None, optfile=None):
SimulationManager().edit_simulation(name, optfile=optfile, **opts)
print("Edited simulation {}".format(
name
))
self.simulation_status(name)
@classmethod
def export_simulation(cls, name=None, path=None):
SimulationManager().export_simulation(name, path)
print("Exported simulation {} to {}".format(
name, path
))
@classmethod
def run_simulation(self, name=None):
# print("Running simulation {}".format(name))
SimulationManager().run_simulation(name)
# print("Finished running simulation {}".format(name))
@classmethod
def restart_simulation(self, name=None):
# print("Running simulation {}".format(name))
SimulationManager().restart_simulation(name)
# print("Finished running simulation {}".format(name))
@classmethod
def test_add_HO(cls):
pm = PotentialManager()
if 'HarmonicOscillator' not in pm.list_potentials():
PotentialInterface.configure_HO()
sm = SimulationManager()
if "test_HO" in sm.list_simulations():
sm.remove_simulation("test_HO")
cls.add_simulation("test_HO",
os.path.join(RynLib.test_data, "HOSimulation", "HOSim")
)
@classmethod
def test_HO(cls):
cls.test_add_HO()
cls.run_simulation("test_HO")
@classmethod
def test_HO_imp(cls):
im = ImportanceSamplerManager()
if "HOSampler" not in im.list_samplers():
SimulationInterface.add_sampler(
"HOSampler",
source=os.path.join(RynLib.test_data, "HOSimulation", "HOTrialWavefunction")
)
sm = SimulationManager()
if "test_HO_imp" in sm.list_simulations():
sm.remove_simulation("test_HO_imp")
pm = PotentialManager()
if 'HarmonicOscillator' not in pm.list_potentials():
PotentialInterface.configure_HO()
cls.add_simulation("test_HO_imp",
os.path.join(RynLib.test_data, "HOSimulation", "HOSimImp")
)
cls.run_simulation("test_HO_imp")
@classmethod
def list_archive(cls):
print("\n".join(SimulationManager().list_archive()))
@classmethod
def archive_simulation(cls, name=None):
SimulationManager().archive_simulation(name)
print("Archived simulation {}".format(name))
@classmethod
def archive_status(cls, name=None):
config = SimulationManager().archive_config(name)
print(
*("{}: {}".format(k, v) for k, v in config.opt_dict.items()),
sep="\n"
)
@classmethod
def list_samplers(cls):
print("\n".join(ImportanceSamplerManager().list_samplers()))
@classmethod
def add_sampler(self, name=None, source=None, config_file=None, test_file=None):
no_config = config_file is None
if config_file is None:
if os.path.exists(os.path.join(source, "config.py")):
config_file = os.path.join(source, "config.py")
if test_file is None:
if os.path.exists(os.path.join(source, "test.py")):
test_file = os.path.join(source, "test.py")
if no_config:
if os.path.exists(os.path.join(source, name)):
source = os.path.join(source, name)
ImportanceSamplerManager().add_sampler(name, source, config_file, test_file=test_file)
print("Added importance sampler {}".format(name))
@classmethod
def remove_sampler(self, name=None):
ImportanceSamplerManager().remove_sampler(name)
print("Removed importance sampler {}".format(name))
@classmethod
def test_sampler(cls, name=None):
import numpy as np
print("Testing importance sampler {}".format(name))
ke, meta = ImportanceSamplerManager().test_sampler(name)
print("Sampler returned average local kinetic energy {}".format(np.average(ke, axis=1)))
@classmethod
def test_sampler_mpi(cls, name=None, input_file=None, **opts):
import numpy as np
samp = ImportanceSamplerManager().test_sampler_mpi(name, input_file=input_file, **opts)
mpi = next(samp)
if mpi.world_rank == 0:
print("Testing importance sampler {}".format(name))
print(mpi)
ke, meta = next(samp)
mpi.finalize_MPI()
if mpi.world_rank == 0:
print(meta['walkers'])
print("Took {}s ({}s/walker)".format(meta['timing'], meta['average'])),
print("Sampler returned average local kinetic energy {}".format(np.average(ke, axis=1)))
mpi.finalize_MPI()
class PotentialInterface:
"""
Defines all of the CLI options for working with potentials
"""
@classmethod
def list_potentials(self):
print("\n".join(PotentialManager().list_potentials()))
@classmethod
def add_potential(self, name=None, src=None, config_file=None, data=None, test_file=None):
no_config = config_file is None
if config_file is None:
if os.path.exists(os.path.join(src, "config.py")):
config_file = os.path.join(src, "config.py")
if data is None:
if os.path.exists(os.path.join(src, "data")):
data = os.path.join(src, "data")
if test_file is None:
if os.path.exists(os.path.join(src, "test.py")):
test_file = os.path.join(src, "test.py")
if no_config:
if os.path.exists(os.path.join(src, name)):
src = os.path.join(src, name)
PotentialManager().add_potential(name, src, config_file=config_file, data=data, test=test_file)
print("Added potential {}".format(name))
@classmethod
def remove_potential(self, name=None):
PotentialManager().remove_potential(name)
print("Removed potential {}".format(name))
@classmethod
def import_potential(cls, name=None, src=None):
PotentialManager().import_potential(name, src)
@classmethod
def export_potential(cls, name=None, dest=None):
PotentialManager().export_potential(name, dest)
@classmethod
def compile_potential(self, name=None, recompile=False):
PotentialManager().compile_potential(name, recompile=recompile)
@classmethod
def configure_entos(cls):
pm = PotentialManager()
entos = RynLib.get_conf().entos_binary
pm.add_potential(
"entos",
src=entos,
test=os.path.join(RynLib.test_data, "test_entos.py"),
wrap_potential=True,
function_name="MillerGroup_entosPotential",
working_directory="/opt/entos",
arguments=(("only_hf", 'bool'),),
linked_libs=["entos"],
include_dirs=[os.path.dirname(entos)],
static_source = True
)
# writes to /config/potentials/entos/src/entos.cpp
pm.compile_potential('entos')
@classmethod
def configure_HO(cls):
cls.add_potential(
"HarmonicOscillator",
os.path.join(RynLib.test_data, "HOSimulation", "HOPotential")
)
PotentialManager().compile_potential('HarmonicOscillator')
@classmethod
def potential_status(self, name=None):
pm = PotentialManager()
status = pm.potential_compiled(name)
config = pm.potential_config(name)
print(
"Compiled: {}".format(status),
*(" {}: {}".format(k, v) for k, v in config.opt_dict.items()),
sep="\n"
)
@classmethod
def test_potential(self, name=None, input_file=None):
print("Testing Potential: {}".format(name))
print("Energies: {}".format(PotentialManager().test_potential(name, input_file=input_file)))
@classmethod
def test_potential_mpi(self, name=None, input_file=None, **opts):
PotentialManager().test_potential_mpi(name, input_file=input_file, **opts)
@classmethod
def test_potential_serial(self, name=None, input_file=None, **opts):
PotentialManager().test_potential_serial(name, input_file=input_file, **opts)
class RynLib:
"""
Defines all of the overall RynLib config things
"""
from RynLib import VERSION_NUMBER
VERSION_NUMBER = VERSION_NUMBER
root = "/config"
config_file = "config.py"
mpi_dir = "/usr/lib/mpi"
test_data = os.path.join(os.path.dirname(__file__), "Tests", "TestData")
flags = dict(
multiprocessing = False,
OpenMPThreads = True,
TBBThreads = False
)
@classmethod
def get_default_env(cls):
import platform
node = platform.node()
if 'hyak' in node:
env = dict(
containerizer="singularity",
entos_binary="/opt/entos/lib/libentos.so",
root_directory="#",
simulation_directory="#/simulations",
sampler_directory="#/impsamps",
potential_directory="#/potentials"
)
elif 'cori' in node:
env = dict(
containerizer="shifter",
entos_binary="/opt/entos/lib/libentos.so",
root_directory="#",
simulation_directory="#/simulations",
sampler_directory="#/impsamps",
potential_directory="#/potentials"
)
else:
env = dict(
containerizer="docker",
entos_binary="/opt/entos/lib/libentos.so",
root_directory="#",
simulation_directory="#/simulations",
sampler_directory="#/impsamps",
potential_directory="#/potentials"
)
return env
@classmethod
def get_conf(cls):
conf_path = os.path.join(cls.root, cls.config_file)
new_conf = not os.path.exists(conf_path)
if new_conf:
ConfigSerializer.serialize(
conf_path,
cls.get_default_env(),
attribute="config"
)
cfig = Config(conf_path)
return cfig
@classmethod
def root_directory(cls):
cf = cls.get_conf()
return cf.root_directory.replace("#", cls.root)
@classmethod
def simulation_directory(cls):
cf = cls.get_conf()
sim_dir = cf.simulation_directory
return os.path.abspath(sim_dir.replace("#", cls.root_directory()))
@classmethod
def potential_directory(cls):
cf = cls.get_conf()
pot_dir = cf.potential_directory
return os.path.abspath(pot_dir.replace("#", cls.root_directory()))
@classmethod
def sampler_directory(cls):
cf = cls.get_conf()
samp_dir = cf.sampler_directory
return os.path.abspath(samp_dir.replace("#", cls.root_directory()))
@classmethod
def reset_config(cls):
default_env = cls.get_default_env()
conf_path = os.path.join(cls.root, cls.config_file)
ConfigSerializer.serialize(
conf_path,
default_env,
attribute="config"
)
@classmethod
def edit_config(cls, **opts):
op2 = {k:v for k,v in opts.items() if (v is not None and not (isinstance(v, str) and v==""))}
cls.get_conf().update(**op2)
@classmethod
def get_container_env(cls):
return cls.get_conf().containerizer
@classmethod
def build_libs(cls, rebuild=False):
from .PlzNumbers import PotentialCaller
from .DoMyCode import Simulation
if rebuild:
PotentialCaller.reload()
Simulation.reload_lib()
cls.reload_dumpi()
else:
Simulation.load_lib()
PotentialCaller.load_lib()
cls.configure_mpi()
# if os.path.isdir(os.path.join(cls.get_conf().mpi_dir)):
# cls.reload_dumpi()
@classmethod
def run_tests(cls, test_dir=None, debug=False, name=None, suite=None):
import sys
curdir = os.getcwd()
root = cls.root
argv = sys.argv
if test_dir is None:
no_user_dir = True
test_dir = "/tests"
else:
no_user_dir = False
try:
cls.root = test_dir
if no_user_dir and os.path.exists(test_dir):
shutil.rmtree(test_dir)
if not os.path.exists(test_dir):
os.mkdir(test_dir)
os.chdir(os.path.dirname(os.path.dirname(__file__)))
if debug:
sys.argv = [argv[0], "-d"]
else:
sys.argv = [argv[0], "-v", "-d"]
if name is not None:
sys.argv.extend(["-n", name])
if suite is not None:
sys.argv.extend(["-f", suite])
import RynLib.Tests.run_tests
finally:
sys.argv = argv
cls.root = root
os.chdir(curdir)
@classmethod
def update_lib(cls):
"""
Pulls the updated RynLib from GitHub
:return:
:rtype:
"""
import subprocess
curdir = os.getcwd()
try:
os.chdir(os.path.dirname(__file__))
subprocess.call(["git", "pull"])#print()
except subprocess.CalledProcessError as e:
print(e.output)
raise
finally:
os.chdir(curdir)
@classmethod
def update_testing_framework(cls):
"""
Pulls the updated RynLib from GitHub
:return:
:rtype:
"""
import subprocess
curdir = os.getcwd()
try:
os.chdir(os.path.join(os.path.dirname(os.path.dirname(__file__)), "Peeves"))
print(subprocess.check_call(["git", "pull"]))
except subprocess.CalledProcessError as e:
print(e.output)
raise
finally:
os.chdir(curdir)
@classmethod
def install_MPI(cls,
mpi_version=None,
mpi_implementation=None,
mpi_dir=None,
mpi_flags=None,
bind_conf = False
):
"""Installs MPI into the containerized environment"""
#This comes from /sw/singularity-images/testing/ngsolve-2.def
import subprocess, tempfile, wget, tarfile
conf = None
if mpi_dir is None:
mpi_dir=cls.mpi_dir
if mpi_implementation is None:
if conf is None:
conf = cls.get_conf()
mpi_implementation=conf.mpi_implementation
if mpi_version is None:
if conf is None:
conf = cls.get_conf()
mpi_version=conf.mpi_version
if mpi_flags is None:
mpi_flags = (
"--disable-oshmem",
"--enable-branch-probabilities",
"--disable-mpi-fortran"
)
# if conf is None:
# conf = cls.get_conf()
# mpi_flags = conf.mpi_flags
MPI_DIR = os.path.abspath(mpi_dir) # os.path.join(mpi_dir, "mpi")
MPI_IMP = mpi_implementation.lower()
if os.path.isdir(MPI_DIR):
shutil.rmtree(MPI_DIR)
os.makedirs(MPI_DIR)
MPI_VERSION = mpi_version
MPI_MAJOR_VERSION = ".".join(MPI_VERSION.split(".")[:2])
if MPI_IMP == "ompi":
MPI_URL = "https://download.open-mpi.org/release/open-mpi/v{MPI_MAJOR_VERSION}/openmpi-{MPI_VERSION}.tar.bz2".format(
MPI_MAJOR_VERSION = MPI_MAJOR_VERSION,
MPI_VERSION = MPI_VERSION
)
else:
MPI_URL = "https://www.mpich.org/static/downloads/{MPI_VERSION}/mpich-{MPI_VERSION}.tar.gz".format(
MPI_MAJOR_VERSION = MPI_MAJOR_VERSION,
MPI_VERSION = MPI_VERSION
)
with tempfile.TemporaryDirectory() as build_dir:
if MPI_IMP == "ompi":
mpi_ext = "openmpi-{MPI_VERSION}".format(MPI_VERSION=MPI_VERSION)
mpi_src = os.path.join(build_dir, mpi_ext + ".tar.bz2")
elif MPI_IMP == "mpich":
mpi_ext = "mpich-{MPI_VERSION}".format(MPI_VERSION=MPI_VERSION)
mpi_src = os.path.join(build_dir, mpi_ext + ".tar.gz")
print("Downloading MPI from {}".format(MPI_URL))
wget.download(MPI_URL, mpi_src)
with tarfile.open(mpi_src) as tar:
tar.extractall(build_dir)
curdir = os.getcwd()
try:
os.chdir(os.path.join(build_dir, mpi_ext))
print("\nCompiling MPI")
subprocess.check_output([
"./configure",
"--prefix={MPI_DIR}".format(MPI_DIR=MPI_DIR),
*mpi_flags
])
subprocess.check_output([
"make",
"-j4",
"install"
])
subprocess.check_output([
"make",
"clean"
])
except subprocess.CalledProcessError as e:
print(e.output.decode())
raise
finally:
os.chdir(curdir)
print("\nInstalling MPI to {}",format(MPI_DIR))
if bind_conf:
conf.update(mpi_dir=MPI_DIR)
@classmethod
def configure_mpi(cls):
mpi_dir = cls.mpi_dir#cls.mpi_dir#cls.get_conf().mpi_dir
if not os.path.isdir(mpi_dir):
cls.install_MPI()
from .Dumpi import MPIManagerObject
MPIManagerObject._load_lib()
@classmethod
def reload_dumpi(cls):
mpi_dir = cls.mpi_dir#cls.get_conf().mpi_dir
if not os.path.isdir(mpi_dir):
cls.install_MPI()
from .Dumpi import MPIManagerObject
# try:
MPIManagerObject._remove_lib()
# except OSError:
# pass
MPIManagerObject._load_lib()
@classmethod
def test_mpi(cls):
mpi_dir = cls.mpi_dir#cls.get_conf().mpi_dir
if not os.path.isdir(mpi_dir):
cls.install_MPI()
from .Dumpi import MPIManagerObject
manager = MPIManagerObject()
print("World Size: {} World Rank: {}".format(manager.world_size, manager.world_rank))
@classmethod
def test_entos(cls):
potential_manager = PotentialManager()
if 'entos' not in potential_manager.list_potentials():
PotentialInterface.configure_entos()
print("Testing Entos:")
# try:
# val = potential_manager.test_potential('entos')
# except Exception as E:
# val = E.args[0]
# print("Energy (no hf_only flag): {}".format(val))
print("Energy w/ MOBML: {}".format(potential_manager.test_potential('entos', parameters=dict(only_hf=False))))
print("Energy w/o MOBML: {}".format(potential_manager.test_potential('entos', parameters=dict(only_hf=True))))
@classmethod
def test_HO(cls):
potential_manager = PotentialManager()
if 'HarmonicOscillator' not in potential_manager.list_potentials():
PotentialInterface.configure_HO()
print("Testing Harmonic Oscillator:")
print("Energy of HO: {}".format(potential_manager.test_potential("HarmonicOscillator")))
@classmethod
def test_entos_mpi(cls,
**opts
):
potential_manager = PotentialManager()
if 'entos' not in potential_manager.list_potentials():
PotentialInterface.configure_entos()
potential_manager.test_potential_mpi(
"entos",
**opts
)
@classmethod
def test_ho_mpi(cls, **opts):
potential_manager = PotentialManager()
if 'HarmonicOscillator' not in potential_manager.list_potentials():
PotentialInterface.configure_HO()
potential_manager.test_potential_mpi(
"HarmonicOscillator",
**opts
)
class ContainerException(IOError):
...