Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Xeus-Cling with OpenMP
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# OpenMP in Xeus-Cling"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Preparation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set the following in your `kernel.json`:\n",
"\n",
"```json\n",
"{\n",
" \"display_name\": \"C++14 OpenMP\",\n",
" \"argv\": [\n",
" \"/home/axel/miniconda3/envs/cling/bin/xeus-cling\",\n",
" \"-f\",\n",
" \"{connection_file}\",\n",
" \"-std=c++14\", \"-fopenmp\"\n",
" ],\n",
" \"language\": \"C++14\"\n",
"}\n",
"```\n",
"\n",
"(replace `/home/axel/` without your output of `echo $HOME/`)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/home/axel/\n"
]
}
],
"source": [
"!echo $HOME/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Assuming you installed xeus-cling with conda in a conda environment named \"cling\", then `<KERNEL_PREFIX>` is most likely in `$HOME/miniconda3/envs/cling/share/jupyter/kernels`. Just modify or copy the existing `xeus-cling-cpp14/` directory."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Some Environment Diagnostics"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/home/axel/miniconda3/envs/cling/bin/cling\n"
]
}
],
"source": [
"!which cling"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.5\n"
]
}
],
"source": [
"!cling --version"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"clang version 5.0.0 (tags/RELEASE_500/final)\n",
"Target: x86_64-unknown-linux-gnu\n",
"Thread model: posix\n",
"InstalledDir: /home/axel/miniconda3/envs/cling/bin\n"
]
}
],
"source": [
"!clang --version"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## OpenMP Hello World"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"#include <omp.h>\n",
"#include <iostream>"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"#pragma cling load(\"libomp\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"omp_get_max_threads()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"int nthreads, tid;\n",
"\n",
"// trying to set the number of threads\n",
"omp_set_dynamic(0); // Explicitly disable dynamic teams\n",
"int const number_of_threads = 4;\n",
"omp_set_num_threads(number_of_threads);"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello World! I am thread = 0 out of 1\n",
"And I am thread 0, ruling the world.\n",
"And I 0\n",
"And I 0\n",
"And I 0\n",
"And I 0\n"
]
}
],
"source": [
"// each thread in here has their own copy of variables\n",
"#pragma omp parallel private(nthreads, tid) num_threads(number_of_threads)\n",
"{\n",
" // all threads\n",
" tid = omp_get_thread_num();\n",
" std::cout << \"Hello World! I am thread = \" << tid\n",
" << \" out of \" << omp_get_num_threads()\n",
" << std::endl;\n",
"\n",
" // thread zero (\"master\") only\n",
" if (tid == 0) \n",
" std::cout << \"And I am thread 0, ruling the world.\"\n",
" << std::endl;\n",
" \n",
" #pragma omp parallel for\n",
" for( int i = 0; i < 4; ++i)\n",
" std::cout << \"And I \" << tid << std::endl;\n",
" \n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "C++14 OpenMP",
"language": "C++14",
"name": "xeus-cling-openmp-cpp14"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@ax3l

This comment has been minimized.

Copy link
Owner Author

@ax3l ax3l commented Mar 28, 2018

@SimeonEhrig

This comment has been minimized.

Copy link

@SimeonEhrig SimeonEhrig commented May 15, 2018

I found the problem. The Argument '-fopenmp' will not passed to the cling instance. If you run a blank cling in the bash without the '-fopenmp' you get the same behavoir.

The following bunch of code, you can check the arguments of the cling instance in a jupyter notebook: https://gist.github.com/SimeonEhrig/05bdb0927d5f799608a18034ee69449a

@ax3l

This comment has been minimized.

Copy link
Owner Author

@ax3l ax3l commented May 15, 2018

@SimeonEhrig thank you, that explains it!

(I think you have to write @ax3l to ping me in a gist comment, did not receive the notification we talked about)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment