0
点赞
收藏
分享

微信扫一扫

openmp 并行计算


​​代码在git​​

example.cpp

#include <iostream>
#include <omp.h>
#include <string>

int main(int argc, char *argv[]) {
std::cout << "number of available processors: " << omp_get_num_procs()
<< std::endl;
std::cout << "number of threads: " << omp_get_max_threads() << std::endl;

auto n = std::stol(argv[1]);
std::cout << "we will form sum of numbers from 1 to " << n << std::endl;

// start timer
auto t0 = omp_get_wtime();

auto s = 0LL;
#pragma omp parallel for reduction(+ : s)
for (auto i = 1; i <= n; i++) {
s += i;
}

// stop timer
auto t1 = omp_get_wtime();

std::cout << "sum: " << s << std::endl;
std::cout << "elapsed wall clock time: " << t1 - t0 << " seconds" << std::endl;

return 0;
}

CMakeLists.txt

# set minimum cmake version
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)

# project name and language
project(recipe-05 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(OpenMP REQUIRED)

add_executable(example example.cpp)

target_link_libraries(example
PUBLIC
OpenMP::OpenMP_CXX
)


举报

相关推荐

0 条评论