Exemple #1
0
    def gif(self, arguments: list = sys.argv[2:]):
        """Convert mp4 file to gif in the same directory"""
        parser = argparse.ArgumentParser(prog='ti gif',
                                         description=f"{self.gif.__doc__}")
        parser.add_argument('-i',
                            '--input',
                            required=True,
                            dest='input_file',
                            type=TaichiMain._mp4_file,
                            help="Path to input MP4 video file")
        parser.add_argument('-f',
                            '--framerate',
                            required=False,
                            default=24,
                            dest='framerate',
                            type=int,
                            help="Frame rate of the output GIF")
        args = parser.parse_args(arguments)

        args.output_file = str(Path(args.input_file).with_suffix('.gif'))
        ti.info(f"Converting {args.input_file} to {args.output_file}")

        # Short circuit for testing
        if self.test_mode: return args
        mp4_to_gif(args.input_file, args.output_file, args.framerate)
Exemple #2
0
 def on_pre_exit(self):
     if hasattr(self, 'video_manager'):
         ext = self.video_manager.taichi_glsl_output_ext
         ti.info('Saving result to {}.{}',
                 self.video_manager.get_output_filename(''), ext
                 or 'gif and mp4')
         self.video_manager.make_video(gif=(not ext or ext == 'gif'),
                                       mp4=(not ext or ext == 'mp4'))
Exemple #3
0
    def materialize(self):
        if self.materialized:
            return
        self.create_program()
        Expr.layout_materialized = True

        def layout():
            for func in self.layout_functions:
                func()

        import taichi as ti
        ti.info("Materializing layout...".format())
        taichi_lang_core.layout(layout)
        self.materialized = True
        for var in self.global_vars:
            assert var.ptr.snode() is not None, 'Some variable(s) not placed'
Exemple #4
0
def _shell_pop_print(old_call):
    if not pybuf_enabled:
        # zero-overhead!
        return old_call

    ti.info('Graphical python shell detected, using wrapped sys.stdout')

    @functools.wraps(old_call)
    def new_call(*args, **kwargs):
        _taichi_skip_traceback = 1
        ret = old_call(*args, **kwargs)
        # print's in kernel won't take effect until ti.sync(), discussion:
        # https://github.com/taichi-dev/taichi/pull/1303#discussion_r444897102
        print(taichi_lang_core.pop_python_print_buffer(), end='')
        return ret

    return new_call
Exemple #5
0
    def video(self, arguments: list = sys.argv[2:]):
        """Make a video using *.png files in the current directory"""
        parser = argparse.ArgumentParser(description=f"{self.video.__doc__}")
        parser.add_argument("inputs", nargs='*', help="PNG file(s) as inputs")
        parser.add_argument('-o',
                            '--output',
                            required=False,
                            default=Path('./video.mp4').resolve(),
                            dest='output_file',
                            type=lambda x: Path(x).resolve(),
                            help="Path to output MP4 video file")
        parser.add_argument('-f',
                            '--framerate',
                            required=False,
                            default=24,
                            dest='framerate',
                            type=int,
                            help="Frame rate of the output MP4 video")
        args = parser.parse_args(arguments)

        if not args.inputs:
            args.inputs = [str(p.resolve()) for p in Path('.').glob('*.png')]

        ti.info(f'Making video using {len(args.inputs)} png files...')
        ti.info(f'frame_rate = {args.framerate}')

        # Short circuit for testing
        if self.test_mode: return args
        make_video(args.inputs,
                   output_path=str(args.output_file),
                   frame_rate=args.framerate)
        ti.info(f'Done! Output video file = {args.output_file}')
Exemple #6
0
    def video(self, arguments: list = sys.argv[2:]):
        """Make a video using *.png files in the current directory"""
        parser = argparse.ArgumentParser(prog='ti video',
                                         description=f"{self.video.__doc__}")
        parser.add_argument("inputs", nargs='*', help="PNG file(s) as inputs")
        parser.add_argument('-o',
                            '--output',
                            required=False,
                            default=Path('./video.mp4').resolve(),
                            dest='output_file',
                            type=lambda x: Path(x).resolve(),
                            help="Path to output MP4 video file")
        parser.add_argument('-f',
                            '--framerate',
                            required=False,
                            default=24,
                            dest='framerate',
                            type=int,
                            help="Frame rate of the output MP4 video")
        parser.add_argument(
            '-c',
            '--crf',
            required=False,
            default=20,
            dest='crf',
            type=int,
            help="Constant rate factor (0-51, lower is higher quality)")
        args = parser.parse_args(arguments)

        if not args.inputs:
            args.inputs = sorted(
                str(p.resolve()) for p in Path('.').glob('*.png'))

        assert 1 <= args.crf <= 51, "The range of the CRF scale is 1-51, where 1 is almost lossless, 20 is the default, and 51 is worst quality possible."

        ti.info(f'Making video using {len(args.inputs)} png files...')
        ti.info(f'frame_rate = {args.framerate}')

        # Short circuit for testing
        if self.test_mode:
            return args
        video.make_video(args.inputs,
                         output_path=str(args.output_file),
                         crf=args.crf,
                         frame_rate=args.framerate)
        ti.info(f'Done! Output video file = {args.output_file}')

        return None
Exemple #7
0
def init(arch=None,
         default_fp=None,
         default_ip=None,
         print_preprocessed=None,
         debug=None,
         **kwargs):
    # Make a deepcopy in case these args reference to items from ti.cfg, which are
    # actually references. If no copy is made and the args are indeed references,
    # ti.reset() could override the args to their default values.
    default_fp = _deepcopy(default_fp)
    default_ip = _deepcopy(default_ip)
    kwargs = _deepcopy(kwargs)
    import taichi as ti
    ti.reset()

    if default_fp is None:  # won't override
        dfl_fp = os.environ.get("TI_DEFAULT_FP")
        if dfl_fp == 32:
            default_fp = core.DataType.f32
        elif dfl_fp == 64:
            default_fp = core.DataType.f64
        elif dfl_fp is not None:
            raise ValueError(
                f'Unrecognized TI_DEFAULT_FP: {dfl_fp}, should be 32 or 64')
    if default_ip is None:
        dfl_ip = os.environ.get("TI_DEFAULT_IP")
        if dfl_ip == 32:
            default_ip = core.DataType.i32
        elif dfl_ip == 64:
            default_ip = core.DataType.i64
        elif dfl_ip is not None:
            raise ValueError(
                f'Unrecognized TI_DEFAULT_IP: {dfl_ip}, should be 32 or 64')

    if print_preprocessed is None:  # won't override
        print_preprocessed = os.environ.get("TI_PRINT_PREPROCESSED")
        if print_preprocessed is not None:
            print_preprocessed = bool(int(print_preprocessed))

    if default_fp is not None:
        ti.get_runtime().set_default_fp(default_fp)
    if default_ip is not None:
        ti.get_runtime().set_default_ip(default_ip)
    if print_preprocessed is not None:
        ti.get_runtime().print_preprocessed = print_preprocessed

    if debug is None:
        debug = bool(int(os.environ.get('TI_DEBUG', '0')))
    if debug:
        ti.set_logging_level(ti.TRACE)
    ti.cfg.debug = debug

    unified_memory = os.environ.get('TI_USE_UNIFIED_MEMORY', '')
    if unified_memory != '':
        use_unified_memory = bool(int(unified_memory))
        ti.cfg.use_unified_memory = use_unified_memory
        if not use_unified_memory:
            ti.trace(
                'Unified memory disabled (env TI_USE_UNIFIED_MEMORY=0). This is experimental.'
            )

    for k, v in kwargs.items():
        setattr(ti.cfg, k, v)

    def bool_int(x):
        return bool(int(x))

    def environ_config(key, cast=bool_int):
        name = 'TI_' + key.upper()
        value = os.environ.get(name, '')
        if len(value):
            setattr(ti.cfg, key, cast(value))

        # TI_ASYNC=   : not work
        # TI_ASYNC=0  : False
        # TI_ASYNC=1  : True

    # does override
    environ_config("print_ir")
    environ_config("verbose")
    environ_config("fast_math")
    environ_config("async")
    environ_config("print_benchmark_stat")
    environ_config("device_memory_fraction", float)
    environ_config("device_memory_GB", float)

    # Q: Why not environ_config("gdb_trigger")?
    # A: We don't have ti.cfg.gdb_trigger yet.
    # Discussion: https://github.com/taichi-dev/taichi/pull/879
    gdb_trigger = os.environ.get('TI_GDB_TRIGGER', '')
    if len(gdb_trigger):
        ti.set_gdb_trigger(bool(int(gdb_trigger)))

    advanced_optimization = os.environ.get('TI_ADVANCED_OPTIMIZATION', '')
    if len(advanced_optimization):
        ti.core.toggle_advanced_optimization(bool(int(advanced_optimization)))

    # Q: Why not environ_config("arch", ti.core.arch_from_name)?
    # A: We need adaptive_arch_select for all.
    env_arch = os.environ.get("TI_ARCH")
    if env_arch is not None:
        ti.info(f'Following TI_ARCH setting up for arch={env_arch}')
        arch = ti.core.arch_from_name(env_arch)

    ti.cfg.arch = adaptive_arch_select(arch)
    print(f'[Taichi] Starting on arch={ti.core.arch_name(ti.cfg.arch)}')

    log_level = os.environ.get("TI_LOG_LEVEL")
    if log_level is not None:
        ti.set_logging_level(log_level.lower())

    ti.get_runtime().create_program()
