簡體   English   中英

C OpenMP並行for循環未在多個線程上運行

[英]C OpenMP parallel for loop is not running on more than one thread

我正在嘗試一個簡單的Open MP示例來並行化for循環,但是我看不到for循環正在多個內核上執行。

這是C程序:

#include </usr/local/Cellar/gcc/5.1.0/lib/gcc/5/gcc/x86_64-apple-darwin14.5.0/5.1.0/include/omp.h>
#include <stdio.h>
#include <unistd.h>
int main() {
    int n;
    #pragma omp parallel
    {
        #pragma omp for
        for(n = 0; n < 10; n++)
            printf(" Thread %d: %d\n", omp_get_thread_num(), n);

        printf("Number of threads: %d\n", omp_get_num_threads());
    }
    printf("Total number of cores in the CPU: %ld\n", sysconf(_SC_NPROCESSORS_ONLN));
}

上面的代碼與此示例非常相似。

當我執行該程序並打印出它正在使用的線程總數(默認情況下,它應該是CPU內核的總數)時,我發現結果1 這是輸出:

10:pavithran$ gcc -fopenmp openMPExample.c -o openMPExample.o
10:pavithran$ ./openMPExample.o 
 Thread 0: 0
 Thread 0: 1
 Thread 0: 2
 Thread 0: 3
 Thread 0: 4
 Thread 0: 5
 Thread 0: 6
 Thread 0: 7
 Thread 0: 8
 Thread 0: 9
Number of threads: 1
Total number of cores in the CPU: 8
10:pavithran$ 

為什么找不到不同線程打印的數字? 為什么線程總數總是1? 有人請幫助我使並行for循環工作。

PS:第一個“ #include ...”語句包含顯式路徑,因為簡單形式: #include <omp.h> 似乎不起作用

您正在使用不支持OpenMP的Apple的gcc clang版本編譯程序。 這就是為什么你得到輸出

$ gcc main.c -fopenmp
main.c:1:10: fatal error: 'omp.h' file not found
#include <omp.h>
         ^
1 error generated.

使用時

#include <omp.h>

(蒙太奇)相反

#include </usr/local/Cellar/gcc/5.1.0/lib/gcc/5/gcc/x86_64-apple-darwin14.5.0/5.1.0/include/omp.h>

使用正確版本的gcc

注意: 基於包含路徑,我假設使用Homebrew安裝gcc 如果不是這種情況,那么最后一節將介紹如何通過具有OpenMP支持的Homebrew安裝gcc

根據所安裝的版本,Homebrew將gcc安裝為gcc-4.9gcc-5 (對於GCC 5.1和GCC 5.2)。 不會創建符號鏈接gcc/usr/local/bin ,當你執行等gcc從您將調用蘋果鐺版本的命令行。 這是我們必須糾正的問題,我們可以通過多種方式使用gcc的Homebrew版本而不是Apple的clang版本。

  1. 通過顯式執行gcc

     $ gcc-5 main.c -fopenmp 
  2. 使用以下命令在/usr/local/bingcc創建符號鏈接

     $ cd /usr/local/bin $ ln -s gcc-5 gcc 

    現在,當您執行gcc -v您應該會看到類似於以下內容(假設$PATH變量設置正確,請參見下文,並且已經通過Homebrew安裝了具有OpenMP支持的gcc ,請參見下文)

     $ gcc -v Using built-in specs. COLLECT_GCC=gcc-5 COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/5.2.0/libexec/gcc/x86_64-apple-darwin14.4.0/5.2.0/lto-wrapper Target: x86_64-apple-darwin14.4.0 Configured with: ../configure --build=x86_64-apple-darwin14.4.0 --prefix=/usr/local/Cellar/gcc/5.2.0 --libdir=/usr/local/Cellar/gcc/5.2.0/lib/gcc/5 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-5 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --with-build-config=bootstrap-debug --disable-werror --with-pkgversion='Homebrew gcc 5.2.0 --without-multilib' --with-bugurl=https://github.com/Homebrew/homebrew/issues --enable-plugin --disable-nls --disable-multilib Thread model: posix gcc version 5.2.0 (Homebrew gcc 5.2.0 --without-multilib) 
  3. gcc的Homebrew版本創建一個別名。 您可以通過添加與

     alias hgcc='gcc-5' 

    .bashrc.bash_profile 更好的是將其添加到名為.bash_aliases的文件中,然后將其包含在.bashrc.bash_profile

     [[ -f ~/.bash_aliases ]] && . ~/.bash_aliases 

    然后,我們將通過hgcc執行它,如下所示

     $ hgcc main.c -fopenmp 

另外:在OS X上安裝具有OpenMP支持的gcc

要安裝帶有OpenMP支持的gcc ,最簡單的方法是通過Homebrew 為此,您需要按照我之前的回答之一使用以下命令

brew install gcc --without-multilib

或者如果您已經通過Homebrew使用安裝了gcc

brew reinstall gcc --without-multilib

這將gcc安裝為gcc-4.9gcc-5 (適用於GCC 5.1和GCC 5.2),具體取決於您安裝的版本。 不會創建符號鏈接gcc/usr/local/bin ,當你執行等gcc從您將調用蘋果鐺版本的命令行。 您可以通過運行gcc -v進行驗證,這將產生與以下類似的輸出

$ gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.4.0
Thread model: posix

檢查路徑設置正確

在(至少在我的Mac系統上)默認的$PATH/usr/bin之前包含/usr/local/bin

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

如果不是這種情況,則需要編輯.bashrc.bash_profile並添加

export PATH=/usr/local/bin:$PATH

這樣可以確保在我們更正Homebrew將其安裝為gcc-4.9gcc-5時,可以在所有Apple版本之前找到gcc的Homebrew版本。

如何設置OMP_NUM_THREADS環境變量? 什么會給你這樣的東西?

 > OMP_NUM_THREADS=8 ./openMPExample.o

(順便說一句,使用以“ .o”結尾的二進制文件可能不是一個好主意)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM