From 9dbc97c32a48003fb13412fad6b4078d66596ba2 Mon Sep 17 00:00:00 2001 From: archibate <17721388340@163.com> Date: Sun, 15 Mar 2020 15:09:38 +0800 Subject: [PATCH 1/4] use argparse for ti test, rename test_python -> test --- .travis.yml | 2 +- appveyor.yml | 1 + python/taichi/main.py | 35 ++++++++++++++++++----------------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index f37088ca7d1a4..cc0a7520ba27d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -56,7 +56,7 @@ script: - export TAICHI_REPO_DIR=$TRAVIS_BUILD_DIR - export PYTHONPATH=$TAICHI_REPO_DIR/python - export PATH=$TAICHI_REPO_DIR/bin:$PATH -- ti test_verbose && cd python && $PYTHON build.py try_upload +- ti test -v && ti test_cpp -v && cd python && $PYTHON build.py try_upload env: global: diff --git a/appveyor.yml b/appveyor.yml index 68002f51c342b..0118079406541 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -60,4 +60,5 @@ build_script: - '%PYTHON% -c "import taichi"' - "%PYTHON% examples/laplace.py" - "%PYTHON% bin/taichi test" + - "%PYTHON% bin/taichi test_cpp" - "cd python && %PYTHON% build.py try_upload" diff --git a/python/taichi/main.py b/python/taichi/main.py index 485a91299f001..1891b6fb48028 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -3,6 +3,7 @@ import shutil import time import random +import argparse from taichi.tools.video import make_video, interpolate_frames, mp4_to_gif, scale_video, crop_video, accelerate_video @@ -30,7 +31,7 @@ def test_python(test_files=(), verbose=False): # run all the tests args = [test_dir] if verbose: - args += ['-s'] + args += ['-s', '-v'] if len(test_files) == 0 or len(test_files) > 4: if int(pytest.main([os.path.join(root_dir, 'misc/empty_pytest.py'), '-n1'])) == 0: # if pytest has xdist try: @@ -57,7 +58,18 @@ def test_cpp(test_files=()): return int(task.run(*test_files)) +def make_argument_parser(): + parser = argparse.ArgumentParser() + parser.add_argument('action', help='See `ti help` for more details') + parser.add_argument('-v', '--verbose', action='store_true', help='run with verbose outputs') + parser.add_argument('files', nargs='*', help='Files to be tested') + return parser + + def main(debug=False): + parser = make_argument_parser() + args = parser.parse_args() + lines = [] print() lines.append(u' *******************************************') @@ -75,13 +87,11 @@ def main(debug=False): import taichi as ti argc = len(sys.argv) - if argc == 1 or sys.argv[1] == 'help': + if argc == 1 or args.action == 'help': print( " Usage: ti run [task name] |-> Run a specific task\n" " ti benchmark |-> Run performance benchmark\n" - " ti test |-> Run all tests\n" - " ti test_verbose |-> Run all tests with verbose outputs\n" - " ti test_python |-> Run python tests\n" + " ti test |-> Run python tests\n" " ti test_cpp |-> Run cpp tests\n" " ti format |-> Reformat modified source files\n" " ti format_all |-> Reformat all source files\n" @@ -95,7 +105,7 @@ def main(debug=False): " ti release |-> Make source code release\n" " ti debug [script.py] |-> Debug script\n") exit(0) - mode = sys.argv[1] + mode = args.action t = time.time() if mode.endswith('.py'): @@ -117,19 +127,10 @@ def main(debug=False): with open(name) as script: script = script.read() exec(script, {'__name__': '__main__'}) - elif mode == "test_python": - return test_python(test_files=sys.argv[2:]) elif mode == "test_cpp": - return test_cpp(test_files=sys.argv[2:]) + return test_cpp(test_files=args.files) elif mode == "test": - if test_python(test_files=sys.argv[2:]) != 0: - return -1 - if len(sys.argv) <= 2: - return test_cpp() - elif mode == "test_verbose": - if test_python(test_files=sys.argv[2:], verbose=True) != 0: - return -1 - return test_cpp() + return test_python(test_files=args.files, verbose=args.verbose) elif mode == "build": ti.core.build() elif mode == "format": From dfe706f6b8a948ae3c037377aaa5a37e0004f528 Mon Sep 17 00:00:00 2001 From: archibate <17721388340@163.com> Date: Sun, 15 Mar 2020 21:36:57 +0800 Subject: [PATCH 2/4] supported_archs detect wanted_archs, --arch arg --- python/taichi/lang/__init__.py | 15 +++++++++++++-- python/taichi/main.py | 5 ++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/python/taichi/lang/__init__.py b/python/taichi/lang/__init__.py index b464d05e150c8..bebb6268f1440 100644 --- a/python/taichi/lang/__init__.py +++ b/python/taichi/lang/__init__.py @@ -214,6 +214,12 @@ def benchmark(func, repeat=100, args=()): elapsed = time.time() - t return elapsed / repeat +wanted_archs = None + +def set_wanted_archs(archs): + global wanted_archs + wanted_archs = archs + def supported_archs(): import taichi as ti archs = [ti.core.host_arch()] @@ -223,6 +229,11 @@ def supported_archs(): archs.append(metal) if ti.core.with_opengl(): archs.append(opengl) + if wanted_archs is not None: + archs, old_archs = [], archs + for arch in old_archs: + if ti.core.arch_name(arch) in wanted_archs: + archs.append(arch) return archs class _ArchCheckers(object): @@ -286,11 +297,11 @@ def all_archs(test): # # Example usage: # -# ti.archs_excluding(ti.cuda, ti.metal) +# @ti.archs_excluding(ti.cuda, ti.metal) # def test_xx(): # ... # -# ti.archs_excluding(ti.cuda, default_fp=ti.f64) +# @ti.archs_excluding(ti.cuda, default_fp=ti.f64) # def test_yy(): # ... def archs_excluding(*excluded_archs, **kwargs): diff --git a/python/taichi/main.py b/python/taichi/main.py index 1891b6fb48028..0acca5ef8139e 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -61,7 +61,8 @@ def test_cpp(test_files=()): def make_argument_parser(): parser = argparse.ArgumentParser() parser.add_argument('action', help='See `ti help` for more details') - parser.add_argument('-v', '--verbose', action='store_true', help='run with verbose outputs') + parser.add_argument('-v', '--verbose', action='store_true', help='Run with verbose outputs') + parser.add_argument('-a', '--arch', help='Specify arch(s) to run test on, e.g. -a opengl,metal') parser.add_argument('files', nargs='*', help='Files to be tested') return parser @@ -85,6 +86,8 @@ def main(debug=False): print(u'\n'.join(lines)) print() import taichi as ti + if args.arch is not None: + ti.set_wanted_archs(args.arch.split(',')) argc = len(sys.argv) if argc == 1 or args.action == 'help': From 153bb3ae6e2bbb8acb138b0a6de395cacccaf923 Mon Sep 17 00:00:00 2001 From: archibate <17721388340@163.com> Date: Thu, 19 Mar 2020 00:34:02 +0800 Subject: [PATCH 3/4] unify test_cpp into test as mentioned in #601 --- .travis.yml | 2 +- appveyor.yml | 1 - python/taichi/main.py | 10 +++++----- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index cc0a7520ba27d..260dbecd80084 100644 --- a/.travis.yml +++ b/.travis.yml @@ -56,7 +56,7 @@ script: - export TAICHI_REPO_DIR=$TRAVIS_BUILD_DIR - export PYTHONPATH=$TAICHI_REPO_DIR/python - export PATH=$TAICHI_REPO_DIR/bin:$PATH -- ti test -v && ti test_cpp -v && cd python && $PYTHON build.py try_upload +- ti test -v && cd python && $PYTHON build.py try_upload env: global: diff --git a/appveyor.yml b/appveyor.yml index 0118079406541..68002f51c342b 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -60,5 +60,4 @@ build_script: - '%PYTHON% -c "import taichi"' - "%PYTHON% examples/laplace.py" - "%PYTHON% bin/taichi test" - - "%PYTHON% bin/taichi test_cpp" - "cd python && %PYTHON% build.py try_upload" diff --git a/python/taichi/main.py b/python/taichi/main.py index 0acca5ef8139e..9c3daed441cfa 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -94,8 +94,7 @@ def main(debug=False): print( " Usage: ti run [task name] |-> Run a specific task\n" " ti benchmark |-> Run performance benchmark\n" - " ti test |-> Run python tests\n" - " ti test_cpp |-> Run cpp tests\n" + " ti test |-> Run all the tests\n" " ti format |-> Reformat modified source files\n" " ti format_all |-> Reformat all source files\n" " ti build |-> Build C++ files\n" @@ -130,10 +129,11 @@ def main(debug=False): with open(name) as script: script = script.read() exec(script, {'__name__': '__main__'}) - elif mode == "test_cpp": - return test_cpp(test_files=args.files) elif mode == "test": - return test_python(test_files=args.files, verbose=args.verbose) + ret = test_python(test_files=args.files, verbose=args.verbose) + if ret: exit(-1) + ret = test_cpp(test_files=args.files) + return ret elif mode == "build": ti.core.build() elif mode == "format": From 494778d32ac19540c2c925221059081ab2049282 Mon Sep 17 00:00:00 2001 From: archibate <17721388340@163.com> Date: Thu, 19 Mar 2020 00:37:16 +0800 Subject: [PATCH 4/4] use return -1 instead of exit(-1) --- python/taichi/main.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/python/taichi/main.py b/python/taichi/main.py index 9c3daed441cfa..a7fb32f3f1573 100644 --- a/python/taichi/main.py +++ b/python/taichi/main.py @@ -106,7 +106,7 @@ def main(debug=False): " ti doc |-> Build documentation\n" " ti release |-> Make source code release\n" " ti debug [script.py] |-> Debug script\n") - exit(0) + return 0 mode = args.action t = time.time() @@ -116,7 +116,7 @@ def main(debug=False): elif mode == "run": if argc <= 2: print("Please specify [task name], e.g. test_math") - exit(-1) + return -1 name = sys.argv[2] task = ti.Task(name) task.run(*sys.argv[3:]) @@ -124,14 +124,14 @@ def main(debug=False): ti.core.set_core_trigger_gdb_when_crash(True) if argc <= 2: print("Please specify [file name], e.g. render.py") - exit(-1) + return -1 name = sys.argv[2] with open(name) as script: script = script.read() exec(script, {'__name__': '__main__'}) elif mode == "test": ret = test_python(test_files=args.files, verbose=args.verbose) - if ret: exit(-1) + if ret: return -1 ret = test_cpp(test_files=args.files) return ret elif mode == "build": @@ -183,7 +183,7 @@ def main(debug=False): elif mode == "video_crop": if len(sys.argv) != 7: print('Usage: ti video_crop fn x_begin x_end y_begin y_end') - exit(-1) + return -1 input_fn = sys.argv[2] assert input_fn[-4:] == '.mp4' output_fn = input_fn[:-4] + '-cropped.mp4' @@ -195,7 +195,7 @@ def main(debug=False): elif mode == "video_speed": if len(sys.argv) != 4: print('Usage: ti video_speed fn speed_up_factor') - exit(-1) + return -1 input_fn = sys.argv[2] assert input_fn[-4:] == '.mp4' output_fn = input_fn[:-4] + '-sped.mp4'