Exemple #8
0
def main():
    lines = []
    print()
    lines.append(u'{:^43}'.format(u' '.join([u'\u262f'] * 8)))
    lines.append(u' *******************************************')
    lines.append(u' **                Taichi                 **')
    lines.append(u' **                ~~~~~~                 **')
    lines.append(u' ** Open Source Computer Graphics Library **')
    lines.append(u' *******************************************')
    lines.append(u'{:^43}'.format(u"\u2630 \u2631 \u2632 \u2633 "
                                  "\u2634 \u2635 \u2636 \u2637"))
    print(u'\n'.join(lines))
    print()
    import taichi as tc

    argc = len(sys.argv)
    if argc == 1 or sys.argv[1] == 'help':
        print(
            "    Usage: ti run [task name]        |-> Run a specific task\n"
            "           ti test                   |-> Run tests\n"
            "           ti daemon                 |-> Start daemon process\n"
            "           ti proj                   |-> List all projects\n"
            "           ti proj activate [name]   |-> Activate project\n"
            "           ti proj deactivate [name] |-> Deactivate project\n"
            "           ti build                  |-> Build C++ files\n"
            "           ti clean asm [*.s]        |-> Clean up gcc ASM\n"
            "           ti plot [*.txt]           |-> Plot a memory usage curve\n"
            "           ti update                 |-> Update taichi and projects\n"
            "           ti video                  |-> Make a video using *.png files in the current folder\n"
            "           ti convert                |-> Delete color controllers in a log file\n"
            "           ti exec                   |-> Invoke a executable in the 'build' folder\n"
            "           ti format                 |-> Format taichi and projects\n"
            "                                         (C++ source and python scripts)\n"
            "           ti statement [statement]  |-> Execute a single statement (with taichi imported as tc\n"
            "           ti [script.py]            |-> Run script\n"
            "           ti debug [script.py]      |-> Debug script\n")
        exit(-1)
    mode = sys.argv[1]

    if mode.endswith('.py'):
        with open(mode) as script:
            script = script.read()
        exec(script, {'__name__': '__main__'})
        exit()

    if mode == "run":
        if argc <= 2:
            print("Please specify [task name], e.g. test_math")
            exit(-1)
        name = sys.argv[2]
        task = tc.Task(name)
        task.run(*sys.argv[3:])
    elif mode == "debug":
        tc.core.set_core_trigger_gdb_when_crash(True)
        if argc <= 2:
            print("Please specify [file name], e.g. render.py")
            exit(-1)
        name = sys.argv[2]
        with open(name) as script:
            script = script.read()
        exec(script, {'__name__': '__main__'})
        exit()
    elif mode == "daemon":
        from taichi.system.daemon import start
        if len(sys.argv) > 2:
            # Master daemon
            start(True)
        else:
            # Slave daemon
            start(False)
    elif mode == "proj":
        if len(sys.argv) == 2:
            print_all_projects()
        elif sys.argv[2] == 'activate':
            proj = sys.argv[3]
            activate_package(proj)
        elif sys.argv[2] == 'deactivate':
            proj = sys.argv[3]
            deactivate_package(proj)
        else:
            assert False
    elif mode == "test":
        task = tc.Task('test')
        task.run(*sys.argv[2:])
    elif mode == "build":
        tc.core.build()
    elif mode == "format":
        tc.core.format()
    elif mode == "statement":
        exec(sys.argv[2])
    elif mode == "plot":
        plot(sys.argv[2])
    elif mode == "update":
        tc.core.update(True)
        tc.core.build()
    elif mode == "asm":
        fn = sys.argv[2]
        os.system(
            r"sed '/^\s*\.\(L[A-Z]\|[a-z]\)/ d' {0} > clean_{0}".format(fn))
    elif mode == "exec":
        import subprocess
        exec_name = sys.argv[2]
        folder = tc.get_bin_directory()
        assert exec_name in os.listdir(folder)
        subprocess.call([os.path.join(folder, exec_name)] + sys.argv[3:])
    elif mode == "interpolate":
        interpolate_frames('.')
    elif mode == "video":
        files = sorted(os.listdir('.'))
        files = list(filter(lambda x: x.endswith('.png'), files))
        if len(sys.argv) >= 3:
            frame_rate = int(sys.argv[2])
        else:
            frame_rate = 24
        if len(sys.argv) >= 4:
            trunc = int(sys.argv[3])
            files = files[:trunc]
        tc.info('Making video using {} png files...', len(files))
        tc.info("frame_rate={}", frame_rate)
        output_fn = 'video.mp4'
        make_video(files, output_path=output_fn, frame_rate=frame_rate)
        tc.info('Done! Output video file = {}', output_fn)
    elif mode == "convert":
        # http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
        # TODO: Windows support
        for fn in sys.argv[2:]:
            print("Converting logging file: {}".format(fn))
            tmp_fn = '/tmp/{}.{:05d}.backup'.format(fn,
                                                    random.randint(0, 10000))
            shutil.move(fn, tmp_fn)
            command = r'sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"'
            os.system('{} {} > {}'.format(command, tmp_fn, fn))
    else:
        print("Unknown command '{}'".format(mode))
        exit(-1)
Exemple #9
0
def init(arch=None,
         default_fp=None,
         default_ip=None,
         _test_mode=False,
         **kwargs):
    import taichi as ti

    # Make a deepcopy in case these args reference to items from ti.cfg, which are
    # actually references. If no copy is made and the args are indeed references,
    # ti.reset() could override the args to their default values.
    default_fp = _deepcopy(default_fp)
    default_ip = _deepcopy(default_ip)
    kwargs = _deepcopy(kwargs)
    ti.reset()

    spec_cfg = _SpecialConfig()
    env_comp = _EnvironmentConfigurator(kwargs, ti.cfg)
    env_spec = _EnvironmentConfigurator(kwargs, spec_cfg)

    # configure default_fp/ip:
    # TODO: move these stuff to _SpecialConfig too:
    env_default_fp = os.environ.get("TI_DEFAULT_FP")
    if env_default_fp:
        if default_fp is not None:
            core.warn(
                f'ti.init argument "default_fp" overridden by environment variable TI_DEFAULT_FP={env_default_fp}'
            )
        if env_default_fp == '32':
            default_fp = f32
        elif env_default_fp == '64':
            default_fp = f64
        elif env_default_fp is not None:
            raise ValueError(
                f'Invalid TI_DEFAULT_FP={env_default_fp}, should be 32 or 64')

    env_default_ip = os.environ.get("TI_DEFAULT_IP")
    if env_default_ip:
        if default_ip is not None:
            core.warn(
                f'ti.init argument "default_ip" overridden by environment variable TI_DEFAULT_IP={env_default_ip}'
            )
        if env_default_ip == '32':
            default_ip = i32
        elif env_default_ip == '64':
            default_ip = i64
        elif env_default_ip is not None:
            raise ValueError(
                f'Invalid TI_DEFAULT_IP={env_default_ip}, should be 32 or 64')

    if default_fp is not None:
        ti.get_runtime().set_default_fp(default_fp)
    if default_ip is not None:
        ti.get_runtime().set_default_ip(default_ip)

    # submodule configurations (spec_cfg):
    env_spec.add('print_preprocessed')
    env_spec.add('log_level', str)
    env_spec.add('gdb_trigger')
    env_spec.add('excepthook')

    # compiler configurations (ti.cfg):
    for key in dir(ti.cfg):
        if key in ['arch', 'default_fp', 'default_ip']:
            continue
        cast = type(getattr(ti.cfg, key))
        if cast is bool:
            cast = None
        env_comp.add(key, cast)

    unexpected_keys = kwargs.keys()
    if len(unexpected_keys):
        raise KeyError(
            f'Unrecognized keyword argument(s) for ti.init: {", ".join(unexpected_keys)}'
        )

    # dispatch configurations that are not in ti.cfg:
    if not _test_mode:
        ti.set_gdb_trigger(spec_cfg.gdb_trigger)
        ti.get_runtime().print_preprocessed = spec_cfg.print_preprocessed
        ti.set_logging_level(spec_cfg.log_level.lower())
        if spec_cfg.excepthook:
            # TODO(#1405): add a way to restore old excepthook
            ti.enable_excepthook()

    # select arch (backend):
    env_arch = os.environ.get('TI_ARCH')
    if env_arch is not None:
        ti.info(f'Following TI_ARCH setting up for arch={env_arch}')
        arch = ti.core.arch_from_name(env_arch)
    ti.cfg.arch = adaptive_arch_select(arch)
    print(f'[Taichi] Starting on arch={ti.core.arch_name(ti.cfg.arch)}')

    if _test_mode:
        return spec_cfg

    # create a new program:
    ti.get_runtime().create_program()
