LoginSignup
26

More than 5 years have passed since last update.

Macでclang+OpenMP 2016

Last updated at Posted at 2016-01-13

2016年現在、OpenMPに対応したclangをビルドする方法をまとめます。ググると OpenMP®/Clang を使えという情報がいくつか見つかるのですが、llvm 3.7.0 から clang は OpenMP を公式サポートしているので、公式のclangを使いましょう。

現在のllvmの最新リリースバージョンは 3.7.1で、trunkは 3.8.0 になっています 1/14(この記事を書いた翌日)には 3.9.0 になっていました… (a07c74e)

clang ビルド手順まとめ

  1. llvm をクローン
  2. tools/ に clang をクローン
  3. projects/ に下記のプロジェクトをクローン
  4. 適当な場所に build ディレクトリを作成(この記事では、llvm の top-level ディレクトリとします)
  5. cd build
  6. cmake -DCMAKE_BUILD_TYPE=Release ..
  7. make -j4
  8. make -j4 install

安定版を使う場合は、適当にtagが付いたものを使えばOKだと思います。

clang+openmp が使えるかどうか確認

コード

test.cpp
#include <iostream>
#include <omp.h>

int main() {
#pragma omp for simd
  for (int i = 0; i < 10; ++i) {
    std::cout << i << std::endl;
  }
  return 0;
}

Building the OpenMP Branch of LLVM/Clang で紹介されていたサンプルコードを拝借しました。

コンパイル

-v をつけた結果を貼っておきます。

% clang++ -fopenmp -v test.cpp
clang version 3.8.0 (https://github.com/llvm-mirror/clang.git 1f54fb1b5a76d0fa9d956baa523845b4861c0915) (https://github.com/llvm-mirror/llvm.git 805ea3283576c755fc1e8bf19872dbb1c8436fca)
Target: x86_64-apple-darwin14.4.0
Thread model: posix
InstalledDir: /usr/local/bin
 "/usr/local/bin/clang-3.8" -cc1 -triple x86_64-apple-macosx10.10.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name test.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 242.2 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /usr/local/bin/../lib/clang/3.8.0 -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/ryuyamamoto/Dropbox/llvm/clang -ferror-limit 19 -fmessage-length 205 -fopenmp -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.10.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/ph/mss0_ys92f9fw0nt9c1qswy40000gq/T/test-727998.o -x c++ test.cpp
clang -cc1 version 3.8.0 based upon LLVM 3.8.0svn default target x86_64-apple-darwin14.4.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/bin/../include/c++/v1
 /usr/local/include
 /usr/local/bin/../lib/clang/3.8.0/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.10.0 -o a.out /var/folders/ph/mss0_ys92f9fw0nt9c1qswy40000gq/T/test-727998.o -lomp -lc++ -lSystem /usr/local/bin/../lib/clang/3.8.0/lib/darwin/libclang_rt.osx.a

実行結果

% ./a.out
0
1
2
3
4
5
6
7
8
9

雑記

  • -DCMAKE_BUILD_TYPE=Release を指定しないとデバッグモードでビルドされるため、clang++ のコンパイル時間が激重になる。参考までに、上記のコードで約5秒(リリースだと0.4秒)。
  • libcxx/libcxxabi を一緒にビルド & インストールしないと、iosteam が見つかりません、のように標準ライブラリが見つからない旨のエラーがでる。Macでclang+OpenMP にあるように、include pathを追加することで解決可能だが、libcxx も一緒にインストールすることでも解決可能
  • OSX 10.10 で試しましたが、他の環境でも同じ方法で問題ないと思います。

参考

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
26