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

Integration openmp #5

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
Binary file added .README.md.swp
Binary file not shown.
2 changes: 1 addition & 1 deletion .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:
run: ${{github.workspace}}/build/main

- name: Run example
run: ${{github.workspace}}/build/simple
run: ${{github.workspace}}/build/matrix_eigen

# - name: Test
# working-directory: ${{ steps.strings.outputs.build-output-dir }}
Expand Down
30 changes: 21 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,32 @@ set(CMAKE_CXX_FLAGS_DEBUG "-g -fsanitize=address,undefined")

# include_directories(include)

# OpenMP
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
set(LINK_LIBS ${LINK_LIBS} ${OpenMP_CXX_LIBRARIES})
endif()

# Eigen
find_package(Eigen3 3.3 REQUIRED NO_MODULE)
set(LINK_LIBS ${LINK_LIBS} Eigen3::Eigen)


# Executables
add_executable(main examples/main.cpp) # create an executable using the specified file
target_link_libraries(main PUBLIC ${LINK_LIBS})

add_executable(simple examples/simple.cpp) # create an executable using the specified file
target_link_libraries(simple PUBLIC ${LINK_LIBS})
add_executable(matrix_eigen examples/matrix_eigen.cpp) # create an executable using the specified file
target_link_libraries(matrix_eigen PUBLIC ${LINK_LIBS})


# OpenMP activation
option(USE_OPENMP "Enable OpenMP support" OFF)

# If ON search for OpenMP
if(USE_OPENMP)
find_package(OpenMP REQUIRED)
if(OpenMP_CXX_FOUND)
add_executable(loop_omp examples/loop_omp.cpp) # create an executable using the specified file
target_compile_options(loop_omp PUBLIC ${OpenMP_CXX_FLAGS})
target_link_libraries(loop_omp PUBLIC ${OpenMP_CXX_FLAGS})
message(STATUS "OpenMP flags added for GCC/Clang: ${OpenMP_CXX_FLAGS}")
else()
message(FATAL_ERROR "OpenMP was requested but could not be found.")
endif()
else()
message(STATUS "OpenMP support disabled")
endif()
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ cmake -S . -B build/ -D CMAKE_BUILD_TYPE=Release
cmake --build build/
```

### Configure a release with OpenMP
```bash
cmake -S . -B build -DUSE_OPENMP=ON
cmake --build build
```

## Troubleshooting

## Reference
Expand Down
29 changes: 29 additions & 0 deletions examples/loop_omp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include <vector>
#include <omp.h>

int main() {
// Initialisation d'un grand tableau pour simuler une charge de travail
const int N = 1000000;
std::vector<int> array(N, 1); // Crée un tableau avec des valeurs de 1
int sum = 0;

// Mesurer le temps avant le calcul
double start_time = omp_get_wtime();

// Début de la région parallèle
#pragma omp parallel for reduction(+:sum)
for (int i = 0; i < N; ++i) {
sum += array[i];
}

// Mesurer le temps après le calcul
double end_time = omp_get_wtime();

// Résultats
std::cout << "La somme est: " << sum << std::endl;
std::cout << "Temps d'exécution avec OpenMP: " << (end_time - start_time) << " secondes" << std::endl;

return 0;
}

File renamed without changes.