Exemple #10
0
    def __init__(self, **kwargs):
        res = kwargs['res']
        self.script_name = sys.argv[0].split('.')[0]
        assert (self.script_name.startswith('opt_'))
        self.script_name = self.script_name[4:]
        self.snapshot_period = kwargs.get('snapshot_period', 0)
        script_fn = os.path.join(os.getcwd(), sys.argv[0])

        suffix = ''

        self.version = kwargs.get('version', 0)
        if 'version' in kwargs:
            suffix += '_v{:0d}'.format(int(self.version))

        self.wireframe = kwargs.get('wireframe', False)
        if 'wireframe' in kwargs:
            if 'wireframe_grid_size' not in kwargs:
                kwargs['wireframe_grid_size'] = 10
            if 'wireframe_thickness' not in kwargs:
                kwargs['wireframe_thickness'] = 3
            if self.wireframe:
                suffix += '_wf{}g{}t{}'.format(int(self.wireframe),
                                               kwargs['wireframe_grid_size'],
                                               kwargs['wireframe_thickness'])
            else:
                suffix += '_wf{}'.format(int(self.wireframe))

        suffix += '_r{:04d}'.format(res[0])

        parser = argparse.ArgumentParser(description='Topology Optimization.')
        parser.add_argument('options',
                            metavar='Option',
                            type=str,
                            nargs='*',
                            help='An option to override')
        parser.add_argument('-c', type=str, help='iteration to start from')

        args = parser.parse_args()

        if args.c is not None:
            suffix += '_continue'

        self.task_id = get_unique_task_id()
        self.suffix = suffix + kwargs.get('suffix', '')

        self.working_directory = os.path.join(tc.get_output_directory(),
                                              'topo_opt', self.script_name,
                                              self.task_id + '_' + self.suffix)
        kwargs['working_directory'] = self.working_directory
        self.snapshot_directory = os.path.join(self.working_directory,
                                               'snapshots')
        self.fem_directory = os.path.join(self.working_directory, 'fem')
        self.fem_obj_directory = os.path.join(self.working_directory,
                                              'fem_obj')

        os.makedirs(self.snapshot_directory, exist_ok=True)
        os.makedirs(self.fem_directory, exist_ok=True)
        os.makedirs(self.fem_obj_directory, exist_ok=True)
        self.max_iterations = kwargs.get('max_iterations', 1000)

        self.log_fn = os.path.join(self.working_directory, 'log.txt')
        tc.start_memory_monitoring(os.path.join(self.working_directory,
                                                'memory_usage.txt'),
                                   interval=0.1)
        tc.duplicate_stdout_to_file(self.log_fn)
        tc.redirect_print_to_log()
        tc.trace("log_fn = {}", self.log_fn)

        with open(script_fn) as f:
            script_content = f.read()

        shutil.copy(sys.argv[0], self.working_directory + "/")
        tc.info("Script backuped")
        tc.info(
            "Script content:\n********************\n\n{}\n*******************",
            script_content)
        print(args)
        super().__init__(name='spgrid_topo_opt', **kwargs)

        if args.c is not None:
            # Restart (continue)
            print(args.options)
            print(args.c)

            last_iter = self.general_action(
                action='load_state',
                #filename=self.get_snapshot_file_name(args.c))
                filename=args.c)
            for o in args.options:
                o = o.split('=')
                assert (len(o) == 2)
                self.override_parameter(o[0], o[1])

            self.i_start = int(last_iter) + 1
            tc.info("\n*** Restarting from iter {}", self.i_start)
            self.run()
            exit()

        # Start from scratch
        self.i_start = 0

        tc.trace("log duplicated")
        if kwargs.get('check_log_file', True):
            assert (os.path.exists(self.log_fn))
Exemple #11
0
    def materialize(self, key=None, args=None, arg_features=None):
        if key is None:
            key = (self.func, 0)
        if not self.runtime.materialized:
            self.runtime.materialize()
        if key in self.compiled_functions:
            return
        grad_suffix = ""
        if self.is_grad:
            grad_suffix = "_grad"
        kernel_name = "{}_{}_{}".format(self.func.__name__, key[1],
                                        grad_suffix)
        import taichi as ti
        ti.info("Compiling kernel {}...".format(kernel_name))

        src = remove_indent(inspect.getsource(self.func))
        tree = ast.parse(src)
        if self.runtime.print_preprocessed:
            import astor
            print('Before preprocessing:')
            print(astor.to_source(tree.body[0]))

        func_body = tree.body[0]
        func_body.decorator_list = []

        visitor = ASTTransformer(
            excluded_paremeters=self.template_slot_locations,
            func=self,
            arg_features=arg_features)

        visitor.visit(tree)
        ast.fix_missing_locations(tree)

        if self.runtime.print_preprocessed:
            import astor
            print('After preprocessing:')
            print(astor.to_source(tree.body[0], indent_with='  '))

        ast.increment_lineno(tree, inspect.getsourcelines(self.func)[1] - 1)

        # Discussions: https://github.com/yuanming-hu/taichi/issues/282
        import copy
        global_vars = copy.copy(self.func.__globals__)

        freevar_names = self.func.__code__.co_freevars
        closure = self.func.__closure__
        if closure:
            freevar_values = list(map(lambda x: x.cell_contents, closure))
            for name, value in zip(freevar_names, freevar_values):
                global_vars[name] = value

        # inject template parameters into globals
        for i in self.template_slot_locations:
            template_var_name = self.argument_names[i]
            global_vars[template_var_name] = args[i]

        local_vars = {}
        exec(
            compile(tree,
                    filename=inspect.getsourcefile(self.func),
                    mode='exec'), global_vars, local_vars)
        compiled = local_vars[self.func.__name__]

        taichi_kernel = taichi_lang_core.create_kernel(kernel_name,
                                                       self.is_grad)

        # Do not change the name of 'taichi_ast_generator'
        # The warning system needs this identifier to remove unnecessary messages
        def taichi_ast_generator():
            self.runtime.inside_kernel = True
            compiled()
            self.runtime.inside_kernel = False

        taichi_kernel = taichi_kernel.define(taichi_ast_generator)

        assert key not in self.compiled_functions
        self.compiled_functions[key] = self.get_function_body(taichi_kernel)
Exemple #12
0
 def print_ast(tree, title=None):
     if not impl.get_runtime().print_preprocessed:
         return
     if title is not None:
         ti.info(f'{title}:')
     print(astor.to_source(tree.body[0], indent_with='    '), flush=True)
Exemple #13
0
def main():
    tc.info('test_logging, a = {}, b = {b}', 10, b=123)
    print(0)
Exemple #14
0
    def materialize(self, key=None, args=None, arg_features=None):
        if key is None:
            key = (self.func, 0)
        if not self.runtime.materialized:
            self.runtime.materialize()
        if key in self.compiled_functions:
            return
        grad_suffix = ""
        if self.is_grad:
            grad_suffix = "_grad"
        kernel_name = "{}_c{}_{}_{}".format(self.func.__name__,
                                            self.kernel_counter, key[1],
                                            grad_suffix)
        import taichi as ti
        ti.info("Compiling kernel {}...".format(kernel_name))

        src = remove_indent(inspect.getsource(self.func))
        tree = ast.parse(src)
        if self.runtime.print_preprocessed:
            import astor
            print('Before preprocessing:')
            print(astor.to_source(tree.body[0]))

        func_body = tree.body[0]
        func_body.decorator_list = []

        local_vars = {}
        # Discussions: https://github.com/yuanming-hu/taichi/issues/282
        import copy
        global_vars = copy.copy(self.func.__globals__)

        for i, arg in enumerate(func_body.args.args):
            anno = arg.annotation
            if isinstance(anno, ast.Name):
                global_vars[anno.id] = self.arguments[i]

        visitor = ASTTransformer(
            excluded_paremeters=self.template_slot_locations,
            func=self,
            arg_features=arg_features)

        visitor.visit(tree)
        ast.fix_missing_locations(tree)

        if self.runtime.print_preprocessed:
            import astor
            print('After preprocessing:')
            print(astor.to_source(tree.body[0], indent_with='  '))

        ast.increment_lineno(tree, inspect.getsourcelines(self.func)[1] - 1)

        freevar_names = self.func.__code__.co_freevars
        closure = self.func.__closure__
        if closure:
            freevar_values = list(map(lambda x: x.cell_contents, closure))
            for name, value in zip(freevar_names, freevar_values):
                global_vars[name] = value

        # inject template parameters into globals
        for i in self.template_slot_locations:
            template_var_name = self.argument_names[i]
            global_vars[template_var_name] = args[i]

        exec(
            compile(tree,
                    filename=inspect.getsourcefile(self.func),
                    mode='exec'), global_vars, local_vars)
        compiled = local_vars[self.func.__name__]

        taichi_kernel = taichi_lang_core.create_kernel(kernel_name,
                                                       self.is_grad)

        # Do not change the name of 'taichi_ast_generator'
        # The warning system needs this identifier to remove unnecessary messages
        def taichi_ast_generator():
            if self.runtime.inside_kernel:
                import taichi as ti
                raise ti.TaichiSyntaxError(
                    "Kernels cannot call other kernels. I.e., nested kernels are not allowed. Please check if you have direct/indirect invocation of kernels within kernels. Note that some methods provided by the Taichi standard library may invoke kernels, and please move their invocations to Python-scope."
                )
            self.runtime.inside_kernel = True
            compiled()
            self.runtime.inside_kernel = False

        taichi_kernel = taichi_kernel.define(taichi_ast_generator)

        assert key not in self.compiled_functions
        self.compiled_functions[key] = self.get_function_body(taichi_kernel)
Exemple #15
0
def init(arch=None,
         default_fp=None,
         default_ip=None,
         _test_mode=False,
         enable_fallback=True,
         **kwargs):
    """Initializes the Taichi runtime.

    This should always be the entry point of your Taichi program. Most
    importantly, it sets the backend used throughout the program.

    Args:
        arch: Backend to use. This is usually :const:`~taichi.lang.cpu` or :const:`~taichi.lang.gpu`.
        default_fp (Optional[type]): Default floating-point type.
        default_ip (Optional[type]): Default integral type.
        **kwargs: Taichi provides highly customizable compilation through
            ``kwargs``, which allows for fine grained control of Taichi compiler
            behavior. Below we list some of the most frequently used ones. For a
            complete list, please check out
            https://github.com/taichi-dev/taichi/blob/master/taichi/program/compile_config.h.

            * ``cpu_max_num_threads`` (int): Sets the number of threads used by the CPU thread pool.
            * ``debug`` (bool): Enables the debug mode, under which Taichi does a few more things like boundary checks.
            * ``print_ir`` (bool): Prints the CHI IR of the Taichi kernels.
            * ``packed`` (bool): Enables the packed memory layout. See https://docs.taichi.graphics/lang/articles/advanced/layout.
    """
    # Check version for users every 7 days if not disabled by users.
    skip = os.environ.get("TI_SKIP_VERSION_CHECK")
    if skip != 'ON':
        try_check_version()

    # Make a deepcopy in case these args reference to items from ti.cfg, which are
    # actually references. If no copy is made and the args are indeed references,
    # ti.reset() could override the args to their default values.
    default_fp = _deepcopy(default_fp)
    default_ip = _deepcopy(default_ip)
    kwargs = _deepcopy(kwargs)
    ti.reset()

    spec_cfg = _SpecialConfig()
    env_comp = _EnvironmentConfigurator(kwargs, ti.cfg)
    env_spec = _EnvironmentConfigurator(kwargs, spec_cfg)

    # configure default_fp/ip:
    # TODO: move these stuff to _SpecialConfig too:
    env_default_fp = os.environ.get("TI_DEFAULT_FP")
    if env_default_fp:
        if default_fp is not None:
            _ti_core.warn(
                f'ti.init argument "default_fp" overridden by environment variable TI_DEFAULT_FP={env_default_fp}'
            )
        if env_default_fp == '32':
            default_fp = ti.f32
        elif env_default_fp == '64':
            default_fp = ti.f64
        elif env_default_fp is not None:
            raise ValueError(
                f'Invalid TI_DEFAULT_FP={env_default_fp}, should be 32 or 64')

    env_default_ip = os.environ.get("TI_DEFAULT_IP")
    if env_default_ip:
        if default_ip is not None:
            _ti_core.warn(
                f'ti.init argument "default_ip" overridden by environment variable TI_DEFAULT_IP={env_default_ip}'
            )
        if env_default_ip == '32':
            default_ip = ti.i32
        elif env_default_ip == '64':
            default_ip = ti.i64
        elif env_default_ip is not None:
            raise ValueError(
                f'Invalid TI_DEFAULT_IP={env_default_ip}, should be 32 or 64')

    if default_fp is not None:
        impl.get_runtime().set_default_fp(default_fp)
    if default_ip is not None:
        impl.get_runtime().set_default_ip(default_ip)

    # submodule configurations (spec_cfg):
    env_spec.add('print_preprocessed')
    env_spec.add('log_level', str)
    env_spec.add('gdb_trigger')
    env_spec.add('excepthook')
    env_spec.add('experimental_real_function')
    env_spec.add('short_circuit_operators')

    # compiler configurations (ti.cfg):
    for key in dir(ti.cfg):
        if key in ['arch', 'default_fp', 'default_ip']:
            continue
        _cast = type(getattr(ti.cfg, key))
        if _cast is bool:
            _cast = None
        env_comp.add(key, _cast)

    unexpected_keys = kwargs.keys()

    if len(unexpected_keys):
        raise KeyError(
            f'Unrecognized keyword argument(s) for ti.init: {", ".join(unexpected_keys)}'
        )

    # dispatch configurations that are not in ti.cfg:
    if not _test_mode:
        ti.set_gdb_trigger(spec_cfg.gdb_trigger)
        impl.get_runtime().print_preprocessed = spec_cfg.print_preprocessed
        impl.get_runtime().experimental_real_function = \
            spec_cfg.experimental_real_function
        impl.get_runtime().short_circuit_operators = \
            spec_cfg.short_circuit_operators
        ti.set_logging_level(spec_cfg.log_level.lower())
        if spec_cfg.excepthook:
            # TODO(#1405): add a way to restore old excepthook
            ti.enable_excepthook()

    # select arch (backend):
    env_arch = os.environ.get('TI_ARCH')
    if env_arch is not None:
        ti.info(f'Following TI_ARCH setting up for arch={env_arch}')
        arch = _ti_core.arch_from_name(env_arch)
    ti.cfg.arch = adaptive_arch_select(arch, enable_fallback, ti.cfg.use_gles)
    if ti.cfg.arch == cc:
        _ti_core.set_tmp_dir(locale_encode(prepare_sandbox()))
    print(f'[Taichi] Starting on arch={_ti_core.arch_name(ti.cfg.arch)}')

    # Torch based ndarray on opengl backend allocates memory on host instead of opengl backend.
    # So it won't work.
    if ti.cfg.arch == opengl and ti.cfg.ndarray_use_torch:
        ti.warn(
            'Opengl backend doesn\'t support torch based ndarray. Setting ndarray_use_torch to False.'
        )
        ti.cfg.ndarray_use_torch = False

    if _test_mode:
        return spec_cfg

    get_default_kernel_profiler().set_kernel_profiler_mode(
        ti.cfg.kernel_profiler)

    # create a new program:
    impl.get_runtime().create_program()

    ti.trace('Materializing runtime...')
    impl.get_runtime().prog.materialize_runtime()

    impl._root_fb = FieldsBuilder()

    if not os.environ.get("TI_DISABLE_SIGNAL_HANDLERS", False):
        impl.get_runtime()._register_signal_handlers()

    return None
