LoginSignup
14
4

More than 5 years have passed since last update.

CMakeでOpenMPアプリケーション開発

Posted at

この記事について

CMakeを使って、OpenMPアプリケーション開発用プロジェクトを作ります。WindowsとLinuxの両方でビルドできるようにします。

  • CMakeを使うことのメリット
    • マルチプラットフォーム対応
    • インクルードパスやライブラリ設定を自動化できる
    • (特に、GitHub等で他の人に渡すときに便利)

環境

  • Windows10 64-bit
    • Visual Studio 2017 Community
  • Ubuntu 16.04 on VirtualBox on Windows 10

対象とするプログラム

以下のように、4スレッドでfor文を回すOpenMPのサンプルコードをビルドするプロジェクトを作ります。

main.cpp
#include <stdio.h>
#include <omp.h>

int main()
{
    omp_set_num_threads(4);
    #pragma omp parallel for
    for (int i = 0; i < 20; i++) {
        printf("%d / %d\n", omp_get_thread_num(), omp_get_num_threads());
    }

    return 0;
}

OpenMPの準備

Windows10(VS)、Linux(Ubuntu 16.04)共に、特別な設定やインストールは不要です。素の状態でOpenMPを使えます。

プロジェクトの作成

└─TestOpenCV
    │  CMakeLists.txt
    │  main.cpp
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(MyOpenMPProject)

# Create Main project
set(SOURCES
    main.cpp
)

set(HEADERS
    # main.h
)

add_executable(Main
    ${SOURCES}
    ${HEADERS}
)

# For OpenMP
find_package(OpenMP REQUIRED)
if(OpenMP_FOUND)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

OpenMPをfind_packageして、必要なCFLAGSを追加しているだけです。
includeパス設定やライブラリリンク設定は不要でした。

14
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
4