def test_run1(): run_script.run1(script, workdir=workdir, ncount=ncount, buffer_size=int(1e6), use_gpu=False, overwrite_datafiles=True) return
def test_component_long_with_monitors(ncount = 1e6): instr = os.path.join(thisdir, "src_psdmon.py") outdir = 'out.debug-acc_source_simple' ncount = int(ncount) run_script.run1( instr, outdir, ncount=ncount, buffer_size=ncount, source_factory=src_factory, overwrite_datafiles=True) return
def test1(): instr = os.path.join(thisdir, "isotropic_box_instrument.py") outdir = 'out.isotropic_box' if os.path.exists(outdir): shutil.rmtree(outdir) run_script.run1( instr, outdir, ncount=1e5, buffer_size=int(1e5), ) return
def main(): num_neutrons = int(1e8) # Run the mcvine instrument first instr = os.path.join(thisdir, "guide_instrument.py") mcvine_outdir = 'out.save_neutrons_before_guide' if os.path.exists(mcvine_outdir): shutil.rmtree(mcvine_outdir) run_script.run1( instr, mcvine_outdir, ncount=num_neutrons, buffer_size=num_neutrons, guide_factory = "mcvine.components.optics.Guide", save_neutrons_before_guide=True, save_neutrons_after_guide=False, overwrite_datafiles=True) return
def test_compare_mcvine(): """ Tests the acc cpu implementation of a straight guide against mcvine """ num_neutrons = 100000 # Run the mcvine instrument first instr = os.path.join(thisdir, "guide_instrument.py") mcvine_outdir = 'out.debug-mcvine_guide_cpu_instrument' if os.path.exists(mcvine_outdir): shutil.rmtree(mcvine_outdir) run_script.run1(instr, mcvine_outdir, ncount=num_neutrons, buffer_size=num_neutrons, guide_factory="mcvine.components.optics.Guide", overwrite_datafiles=True) # Run our guide implementation outdir = 'out.debug-guide_cpu_instrument' if os.path.exists(outdir): shutil.rmtree(outdir) run_script.run1(instr, outdir, ncount=num_neutrons, buffer_size=num_neutrons, guide_mod="mcvine.acc.components.optics.numpy_guide", overwrite_datafiles=True) # Compare output files mcvine_Ixy = hh.load(os.path.join(mcvine_outdir, "Ixy.h5")) mcvine_Ixdivx = hh.load(os.path.join(mcvine_outdir, "Ixdivx.h5")) Ixy = hh.load(os.path.join(outdir, "Ixy.h5")) Ixdivx = hh.load(os.path.join(outdir, "Ixdivx.h5")) global interactive if interactive: from histogram import plot as plotHist plotHist(mcvine_Ixy) plotHist(mcvine_Ixdivx) plotHist(Ixy) plotHist(Ixdivx) assert mcvine_Ixy.shape() == Ixy.shape() assert mcvine_Ixdivx.shape() == Ixdivx.shape() assert np.allclose(mcvine_Ixy.data().storage(), Ixy.data().storage()) assert np.allclose(mcvine_Ixdivx.data().storage(), Ixdivx.data().storage()) return
def test_compare_mcvine(): """ Tests the acc cpu implementation of a straight guide against mcvine """ num_neutrons = int(1e6) # Run the mcvine instrument first instr = os.path.join(thisdir, "tapered_guide_instrument.py") cpu_outdir = 'out.debug-tapered_guide_cpu_instrument' if os.path.exists(cpu_outdir): shutil.rmtree(cpu_outdir) run_script.run1(instr, cpu_outdir, ncount=num_neutrons, guide_factory="mcvine.components.optics.Guide_tapering", overwrite_datafiles=True) outdir = 'out.debug-tapered_guide_gpu_instrument' if os.path.exists(outdir): shutil.rmtree(outdir) run_script.run1(instr, outdir, ncount=num_neutrons, guide_mod="mcvine.acc.components.optics.guide_tapering", overwrite_datafiles=True) # Compare output files cpu_Ixy = hh.load(os.path.join(cpu_outdir, "Ixy.h5")) cpu_Ixdivx = hh.load(os.path.join(cpu_outdir, "Ixdivx.h5")) Ixy = hh.load(os.path.join(outdir, "Ixy.h5")) Ixdivx = hh.load(os.path.join(outdir, "Ixdivx.h5")) global interactive if interactive: from histogram import plot as plotHist plotHist(cpu_Ixy) plotHist(cpu_Ixdivx) plotHist(Ixy) plotHist(Ixdivx) assert cpu_Ixy.shape() == Ixy.shape() assert cpu_Ixdivx.shape() == Ixdivx.shape() assert np.allclose(cpu_Ixy.I, Ixy.I) assert np.allclose(cpu_Ixdivx.I, Ixdivx.I) return
def calc_runtime(n, nonacc_guide_factory, acc_guide_mod, mcvine_iters=1, acc_iters=10): """ Calculate the average runtimes of mcvine and acc implementations with a specific number of neutrons and iterations to run each implementation Parameters ---------- n : number of neutrons to run each implementation nonacc_guide_factory : factory of nonacc mcvine guide component acc_guide_mod : acc mcvine guide module mcvine_iters : number of iterations to run mcvine script to obtain runtime acc_iters : number of iterations to run acc script to obtain runtime Returns ------- Tuple containing average runtime (in ns) of mcvine and acc """ if mcvine_iters <= 0 or acc_iters <= 0: raise RuntimeError("Iteration count must be at least 1.") instr = os.path.join(thisdir, "guide_instrument.py") mcvine_outdir = 'out.debug-mcvine-timing' if os.path.exists(mcvine_outdir): shutil.rmtree(mcvine_outdir) outdir = 'out.debug-acc-timing' if os.path.exists(outdir): shutil.rmtree(outdir) # Lists holding the runtime for each iteration mcvine_times = [] acc_times = [] for iter in range(mcvine_iters): mcvine_start = time.time_ns() run_script.run1(instr, mcvine_outdir, ncount=n, guide_factory=nonacc_guide_factory, overwrite_datafiles=True) mcvine_end = time.time_ns() mcvine_dur = mcvine_end - mcvine_start mcvine_times.append(mcvine_dur) for iter in range(acc_iters): acc_start = time.time_ns() run_script.run1(instr, outdir, ncount=n, guide_mod=acc_guide_mod, overwrite_datafiles=True) acc_end = time.time_ns() acc_dur = acc_end - acc_start acc_times.append(acc_dur) mcvine_avg = np.sum(np.asarray(mcvine_times)) / len(mcvine_times) acc_avg = np.sum(np.asarray(acc_times)) / len(acc_times) return mcvine_avg, acc_avg
def run(instrument_script, ncount, niters, mpi=False, nodes=1, acc_run=False, skip_ncounts=[], **kwds): outdir = 'out.debug-check-speed' if os.path.exists(outdir): shutil.rmtree(outdir) # TODO: this stdout file gets overwritten on each call, move or use append? stdout = open("./check-speed-stdout.txt", "w") sys.stderr = stdout avg_times = [] for n in ncount: if n in skip_ncounts: # insert element to preserve ordering avg_times.append(np.nan) continue print(" Running '{}' with n={}".format(instrument_script, n)) times = [] # redirect script output to reduce terminal clutter for iter in range(niters + 1): sys.stdout = stdout buffer_size = int(n) if n > 1e6 else int(1e6) if buffer_size > 1e9: buffer_size = int(1e9) # get the runtime for the script. Note: this probably isn't the # best timing, since it will include the overhead of launching the # script and any file IO done inside the script, but there is no # current way to time individual components. # TODO: the stdout could be parsed to pull the process timings for acc components # TODO: there is still mcvine component output, likely from a subprocess in which this redirection has no effect if mpi: # run script in MPI mode # NOTE: with mcvine=0.44, pyyaml 5.3 must be used to avoid yaml load error. This is fixed in newer versions of mcvine. time_before = time.time_ns() run_script.run_mpi(instrument_script, outdir, nodes=nodes, buffer_size=buffer_size, ncount=n, overwrite_datafiles=True, **kwds) time_after = time.time_ns() elif acc_run: # use accelerated run script time_before = time.time_ns() run_acc_script.run(instrument_script, outdir, buffer_size=buffer_size, ncount=n, overwrite_datafiles=True, **kwds) time_after = time.time_ns() else: # use default run script (mcvine, single node) time_before = time.time_ns() run_script.run1(instrument_script, outdir, buffer_size=buffer_size, ncount=n, overwrite_datafiles=True, **kwds) time_after = time.time_ns() # skip first run if iter < 1: continue delta = time_after - time_before times.append(delta) sys.stdout = sys.__stdout__ time_avg = np.sum(np.asarray(times)) / len(times) avg_times.append(time_avg) print(" TIME = {} ms ({} s)".format(time_avg * 1e-6, time_avg * 1e-9)) sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ stdout.close() return avg_times
def compare_acc_nonacc( className, monitors, tolerances, num_neutrons, debug, workdir=None, instr=None, interactive=False, acc_component_spec = None, nonacc_component_spec = None, ): """ Tests the acc cpu implementation of an instrument against mcvine. Parameters: className (str): the name of the instrument class under test, e.g., "Guide" monitors (list): the names of the monitors to use in testing, e.g., "Ixy" tolerances (dict): tolerance of float comparisons, e.g., "float32": 1e-8 num_neutrons (int): how many neutrons to use in the testing debug (bool): if to save the neutrons that exit the instrument """ if debug: assert num_neutrons < 1001 classname = className.lower() # Run the mcvine instrument first instr = instr or os.path.join(workdir, f"{classname}_instrument.py") mcvine_outdir = f"out.debug-mcvine_{classname}_cpu_instrument" if os.path.exists(mcvine_outdir): shutil.rmtree(mcvine_outdir) # there is inconsistency in the simulation instrument implementations. # for example, tests/components/optics/guide_instrument.py # implements "is_acc", "factory" and "module" kwargs, # but tests/components/optics/slit_instrument.py # only implements "is_acc". # The implementation here works, but we just need to make sure # the {classname}_instrument.py implements "is_acc" kwd correctly. nonacc_component_spec = nonacc_component_spec or dict( factory=f"mcvine.components.optics.{className}", is_acc = False, ) run_script.run1( instr, mcvine_outdir, ncount=num_neutrons, buffer_size=num_neutrons, save_neutrons_after=debug, overwrite_datafiles=True, **nonacc_component_spec ) # Run our accelerated implementation outdir = f"out.debug-{classname}_gpu_instrument" if os.path.exists(outdir): shutil.rmtree(outdir) acc_component_spec = acc_component_spec or dict( module=f"mcvine.acc.components.optics.{classname}", is_acc = True, ) run_script.run1( instr, outdir, ncount=num_neutrons, buffer_size=num_neutrons, save_neutrons_after=debug, overwrite_datafiles=True, **acc_component_spec ) # Compare output files tolerance = tolerances[floattype] for monitor in monitors: mcvine = hh.load(os.path.join(mcvine_outdir, monitor + ".h5")) mcvine_acc = hh.load(os.path.join(outdir, monitor + ".h5")) if interactive: from histogram import plot as plotHist plotHist(mcvine) plotHist(mcvine_acc) plotHist((mcvine_acc - mcvine) / mcvine) assert mcvine.shape() == mcvine_acc.shape() assert np.allclose(mcvine.data().storage(), mcvine_acc.data().storage(), atol=tolerance)
#!/usr/bin/env python import os, shutil thisdir = os.path.dirname(__file__) from mcvine import run_script from mcvine import run_script instr = os.path.join(thisdir, "sgm_instrument.py") ncount = 1e6 outdir = f'out.nonacc_src_guide_mon_n{ncount}' if os.path.exists(outdir): shutil.rmtree(outdir) ncount = int(ncount) print(type(ncount)) # run_script.run_mpi( run_script.run1( instr, outdir, # nodes=40, ncount=ncount, buffer_size=int(1e6), overwrite_datafiles=True)