Exemple #16
0
def main(debug=False):
  lines = []
  print()
  lines.append(u' *******************************************')
  lines.append(u' **     Taichi Programming Language       **')
  lines.append(u' *******************************************')
  if debug:
    lines.append(u' *****************Debug Mode****************')
    os.environ['TI_DEBUG'] = '1'
  print(u'\n'.join(lines))
  print()
  import taichi as ti

  ti.tc_core.set_core_debug(debug)

  argc = len(sys.argv)
  if argc == 1 or sys.argv[1] == '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_cpp               |-> Run cpp tests\n"
        "           ti format                 |-> Reformat modified source files\n"
        "           ti format_all             |-> Reformat all source files\n"
        "           ti build                  |-> Build C++ files\n"
        "           ti video                  |-> Make a video using *.png files in the current folder\n"
        "           ti video_scale            |-> Scale video resolution \n"
        "           ti video_crop             |-> Crop video\n"
        "           ti video_speed            |-> Speed up video\n"
        "           ti gif                    |-> Convert mp4 file to gif\n"
        "           ti doc                    |-> Build documentation\n"
        "           ti release                |-> Make source code release\n"
        "           ti debug [script.py]      |-> Debug script\n")
    exit(0)
  mode = sys.argv[1]

  t = time.time()
  if mode.endswith('.py'):
    import subprocess
    subprocess.call([sys.executable] + sys.argv[1:])
  elif mode == "run":
    if argc <= 2:
      print("Please specify [task name], e.g. test_math")
      exit(-1)
    name = sys.argv[2]
    task = ti.Task(name)
    task.run(*sys.argv[3:])
  elif mode == "debug":
    ti.core.set_core_trigger_gdb_when_crash(True)
    if argc <= 2:
      print("Please specify [file name], e.g. render.py")
      exit(-1)
    name = sys.argv[2]
    with open(name) as script:
      script = script.read()
    exec(script, {'__name__': '__main__'})
  elif mode == "test_python":
    return test_python()
  elif mode == "test_cpp":
    return test_cpp()
  elif mode == "test":
    if test_python() != 0:
      return -1
    return test_cpp()
  elif mode == "test_verbose":
    if test_python(True) != 0:
      return -1
    return test_cpp()
  elif mode == "build":
    ti.core.build()
  elif mode == "format":
    ti.core.format()
  elif mode == "format_all":
    ti.core.format(all=True)
  elif mode == "statement":
    exec(sys.argv[2])
  elif mode == "update":
    ti.core.update(True)
    ti.core.build()
  elif mode == "asm":
    fn = sys.argv[2]
    os.system(r"sed '/^\s*\.\(L[A-Z]\|[a-z]\)/ d' {0} > clean_{0}".format(fn))
  elif mode == "interpolate":
    interpolate_frames('.')
  elif mode == "amal":
    cwd = os.getcwd()
    os.chdir(ti.get_repo_directory())
    with open('misc/amalgamate.py') as script:
      script = script.read()
    exec(script, {'__name__': '__main__'})
    os.chdir(cwd)
    shutil.copy(
        os.path.join(ti.get_repo_directory(), 'build/taichi.h'), './taichi.h')
  elif mode == "doc":
    os.system('cd {}/docs && sphinx-build -b html . build'.format(ti.get_repo_directory()))
  elif mode == "video":
    files = sorted(os.listdir('.'))
    files = list(filter(lambda x: x.endswith('.png'), files))
    if len(sys.argv) >= 3:
      frame_rate = int(sys.argv[2])
    else:
      frame_rate = 24
    if len(sys.argv) >= 4:
      trunc = int(sys.argv[3])
      files = files[:trunc]
    ti.info('Making video using {} png files...', len(files))
    ti.info("frame_rate={}", frame_rate)
    output_fn = 'video.mp4'
    make_video(files, output_path=output_fn, frame_rate=frame_rate)
    ti.info('Done! Output video file = {}', output_fn)
  elif mode == "video_scale":
    input_fn = sys.argv[2]
    assert input_fn[-4:] == '.mp4'
    output_fn = input_fn[:-4] + '-scaled.mp4'
    ratiow = float(sys.argv[3])
    if len(sys.argv) >= 5:
      ratioh = float(sys.argv[4])
    else:
      ratioh = ratiow
    scale_video(input_fn, output_fn, ratiow, ratioh)
  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)
    input_fn = sys.argv[2]
    assert input_fn[-4:] == '.mp4'
    output_fn = input_fn[:-4] + '-cropped.mp4'
    x_begin = float(sys.argv[3])
    x_end = float(sys.argv[4])
    y_begin = float(sys.argv[5])
    y_end = float(sys.argv[6])
    crop_video(input_fn, output_fn, x_begin, x_end, y_begin, y_end)
  elif mode == "video_speed":
    if len(sys.argv) != 4:
      print('Usage: ti video_speed fn speed_up_factor')
      exit(-1)
    input_fn = sys.argv[2]
    assert input_fn[-4:] == '.mp4'
    output_fn = input_fn[:-4] + '-sped.mp4'
    speed = float(sys.argv[3])
    accelerate_video(input_fn, output_fn, speed)
  elif mode == "gif":
    input_fn = sys.argv[2]
    assert input_fn[-4:] == '.mp4'
    output_fn = input_fn[:-4] + '.gif'
    ti.info('Converting {} to {}'.format(input_fn, output_fn))
    framerate = 24
    mp4_to_gif(input_fn, output_fn, framerate)
  elif mode == "convert":
    # http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
    # TODO: Windows support
    for fn in sys.argv[2:]:
      print("Converting logging file: {}".format(fn))
      tmp_fn = '/tmp/{}.{:05d}.backup'.format(fn, random.randint(0, 10000))
      shutil.move(fn, tmp_fn)
      command = r'sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"'
      os.system('{} {} > {}'.format(command, tmp_fn, fn))
  elif mode == "release":
    from git import Git
    import zipfile
    import hashlib
    g = Git(ti.get_repo_directory())
    g.init()
    with zipfile.ZipFile('release.zip', 'w') as zip:
      files = g.ls_files().split('\n')
      os.chdir(ti.get_repo_directory())
      for f in files:
        if not os.path.isdir(f):
          zip.write(f)
    ver = ti.__version__
    md5 = hashlib.md5()
    with open('release.zip', "rb") as f:
      for chunk in iter(lambda: f.read(4096), b""):
        md5.update(chunk)
    md5 = md5.hexdigest()
    commit = ti.core.get_commit_hash()[:8]
    fn = f'taichi-src-v{ver[0]}-{ver[1]}-{ver[2]}-{commit}-{md5}.zip'
    import shutil
    shutil.move('release.zip', fn)
  else:
    name = sys.argv[1]
    print('Running task [{}]...'.format(name))
    task = ti.Task(name)
    task.run(*sys.argv[2:])
  print()
  print(">>> Running time: {:.2f}s".format(time.time() - t))
Exemple #17
0
def main(debug=False):
    lines = []
    print()
    lines.append(u' *******************************************')
    lines.append(u' **                Taichi                 **')
    lines.append(u' **                                       **')
    lines.append(u' ** High-Performance Programming Language **')
    lines.append(u' *******************************************')
    if debug:
        lines.append(u' *****************Debug Mode****************')
        lines.append(u' *******************************************')
        os.environ['TI_DEBUG'] = '1'
    print(u'\n'.join(lines))
    print()
    import taichi as ti

    ti.tc_core.set_core_debug(debug)

    argc = len(sys.argv)
    if argc == 1 or sys.argv[1] == 'help':
        print(
            "    Usage: ti run [task name]        |-> Run a specific task\n"
            "           ti test                   |-> Run all tests\n"
            "           ti test_python            |-> Run python tests\n"
            "           ti test_cpp               |-> Run cpp tests\n"
            "           ti build                  |-> Build C++ files\n"
            "           ti video                  |-> Make a video using *.png files in the current folder\n"
            "           ti video_scale            |-> Scale video resolution \n"
            "           ti video_crop             |-> Crop video\n"
            "           ti video_speed            |-> Speed up video\n"
            "           ti gif                    |-> Convert mp4 file to gif\n"
            "           ti doc                    |-> Build documentation\n"
            "           ti debug [script.py]      |-> Debug script\n")
        exit(-1)
    mode = sys.argv[1]

    t = time.time()
    if mode.endswith('.py'):
        import subprocess
        subprocess.call([sys.executable] + sys.argv[1:])
    elif mode == "run":
        if argc <= 2:
            print("Please specify [task name], e.g. test_math")
            exit(-1)
        name = sys.argv[2]
        task = ti.Task(name)
        task.run(*sys.argv[3:])
    elif mode == "debug":
        ti.core.set_core_trigger_gdb_when_crash(True)
        if argc <= 2:
            print("Please specify [file name], e.g. render.py")
            exit(-1)
        name = sys.argv[2]
        with open(name) as script:
            script = script.read()
        exec(script, {'__name__': '__main__'})
    elif mode == "test_python":
        return test_python()
    elif mode == "test_cpp":
        return test_cpp()
    elif mode == "test":
        if test_python() != 0:
            return -1
        return test_cpp()
    elif mode == "build":
        ti.core.build()
    elif mode == "format":
        ti.core.format()
    elif mode == "statement":
        exec(sys.argv[2])
    elif mode == "update":
        ti.core.update(True)
        ti.core.build()
    elif mode == "asm":
        fn = sys.argv[2]
        os.system(
            r"sed '/^\s*\.\(L[A-Z]\|[a-z]\)/ d' {0} > clean_{0}".format(fn))
    elif mode == "interpolate":
        interpolate_frames('.')
    elif mode == "amal":
        cwd = os.getcwd()
        os.chdir(ti.get_repo_directory())
        with open('misc/amalgamate.py') as script:
            script = script.read()
        exec(script, {'__name__': '__main__'})
        os.chdir(cwd)
        shutil.copy(os.path.join(ti.get_repo_directory(), 'build/taichi.h'),
                    './taichi.h')
    elif mode == "doc":
        os.system('cd docs && sphinx-build -b html . build')
    elif mode == "video":
        files = sorted(os.listdir('.'))
        files = list(filter(lambda x: x.endswith('.png'), files))
        if len(sys.argv) >= 3:
            frame_rate = int(sys.argv[2])
        else:
            frame_rate = 24
        if len(sys.argv) >= 4:
            trunc = int(sys.argv[3])
            files = files[:trunc]
        ti.info('Making video using {} png files...', len(files))
        ti.info("frame_rate={}", frame_rate)
        output_fn = 'video.mp4'
        make_video(files, output_path=output_fn, frame_rate=frame_rate)
        ti.info('Done! Output video file = {}', output_fn)
    elif mode == "video_scale":
        input_fn = sys.argv[2]
        assert input_fn[-4:] == '.mp4'
        output_fn = input_fn[:-4] + '-scaled.mp4'
        ratiow = float(sys.argv[3])
        if len(sys.argv) >= 5:
            ratioh = float(sys.argv[4])
        else:
            ratioh = ratiow
        scale_video(input_fn, output_fn, ratiow, ratioh)
    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)
        input_fn = sys.argv[2]
        assert input_fn[-4:] == '.mp4'
        output_fn = input_fn[:-4] + '-cropped.mp4'
        x_begin = float(sys.argv[3])
        x_end = float(sys.argv[4])
        y_begin = float(sys.argv[5])
        y_end = float(sys.argv[6])
        crop_video(input_fn, output_fn, x_begin, x_end, y_begin, y_end)
    elif mode == "video_speed":
        if len(sys.argv) != 4:
            print('Usage: ti video_speed fn speed_up_factor')
            exit(-1)
        input_fn = sys.argv[2]
        assert input_fn[-4:] == '.mp4'
        output_fn = input_fn[:-4] + '-sped.mp4'
        speed = float(sys.argv[3])
        accelerate_video(input_fn, output_fn, speed)
    elif mode == "gif":
        input_fn = sys.argv[2]
        assert input_fn[-4:] == '.mp4'
        output_fn = input_fn[:-4] + '.gif'
        ti.info('Converting {} to {}'.format(input_fn, output_fn))
        framerate = 24
        mp4_to_gif(input_fn, output_fn, framerate)
    elif mode == "convert":
        # http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
        # TODO: Windows support
        for fn in sys.argv[2:]:
            print("Converting logging file: {}".format(fn))
            tmp_fn = '/tmp/{}.{:05d}.backup'.format(fn,
                                                    random.randint(0, 10000))
            shutil.move(fn, tmp_fn)
            command = r'sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"'
            os.system('{} {} > {}'.format(command, tmp_fn, fn))
    elif mode == "merge":
        import cv2  # TODO: remove this dependency
        import numpy as np
        folders = sys.argv[2:]
        os.makedirs('merged', exist_ok=True)
        for fn in sorted(os.listdir(folders[0])):
            imgs = []
            for fld in folders:
                img = cv2.imread(os.path.join(fld, fn))
                imgs.append(img)
            img = np.hstack(imgs)
            cv2.imwrite(os.path.join('merged', fn), img)
    else:
        name = sys.argv[1]
        print('Running task [{}]...'.format(name))
        task = ti.Task(name)
        task.run(*sys.argv[2:])
    print()
    print(">>> Running time: {:.2f}s".format(time.time() - t))
