/jupyter-cpp-kernel

Jupyter Kernel for C++

Primary LanguagePythonApache License 2.0Apache-2.0

jupyter-cpp-kernel

An kernel implementation for C++, it

  • treat each cell as a unique c++ main function.
  • support many magic templates.
  • support complex compile configuration, for example, you can use this for CUDA programming.

Each code cell will be compiled as a c++ file, so it is slow but stable, and this idea is different from the kernel for the well known cling project.

This project is based on the code of jupyter-c-kernel, great work!

install

the requirements are

  • g++ (that support c++11)
  • python3
  • jupyter

to install as follows

  • python3 setup.py install
  • install_cpp_kernel
  • ipython notebook

usage and magics

There are some special tokens that helps to auto insert some code.

includes

//% includes: full

will insert some high frequency header fills, such as

  • iostream
  • vector
  • map
  • set

to include some specific header file, just includes its name

//% includes: algorithm

// is equal to #include <algorithm>

main

Kernal will automatically add main function wrapper, so just insert code block as follows will get

//% includes: full

std::cout << "hello world" << endl;

works.

But sometimes, we might need to define some data structure, so better to define a seperate main function.

//% includes: full
//% main: no

struct Node {
  int v;
  Node* left, *right;
};

int main() {
  Node root;
  
  return 0;
}

cflags and ldflags

By setting cflags and ldflags, one can control the compile period, for example, to use Eigen

//% cflags: -I xxx/Eigen
//% includes: Eigen

// use eigen logic

compiler flag to support CUDA programming

//% compiler: nvcc
//% main: no

__global__
void VecAdd(float* A, float* B, float* C) {
  // ...
}