{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "I recently discovered [cling](https://root.cern.ch/drupal/content/cling) after I saw this Tweet\n", "\n", "
I made a C++ kernel for Jupyter for some reason: https://t.co/7ApuZyeR0r
— Min RK (@minrk) May 19, 2015
\n",
"\n",
"\n",
"From the cling website, \"Cling is an interactive C++ interpreter, built on the top of LLVM and Clang libraries. Its advantages over the standard interpreters are that it has command line prompt and uses just-in-time (JIT) compiler for compilation.\" As I write (or mostly teach through dual-coding with students) quite a bit of C++ and have already been utilizing the [IPython](http://ipython.org/)/[Jupyter](https://jupyter.org/) notebook as a teaching tool for sometime, I was immediately interested in using [MinRK](https://github.com/minrk)'s [clingkernel](https://github.com/minrk/clingkernel) for this purpose as well as exploring and testing out my own ideas. As I learned more about cling, I found most of the examples to be very trivial and those that inluded the calling/loading of external functions typically only utilized the functions from the standard library. I was eager to give it try on something a little more complex, and because most of the code we write in my group is heavily dependent on [Trilinos](http://trilinos.org/), I thought I would attempt to solve a simple linear algebra system of the form\n",
"\n",
"$$\n",
"A \\vec{x} = \\vec{b}\n",
"$$\n",
"\n",
"using [AztecOO](http://trilinos.org/packages/epetra/) solvers with [Epetra](http://trilinos.org/packages/aztecoo/) datastructures from Trilinos (at least AztecOO and Epetra). This is an adaptation of the example code provided in Figure 1 of the [AztecOO User's guide](http://trilinos.org/oldsite/packages/aztecoo/AztecOOUserGuide.pdf).\n",
"\n",
"Of course, to reproduce the code below, you will need to [install cling](https://github.com/vgvassilev/cling) and Trilinos. Additionally, if you want to use MinRK's [clingkernel](https://github.com/minrk/clingkernel) you need to install the [Jupyter notebook](https://github.com/jupyter/notebook#jupyter-notebook) from the `master` branch, and install the kernel. With that, you should be able to download this notebook and execute it with few modifications."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Cling provides a few metaprocessing commands to perform actions that you would normal provide as arguments to the build system, such as providing locations for include files, loading shared libraries, etc. The documentation for the metaprocessing commands are\n",
"\n",
"````\n",
".x test.cpp – load a file (if the file defines void test() it will be executed)\n",
".L libname – load a libary or file\n",
".x filename.cxx – loads filename and calls void filename() if defined\n",
".I path – adds an include path\n",
".printAST – shows the abstract syntax tree after each processed entity\n",
".q – exit cling\n",
".help – display a brief description\n",
"````\n",
"\n",
"We'll start by loading the location of the installed header files in my Trilinos build (here I only built AztecOO and Epetra in serial to keep it simple). Cling utilizes the Clang compiler which is pretty picky, so I had to edit the header files in a few locations where `#include` statments where provided with angle brackets `< >` for local includes, where the compiler wanted double quotes. In other words, I had to change\n",
"\n",
"````\n",
"#include