Skip to content

Commit

Permalink
wewe
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Aug 24, 2020
1 parent 4aa7418 commit 9f047e7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
24 changes: 15 additions & 9 deletions docs/export_kernels.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ We provide a useful tool to compose them, type these commands to your console:

.. code-block:: bash
python3 -m taichi cc_compose mpm88.yml mpm88.c
python3 -m taichi cc_compose mpm88.yml mpm88.c mpm88.h
This composes all the kernels and runtimes in ``mpm88.yml`` into a single C
source file ``mpm88.c``:
Expand All @@ -104,9 +104,12 @@ source file ``mpm88.c``:
...
And a C header file ``mpm88.h`` for declarations of data structures, functions
(kernels) for this file.

.. note::

The generated C source are promised to be C99 compatible.
The generated C source is promised to be C99 compatible.

It should be functional when being compiled as C++ as well.

Expand All @@ -121,7 +124,7 @@ To call the kernel ``init_c6_0``, for example:
.. code-block:: cpp
extern struct Ti_Context Ti_ctx;
extern "C" void Tk_init_c6_0(struct Ti_Context *ti_ctx);
extern void Tk_init_c6_0(struct Ti_Context *ti_ctx);
...
Tk_init_c6_0(&Ti_ctx);
Expand All @@ -137,6 +140,7 @@ Or, if you need multiple Taichi context within one program:
};
MyRenderer::MyRenderer() {
// allocate buffers on your own:
per_renderer_taichi_context.root = malloc(...);
...
Tk_init_c6_0(&per_renderer_taichi_context);
Expand All @@ -151,7 +155,7 @@ To specify scalar arguments for kernels:
.. code-block:: cpp
extern struct Ti_Context Ti_ctx;
extern "C" void Tk_my_kernel_c8_0(struct Ti_Context *ti_ctx);
extern void Tk_my_kernel_c8_0(struct Ti_Context *ti_ctx);
...
Ti_ctx.args[0].val_f64 = 3.14; // first argument, float64
Ti_ctx.args[1].val_i32 = 233; // second argument, int32
Expand All @@ -168,16 +172,18 @@ To pass external arrays as arguments for kernels:
.. code-block:: cpp
extern struct Ti_Context Ti_ctx;
extern "C" void Tk_matrix_to_ext_arr_c12_0(struct Ti_Context *ti_ctx);
extern void Tk_matrix_to_ext_arr_c12_0(struct Ti_Context *ti_ctx);
...
float img[512 * 512 * 3];
float img[640 * 480 * 3];
Ti_ctx.args[0].ptr_f32 = img; // first argument, float32 pointer to array
Ti_ctx.earg[0 * 8 + 0] = 512; // img.shape[0]
Ti_ctx.earg[0 * 8 + 0] = 512; // img.shape[1]
Ti_ctx.earg[0 * 8 + 0] = 640; // img.shape[0]
Ti_ctx.earg[0 * 8 + 0] = 480; // img.shape[1]
Ti_ctx.earg[0 * 8 + 0] = 3; // img.shape[2]
Tk_matrix_to_ext_arr_c12_0(&Ti_ctx);
some_how_show_the_image(img);
// note that the array used in Taichi is row-major:
printf("img[3, 2, 1] = %f\n", img[(3 * 480 + 2) * 3 + 1]);
Taichi.js (WIP)
---------------
Expand Down
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ The Taichi Programming Language
:caption: Miscellaneous
:maxdepth: 3

faq
gui
debugging
export_results
cli_utilities
export_kernels
faq
global_settings
export_kernels
extension_libraries
acknowledgments

Expand Down
2 changes: 2 additions & 0 deletions docs/syntax.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _syntax:

Syntax
======

Expand Down
25 changes: 18 additions & 7 deletions python/taichi/cc_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@


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

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

def emit_header(self, line):
print(line, file=self.hdrout)

def run(self):
if self.emscripten:
self.emit('#include <emscripten.h>')
Expand All @@ -23,20 +27,22 @@ def run(self):
func(e)

def do_unknown(self, e):
print(f"// Unknown action type: {e['action']}")
pass

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

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

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

self.emit(source)
self.emit_header(source)
if self.emscripten:
self.emit('EMSCRIPTEN_KEEPALIVE')
self.emit('struct Ti_S0root Ti_root;')
Expand Down Expand Up @@ -64,6 +70,8 @@ def do_allocate_buffer(self, e):

if self.emscripten:
self.emit('EMSCRIPTEN_KEEPALIVE')
self.emit_header('extern struct Ti_Context Ti_ctx;')
self.emit_header('')
self.emit('struct Ti_Context Ti_ctx = {')
self.emit(' &Ti_root, Ti_gtmp, Ti_args, Ti_earg,')
self.emit('};')
Expand All @@ -77,20 +85,23 @@ def do_compile_kernel(self, e):
self.emit('EMSCRIPTEN_KEEPALIVE')
self.emit(source)
self.emit('')
declaration = source.split('{', 1)[0].strip()
self.emit_header(f'extern {declaration};')

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


def main(fin_name, fout_name, emscripten=False):
def main(fin_name, fout_name, hdrout_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()
with open(hdrout_name, 'w') as hdrout:
with open(fout_name, 'w') as fout:
comp = Composer(obj, fout, hdrout, emscripten)
comp.run()


if __name__ == '__main__':
main(sys.argv[1], sys.argv[2], len(sys.argv) > 3)
main(sys.argv[1], sys.argv[2], sys.argv[3], len(sys.argv) > 4)
4 changes: 3 additions & 1 deletion python/taichi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,8 @@ def cc_compose(self, arguments: list = sys.argv[2:]):
)
parser.add_argument(
'fout_name', help='The output C source file name, e.g. program.c')
parser.add_argument(
'hdrout_name', help='The output C header file name, e.g. program.h')
parser.add_argument(
'-e',
'--emscripten',
Expand All @@ -1039,7 +1041,7 @@ def cc_compose(self, arguments: list = sys.argv[2:]):
args = parser.parse_args(arguments)

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


def main():
Expand Down
1 change: 1 addition & 0 deletions taichi/backends/cc/runtime/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ static inline Ti_f32 Ti_rand_f32(void) {
return (Ti_f32) drand48(); // [0.0, 1.0)
}

) "\n" STR(
)

#define _CC_INSIDE_KERNEL
Expand Down

0 comments on commit 9f047e7

Please sign in to comment.