Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cc] Compose ActionRecorder outputs into a single C file for Emscripten #1629

Merged
merged 18 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions misc/cc_action_record.py

This file was deleted.

96 changes: 96 additions & 0 deletions python/taichi/cc_compose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import yaml
import warnings
import sys


class Composer:
def __init__(self, fout, entries, emscripten=False):
self.fout = fout
self.entries = entries
self.emscripten = emscripten
self.launches = []

def emit(self, line):
print(line, file=self.fout)

def run(self):
if self.emscripten:
self.emit('#include <emscripten.h>')

for e in self.entries:
action = e['action']
func = getattr(self, 'do_' + action, self.do_unknown)
func(e)

def do_unknown(self, e):
self.emit(f"Unknown action type: {e['action']}")

def do_compile_runtime(self, e):
header = e['runtime_header']
source = e['runtime_source']

self.emit(header)
self.emit(source)
self.emit('')

def do_compile_layout(self, e):
source = e['layout_source']

self.emit(source)
if self.emscripten:
self.emit('EMSCRIPTEN_KEEPALIVE')
self.emit('struct Ti_S0root Ti_root;')
self.emit('')

def do_allocate_buffer(self, e):
root_size = e['root_size']
gtmp_size = e['gtmp_size']
extr_size = 4 * 1024 * 1024 # pinpoint: 4 MB

if self.emscripten:
self.emit('EMSCRIPTEN_KEEPALIVE')
self.emit(f'Ti_i8 Ti_extr[{extr_size}];')

self.emit(f'Ti_i8 Ti_gtmp[{gtmp_size}];')

if self.emscripten:
self.emit('EMSCRIPTEN_KEEPALIVE')

self.emit('union Ti_BitCast Ti_args[8];')
if self.emscripten:
self.emit('EMSCRIPTEN_KEEPALIVE')
self.emit('Ti_i32 Ti_earg[8 * 8];')
self.emit('')

if self.emscripten:
self.emit('EMSCRIPTEN_KEEPALIVE')
self.emit('struct Ti_Context Ti_ctx = {')
self.emit(' &Ti_root, Ti_gtmp, Ti_args, Ti_earg,')
self.emit('};')
self.emit('')

def do_compile_kernel(self, e):
name = e['kernel_name']
source = e['kernel_source']

if self.emscripten:
self.emit('EMSCRIPTEN_KEEPALIVE')
self.emit(source)
self.emit('')

def do_launch_kernel(self, e):
self.launches.append(e)


def main(fin_name, fout_name, emscripten=False):
with open(fin_name, 'r') as fin:
warnings.filterwarnings('ignore')
obj = yaml.load(fin)

with open(fout_name, 'w') as fout:
comp = Composer(fout, obj, emscripten)
comp.run()


if __name__ == '__main__':
main(sys.argv[1], sys.argv[2], len(sys.argv) > 3)
25 changes: 18 additions & 7 deletions python/taichi/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,17 @@ def check_exists(src):
)


def prepare_sandbox(src):
global g_tmp_dir
check_exists(src)
def prepare_sandbox():
'''
return a runtime temporary directory name, which will be automatically
deleted on exit.
archibate marked this conversation as resolved.
Show resolved Hide resolved
'''
import atexit
import shutil
from tempfile import mkdtemp
tmp_dir = mkdtemp(prefix='taichi-')
atexit.register(shutil.rmtree, tmp_dir)
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/'))
return tmp_dir

Expand All @@ -285,6 +285,8 @@ def get_unique_task_id():
import_ti_core()
if get_os_name() != 'win':
dll = ctypes.CDLL(get_core_shared_object(), mode=ctypes.RTLD_LOCAL)
# TODO: add tmp dir for win too, so that C backend can be functional:
ti_core.set_tmp_dir(prepare_sandbox())
yuanming-hu marked this conversation as resolved.
Show resolved Hide resolved
archibate marked this conversation as resolved.
Show resolved Hide resolved

