Skip to content

Commit 9ac780b

Browse files
committed
refactors async
1 parent 6197322 commit 9ac780b

File tree

4 files changed

+550
-315
lines changed

4 files changed

+550
-315
lines changed

cmake/templates/index.html.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
if (typeof Module !== 'undefined') {
1313
Module.onRuntimeInitialized = function() {
1414
// Optionally, pass arguments to main in an array.
15-
Module._main([]);
15+
Module.ccall('main', 'number', [], [], { async: true });
1616
};
1717
} else {
1818
console.error('Module is undefined. Check that your generated JS file is loaded properly.');

examples/hello_world/run.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ int main(int argc, char **argv) {
3838
Tensor output = createTensor(ctx, Shape{N}, kf32);
3939
std::promise<void> promise;
4040
std::future<void> future = promise.get_future();
41-
Kernel op = createKernel(ctx, {kGelu, 256, kf32},
41+
std::future<Kernel> kernelFuture = createKernel(ctx, {kGelu, 256, kf32},
4242
Bindings{input, output},
4343
{cdiv(N, 256), 1, 1});
44-
dispatchKernel(ctx, op, promise);
45-
wait(ctx, future);
46-
toCPU(ctx, output, outputArr.data(), sizeof(outputArr));
44+
Kernel op = waitForFuture(ctx.instance, kernelFuture);
45+
std::future<void> dispatchFuture = dispatchKernel(ctx, op);
46+
waitForFuture(ctx.instance, dispatchFuture);
47+
std::future<void> cpuFuture = toCPU(ctx, output, outputArr.data(), sizeof(outputArr));
48+
waitForFuture(ctx.instance, cpuFuture);
4749
for (int i = 0; i < 12; ++i) {
4850
printf(" gelu(%.2f) = %.2f\n", inputArr[i], outputArr[i]);
4951
}

0 commit comments

Comments
 (0)