c - 将启用 PGI OpenACC 的库与 gcc 链接 [英] c - Linking a PGI OpenACC-enabled library with gcc

查看:16
本文介绍了c - 将启用 PGI OpenACC 的库与 gcc 链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我的问题依赖于使用两个不同的编译器编译/构建文件(使用库),同时利用源文件中的 OpenACC 结构.

Briefly speaking, my question relies in between compiling/building files (using libraries) with two different compilers while exploiting OpenACC constructs in source files.

我有一个具有 OpenACC 结构的 C 源文件.它只有一个简单的函数来计算数组的总和:

I have a C source file that has an OpenACC construct. It has only a simple function that computes total sum of an array:

#include <stdio.h>
#include <stdlib.h>
#include <openacc.h>

double calculate_sum(int n, double *a) {
    double sum = 0;
    int i;

    printf("Num devices: %d
", acc_get_num_devices(acc_device_nvidia));

    #pragma acc parallel copyin(a[0:n])
    #pragma acc loop
    for(i=0;i<n;i++) {
        sum += a[i];
    }

    return sum;
}

我可以使用以下行轻松编译它:

I can easily compile it using following line:

pgcc -acc -ta=nvidia -c libmyacc.c

然后,通过以下行创建一个静态库:

Then, create a static library by following line:

ar -cvq libmyacc.a libmyacc.o

为了使用我的库,我写了一段代码如下:

To use my library, I wrote a piece of code as following:

#include <stdio.h>
#include <stdlib.h>

#define N 1000

extern double calculate_sum(int n, double *a);

int main() {
    printf("Hello --- Start of the main.
");
    double *a = (double*) malloc(sizeof(double) * N);
    int i;
    for(i=0;i<N;i++) {
        a[i] = (i+1) * 1.0;
    }

    double sum = 0.0;
    for(i=0;i<N;i++) {
        sum += a[i];
    }
    printf("Sum: %.3f
", sum);


    double sum2 = -1;
    sum2 = calculate_sum(N, a);
    printf("Sum2: %.3f
", sum2);

    return 0;
}

现在,我可以使用这个带有 PGI 编译器本身的静态库来编译上面的源代码(f1.c):

Now, I can use this static library with PGI compiler itself to compile above source (f1.c):

pgcc -acc -ta=nvidia f1.c libmyacc.a

它会完美执行.但是,对于 gcc,它有所不同.我的问题就在这里.如何使用 gcc 正确构建它?

And it will execute flawlessly. However, it differs for gcc. My question relies in here. How can I built it properly with gcc?

感谢 Jeff 对这个问题的评论:用 gcc 链接器链接 pgi 编译库,现在我可以构建我的源代码了文件 (f1.c) 没有错误,但可执行文件会发出一些致命错误.

Thanks to Jeff's comment on this question: linking pgi compiled library with gcc linker, now I can build my source file (f1.c) without errors, but the executable file emits some fatal errors.

这是我用 gcc (f1.c) 编译源文件时使用的:

This is what I use to compile my source file with gcc (f1.c):

gcc f1.c -L/opt/pgi/linux86-64/16.5/lib -L/usr/lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5 -L.-laccapi -laccg -laccn -laccg2 -ldl -lcudadevice -lpgmp -lnuma -lpthread -lnspgc -lpgc -lm -lgcc -lc -lgcc -lmyacc

这是错误:

Num devices: 2
Accelerator Fatal Error: No CUDA device code available

<小时>

感谢使用 PGI 编译器编译 f1.c 时的 -v 选项,我看到编译器从 PGI 和 NVidia 调用了许多其他工具(例如 pgacclnknvlink).


Thanks to -v option when compiling f1.c with PGI compiler, I see that the compiler invokes so many other tools from PGI and NVidia (like pgacclnk and nvlink).

我的问题:

  1. 我是不是走错路了?我可以从 GCC 调用 PGI 编译库中的函数并在这些函数中使用 OpenACC 吗?
  2. 如果上面的回答是肯定的,我可以使用没有 PGI 采取的步骤(调用 pgacclnknvlink)的静止链接吗?
  3. 如果上面的回答也是肯定的,我该怎么办?
  1. Am I on the wrong path? Can I call functions in PGI compiled libraries from GCC and use OpenACC within those functions?
  2. If answer to above is positive, can I use still link without steps (calling pgacclnk and nvlink) that PGI takes?
  3. If answer to above is positive too, what should I do?

推荐答案

将-ta=tesla:nordc"添加到您的 pgcc 编译中.默认情况下,PGI 对 GPU 代码使用运行时动态编译 (RDC).但是 RDC 需要 gcc 不支持的额外链接步骤(使用 nvlink).nordc"子选项禁用 RDC,因此您将能够在库中使用 OpenACC 代码.但是,通过禁用 RDC,您将无法再从计算区域调用外部设备例程.

Add "-ta=tesla:nordc" to your pgcc compilation. By default PGI uses runtime dynamic compilation (RDC) for the GPU code. However RDC requires an extra link step (with nvlink) that gcc does not support. The "nordc" sub-option disables RDC so you'll be able to use OpenACC code in a library. However by disabling RDC you can no longer call external device routines from a compute region.

% pgcc -acc -ta=tesla:nordc -c libmyacc.c
% ar -cvq libmyacc.a libmyacc.o
a - libmyacc.o
% gcc f1.c -L/proj/pgi/linux86-64/16.5/lib -L/usr/lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5 -L. -laccapi -laccg -laccn -laccg2 -ldl -lcudadevice -lpgmp -lnuma -lpthread -lnspgc -lpgc -lm -lgcc -lc -lgcc -lmyacc
% a.out
Hello --- Start of the main.
Sum: 500500.000
Num devices: 8
Sum2: 500500.000

希望这会有所帮助,垫子

Hope this helps, Mat

这篇关于c - 将启用 PGI OpenACC 的库与 gcc 链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