ti_core.set_python_package_dir(package_root())
os.makedirs(ti_core.get_repo_dir(), exist_ok=True)
Expand All @@ -295,12 +297,15 @@ def get_unique_task_id():
os.environ['DYLD_FALLBACK_LIBRARY_PATH'] = get_runtime_directory()
lib_path = os.path.join(bin_dir, 'libtaichi_core.dylib')
tmp_cwd = os.getcwd()
tmp_dir = prepare_sandbox(lib_path)
tmp_dir = prepare_sandbox()
check_exists(lib_path)
shutil.copy(lib_path, os.path.join(tmp_dir, 'taichi_core.so'))
os.chdir(tmp_dir)
sys.path.append(tmp_dir)
import taichi_core as ti_core
os.chdir(tmp_cwd)

# TODO: unify importing infrastructure:
elif get_os_name() == 'linux':
bin_dir = get_bin_directory()
if 'LD_LIBRARY_PATH' in os.environ:
Expand All @@ -310,7 +315,9 @@ def get_unique_task_id():
lib_path = os.path.join(bin_dir, 'libtaichi_core.so')
check_exists(lib_path)
tmp_cwd = os.getcwd()
tmp_dir = prepare_sandbox(lib_path)
tmp_dir = prepare_sandbox()
check_exists(lib_path)
shutil.copy(lib_path, os.path.join(tmp_dir, 'taichi_core.so'))
os.chdir(tmp_dir)
sys.path.append(tmp_dir)
try:
Expand Down Expand Up @@ -423,6 +430,10 @@ def at_startup():

ti_core.set_core_state_python_imported(True)

record_file = os.environ.get('TI_ACTION_RECORD')
if record_file:
ti_core.start_recording(record_file)


def start_memory_monitoring(output_fn, pid=-1, interval=1):
# removing dependency on psutil
Expand Down
24 changes: 24 additions & 0 deletions python/taichi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,30 @@ def dist(self, arguments: list = sys.argv[2:]):
sys.argv.append(args.mode)
runpy.run_path('build.py')

@register
def cc_compose(self, arguments: list = sys.argv[2:]):
"""Compose C backend action record into a complete C file"""
parser = argparse.ArgumentParser(prog='ti cc_compose',
description=f"{self.cc_compose.__doc__}")
parser.add_argument(
'fin_name',
help='Action record YAML file name from C backend, e.g. program.yml')
parser.add_argument(
'fout_name',
help='The output C source file name, e.g. program.c')
parser.add_argument(
'-e',
'--emscripten',
required=False,
default=False,
dest='emscripten',
action='store_true',
help='Generate output C file for Emscripten instead of raw C')
args = parser.parse_args(arguments)

from .cc_compose import main
main(args.fin_name, args.fout_name, args.emscripten)


def main():
cli = TaichiMain()
Expand Down
2 changes: 0 additions & 2 deletions python/taichi/testing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import taichi as ti

print('[Taichi] loading test module')