Exemple #18
0
def main(debug=False):
    argc = len(sys.argv)
    if argc == 1:
        mode = 'help'
        parser_args = sys.argv
    else:
        mode = sys.argv[1]
        parser_args = sys.argv[2:]
    parser = make_argument_parser()
    args = parser.parse_args(args=parser_args)

    lines = []
    print()
    lines.append(u' *******************************************')
    lines.append(u' **     Taichi Programming Language       **')
    lines.append(u' *******************************************')
    if 'TI_DEBUG' in os.environ:
        val = os.environ['TI_DEBUG']
        if val not in ['0', '1']:
            raise ValueError(
                "Environment variable TI_DEBUG can only have value 0 or 1.")
    if debug:
        lines.append(u' *****************Debug Mode****************')
        os.environ['TI_DEBUG'] = '1'
    print(u'\n'.join(lines))
    print()
    import taichi as ti
    if args.arch is not None:
        arch = args.arch
        if args.exclusive:
            arch = '^' + arch
        print(f'Running on Arch={arch}')
        os.environ['TI_WANTED_ARCHS'] = arch

    if mode == 'help':
        print(
            "    Usage: ti run [task name]        |-> Run a specific task\n"
            "           ti test                   |-> Run all the tests\n"
            "           ti benchmark              |-> Run python tests in benchmark mode\n"
            "           ti baseline               |-> Archive current benchmark result as baseline\n"
            "           ti regression             |-> Display benchmark regression test result\n"
            "           ti format                 |-> Reformat modified source files\n"
            "           ti format_all             |-> Reformat all source files\n"
            "           ti build                  |-> Build C++ files\n"
            "           ti video                  |-> Make a video using *.png files in the current folder\n"
            "           ti video_scale            |-> Scale video resolution \n"
            "           ti video_crop             |-> Crop video\n"
            "           ti video_speed            |-> Speed up video\n"
            "           ti gif                    |-> Convert mp4 file to gif\n"
            "           ti doc                    |-> Build documentation\n"
            "           ti release                |-> Make source code release\n"
            "           ti debug [script.py]      |-> Debug script\n"
            "           ti example [name]         |-> Run an example by name\n"
        )
        return 0

    t = time.time()
    if mode.endswith('.py'):
        import subprocess
        subprocess.call([sys.executable, mode] + sys.argv[1:])
    elif mode == "run":
        if argc <= 1:
            print("Please specify [task name], e.g. test_math")
            return -1
        print(sys.argv)
        name = sys.argv[1]
        task = ti.Task(name)
        task.run(*sys.argv[2:])
    elif mode == "debug":
        ti.core.set_core_trigger_gdb_when_crash(True)
        if argc <= 2:
            print("Please specify [file name], e.g. render.py")
            return -1
        name = sys.argv[2]
        with open(name) as script:
            script = script.read()
        exec(script, {'__name__': '__main__'})
    elif mode == "test":
        if len(args.files):
            if args.cpp:
                return test_cpp(args)
            else:
                return test_python(args)
        elif args.cpp:
            return test_cpp(args)
        else:
            ret = test_python(args)
            if ret != 0:
                return ret
            return test_cpp(args)
    elif mode == "benchmark":
        import shutil
        commit_hash = ti.core.get_commit_hash()
        with os.popen('git rev-parse HEAD') as f:
            current_commit_hash = f.read().strip()
        assert commit_hash == current_commit_hash, f"Built commit {commit_hash:.6} differs from current commit {current_commit_hash:.6}, refuse to benchmark"
        os.environ['TI_PRINT_BENCHMARK_STAT'] = '1'
        output_dir = get_benchmark_output_dir()
        shutil.rmtree(output_dir, True)
        os.mkdir(output_dir)
        os.environ['TI_BENCHMARK_OUTPUT_DIR'] = output_dir
        if os.environ.get('TI_WANTED_ARCHS') is None and not args.tprt:
            # since we only do number-of-statements benchmark for SPRT
            os.environ['TI_WANTED_ARCHS'] = 'x64'
        if args.tprt:
            os.system('python benchmarks/run.py')
            # TODO: benchmark_python(args)
        else:
            test_python(args)
    elif mode == "baseline":
        import shutil
        baseline_dir = get_benchmark_baseline_dir()
        output_dir = get_benchmark_output_dir()
        shutil.rmtree(baseline_dir, True)
        shutil.copytree(output_dir, baseline_dir)
        print('[benchmark] baseline data saved')
    elif mode == "regression":
        baseline_dir = get_benchmark_baseline_dir()
        output_dir = get_benchmark_output_dir()
        display_benchmark_regression(baseline_dir, output_dir, args)
    elif mode == "build":
        ti.core.build()
    elif mode == "format":
        diff = None
        if len(sys.argv) >= 3:
            diff = sys.argv[2]
        ti.core.format(diff=diff)
    elif mode == "format_all":
        ti.core.format(all=True)
    elif mode == "statement":
        exec(sys.argv[2])
    elif mode == "update":
        ti.core.update(True)
        ti.core.build()
    elif mode == "asm":
        fn = sys.argv[2]
        os.system(
            r"sed '/^\s*\.\(L[A-Z]\|[a-z]\)/ d' {0} > clean_{0}".format(fn))
    elif mode == "interpolate":
        interpolate_frames('.')
    elif mode == "doc":
        os.system('cd {}/docs && sphinx-build -b html . build'.format(
            ti.get_repo_directory()))
    elif mode == "video":
        files = sorted(os.listdir('.'))
        files = list(filter(lambda x: x.endswith('.png'), files))
        if len(sys.argv) >= 3:
            frame_rate = int(sys.argv[2])
        else:
            frame_rate = 24
        if len(sys.argv) >= 4:
            trunc = int(sys.argv[3])
            files = files[:trunc]
        ti.info('Making video using {} png files...', len(files))
        ti.info("frame_rate={}", frame_rate)
        output_fn = 'video.mp4'
        make_video(files, output_path=output_fn, frame_rate=frame_rate)
        ti.info('Done! Output video file = {}', output_fn)
    elif mode == "video_scale":
        input_fn = sys.argv[2]
        assert input_fn[-4:] == '.mp4'
        output_fn = input_fn[:-4] + '-scaled.mp4'
        ratiow = float(sys.argv[3])
        if len(sys.argv) >= 5:
            ratioh = float(sys.argv[4])
        else:
            ratioh = ratiow
        scale_video(input_fn, output_fn, ratiow, ratioh)
    elif mode == "video_crop":
        if len(sys.argv) != 7:
            print('Usage: ti video_crop fn x_begin x_end y_begin y_end')
            return -1
        input_fn = sys.argv[2]
        assert input_fn[-4:] == '.mp4'
        output_fn = input_fn[:-4] + '-cropped.mp4'
        x_begin = float(sys.argv[3])
        x_end = float(sys.argv[4])
        y_begin = float(sys.argv[5])
        y_end = float(sys.argv[6])
        crop_video(input_fn, output_fn, x_begin, x_end, y_begin, y_end)
    elif mode == "video_speed":
        if len(sys.argv) != 4:
            print('Usage: ti video_speed fn speed_up_factor')
            return -1
        input_fn = sys.argv[2]
        assert input_fn[-4:] == '.mp4'
        output_fn = input_fn[:-4] + '-sped.mp4'
        speed = float(sys.argv[3])
        accelerate_video(input_fn, output_fn, speed)
    elif mode == "gif":
        input_fn = sys.argv[2]
        assert input_fn[-4:] == '.mp4'
        output_fn = input_fn[:-4] + '.gif'
        ti.info('Converting {} to {}'.format(input_fn, output_fn))
        framerate = 24
        mp4_to_gif(input_fn, output_fn, framerate)
    elif mode == "convert":
        import shutil
        # http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
        # TODO: Windows support
        for fn in sys.argv[2:]:
            print("Converting logging file: {}".format(fn))
            tmp_fn = '/tmp/{}.{:05d}.backup'.format(fn,
                                                    random.randint(0, 10000))
            shutil.move(fn, tmp_fn)
            command = r'sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"'
            os.system('{} {} > {}'.format(command, tmp_fn, fn))
    elif mode == "release":
        from git import Git
        import zipfile
        import hashlib
        g = Git(ti.get_repo_directory())
        g.init()
        with zipfile.ZipFile('release.zip', 'w') as zip:
            files = g.ls_files().split('\n')
            os.chdir(ti.get_repo_directory())
            for f in files:
                if not os.path.isdir(f):
                    zip.write(f)
        ver = ti.__version__
        md5 = hashlib.md5()
        with open('release.zip', "rb") as f:
            for chunk in iter(lambda: f.read(4096), b""):
                md5.update(chunk)
        md5 = md5.hexdigest()
        commit = ti.core.get_commit_hash()[:8]
        fn = f'taichi-src-v{ver[0]}-{ver[1]}-{ver[2]}-{commit}-{md5}.zip'
        import shutil
        shutil.move('release.zip', fn)
    elif mode == "example":
        if len(sys.argv) != 3:
            sys.exit(
                f"Invalid arguments! Usage: ti example [name]\nAvailable example names are: {sorted(get_available_examples())}"
            )
        example = sys.argv[2]
        run_example(name=example)
    else:
        name = sys.argv[1]
        print('Running task [{}]...'.format(name))
        task = ti.Task(name)
        task.run(*sys.argv[2:])
    print()
    print(">>> Running time: {:.2f}s".format(time.time() - t))
    return 0
