{"id":61,"date":"2020-03-19T19:00:05","date_gmt":"2020-03-19T17:00:05","guid":{"rendered":"http:\/\/terboven.com\/?p=61"},"modified":"2020-05-06T15:34:51","modified_gmt":"2020-05-06T13:34:51","slug":"using-c-with-openmp-in-jupyter-notebooks","status":"publish","type":"post","link":"https:\/\/terboven.com\/?p=61","title":{"rendered":"Using C++ with OpenMP in jupyter notebooks"},"content":{"rendered":"<p>Many people might know jupyter notebooks as an interactive, web-based environment for python programming, often in the context of data analysis. However, if the cling kernel is used, it becomes possible to interactively work with C\/C++ code. The so-called kernel is the interpreter used for the evaluation of code cells and <a href=\"https:\/\/github.com\/root-project\/cling\" target=\"_blank\" rel=\"noopener noreferrer\">cling<\/a> is an interactive C++ interpreter. This could look like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-72 size-full\" src=\"http:\/\/terboven.com\/wp-content\/uploads\/2020\/03\/jupyter_01_cpp.png\" alt=\"C++ in jupyter notebook\" width=\"565\" height=\"295\" srcset=\"https:\/\/terboven.com\/wp-content\/uploads\/2020\/03\/jupyter_01_cpp.png 565w, https:\/\/terboven.com\/wp-content\/uploads\/2020\/03\/jupyter_01_cpp-300x157.png 300w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/p>\n<p>In the <a href=\"https:\/\/www.stifterverband.org\/digital-lehrfellows-nrw\/2019\/oden_terboven\" target=\"_blank\" rel=\"noopener noreferrer\">IkapP project<\/a> funded by the Stifterverband and state of North Rhine-Westphalia, one goal is to remove entry barriers faced by students using HPC systems in lectures. One step towards this goal is the creation of a virtual lab environment for parallel programming that can also be used for interactive experiments with different parallelization approaches. Users can, e.g., interactively experience performance results of code changes on real HPC systems. There are many parallel programming models in use and of relevance for our lectures, but we wanted to start with OpenMP and MPI. However, cling does not support OpenMP out of the box.<\/p>\n<p>At the time of this writing, the current version of xeus-cling is 0.8.1, which is not based on a recent version of clang. So in principle, OpenMP Version 3.1 should be supported, which means tasking will be available, but offloading will not be available. OpenMP &#8220;in action&#8221; in a jupyter notebook could look like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-73 size-full\" src=\"http:\/\/terboven.com\/wp-content\/uploads\/2020\/03\/jupyter_02_cpp_openmp.png\" alt=\"C++ with OpenMP in jupyter notebook\" width=\"575\" height=\"225\" srcset=\"https:\/\/terboven.com\/wp-content\/uploads\/2020\/03\/jupyter_02_cpp_openmp.png 575w, https:\/\/terboven.com\/wp-content\/uploads\/2020\/03\/jupyter_02_cpp_openmp-300x117.png 300w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/p>\n<p>In order for such notebooks to work correctly, we had to fix a few things in the xeus-cling code, in particular to ensure correct output from multiple threads. The corresponding patches were created and submitted by <a href=\"https:\/\/www.hahnjo.de\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jonas Hahnfeld<\/a>, a student worker in the IkapP project at RWTH. They have been accepted to mainline (<a href=\"https:\/\/github.com\/jupyter-xeus\/xeus-cling\/pull\/314\" target=\"_blank\" rel=\"nofollow noreferrer noopener\">#314<\/a>, <a href=\"https:\/\/github.com\/jupyter-xeus\/xeus-cling\/pull\/315\" target=\"_blank\" rel=\"nofollow noreferrer noopener\">#315<\/a>, <a href=\"https:\/\/github.com\/jupyter-xeus\/xeus-cling\/pull\/320\" target=\"_blank\" rel=\"nofollow noreferrer noopener\">#320<\/a>, <a href=\"https:\/\/github.com\/jupyter-xeus\/xeus-cling\/pull\/332\" target=\"_blank\" rel=\"nofollow noreferrer noopener\">#332<\/a>, <a href=\"https:\/\/github.com\/jupyter-xeus\/xeus-cling\/pull\/319\" target=\"_blank\" rel=\"nofollow noreferrer noopener\">#319<\/a>, <a href=\"https:\/\/github.com\/jupyter-xeus\/xeus-cling\/pull\/316\" target=\"_blank\" rel=\"nofollow noreferrer noopener\">#316, <\/a><a href=\"https:\/\/github.com\/jupyter-xeus\/xeus-cling\/pull\/324\" target=\"_blank\" rel=\"nofollow noreferrer noopener\">#324<\/a><a href=\"https:\/\/github.com\/jupyter-xeus\/xeus-cling\/pull\/316\" target=\"_blank\" rel=\"nofollow noreferrer noopener\">, <\/a><a href=\"https:\/\/github.com\/jupyter-xeus\/xeus-cling\/pull\/325\" target=\"_blank\" rel=\"nofollow noreferrer noopener\">#325<\/a> (also submitted to xeus-python) and <a href=\"https:\/\/github.com\/jupyter-xeus\/xeus-cling\/pull\/329\" target=\"_blank\" rel=\"nofollow noreferrer noopener\">#329<\/a>), but since our submission there has been no new release.<\/p>\n\n\n<p><strong>Compiling and Installing xeus-cling on CentOS 7.7<\/strong><\/p>\n\n\n\n<p>The production environment on RWTH&#8217;s HPC systems is CentOS 7.7. The build instructions were compiled by Jonas. In order to build xeus-clang for a jupyter environment, do for each of the following projects (in this order):<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/jarro2783\/cxxopts\" target=\"_blank\">https:\/\/github.com\/jarro2783\/cxxopts<\/a>, <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/nlohmann\/json\" target=\"_blank\">https:\/\/github.com\/nlohmann\/json<\/a>, <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/zeux\/pugixml\" target=\"_blank\">https:\/\/github.com\/zeux\/pugixml<\/a>, <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/xtensor-stack\/xtl\" target=\"_blank\">https:\/\/github.com\/xtensor-stack\/xtl<\/a>, <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/zeromq\/libzmq,\" target=\"_blank\">https:\/\/github.com\/zeromq\/libzmq,<\/a> <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/zeromq\/cppzmq\" target=\"_blank\">https:\/\/github.com\/zeromq\/cppzmq<\/a>, <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/jupyter-xeus\/xeus\" target=\"_blank\">https:\/\/github.com\/jupyter-xeus\/xeus<\/a>, <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/jupyter-xeus\/xeus-cling\" target=\"_blank\">https:\/\/github.com\/jupyter-xeus\/xeus-cling<\/a><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ git clone https:\/\/github.com\/org\/repo src\n$ mkdir build\n$ cd build\n$ cmake -DCMAKE_BUILD_TYPE=Release \\\n-DCMAKE_INSTALL_PREFIX=\/path\/to\/install\/xeus-cling\/ \\\n-DCMAKE_C_COMPILER=\/path\/to\/install\/xeus-cling\/bin\/clang \\\n-DCMAKE_CXX_COMPILER=\/path\/to\/install\/xeus-cling\/bin\/clang++ \\  ..\/src\n$ make -j32\n$ make install<\/pre>\n\n\n\n<p>After that, activate the kernels via<\/p>\n\n\n\n<p><code>for k in xcpp11 xcpp14 xcpp17; do<\/code><br><code>cp -r ..\/..\/..\/..\/xeus-cling\/share\/jupyter\/kernels\/$k \/path\/to\/install\/jupyter\/share\/jupyter\/kernels\/;<\/code><br><code>done<\/code><\/p>\n\n\n\n<p>and add <code>-fopenmp<\/code> to each <code>kernel.json<\/code> to enable OpenMP. Finally, let cling find the runtime libraries by adding to <code>jupyterhub_config.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">c.Spawner.environment = {\n  'LD_LIBRARY_PATH': '\/path\/to\/install\/xeus-cling\/lib\/',\n}<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many people might know jupyter notebooks as an interactive, web-based environment for python programming, often in the context of data analysis. However, if the cling kernel is used, it becomes possible to interactively work with C\/C++ code. The so-called kernel is the interpreter used for the evaluation of code cells and cling is an interactive &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/terboven.com\/?p=61\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using C++ with OpenMP in jupyter notebooks&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[17,3,18],"tags":[20,22,23,19,5,21],"class_list":["post-61","post","type-post","status-publish","format-standard","hentry","category-hpc","category-openmp","category-teaching","tag-c","tag-cling","tag-ikapp","tag-jupyter","tag-openmp","tag-xeus-cling"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/terboven.com\/index.php?rest_route=\/wp\/v2\/posts\/61","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/terboven.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/terboven.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/terboven.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/terboven.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=61"}],"version-history":[{"count":17,"href":"https:\/\/terboven.com\/index.php?rest_route=\/wp\/v2\/posts\/61\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/terboven.com\/index.php?rest_route=\/wp\/v2\/posts\/61\/revisions\/85"}],"wp:attachment":[{"href":"https:\/\/terboven.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/terboven.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/terboven.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}