Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing exercise 10 #6

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions content/code/10_taskloop/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# set minimum cmake version
cmake_minimum_required(VERSION 3.18)

# project name and language
project(taskloop LANGUAGES CXX)

add_executable(taskloop taskloop.cpp)

# FIXME find the suitable OpenMP component

# FIXME link with imported target
target_link_libraries(taskloop ...)
36 changes: 36 additions & 0 deletions content/code/10_taskloop/taskloop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// example adapted from
// http://www.openmp.org/wp-content/uploads/openmp-examples-4.5.0.pdf page 85

#include <cstdlib>
#include <iostream>

void long_running_task(){
// do something
std::cout << "long_running_task" << std::endl;
};

void loop_body(int i, int j){
// do something
std::cout << "i = " << i << " j = " << j << std::endl;
};

void parallel_work() {
int i, j;
#pragma omp taskgroup
{
#pragma omp task
long_running_task(); // can execute concurrently

#pragma omp taskloop private(j) grainsize(500) nogroup
for (i = 0; i < 100; i++) { // can execute concurrently
for (j = 0; j < i; j++) {
loop_body(i, j);
}
}
}
}

int main() {
parallel_work();
return EXIT_SUCCESS;
}
15 changes: 5 additions & 10 deletions content/dependencies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ A complete list of ``Find<PackageName>.cmake`` can be found from the command-lin

$ cmake --help-module-list | grep "Find"

.. typealong:: Using OpenMP
.. exercise:: Exercise 10: Using OpenMP

We want to compile the following OpenMP sample code: [#omp]_

.. literalinclude:: code/09_taskloop/solution/taskloop.cpp
.. literalinclude:: code/10_taskloop/taskloop.cpp
:language: c++

Note the usage of the ``taskloop`` construct, which was introduced in OpenMP
Expand All @@ -116,18 +116,13 @@ A complete list of ``Find<PackageName>.cmake`` can be found from the command-lin
we find that the module provides the components ``C``, ``CXX``, and
``Fortran`` and that ``OpenMP::OpenMP_CXX`` target will be provided, if
detection is successful.
Thus, we do the following:

.. code-block:: cmake

find_package(OpenMP 4.5 REQUIRED COMPONENTS CXX)

target_link_libraries(task-loop PRIVATE OpenMP::OpenMP_CXX)
The scaffold project is in ``content/code/10_taskloop``. You will need to
find the suitable OpenMP libary and link against the imported target.

We can configure and build verbosely. [#verbose]_
Notice that compiler flags, include directories, and link libraries are properly resolved by CMake.

You can find the complete working example in ``content/code/10_taskloop/solution``.
You can find the complete working example in the ``solution`` subfolder.

.. exercise:: Exercise 11: Using MPI

Expand Down
Loading