def import_ti_core(): global ti_core if settings.get_os_name() != 'win': old_flags = sys.getdlopenflags() sys.setdlopenflags(2 | 8) # RTLD_NOW | RTLD_DEEPBIND else: pyddir = os.path.join(package_root(), 'lib') os.environ['PATH'] += ';' + pyddir try: import taichi_core as core except Exception as e: if isinstance(e, ImportError): print( Fore.YELLOW + "Share object taichi_core import failed, " "check this page for possible solutions:\n" "https://taichi.readthedocs.io/en/stable/install.html#troubleshooting" + Fore.RESET) if settings.get_os_name() == 'win': e.msg += '\nConsider installing Microsoft Visual C++ Redistributable: https://aka.ms/vs/16/release/vc_redist.x64.exe' elif settings.get_os_name() == 'linux': e.msg += '\nConsider installing libtinfo5: sudo apt-get install libtinfo5' raise e from None ti_core = core if settings.get_os_name() != 'win': sys.setdlopenflags(old_flags) lib_dir = os.path.join(package_root(), 'lib') core.set_lib_dir(locale_encode(lib_dir))
def get_dll_name(name): if settings.get_os_name() == 'linux': return 'libtaichi_%s.so' % name elif settings.get_os_name() == 'osx': return 'libtaichi_%s.dylib' % name elif settings.get_os_name() == 'win': return 'taichi_%s.dll' % name else: raise Exception(f"Unknown OS: {settings.get_os_name()}")
def load_module(name, verbose=True): if verbose: print('Loading module', name) try: if settings.get_os_name() == 'osx': mode = ctypes.RTLD_LOCAL else: mode = ctypes.RTLD_GLOBAL if '.so' in name: ctypes.PyDLL(name, mode=mode) else: ctypes.PyDLL(os.path.join(settings.get_repo_directory(), 'build', get_dll_name(name)), mode=mode) except Exception as e: print(Fore.YELLOW + "Warning: module [{}] loading failed: {}".format(name, e) + Style.RESET_ALL)
def mp4_to_gif(input_fn, output_fn, framerate): # Generate the palette palette_name = 'palette.png' if get_os_name() == 'win': command = get_ffmpeg_path( ) + " -loglevel panic -i %s -vf 'palettegen' -y %s" % (input_fn, palette_name) else: command = get_ffmpeg_path( ) + " -loglevel panic -i %s -vf 'fps=%d,scale=320:640:flags=lanczos,palettegen' -y %s" % ( input_fn, framerate, palette_name) # print command os.system(command) # Generate the GIF command = get_ffmpeg_path( ) + " -loglevel panic -i %s -i %s -lavfi paletteuse -y %s" % ( input_fn, palette_name, output_fn) # print command os.system(command) os.remove(palette_name)
def build(): tmp_cwd = os.getcwd() bin_dir = settings.get_build_directory() try: os.mkdir(bin_dir) except: pass os.chdir(bin_dir) import multiprocessing print('Building taichi...') num_make_threads = min(20, multiprocessing.cpu_count()) if settings.get_os_name() == 'win': make_ret = os.system( "msbuild /p:Configuration=Release /p:Platform=x64 /m taichi.sln") else: make_ret = os.system('make -j {}'.format(num_make_threads)) if make_ret != 0: print(' Error: Build failed.') exit(-1) os.chdir(tmp_cwd)
def vector_to_fast_image(img: template(), out: ext_arr()): # FIXME: Why is ``for i, j in img:`` slower than: for i, j in ti.ndrange(*img.shape): r, g, b = 0, 0, 0 color = img[i, img.shape[1] - 1 - j] if ti.static(img.dtype in [ti.f32, ti.f64]): r, g, b = min(255, max(0, int(color * 255))) else: impl.static_assert(img.dtype == ti.u8) r, g, b = color idx = j * img.shape[0] + i # We use i32 for |out| since OpenGL and Metal doesn't support u8 types if ti.static(settings.get_os_name() != 'osx'): out[idx] = (r << 16) + (g << 8) + b else: # What's -16777216? # # On Mac, we need to set the alpha channel to 0xff. Since Mac's GUI # is big-endian, the color is stored in ABGR order, and we need to # add 0xff000000, which is -16777216 in I32's legit range. (Albeit # the clarity, adding 0xff000000 doesn't work.) alpha = -16777216 out[idx] = (b << 16) + (g << 8) + r + alpha
def check_exists(src): if not os.path.exists(src): raise FileNotFoundError( f'File "{src}" not exist. Installation corrupted or build incomplete?' ) def get_unique_task_id(): import datetime import random return datetime.datetime.now().strftime('task-%Y-%m-%d-%H-%M-%S-r') + ( '%05d' % random.randint(0, 10000)) sys.path.append(os.path.join(package_root(), 'lib')) if settings.get_os_name() != 'win': link_src = os.path.join(package_root(), 'lib', 'taichi_core.so') link_dst = os.path.join(package_root(), 'lib', 'libtaichi_core.so') # For llvm jit to find the runtime symbols if not os.path.exists(link_dst): os.symlink(link_src, link_dst) import_ti_core() ti_core.set_python_package_dir(package_root()) os.makedirs(ti_core.get_repo_dir(), exist_ok=True) log_level = os.environ.get('TI_LOG_LEVEL', '') if log_level: ti_core.set_logging_level(log_level)
print(f'[Taichi] preparing sandbox at {tmp_dir}') os.mkdir(os.path.join(tmp_dir, 'runtime/')) return tmp_dir def get_unique_task_id(): import datetime import random return datetime.datetime.now().strftime('task-%Y-%m-%d-%H-%M-%S-r') + ( '%05d' % random.randint(0, 10000)) if is_release(): print("[Taichi] mode=release") sys.path.append(os.path.join(package_root(), 'lib')) if settings.get_os_name() != 'win': link_src = os.path.join(package_root(), 'lib', 'taichi_core.so') link_dst = os.path.join(package_root(), 'lib', 'libtaichi_core.so') # For llvm jit to find the runtime symbols if not os.path.exists(link_dst): os.symlink(link_src, link_dst) import_ti_core() if settings.get_os_name() != 'win': dll = ctypes.CDLL(get_core_shared_object(), mode=ctypes.RTLD_LOCAL) # The C backend needs a temporary directory for the generated .c and compiled .so files: ti_core.set_tmp_dir(locale_encode(prepare_sandbox( ))) # TODO: always allocate a tmp_dir for all situations ti_core.set_python_package_dir(package_root()) os.makedirs(ti_core.get_repo_dir(), exist_ok=True) else: