Skip to content

Commit

Permalink
move runtime file inside the sandbox as well suggested by @KLozes #501 (
Browse files Browse the repository at this point in the history
#517)

* move runtime file inside the sandbox as well (as @KLozes suggested in #501)

* Update util.py

Co-authored-by: Yuanming Hu <[email protected]>
  • Loading branch information
archibate and yuanming-hu authored Feb 22, 2020
1 parent 18493d8 commit 794e923
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
9 changes: 7 additions & 2 deletions python/taichi/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def in_docker():
return True


def import_tc_core():
def import_tc_core(tmp_dir=None):
global tc_core
if get_os_name() != 'win':
old_flags = sys.getdlopenflags()
Expand All @@ -38,6 +38,8 @@ def import_tc_core():
sys.setdlopenflags(old_flags)
lib_dir = os.path.join(package_root(), 'lib')
core.set_lib_dir(locale_encode(lib_dir))
if tmp_dir is not None:
core.set_tmp_dir(locale_encode(tmp_dir))


def locale_encode(s):
Expand Down Expand Up @@ -160,6 +162,7 @@ def build():
os.chdir(tmp_cwd)

def prepare_sandbox(src):
global g_tmp_dir
assert os.path.exists(src)
import atexit
import shutil
Expand All @@ -169,6 +172,8 @@ def prepare_sandbox(src):
print(f'[Taichi] preparing sandbox at {tmp_dir}')
dest = os.path.join(tmp_dir, 'taichi_core.so')
shutil.copy(src, dest)
os.mkdir(os.path.join(tmp_dir, 'runtime/'))
print(f'[Taichi] sandbox prepared')
return tmp_dir


Expand Down Expand Up @@ -213,7 +218,7 @@ def prepare_sandbox(src):
os.chdir(tmp_dir)
sys.path.append(tmp_dir)
try:
import_tc_core()
import_tc_core(tmp_dir)
except Exception as e:
from colorama import Fore, Back, Style
print_red_bold("Taichi core import failed: ", end='')
Expand Down
2 changes: 1 addition & 1 deletion python/taichi/misc/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_project_directory(project=None):


def get_runtime_directory():
bin_rel_path = ['extneral', 'lib']
bin_rel_path = ['external', 'lib']
return os.environ.get('TAICHI_BIN_DIR',
os.path.join(get_repo_directory(), *bin_rel_path))

Expand Down
2 changes: 2 additions & 0 deletions taichi/python_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ TI_NAMESPACE_END
TLANG_NAMESPACE_BEGIN

std::string compiled_lib_dir;
std::string runtime_tmp_dir;

Expr expr_index(const Expr &expr, const Expr &index) {
return expr[index];
Expand Down Expand Up @@ -445,6 +446,7 @@ void export_lang(py::module &m) {
m.def("libdevice_path", libdevice_path);

m.def("set_lib_dir", [&](const std::string &dir) { compiled_lib_dir = dir; });
m.def("set_tmp_dir", [&](const std::string &dir) { runtime_tmp_dir = dir; });

m.def("get_commit_hash", get_commit_hash);
m.def("get_version_string", get_version_string);
Expand Down
12 changes: 10 additions & 2 deletions taichi/taichi_llvm_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,15 @@ std::string get_runtime_fn(Arch arch) {
return fmt::format("runtime_{}.bc", arch_name(arch));
}

std::string get_runtime_src_dir() {
return get_repo_dir() + "/taichi/runtime/";
}

std::string get_runtime_dir() {
return get_repo_dir() + "/taichi/runtime/";
if (runtime_tmp_dir.size() == 0)
return get_runtime_src_dir();
else
return runtime_tmp_dir + "/runtime/";
}

void compile_runtime_bitcode(Arch arch) {
Expand All @@ -110,6 +117,7 @@ void compile_runtime_bitcode(Arch arch) {
auto clang = find_existing_command({"clang-7", "clang-8", "clang-9", "clang"});
TI_ASSERT(command_exist("llvm-as"));
TI_TRACE("Compiling runtime module bitcode...");
auto runtime_src_folder = get_runtime_src_dir();
auto runtime_folder = get_runtime_dir();
std::string macro = fmt::format(" -D ARCH_{} ", arch_name(arch));
#if defined(TI_ARCH_ARM)
Expand All @@ -118,7 +126,7 @@ void compile_runtime_bitcode(Arch arch) {
int ret = std::system(
fmt::format(
"{} -S {}runtime.cpp -o {}runtime.ll -emit-llvm -std=c++17 {}",
clang, runtime_folder, runtime_folder, macro)
clang, runtime_src_folder, runtime_folder, macro)
.c_str());
if (ret) {
TI_ERROR("Runtime compilation failed.");
Expand Down
1 change: 1 addition & 0 deletions taichi/tlang_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ struct CompileConfig {

extern CompileConfig default_compile_config;
extern std::string compiled_lib_dir;
extern std::string runtime_tmp_dir;

bool command_exist(const std::string &command);

Expand Down

0 comments on commit 794e923

Please sign in to comment.