def test_choice_class_value_pre(): '''check that the Choice class returns the specified inputs modified by the pre value if it is provided''' test_name = "umm" test_values = ["test1", "test2", "test3"] pre_value = "pre_" choice = Choice(test_name, test_values, pre=pre_value) assert len(choice.options) == len(test_values) for idx, _ in enumerate(test_values): assert choice.options[idx] == pre_value + test_values[idx]
def test_choice_class_value_vanilla(): '''check that the Choice class stores the required name and inputs arguments correctly''' test_values = ["test1", "test2", "test3"] test_name = "err" choice = Choice(name=test_name, inputs=test_values) assert len(choice.options) == len(test_values) for idx, _ in enumerate(test_values): assert choice.options[idx] == test_values[idx] assert choice.name == test_name
def test_run(capsys): '''check that bruteforce runs when we provide a valid set of inputs''' from melody.inputs import Choice inputs = [Choice(name="c", inputs=["c1", "c2"])] def function(input_var): '''test function''' return 0, input_var search_method = BruteForce(function=function, inputs=inputs) search_method.run() out, _ = capsys.readouterr() assert "[{'c': 'c1'}] 0 [{'c': 'c1'}]" in out assert "[{'c': 'c2'}] 0 [{'c': 'c2'}]" in out
def test_func_too_many_ret_args(): '''check that an exception is raised if too many function return arguments are provided''' from melody.inputs import Choice choice = Choice(name="c", inputs=["c1", "c2"]) inputs = [choice] def function(inputs): '''test function''' return inputs, 0, 0 search_method = BruteForce(inputs=inputs, function=function) with pytest.raises(RuntimeError) as excinfo: search_method.run() assert "function must return 2 values" in str(excinfo.value)
def test_melody_search(capsys): '''check that melody search runs when we provide a valid set of inputs''' from melody.inputs import Choice from melody.search import BruteForce inputs = [Choice(name="c", inputs=["c1", "c2"])] def function(input_var): '''test function''' return 0, input_var search_method = BruteForce melody = Melody(method=search_method, inputs=inputs, function=function) melody.search() out, _ = capsys.readouterr() assert "[{'c': 'c1'}] 0 [{'c': 'c1'}]" in out assert "[{'c': 'c2'}] 0 [{'c': 'c2'}]" in out
def test_state_run(capsys, monkeypatch): '''check that bruteforce runs when we provide a valid set of inputs with state''' from melody.inputs import Choice choice = Choice(name="c", inputs=["c1", "c2"]) monkeypatch.setattr(choice, "_state", True) inputs = [choice] def function(input_var): '''test function''' return 0, input_var state = "state" search_method = BruteForce(function=function, inputs=inputs, state=state) search_method.run() out, _ = capsys.readouterr() assert "[{'c': 'c1'}] 0 [{'c': 'c1'}]" in out assert "[{'c': 'c2'}] 0 [{'c': 'c2'}]" in out
def __init__(self): Choice.__init__(self, name="Array Bounds", options=["", "Specified", "Constant"])
siblings = loop.parent.children my_index = siblings.index(loop) option = [] self._recurse(siblings, my_index, option, my_options, invoke) return my_options @property def name(self): '''Returns the name of the melody input class''' return self._name FILE = ("shallow/shallow_alg.f90") _, INVOKE_INFO = parse(FILE, api="gocean1.0") PSY = PSyFactory("gocean1.0").create(INVOKE_INFO) # TBD ArrayBounds(), INPUTS = [ GOLoopFuse(), ModuleInline(PSY), Choice(name="Problem Size", inputs=["64", "128", "256", "512", "1024"]) ] MELODY = Melody(inputs=INPUTS, function=test_function, state=PSY, method=BruteForce) MELODY.search()
# '''A first example showing how to initialise and run melody. The different input types are shown, along with a dummy test function, using a brute force search''' from melody.inputs import Fixed, Switch, Choice from melody.search import BruteForce from melody.main import Melody from execute import execute # inputs taken from https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html # more flags and info is here ... https://gcc.gnu.org/onlinedocs/gcc-4.5.3/ # gcc/i386-and-x86_002d64-Options.html INPUTS = [ Fixed(name="F90", value="gfortran"), Choice(name="F90FLAGS", inputs=["-O2", "-O3", "-Ofast"]), Choice(name="native", inputs=["", "-mtune=native", "-march=native"]), Switch(name="Inline limit", on="-finline-limit=5000"), Fixed(name="Problem Size", value="128")] # TBD # *** try different inline-limit values # -fwhole-program needs to be part of compile and fails as we link timer # libraries Switch(name="Single program", on="-fwhole-program"), # flto needs to be on both compile and link as do all optimisation options # ignore lower levels of optimisation # Choice(name="F90FLAGS", inputs=["", "-O", "-O1", "-O2", "-O3", "-Ofast"]), # start with one problem size to limit the time # Choice(name="Problem Size", inputs=["64", "128", "256"])]
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # # Author R. Ford STFC Daresbury Lab. # '''A first example showing how to initialise and run melody. The different input types are shown, along with a dummy test function, using a brute force search''' from melody.inputs import Switch, Choice, IntRange, FloatRange, Subsets from melody.search import BruteForce from melody.main import Melody def test_function(_): '''A dummy test function. The function takes a list of inputs as an argument and returns whether the execution was successful (in our case we always return True) followed by the target output (in our case we simply output a fixed value''' return True, {"value": 10} INPUTS = [ Switch(name="Debug Flag", off="", on="-g"), Choice(name="Opt Flags", inputs=["-O", "-O2", "-O3"]), IntRange(name="Segment Size", low=0, high=20, step=10), FloatRange(name="Tolerance", low=0.0, high=1.0, step=0.5), Subsets(name="Silly", inputs=["-fast", "-great", "-super"])] MELODY = Melody(inputs=INPUTS, function=test_function, method=BruteForce) MELODY.search()
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # # Author R. Ford STFC Daresbury Lab. # '''A demonstration of how to search some gromacs options. We will use jinja2 to set input values, Longbow to run gromacs and some text processing to extract the required results. However, we do not yet do so.''' from melody.inputs import Choice, FloatRange from melody.main import Melody from melody.search import BruteForce from launch import launch INPUTS = [ Choice(name="tcoupl", inputs=["Berendsen", "Nose-Hoover"]), FloatRange(name="rcoulomb", low=1.1, high=1.6, step=0.1) ] MELODY = Melody(inputs=INPUTS, function=launch, method=BruteForce) MELODY.search()