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

C/C++ bindings #439

Closed
yuanming-hu opened this issue Feb 5, 2020 · 8 comments
Closed

C/C++ bindings #439

yuanming-hu opened this issue Feb 5, 2020 · 8 comments
Labels
discussion Welcome discussion! feature request Suggest an idea on this project stale stale issues and PRs welcome contribution

Comments

@yuanming-hu
Copy link
Member

Concisely describe the proposed feature
A few users want to ship compiled Taichi kernels, so that they can run it without Python

Describe the solution you'd like
We can add a method like ti.export_all(lang='C'/'C++'), to dump

  • A header that contains all the interfaces
  • A shared object with all compiled kernels

Then users can basically do something like

#include "mpm99_exported.h"

int main() {
  initialize_taichi();
  mpm99_substep();
  finalize_taichi();
}

... or using a more OOP C++ version.

Additional comments
If you also need this or have any suggestions, please feel free to comment! :-)

@yuanming-hu yuanming-hu added feature request Suggest an idea on this project welcome contribution discussion Welcome discussion! labels Feb 5, 2020
@samuela
Copy link
Contributor

samuela commented Feb 14, 2020

I would really like the ability to do this, esp. with rust support.

@yuanming-hu
Copy link
Member Author

Yeah I think we can start with a standard C interface and most other languages like C++/rust/go/Ruby can make use of it as well.

@samuela
Copy link
Contributor

samuela commented Feb 15, 2020

Makes sense. I'd be happy to help out with this but I'm personally not sure where to start.

@yuanming-hu
Copy link
Member Author

For CPU code we can just dump LLVM IR and use llc to compile it into a .obj. Not sure what else we should do to make it a loadable shared object. The place where the optimized LLVM IR is emitted:

module->print(llvm::errs(), nullptr);

We should explore this direction and a good starting point is to compile a simple taichi kernel, such as

for i in range(n):
  a[i] += 1

Some related discussions on SO: https://stackoverflow.com/questions/22956761/generate-binary-code-shared-library-from-embedded-llvm-in-c

For GPU code we can simply dump the compiled PTX and invoke the CUDA runtime to load and run the PTX code.

@archibate
Copy link
Collaborator

Possible solution:
First, dump llvm::errs() into /tmp/a.ll.
Second, call llc /tmp/a.ll -o /tmp/a.o.
Third, call gcc -fPIC -shared -o /tmp/a.so /tmp/a.o.

@archibate
Copy link
Collaborator

How to convert llvm::errs() into std::string?

@github-actions
Copy link

Warning: The issue has been out-of-update for 50 days, marking stale.

@github-actions github-actions bot added the stale stale issues and PRs label May 21, 2020
@archibate
Copy link
Collaborator

#1332 could be a systematic solution for this.
Note: Currently the experimental C backend is already capable of creating .so, which could be later linked into user programs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Welcome discussion! feature request Suggest an idea on this project stale stale issues and PRs welcome contribution
Projects
None yet
Development

No branches or pull requests

4 participants