Skip to content

Commit 725c78f

Browse files
author
Vasileios Karakasis
authored
Merge pull request #1870 from teojgo/doc/pthread_tutorial_option
[doc] Add `-pthread` to the multithreaded tutorial test
2 parents 8bf4581 + 4ee8fb4 commit 725c78f

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

docs/tutorial_basics.rst

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ We extend our C++ "Hello, World!" example to print the greetings from multiple t
417417
:language: cpp
418418
:lines: 6-
419419

420-
This program takes as argument the number of threads it will create and it uses ``std::thread``, which is C++11 addition, meaning that we will need to pass ``-std=c++11`` to our compilers.
420+
This program takes as argument the number of threads it will create and it uses ``std::thread``, which is a C++11 addition, meaning that we will need to pass ``-std=c++11`` to our compilers.
421421
Here is the corresponding ReFrame test, where the new concepts introduced are highlighted:
422422

423423
.. code-block:: console
@@ -429,12 +429,30 @@ Here is the corresponding ReFrame test, where the new concepts introduced are hi
429429
:lines: 6-
430430
:emphasize-lines: 11-13
431431

432+
433+
In order to compile applications using ``std::thread`` with GCC and Clang, the ``-pthread`` option has to be passed to the compiler.
434+
Since the above option might not be valid for other compilers, we use pipeline hooks to differentiate based on the programming environment as follows:
435+
436+
.. code-block:: python
437+
438+
@rfm.run_before('compile')
439+
def set_threading_flags(self):
440+
environ = self.current_environ.name
441+
if environ in {'clang', 'gnu'}:
442+
self.build_system.cxxflags += ['-pthread']
443+
444+
445+
.. note::
446+
447+
The pipeline hooks, as well as the regression test pipeline itself, are covered in more detail later on in the tutorial.
448+
449+
432450
ReFrame delegates the compilation of a test to a *build system*, which is an abstraction of the steps needed to compile the test.
433451
Build systems take also care of interactions with the programming environment if necessary.
434452
Compilation flags are a property of the build system.
435453
If not explicitly specified, ReFrame will try to pick the correct build system (e.g., CMake, Autotools etc.) by inspecting the test resources, but in cases as the one presented here where we need to set the compilation flags, we need to specify a build system explicitly.
436-
In this example, we instruct ReFrame to compile a single source file using the ``-std=c++11 -Wall`` compilation flags.
437-
Finally, we set the arguments to be passed to the generated executable in :attr:`~reframe.core.pipeline.RegressionTest.executable_opts`.
454+
In this example, we instruct ReFrame to compile a single source file using the ``-std=c++11 -pthread -Wall`` compilation flags.
455+
Finally, we set the arguments to be passed to the generated executable in :attr:`executable_opts <reframe.core.pipeline.RegressionTest.executable_opts>`.
438456

439457

440458
.. code-block:: console
@@ -1075,7 +1093,7 @@ Let's see and comment the changes:
10751093
First of all, we need to add the new programming environments in the list of the supported ones.
10761094
Now there is the problem that each compiler has its own flags for enabling OpenMP, so we need to differentiate the behavior of the test based on the programming environment.
10771095
For this reason, we define the flags for each compiler in a separate dictionary (``self.flags``) and we set them in the :func:`setflags` pipeline hook.
1078-
Let's explain what is this all about.
1096+
We have first seen the pipeline hooks in the multithreaded "Hello, World!" example and now we explain them in more detail.
10791097
When ReFrame loads a test file, it instantiates all the tests it finds in it.
10801098
Based on the system ReFrame runs on and the supported environments of the tests, it will generate different test cases for each system partition and environment combination and it will finally send the test cases for execution.
10811099
During its execution, a test case goes through the *regression test pipeline*, which is a series of well defined phases.

tutorials/basics/hellomp/hellomp1.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ def __init__(self):
1717
self.build_system.cxxflags = ['-std=c++11', '-Wall']
1818
self.executable_opts = ['16']
1919
self.sanity_patterns = sn.assert_found(r'Hello, World\!', self.stdout)
20+
21+
@rfm.run_before('compile')
22+
def set_threading_flags(self):
23+
environ = self.current_environ.name
24+
if environ in {'clang', 'gnu'}:
25+
self.build_system.cxxflags += ['-pthread']

tutorials/basics/hellomp/hellomp2.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ def __init__(self):
1919
num_messages = sn.len(sn.findall(r'\[\s?\d+\] Hello, World\!',
2020
self.stdout))
2121
self.sanity_patterns = sn.assert_eq(num_messages, 16)
22+
23+
@rfm.run_before('compile')
24+
def set_threading_flags(self):
25+
environ = self.current_environ.name
26+
if environ in {'clang', 'gnu'}:
27+
self.build_system.cxxflags += ['-pthread']

tutorials/basics/hellomp/hellomp3.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ def __init__(self):
2020
num_messages = sn.len(sn.findall(r'\[\s?\d+\] Hello, World\!',
2121
self.stdout))
2222
self.sanity_patterns = sn.assert_eq(num_messages, 16)
23+
24+
@rfm.run_before('compile')
25+
def set_threading_flags(self):
26+
environ = self.current_environ.name
27+
if environ in {'clang', 'gnu'}:
28+
self.build_system.cxxflags += ['-pthread']

0 commit comments

Comments
 (0)