diff --git a/notebooks/openmp-notebooks/hello_world.ipynb b/notebooks/openmp-notebooks/hello_world.ipynb index 3d23faef..7946a1e3 100644 --- a/notebooks/openmp-notebooks/hello_world.ipynb +++ b/notebooks/openmp-notebooks/hello_world.ipynb @@ -8,24 +8,12 @@ "outputs": [], "source": [ "#include \n", - "#include \n", - "#include " + "#include " ] }, { "cell_type": "code", "execution_count": 2, - "id": "ef1cd58a-672c-4a6f-843a-6c88fc4911f3", - "metadata": {}, - "outputs": [], - "source": [ - "Cpp::LoadLibrary(\"libomp\");\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 3, "id": "c2b754ad-9553-4a42-b990-f990a9a269ed", "metadata": {}, "outputs": [], @@ -48,7 +36,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "id": "a37a13d4-fc82-496e-8f42-9e718a8c2aa0", "metadata": {}, "outputs": [ @@ -56,16 +44,24 @@ "name": "stdout", "output_type": "stream", "text": [ - "max threads: 8\n", - "Hello World from thread = 0 with 8 threads\n", - "Hello World from thread = 3 with 8 threads\n", - "Hello World from thread = 4 with 8 threads\n", - "Hello World from thread = 2 with 8 threads\n", - "Hello World from thread = 7 with 8 threads\n", - "Hello World from thread = 1 with 8 threads\n", - "Hello World from thread = 6 with 8 threads\n", - "Hello World from thread = 5 with 8 threads\n", - "all done, with hopefully 8 threads\n" + "max threads: 16\n", + "Hello World from thread = 0 with 16 threads\n", + "Hello World from thread = 3 with 16 threads\n", + "Hello World from thread = 13 with 16 threads\n", + "Hello World from thread = 4 with 16 threads\n", + "Hello World from thread = 12 with 16 threads\n", + "Hello World from thread = 1 with 16 threads\n", + "Hello World from thread = 14 with 16 threads\n", + "Hello World from thread = 15 with 16 threads\n", + "Hello World from thread = 8 with 16 threads\n", + "Hello World from thread = 6 with 16 threads\n", + "Hello World from thread = 11 with 16 threads\n", + "Hello World from thread = 5 with 16 threads\n", + "Hello World from thread = 7 with 16 threads\n", + "Hello World from thread = 10 with 16 threads\n", + "Hello World from thread = 9 with 16 threads\n", + "Hello World from thread = 2 with 16 threads\n", + "all done, with hopefully 16 threads\n" ] } ], @@ -76,7 +72,7 @@ ], "metadata": { "kernelspec": { - "display_name": "C++17 (xcpp+OpenMP)", + "display_name": "C++17 + OpenMP", "language": "cpp", "name": "xcpp17-omp" }, diff --git a/notebooks/openmp-notebooks/linked_list.ipynb b/notebooks/openmp-notebooks/linked_list.ipynb index 607ceb18..31fb12a8 100644 --- a/notebooks/openmp-notebooks/linked_list.ipynb +++ b/notebooks/openmp-notebooks/linked_list.ipynb @@ -9,8 +9,7 @@ "source": [ "#include \n", "#include \n", - "#include \n", - "#include " + "#include " ] }, { @@ -31,16 +30,6 @@ { "cell_type": "code", "execution_count": 3, - "id": "8da842e1-db02-49e0-929d-4e67cbc08172", - "metadata": {}, - "outputs": [], - "source": [ - "Cpp::LoadLibrary(\"libomp\");" - ] - }, - { - "cell_type": "code", - "execution_count": 4, "id": "22f97c49-78d1-496e-ac7c-978aed95331a", "metadata": {}, "outputs": [], @@ -54,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "b16b1e8a-8831-4b8d-9d57-09deeaaa88ee", "metadata": {}, "outputs": [], @@ -66,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "id": "0ef8af6c-1d6f-4c68-84bc-3dd1d8092b06", "metadata": {}, "outputs": [], @@ -85,7 +74,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "id": "1fa0307d-fdc9-4503-95cb-1c6448791354", "metadata": {}, "outputs": [], @@ -101,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "id": "03acb599-9329-49ff-8aff-c0902adb6c3c", "metadata": {}, "outputs": [], @@ -130,7 +119,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "id": "f2dfb41b-e55f-43c0-b7f6-546a1697acb1", "metadata": {}, "outputs": [], @@ -188,7 +177,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "id": "353e5dfd-fcae-43e6-97e3-ec98070811a1", "metadata": {}, "outputs": [ @@ -199,14 +188,14 @@ "Process linked list\n", " Each linked list node will be processed by function 'processwork()'\n", " Each ll node will compute 5 fibonacci numbers beginning with 38\n", - "Threads: 8\n", + "Threads: 16\n", "38 : 39088169\n", "39 : 63245986\n", "40 : 102334155\n", "41 : 165580141\n", "42 : 267914296\n", "43 : 433494437\n", - "Compute Time: 2.617225 seconds\n" + "Compute Time: 3.560749 seconds\n" ] } ], @@ -217,7 +206,7 @@ ], "metadata": { "kernelspec": { - "display_name": "C++17 (xcpp+OpenMP)", + "display_name": "C++17 + OpenMP", "language": "cpp", "name": "xcpp17-omp" }, diff --git a/notebooks/openmp-notebooks/mandel.ipynb b/notebooks/openmp-notebooks/mandel.ipynb index 2d5f8a33..0aeb21a3 100644 --- a/notebooks/openmp-notebooks/mandel.ipynb +++ b/notebooks/openmp-notebooks/mandel.ipynb @@ -10,23 +10,12 @@ "#include \n", "#include \n", "#include \n", - "#include \n", - "#include " + "#include " ] }, { "cell_type": "code", "execution_count": 2, - "id": "4e037323-fbc6-43c8-a17f-6044997bf49b", - "metadata": {}, - "outputs": [], - "source": [ - "Cpp::LoadLibrary(\"libomp\");" - ] - }, - { - "cell_type": "code", - "execution_count": 3, "id": "8b66f96a-14ef-4f23-8024-bcfc42b31e4e", "metadata": {}, "outputs": [], @@ -37,7 +26,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "id": "d89dd57c-fe19-4233-a33a-df9b24fae98a", "metadata": {}, "outputs": [], @@ -47,7 +36,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "5c35c479-2f79-46b7-bc66-24be6b1694e0", "metadata": {}, "outputs": [], @@ -75,7 +64,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "id": "ea116fef-7d05-4e29-97a1-55c85c7241d8", "metadata": {}, "outputs": [], @@ -107,7 +96,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "id": "39cf129c-8106-4e67-a2f1-1a7fff17cd38", "metadata": {}, "outputs": [ @@ -115,7 +104,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Area of Mandlebrot set = 3.80247750 +/- 0.00380248\n", + "Area of Mandlebrot set = 5.23447313 +/- 0.00523447\n", "Correct answer should be around 1.510659\n" ] } @@ -127,7 +116,7 @@ ], "metadata": { "kernelspec": { - "display_name": "C++17 (xcpp+OpenMP)", + "display_name": "C++17 + OpenMP", "language": "cpp", "name": "xcpp17-omp" }, diff --git a/notebooks/openmp-notebooks/openmp-demo.ipynb b/notebooks/openmp-notebooks/openmp-demo.ipynb index fbc24f6f..6c3861a7 100644 --- a/notebooks/openmp-notebooks/openmp-demo.ipynb +++ b/notebooks/openmp-notebooks/openmp-demo.ipynb @@ -8,23 +8,12 @@ "outputs": [], "source": [ "#include \n", - "#include \n", - "#include " + "#include " ] }, { "cell_type": "code", "execution_count": 2, - "id": "1b4dac8e-3ad2-46eb-b801-ba717e664b93", - "metadata": {}, - "outputs": [], - "source": [ - "Cpp::LoadLibrary(\"libomp\");" - ] - }, - { - "cell_type": "code", - "execution_count": 3, "id": "5001e441-1fa5-4bdc-9fa5-2ca103ae484f", "metadata": {}, "outputs": [ @@ -45,7 +34,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "id": "53fb7656-b72e-42bc-ade7-2ae2077142da", "metadata": {}, "outputs": [ @@ -53,12 +42,20 @@ "name": "stdout", "output_type": "stream", "text": [ - "Hello World!Hello World!\n", - "Hello World!Hello World!\n", "Hello World!\n", - "\n", "Hello World!\n", - "\n", + "Hello World!\n", + "Hello World!\n", + "Hello World!\n", + "Hello World!\n", + "Hello World!\n", + "Hello World!\n", + "Hello World!\n", + "Hello World!\n", + "Hello World!\n", + "Hello World!\n", + "Hello World!\n", + "Hello World!\n", "Hello World!\n", "Hello World!\n" ] @@ -76,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "efcdfdb6-a60b-46af-8194-75ef9cc0e27f", "metadata": {}, "outputs": [ @@ -84,14 +81,22 @@ "name": "stdout", "output_type": "stream", "text": [ - "Hello World! (Hello World! (Hello World! (Hello World! (34)\n", - "Hello World! (7)\n", - "0)\n", - "2Hello World! (6))\n", - "\n", + "Hello World! (Hello World! (Hello World! (Hello World! (Hello World! (12Hello World! (1)\n", ")\n", + "9)\n", + "11)\n", + "Hello World! (14)\n", + "Hello World! (13)\n", + "Hello World! (7)\n", "Hello World! (5)\n", - "Hello World! (1)\n" + "Hello World! (6)\n", + "8)\n", + "Hello World! (3)\n", + "Hello World! (4)\n", + "0)\n", + "Hello World! (10)\n", + "Hello World! (15)\n", + "Hello World! (2)\n" ] } ], @@ -107,7 +112,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "id": "d86a9efa-ba28-4cb6-bbfc-abc00ee63506", "metadata": {}, "outputs": [ @@ -115,14 +120,22 @@ "name": "stdout", "output_type": "stream", "text": [ - "Hello World! (Hello World! (34))\n", - "Hello World! (0)\n", + "Hello World! (Hello World! (Hello World! (Hello World! (Hello World! (Hello World! (9Hello World! (8)Hello World! (\n", + "Hello World! ()\n", + "11)\n", + "1)\n", "Hello World! (2)\n", - "Hello World! (Hello World! (1)\n", - "\n", - "7)Hello World! (\n", - "6)\n", + "3)\n", + "Hello World! (7)\n", + "Hello World! (6)\n", + "Hello World! (15)\n", + "Hello World! (10)\n", + "14)\n", "Hello World! (5)\n", + "0)\n", + "Hello World! (4)\n", + "13)\n", + "12)\n", "This is another message! (0)\n", "Goodbye World! (0)\n", "Goodbye World! (1)\n" @@ -148,7 +161,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "id": "5557e01a-7c7d-4b54-8545-962ad11027df", "metadata": {}, "outputs": [ @@ -156,12 +169,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "Initialize a[] time: 0.588681\n", - "Initialize b[] time: 0.513927\n", - "Add arrays time: 1.58928\n", - "Average result time: 0.637053\n", + "Initialize a[] time: 0.341905\n", + "Initialize b[] time: 0.392285\n", + "Add arrays time: 0.401034\n", + "Average result time: 0.191882\n", "Average: 5e+08\n", - "Total time: 3.33191\n" + "Total time: 1.32776\n" ] } ], @@ -214,7 +227,7 @@ ], "metadata": { "kernelspec": { - "display_name": "C++17 (xcpp+OpenMP)", + "display_name": "C++17 + OpenMP", "language": "cpp", "name": "xcpp17-omp" }, diff --git a/notebooks/openmp-notebooks/pi_integral.ipynb b/notebooks/openmp-notebooks/pi_integral.ipynb index f3ef4413..6f98bb38 100644 --- a/notebooks/openmp-notebooks/pi_integral.ipynb +++ b/notebooks/openmp-notebooks/pi_integral.ipynb @@ -8,23 +8,12 @@ "outputs": [], "source": [ "#include \n", - "#include \n", - "#include " + "#include " ] }, { "cell_type": "code", "execution_count": 2, - "id": "93b89565-44fe-4729-980b-d4f897161b0b", - "metadata": {}, - "outputs": [], - "source": [ - "Cpp::LoadLibrary(\"libomp\");" - ] - }, - { - "cell_type": "code", - "execution_count": 3, "id": "9078ac79-ca50-4fef-b785-37f35fec3cab", "metadata": {}, "outputs": [], @@ -35,7 +24,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "id": "f3c10995-6f29-4d71-9e61-1993ca9d1cc9", "metadata": {}, "outputs": [], @@ -73,7 +62,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "0f84442a-d947-4860-bd3c-aeeea963b419", "metadata": {}, "outputs": [ @@ -81,18 +70,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "Num threads available: 8\n", + "Num threads available: 16\n", "Num threads allocated for this run: 1\n", - "pi is 3.141593 in 0.179501 seconds using 1 threads\n", + "pi is 3.141593 in 0.965932 seconds using 1 threads\n", "\n", "Num threads allocated for this run: 2\n", - "pi is 3.141592 in 0.184605 seconds using 2 threads\n", + "pi is 3.141593 in 0.536038 seconds using 2 threads\n", "\n", "Num threads allocated for this run: 3\n", - "pi is 3.141593 in 0.097145 seconds using 3 threads\n", + "pi is 3.141594 in 0.484255 seconds using 3 threads\n", "\n", "Num threads allocated for this run: 4\n", - "pi is 3.141593 in 0.071473 seconds using 4 threads\n", + "pi is 3.141593 in 0.480806 seconds using 4 threads\n", "\n" ] } @@ -104,7 +93,7 @@ ], "metadata": { "kernelspec": { - "display_name": "C++17 (xcpp+OpenMP)", + "display_name": "C++17 + OpenMP", "language": "cpp", "name": "xcpp17-omp" }, diff --git a/share/jupyter/kernels/xc11-omp/kernel.json.in b/share/jupyter/kernels/xc11-omp/kernel.json.in index fc5dbbbd..e968cce8 100644 --- a/share/jupyter/kernels/xc11-omp/kernel.json.in +++ b/share/jupyter/kernels/xc11-omp/kernel.json.in @@ -1,9 +1,5 @@ { "display_name": "C11 + OpenMP", - "env": { - "PATH":"@XEUS_CPP_PATH@", - "LD_LIBRARY_PATH":"@OpenMPLibraryDir@" - }, "argv": [ "@XEUS_CPP_KERNELSPEC_PATH@xcpp", "-f", diff --git a/share/jupyter/kernels/xc17-omp/kernel.json.in b/share/jupyter/kernels/xc17-omp/kernel.json.in index 44676999..c5ae6b8b 100644 --- a/share/jupyter/kernels/xc17-omp/kernel.json.in +++ b/share/jupyter/kernels/xc17-omp/kernel.json.in @@ -1,9 +1,5 @@ { "display_name": "C17 + OpenMP", - "env": { - "PATH":"@XEUS_CPP_PATH@", - "LD_LIBRARY_PATH":"@OpenMPLibraryDir@" - }, "argv": [ "@XEUS_CPP_KERNELSPEC_PATH@xcpp", "-f", diff --git a/share/jupyter/kernels/xc23-omp/kernel.json.in b/share/jupyter/kernels/xc23-omp/kernel.json.in index 9e7b0a7a..60751766 100644 --- a/share/jupyter/kernels/xc23-omp/kernel.json.in +++ b/share/jupyter/kernels/xc23-omp/kernel.json.in @@ -1,9 +1,5 @@ { "display_name": "C23 + OpenMP", - "env": { - "PATH":"@XEUS_CPP_PATH@", - "LD_LIBRARY_PATH":"@OpenMPLibraryDir@" - }, "argv": [ "@XEUS_CPP_KERNELSPEC_PATH@xcpp", "-f", diff --git a/share/jupyter/kernels/xcpp17-omp/kernel.json.in b/share/jupyter/kernels/xcpp17-omp/kernel.json.in index 6ffc8423..f5d59009 100644 --- a/share/jupyter/kernels/xcpp17-omp/kernel.json.in +++ b/share/jupyter/kernels/xcpp17-omp/kernel.json.in @@ -1,9 +1,5 @@ { "display_name": "C++17 + OpenMP", - "env": { - "PATH":"@XEUS_CPP_PATH@", - "LD_LIBRARY_PATH":"@OpenMPLibraryDir@" - }, "argv": [ "@XEUS_CPP_KERNELSPEC_PATH@xcpp", "-f", diff --git a/share/jupyter/kernels/xcpp20-omp/kernel.json.in b/share/jupyter/kernels/xcpp20-omp/kernel.json.in index f9546231..a4646b67 100644 --- a/share/jupyter/kernels/xcpp20-omp/kernel.json.in +++ b/share/jupyter/kernels/xcpp20-omp/kernel.json.in @@ -1,9 +1,5 @@ { "display_name": "C++20 + OpenMP", - "env": { - "PATH":"@XEUS_CPP_PATH@", - "LD_LIBRARY_PATH":"@OpenMPLibraryDir@" - }, "argv": [ "@XEUS_CPP_KERNELSPEC_PATH@xcpp", "-f", diff --git a/share/jupyter/kernels/xcpp23-omp/kernel.json.in b/share/jupyter/kernels/xcpp23-omp/kernel.json.in index abcdf6cc..9eaa78ee 100644 --- a/share/jupyter/kernels/xcpp23-omp/kernel.json.in +++ b/share/jupyter/kernels/xcpp23-omp/kernel.json.in @@ -1,9 +1,5 @@ { "display_name": "C++23 + OpenMP", - "env": { - "PATH":"@XEUS_CPP_PATH@", - "LD_LIBRARY_PATH":"@OpenMPLibraryDir@" - }, "argv": [ "@XEUS_CPP_KERNELSPEC_PATH@xcpp", "-f", diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 14972324..7ad3eabb 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -18,6 +18,8 @@ #include "xinput.hpp" #include "xinspect.hpp" #include "xmagics/os.hpp" +#include +#include #include #ifndef EMSCRIPTEN #include "xmagics/xassist.hpp" @@ -48,7 +50,41 @@ void* createInterpreter(const Args &ExtraArgs = {}) { ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end()); // FIXME: We should process the kernel input options and conditionally pass // the gpu args here. - return Cpp::CreateInterpreter(ClangArgs/*, {"-cuda"}*/); + Cpp::TInterp_t res = Cpp::CreateInterpreter(ClangArgs /*, {"-cuda"}*/); + if (!res) + { + return res; + } + + // clang-repl does not load libomp.so when -fopenmp flag is used + // we need to explicitly load it + if (std::find_if( + ClangArgs.begin(), + ClangArgs.end(), + [](const std::string& s) + { + return s == "-fopenmp"; + } + ) + != ClangArgs.end()) + { + if (Cpp::LoadLibrary("libomp")) + { + return res; + } + std::string conda_prefix = std::getenv("CONDA_PREFIX"); +#if __APPLE__ +#define LIBRARY_PREFIX ".dylib" +#else +#define LIBRARY_PREFIX ".so" +#endif + std::string omp_lib_path = conda_prefix + "/lib/libomp" LIBRARY_PREFIX; + if (!Cpp::LoadLibrary(omp_lib_path.c_str())) + { + std::cerr << "Failed to load libomp (tried location: " << omp_lib_path << ")\n"; + } + } + return res; } using namespace std::placeholders; diff --git a/test/test_xcpp_kernel.py b/test/test_xcpp_kernel.py index 2c0e88f5..49fe87cd 100644 --- a/test/test_xcpp_kernel.py +++ b/test/test_xcpp_kernel.py @@ -237,8 +237,6 @@ class BaseXCppOpenMPTests(jupyter_kernel_test.KernelTests): code_omp=""" #include #include - #include - Cpp::LoadLibrary("libomp"); """ code_omp_2="""