[英]Do I have to build gcc-10 from source on Ubuntu 18.04 to have OpenMP GPU target offload?
[英]OpenMP 4.5 won't offload to GPU with target directive
我正在嘗試使用 openMP 制作一個簡單的 GPU 卸載程序。 但是,當我嘗試卸載它時,它仍然在默認設備上運行,即我的 CPU。
我已經安裝了一個支持 CUDA 的編譯器 g++ 7.2.0(在我使用的集群上)。 當我運行下面的代碼時,它告訴我它可以看到 8 個 GPU,但是當我嘗試卸載時,它說它仍在 CPU 上。
#include <omp.h>
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#define n 10000
#define m 10000
using namespace std;
int main()
{
double tol = 1E-10;
double err = 1;
size_t iter_max = 10;
size_t iter = 0;
bool notGPU[1] = {true};
double Anew[n][m];
double A[n][m];
int target[1];
target[0] = omp_get_initial_device();
cout << "Total Devices: " << omp_get_num_devices() << endl;
cout << "Target: " << target[0] << endl;
for (int iter = 0; iter < iter_max; iter++){
#pragma omp target
{
err = 0.0;
#pragma omp parallel for reduction(max:err)
for (int j = 1; j < n-1; ++j){
target[0] = omp_is_initial_device();
for (int i = 1; i < m-1; i++){
Anew[j][i] = 0.25 * (A[j][i+1] + A[j][i-1] + A[j-1][i] + A[j+1][i]);
err = fmax(err, fabs(Anew[j][i] - A[j][i]));
}
}
}
}
if (target[0]){
cout << "not on GPU" << endl;
} else{
cout << "On GPU" << endl;}
return 0;
}
當我運行它時,我總是發現它不在 GPU 上,而是有 8 個設備可用。
這不是一個有據可查的過程!
你必須安裝一些看起來有點像的包:
sudo apt install gcc-offload-nvptx
您還需要向編譯字符串添加其他標志。 我在下面匯總了其中的一些。 混合和匹配直到某些東西起作用,或者將它們用作進一步谷歌搜索的基礎。
gcc -fopenmp -foffload=x86_64-intelmicemul-linux-gnu="-mavx2" -foffload=nvptx-none -foffload="-O3" -O2 test.c -fopenmp-targets=nvptx64-nvidia-cuda
當我上次在 2018 年用 GCC 嘗試這個時,它只是沒有用。 當時 OpenMP 的目標卸載僅適用於IBM XL編譯器,而 OpenACC(與 OpenMP 類似的指令集)僅適用於 Nvidia 的PGI 編譯器。 我發現 PGI 在編譯 C/C++ 方面比其他人做得更差(似乎效率低下,非標准標志),但社區版是免費提供的,稍微翻譯一下就可以讓您在 OpenACC 中快速運行。
IBM XL 似乎在編譯方面做得很好,但我不知道它是否免費提供。
GCC 的情況可能已經改變。 如果你找到了讓它工作的方法,我很感激你在這里發表評論。 我強烈建議你停止嘗試 GCC7 並獲得 GCC8 或 GCC9。 GPU 卸載是一個快速發展的領域,您需要最新的編譯器來充分利用它。
看起來您在 #pragma omp 目標行中缺少設備(id):
#pragma omp target device(/*your device id here*/)
沒有它,您就沒有明確要求 OpenMP 在您的 CPU 之外的任何地方運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.