diff --git a/Dockerfile b/Dockerfile index f836421..04c84c7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,6 +22,8 @@ COPY clangmi.cpp . COPY clangmi.cppm . COPY clangmipy11.cpp . COPY Seg.hpp . + +# Copying this header makes a minor fix to suppress a warning message. COPY runtime_support.hpp /usr/local/include/hpx/runtime/components/server/ RUN useradd -m jovyan @@ -30,8 +32,11 @@ COPY --chown=jovyan cppnow2021.ipynb /home/jovyan/ RUN mkdir -p /home/jovyan/images COPY --chown=jovyan cppnow.png /home/jovyan/images +COPY runtime_support.hpp /usr/local/include/hpx/runtime/components/server/runtime_support.hpp USER jovyan WORKDIR /home/jovyan ENV PYTHONPATH /usr/local/python +RUN pip3 install --user jupyterthemes +RUN /home/jovyan/.local/bin/jt -t solarizedl -tfs 15 -fs 15 -ofs 15 -cellw 100% CMD jupyter notebook --ip 0.0.0.0 --port $PORT diff --git a/cppnow2021.ipynb b/cppnow2021.ipynb index 187fb4b..996588f 100644 --- a/cppnow2021.ipynb +++ b/cppnow2021.ipynb @@ -5,8 +5,7 @@ "id": "71d92085", "metadata": {}, "source": [ - "\"C++\n", - "

Interactive C++ in a Jupyter Notebook Using Modules for Incremental Compilation

" + "\"C++" ] }, { @@ -17,9 +16,9 @@ "## Once Upon a Time...\n", "* A colleague and I wanted to teach C++\n", "* We wanted to make it easier to get started, so we used\n", - " - Docker (a container environment that leverages the kernel)\n", - " - Cling (an Iterpreted version of Clang)\n", + " - Cling (an Interpreted version of Clang)\n", " - Jupyter/JupyterHub\n", + " - Docker (a container environment that leverages the kernel)\n", "* We created a tool called C++Explorer for teaching C++(https://github.com/stevenrbrandt/CxxExplorer). But this talk isn't about that..." ] }, @@ -46,6 +45,29 @@ "* Through \"magicks,\" i.e. cells beginning with %%, a cell may do something other than a Python command." ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "2f70588e", + "metadata": {}, + "outputs": [], + "source": [ + "from IPython.core.magic import register_cell_magic\n", + "@register_cell_magic\n", + "def bash(line, cell): get_ipython().system(cell)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9c96ac93", + "metadata": {}, + "outputs": [], + "source": [ + "%%bash\n", + "echo Hello, world!" + ] + }, { "cell_type": "markdown", "id": "6c0d7200", @@ -111,6 +133,70 @@ "* works well most of the time" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "0905ff5e", + "metadata": {}, + "outputs": [], + "source": [ + "%%writefile aloha.cppm\n", + "export module aloha;\n", + "#include \n", + "export {\n", + " void aloha_world() {\n", + " std::cout << \"Aloha, world!\" << std::endl;\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "442e0691", + "metadata": {}, + "outputs": [], + "source": [ + "%%bash\n", + "rm -f aloha.pcm aloha.o\n", + "clang++ -std=c++2a -fmodules-ts \\\n", + " --precompile -x c++-module -c aloha.cppm \\\n", + " -fimplicit-modules -fimplicit-module-maps \\\n", + " -stdlib=libc++ # create .pcm file\n", + "clang++ -std=c++2a -fmodules-ts -c aloha.cppm \\\n", + " -fimplicit-modules -fimplicit-module-maps \\\n", + " -stdlib=libc++ # create .o file" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8d102c41", + "metadata": {}, + "outputs": [], + "source": [ + "%%writefile aloha.cpp\n", + "import aloha;\n", + "\n", + "int main() {\n", + " aloha_world();\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22edd4ad", + "metadata": {}, + "outputs": [], + "source": [ + "%%bash\n", + "clang++ -std=c++2a -fmodules-ts -o aloha aloha.cpp \\\n", + " aloha.o -fimplicit-modules -fimplicit-module-maps \\\n", + " -stdlib=libc++ -fmodule-file=aloha.pcm\n", + "./aloha" + ] + }, { "cell_type": "code", "execution_count": null, @@ -622,6 +708,82 @@ "std::cout << a.get() << std::endl;" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "c2e68f30", + "metadata": {}, + "outputs": [], + "source": [ + "%%def_code\n", + "import ;\n", + "std::vector v = { 1, 2, 3, 4, 5, 6 };" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0df0e9f8", + "metadata": {}, + "outputs": [], + "source": [ + "# Set the parallelism for HPX\n", + "runcode.runflags = [\"-t\",\"4\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "067a34a1", + "metadata": {}, + "outputs": [], + "source": [ + "%%run_code\n", + "\n", + "#include \n", + "#include \n", + "#include \n", + "\n", + "hpx::for_loop(\n", + " hpx::execution::par, 0, v.size(),\n", + " [](std::size_t n) { std::cout << \"n=\" << n << std::endl;});" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "aa94cf77", + "metadata": {}, + "outputs": [], + "source": [ + "%%run_code\n", + "#include \n", + "#include \n", + "\n", + "auto result1 = [](){ return 41; };\n", + "auto result2 = [](hpx::future s){ return 1+s.get(); };\n", + "\n", + "hpx::future t = hpx::async(result1);\n", + "hpx::future t2 = t.then(result2);\n", + "std::cout << t2.get() << std::endl;" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f75c0dcd", + "metadata": {}, + "outputs": [], + "source": [ + "%%run_code\n", + "#include \n", + "#include \n", + "hpx::parallel::reverse(hpx::execution::par,\n", + " v.begin(),v.end());\n", + "for(auto i = v.begin(); i != v.end(); ++i)\n", + " std::cout << *i << std::endl;" + ] + }, { "cell_type": "markdown", "id": "identified-return", diff --git a/runcode.py b/runcode.py index 876e113..fa13dc0 100644 --- a/runcode.py +++ b/runcode.py @@ -60,6 +60,7 @@ def register_cell_magic(x): verbosity = 0 appflags = [] modflags = [] +runflags = [] cxx_std = "-std=c++2a" def expand_flags(line): @@ -240,7 +241,7 @@ def run_code_(line, code): r = run_cmd(cmd) t2 = time() if r == 0: - run_cmd(["./exec_step_%s" % session]) + run_cmd(["./exec_step_%s" % session]+runflags) t3 = time() print(colored("compile time: %.2f" % (t2-t1),"green")) print(colored("run time: %.2f" % (t3-t2),"green")) diff --git a/runtime_support.hpp b/runtime_support.hpp index 3dcb15e..2bc8691 100644 --- a/runtime_support.hpp +++ b/runtime_support.hpp @@ -29,7 +29,8 @@ #include #include #include -#include +//#include +#include #include #include