Exemple #19
0
def main():
  lines = []
  print()
  lines.append(u'{:^43}'.format(u' '.join([u'\u262f'] * 8)))
  lines.append(u' ******************************************')
  lines.append(u' ** Taichi - A Computer Graphics Library **')
  lines.append(u' ******************************************')
  lines.append(u'{:^43}'.format(u"\u2630 \u2631 \u2632 \u2633 "
                                "\u2634 \u2635 \u2636 \u2637"))
  print(u'\n'.join(lines))
  print()

  argc = len(sys.argv)
  if argc == 1 or sys.argv[1] == 'help':
    print(
        "    Usage: ti run [task name]        |-> Run a specific task\n"
        "           ti test                   |-> Run tests\n"
        "           ti build                  |-> Build C++ files\n"
        "           ti update                 |-> Update taichi and projects\n"
        "           ti video                  |-> Make a video using *.png files in the current folder\n"
        "           ti convert                |-> Delete color controllers in a log file\n"
        "           ti exec                   |-> Invoke a executable in the 'build' folder\n"
        "           ti format                 |-> Format taichi and projects\n"
        "                                         (C++ source and python scripts)\n"
        "           ti *.py [arguments]       |-> Run scripts\n")
    exit(-1)
  mode = sys.argv[1]

  if mode.endswith('.py'):
    with open(mode) as script:
      exec(script.read())
    exit()

  if mode == "run":
    if argc <= 2:
      print("Please specify [task name], e.g. test_math")
      exit(-1)
    name = sys.argv[2]
    task = tc.Task(name)
    task.run(sys.argv[3:])
  elif mode == "test":
    # tc.core.set_core_trigger_gdb_when_crash(True)
    task = tc.Task('test')
    task.run(sys.argv[2:])
  elif mode == "build":
    tc.core.build()
  elif mode == "format":
    tc.core.format()
  elif mode == "update":
    tc.core.update(True)
    tc.core.build()
  elif mode == "exec":
    import subprocess
    exec_name = sys.argv[2]
    folder = tc.get_bin_directory()
    assert exec_name in os.listdir(folder)
    subprocess.call([os.path.join(folder, exec_name)] + sys.argv[3:])
  elif mode == "video":
    files = sorted(os.listdir('.'))
    files = list(filter(lambda x: x.endswith('.png'), files))
    tc.info('Making video using {} png files...', len(files))
    output_fn = 'video.mp4'
    make_video(files, output_path=output_fn)
    tc.info('Done! Output video file = {}', output_fn)
  elif mode == "convert":
    # http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
    # TODO: Windows support
    for fn in sys.argv[2:]:
      print("Converting logging file: {}".format(fn))
      tmp_fn = '/tmp/{}.{:05d}.backup'.format(fn, random.randint(0, 10000))
      shutil.move(fn, tmp_fn)
      command = r'sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"'
      os.system('{} {} > {}'.format(command, tmp_fn, fn))
  else:
    print("Unknown command '{}'".format(mode))
    exit(-1)