## Helper functions
def approx(expected, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion taichi/backends/cc/cc_configuation.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct CCConfiguation {
std::string compile_cmd, link_cmd;

CCConfiguation()
: compile_cmd("gcc -Wc99-c11-compat -c -o '{}' '{}'"),
: compile_cmd("gcc -Wc99-c11-compat -c -o '{}' '{}' -O3"),
link_cmd("gcc -shared -fPIC -o '{}' '{}'") {
}
};
Expand Down
1 change: 1 addition & 0 deletions taichi/backends/cc/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ union Ti_BitCast {
Ti_u8 *ptr_u8;
Ti_f32 *ptr_f32;
Ti_f64 *ptr_f64;
void *ptr_void;
};

struct Ti_Context {
Expand Down
12 changes: 6 additions & 6 deletions taichi/backends/opengl/shaders/random.glsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ STR(
uvec4 _rand_;

void _init_rand() {
uint i = (54321 + gl_GlobalInvocationID.x) * (12345 + _rand_state_);
_rand_.x = 123456789 * i * 1000000007;
_rand_.y = 362436069;
_rand_.z = 521288629;
_rand_.w = 88675123;
uint i = (54321u + gl_GlobalInvocationID.x) * (12345u + uint(_rand_state_));
_rand_.x = 123456789u * i * 1000000007u;
_rand_.y = 362436069u;
_rand_.z = 521288629u;
_rand_.w = 88675123u;

// Yes, this is not an atomic operation, but just fine since no matter
// how `_rand_state_` changes, `gl_GlobalInvocationID.x` can still help
Expand All @@ -22,7 +22,7 @@ uint _rand_u32() {
uint t = _rand_.x ^ (_rand_.x << 11);
_rand_.xyz = _rand_.yzw;
_rand_.w = (_rand_.w ^ (_rand_.w >> 19)) ^ (t ^ (t >> 8));
return _rand_.w * 1000000007;
return _rand_.w * 1000000007u;
}

float _rand_f32() {
Expand Down
18 changes: 2 additions & 16 deletions taichi/program/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,8 @@ void Kernel::set_arg_float(int i, float64 d) {
TI_ASSERT_INFO(
!args[i].is_nparray,
"Assigning a scalar value to a numpy array argument is not allowed");

ActionRecorder::get_instance().record(
"set_kernel_arg_float64", {ActionArg("kernel_name", name),
ActionArg("arg_id", i), ActionArg("val", d)});

auto dt = args[i].dt;

if (dt == DataType::f32) {
program.context.set_arg(i, (float32)d);
} else if (dt == DataType::f64) {
Expand Down Expand Up @@ -154,12 +150,8 @@ void Kernel::set_arg_int(int i, int64 d) {
TI_ASSERT_INFO(
!args[i].is_nparray,
"Assigning scalar value to numpy array argument is not allowed");

ActionRecorder::get_instance().record(
"set_kernel_arg_int64", {ActionArg("kernel_name", name),
ActionArg("arg_id", i), ActionArg("val", d)});

auto dt = args[i].dt;

if (dt == DataType::i32) {
program.context.set_arg(i, (int32)d);
} else if (dt == DataType::i64) {
Expand Down Expand Up @@ -247,12 +239,6 @@ void Kernel::set_arg_nparray(int i, uint64 ptr, uint64 size) {
TI_ASSERT_INFO(args[i].is_nparray,
"Assigning numpy array to scalar argument is not allowed");

ActionRecorder::get_instance().record(
"set_kernel_arg_ext_ptr",
{ActionArg("kernel_name", name), ActionArg("arg_id", i),
ActionArg("address", fmt::format("0x{:x}", ptr)),
ActionArg("array_size_in_bytes", (int64)size)});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are still people using information like set_kernel_arg_int64. Can we keep these for now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it doesn't even include dtype... They are just spams in our compile-only no-launch action parser, both in CC and [数据删除] in my pratice...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But there are still people depending on this. I don't think we should remove them immediately. Maybe a better solution is to mark them as a different type of message.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the feature is purposely undocumented and used by only you and me and the [数据删除]? Also did you confirmed this doesn't harm performance? IMO we'd remove all the run-time action output iapr, since only compile-time kernel information are required for reconstructing a Taichi program outside, e.g., this PR, did that easily.

args[i].size = size;
program.context.set_arg(i, ptr);
}
Expand Down
1 change: 0 additions & 1 deletion taichi/util/str.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ std::string c_quoted(std::string const &str) {
REG_ESC('\n', "n");
REG_ESC('\a', "a");
REG_ESC('\b', "b");
REG_ESC('\?', "?");
REG_ESC('\v', "v");
REG_ESC('\t', "t");
REG_ESC('\f', "f");
Expand Down