Exemple #20
0
def main():
    lines = []
    print()
    lines.append(u'{:^43}'.format(u' '.join([u'\u262f'] * 8)))
    lines.append(u' *******************************************')
    lines.append(u' **                Taichi                 **')
    lines.append(u' **                ~~~~~~                 **')
    lines.append(u' ** Open Source Computer Graphics Library **')
    lines.append(u' *******************************************')
    lines.append(u'{:^43}'.format(u"\u2630 \u2631 \u2632 \u2633 "
                                  "\u2634 \u2635 \u2636 \u2637"))
    print(u'\n'.join(lines))
    print()
    import taichi as tc

    argc = len(sys.argv)
    if argc == 1 or sys.argv[1] == 'help':
        print(
            "    Usage: ti run [task name]        |-> Run a specific task\n"
            "           ti test                   |-> Run tests\n"
            "           ti daemon                 |-> Start daemon process\n"
            "           ti install                |-> Install package\n"
            "           ti proj                   |-> List all projects\n"
            "           ti proj activate [name]   |-> Activate project\n"
            "           ti proj deactivate [name] |-> Deactivate project\n"
            "           ti build                  |-> Build C++ files\n"
            "           ti amal                   |-> Generate amalgamated taichi.h\n"
            "           ti clean asm [*.s]        |-> Clean up gcc ASM\n"
            "           ti plot [*.txt]           |-> Plot a memory usage curve\n"
            "           ti update                 |-> Update taichi and projects\n"
            "           ti video                  |-> Make a video using *.png files in the current folder\n"
            "           ti convert                |-> Delete color controllers in a log file\n"
            "           ti exec                   |-> Invoke a executable in the 'build' folder\n"
            "           ti format                 |-> Format taichi and projects\n"
            "                                         (C++ source and python scripts)\n"
            "           ti statement [statement]  |-> Execute a single statement (with taichi imported as tc\n"
            "           ti [script.py]            |-> Run script\n"
            "           ti doc                    |-> Build documentation\n"
            "           ti merge                  |-> Merge images in folders horizontally\n"
            "           ti debug [script.py]      |-> Debug script\n")
        exit(-1)
    mode = sys.argv[1]

    if mode.endswith('.py'):
        with open(mode) as script:
            script = script.read()
        exec(script, {'__name__': '__main__'})
        exit()

    if mode.endswith('.cpp'):
        command = 'g++ {} -o {} -g -std=c++14 -O3 -lX11 -lpthread'.format(
            mode, mode[:-4])
        print(command)
        ret = os.system(command)
        if ret == 0:
            os.system('./{}'.format(mode[:-4]))
        exit()

    if mode == "run":
        if argc <= 2:
            print("Please specify [task name], e.g. test_math")
            exit(-1)
        name = sys.argv[2]
        task = tc.Task(name)
        task.run(*sys.argv[3:])
    elif mode == "debug":
        tc.core.set_core_trigger_gdb_when_crash(True)
        if argc <= 2:
            print("Please specify [file name], e.g. render.py")
            exit(-1)
        name = sys.argv[2]
        with open(name) as script:
            script = script.read()
        exec(script, {'__name__': '__main__'})
        exit()
    elif mode == "daemon":
        from taichi.system.daemon import start
        if len(sys.argv) > 2:
            # Master daemon
            start(True)
        else:
            # Slave daemon
            start(False)
    elif mode == "proj":
        if len(sys.argv) == 2:
            print_all_projects()
        elif sys.argv[2] == 'activate':
            proj = sys.argv[3]
            activate_package(proj)
        elif sys.argv[2] == 'deactivate':
            proj = sys.argv[3]
            deactivate_package(proj)
        else:
            assert False
    elif mode == "test":
        task = tc.Task('test')
        task.run(*sys.argv[2:])
    elif mode == "build":
        tc.core.build()
    elif mode == "format":
        tc.core.format()
    elif mode == "statement":
        exec(sys.argv[2])
    elif mode == "plot":
        plot(sys.argv[2])
    elif mode == "update":
        tc.core.update(True)
        tc.core.build()
    elif mode == "install":
        os.chdir(tc.get_directory('projects'))
        pkg = sys.argv[2]
        if pkg not in packages:
            tc.error('package {} not found.'.format(pkg))
        else:
            tc.info('Installing package {}...'.format(pkg))
            url = packages[pkg]
        os.system('git clone {} {} --depth=1'.format(url, pkg))
        tc.core.build()
    elif mode == "asm":
        fn = sys.argv[2]
        os.system(
            r"sed '/^\s*\.\(L[A-Z]\|[a-z]\)/ d' {0} > clean_{0}".format(fn))
    elif mode == "exec":
        import subprocess
        exec_name = sys.argv[2]
        folder = tc.get_bin_directory()
        assert exec_name in os.listdir(folder)
        subprocess.call([os.path.join(folder, exec_name)] + sys.argv[3:])
    elif mode == "interpolate":
        interpolate_frames('.')
    elif mode == "amal":
        cwd = os.getcwd()
        os.chdir(tc.get_repo_directory())
        with open('misc/amalgamate.py') as script:
            script = script.read()
        exec(script, {'__name__': '__main__'})
        os.chdir(cwd)
        shutil.copy(os.path.join(tc.get_repo_directory(), 'build/taichi.h'),
                    './taichi.h')
        exit()
    elif mode == "doc":
        os.system('cd docs && sphinx-build -b html . build')
    elif mode == "video":
        files = sorted(os.listdir('.'))
        files = list(filter(lambda x: x.endswith('.png'), files))
        if len(sys.argv) >= 3:
            frame_rate = int(sys.argv[2])
        else:
            frame_rate = 24
        if len(sys.argv) >= 4:
            trunc = int(sys.argv[3])
            files = files[:trunc]
        tc.info('Making video using {} png files...', len(files))
        tc.info("frame_rate={}", frame_rate)
        output_fn = 'video.mp4'
        make_video(files, output_path=output_fn, frame_rate=frame_rate)
        tc.info('Done! Output video file = {}', output_fn)
    elif mode == "convert":
        # http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
        # TODO: Windows support
        for fn in sys.argv[2:]:
            print("Converting logging file: {}".format(fn))
            tmp_fn = '/tmp/{}.{:05d}.backup'.format(fn,
                                                    random.randint(0, 10000))
            shutil.move(fn, tmp_fn)
            command = r'sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"'
            os.system('{} {} > {}'.format(command, tmp_fn, fn))
    elif mode == "merge":
        import cv2  # TODO: remove this dependency
        import numpy as np
        folders = sys.argv[2:]
        os.makedirs('merged', exist_ok=True)
        for fn in sorted(os.listdir(folders[0])):
            imgs = []
            for fld in folders:
                img = cv2.imread(os.path.join(fld, fn))
                imgs.append(img)
            img = np.hstack(imgs)
            cv2.imwrite(os.path.join('merged', fn), img)
    else:
        name = sys.argv[1]
        print('Running task [{}]...'.format(name))
        task = tc.Task(name)
        task.run(*sys.argv[2:])
Exemple #21
0
def main(debug=False):
  lines = []
  print()
  lines.append(u' *******************************************')
  lines.append(u' **                Taichi                 **')
  lines.append(u' **                                       **')
  lines.append(u' ** High-Performance Programming Language **')
  lines.append(u' *******************************************')
  print(u'\n'.join(lines))
  print()
  import taichi as ti

  ti.tc_core.set_core_debug(debug)

  argc = len(sys.argv)
  if argc == 1 or sys.argv[1] == 'help':
    print(
      "    Usage: ti run [task name]        |-> Run a specific task\n"
      "           ti test                   |-> Run all tests\n"
      "           ti test_python            |-> Run python tests\n"
      "           ti test_cpp               |-> Run cpp tests\n"
      "           ti build                  |-> Build C++ files\n"
      "           ti video                  |-> Make a video using *.png files in the current folder\n"
      "           ti doc                    |-> Build documentation\n"
      "           ti debug [script.py]      |-> Debug script\n")
    exit(-1)
  mode = sys.argv[1]

  t = time.time()
  if mode.endswith('.py'):
    with open(mode) as script:
      script = script.read()
    exec(script, {'__name__': '__main__'})
  elif mode.endswith('.cpp'):
    command = 'g++ {} -o {} -g -std=c++14 -O3 -lX11 -lpthread'.format(mode, mode[:-4])
    print(command)
    ret = os.system(command)
    if ret == 0:
      os.system('./{}'.format(mode[:-4]))
  elif mode == "run":
    if argc <= 2:
      print("Please specify [task name], e.g. test_math")
      exit(-1)
    name = sys.argv[2]
    task = ti.Task(name)
    task.run(*sys.argv[3:])
  elif mode == "debug":
    ti.core.set_core_trigger_gdb_when_crash(True)
    if argc <= 2:
      print("Please specify [file name], e.g. render.py")
      exit(-1)
    name = sys.argv[2]
    with open(name) as script:
      script = script.read()
    exec(script, {'__name__': '__main__'})
  elif mode == "test_python":
    return test_python()
  elif mode == "test_cpp":
    return test_cpp()
  elif mode == "test":
    if test_python() != 0:
      return -1
    return test_cpp()
  elif mode == "build":
    ti.core.build()
  elif mode == "format":
    ti.core.format()
  elif mode == "statement":
    exec(sys.argv[2])
  elif mode == "update":
    ti.core.update(True)
    ti.core.build()
  elif mode == "asm":
    fn = sys.argv[2]
    os.system(r"sed '/^\s*\.\(L[A-Z]\|[a-z]\)/ d' {0} > clean_{0}".format(fn))
  elif mode == "exec":
    import subprocess
    exec_name = sys.argv[2]
    folder = ti.get_bin_directory()
    assert exec_name in os.listdir(folder)
    subprocess.call([os.path.join(folder, exec_name)] + sys.argv[3:])
  elif mode == "interpolate":
    interpolate_frames('.')
  elif mode == "amal":
    cwd = os.getcwd()
    os.chdir(ti.get_repo_directory())
    with open('misc/amalgamate.py') as script:
      script = script.read()
    exec(script, {'__name__': '__main__'})
    os.chdir(cwd)
    shutil.copy(os.path.join(ti.get_repo_directory(), 'build/taichi.h'), './taichi.h')
  elif mode == "doc":
    os.system('cd docs && sphinx-build -b html . build')
  elif mode == "video":
    files = sorted(os.listdir('.'))
    files = list(filter(lambda x: x.endswith('.png'), files))
    if len(sys.argv) >= 3:
      frame_rate = int(sys.argv[2])
    else:
      frame_rate = 24
    if len(sys.argv) >= 4:
      trunc = int(sys.argv[3])
      files = files[:trunc]
    ti.info('Making video using {} png files...', len(files))
    ti.info("frame_rate={}", frame_rate)
    output_fn = 'video.mp4'
    make_video(files, output_path=output_fn, frame_rate=frame_rate)
    ti.info('Done! Output video file = {}', output_fn)
  elif mode == "convert":
    # http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
    # TODO: Windows support
    for fn in sys.argv[2:]:
      print("Converting logging file: {}".format(fn))
      tmp_fn = '/tmp/{}.{:05d}.backup'.format(fn, random.randint(0, 10000))
      shutil.move(fn, tmp_fn)
      command = r'sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"'
      os.system('{} {} > {}'.format(command, tmp_fn, fn))
  elif mode == "merge":
    import cv2 # TODO: remove this dependency
    import numpy as np
    folders = sys.argv[2:]
    os.makedirs('merged', exist_ok=True)
    for fn in sorted(os.listdir(folders[0])):
      imgs = []
      for fld in folders:
        img = cv2.imread(os.path.join(fld, fn))
        imgs.append(img)
      img = np.hstack(imgs)
      cv2.imwrite(os.path.join('merged', fn), img)
  else:
    name = sys.argv[1]
    print('Running task [{}]...'.format(name))
    task = ti.Task(name)
    task.run(*sys.argv[2:])
  print()
  print(">>> Running time: {:.2f}s".format(time.time() - t))