使用OpenACC和cublasDgemv将g ++与pgi编译的代码链接时出现内存错误

问题描述 投票:0回答:1

为了在带有g ++的应用程序中将我的GPU与OpenACC和cublas一起使用,我设置了一个小测试示例。为此,我创建了文件:

  • main.cpp
  • pgiCudaCode.h
  • pgiCudaCode.cpp

我的测试系统使用了带有Nvidia GTX1070卡的g ++版本7.5.0和pgc ++版本19.10-0的Ubuntu 18.04 Linux。

文件pgiCudaCode.cpp对使用openACC和cublas的一般矩阵-矢量相乘产生了一些影响。该文件使用PGI编译器和以下命令进行编译:

pgc++ -fast -Minfo=opt -ta:tesla:cc60,managed,nordc -Mcudalib=cublas -Minfo=accel -fPIC pgiCudaCode.cpp -c pgiCudaCode.o

我确实使用选项nordc以便与g ++一起使用。

主文件已编译并与g ++链接:

g++ -fPIC pgiCudaCode.o -L/opt/pgi/linux86-64/19.10/lib/ -laccapi -laccg -laccn -laccg2 -lpgiman -ldl -lcudadevice -lcudapgi -lomp -lnuma -lpthread -lnspgc -lpgc -lm -lgcc -lc -lgcc -lpgmath -lblas -lpgatm -lpgkomp -L/opt/pgi/linux86-64/2019/cuda/10.1/lib64/ -lcublas -lcublasLt -lcudart main.cpp -o mainGCC

在我的Ubuntu 18.04上设置了这些导出之后

export LD_LIBRARY_PATH="/opt/pgi/linux86-64/19.10/lib/:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH="/opt/pgi/linux86-64/2019/cuda/10.1/lib64/:$LD_LIBRARY_PATH"

我可以运行Executable mainGCC并获得以下输出:

./mainGCC 
Vector 1:
1
1

Matrix:
        1       3
        2       4

matrix*vec pure openACC:
4
6

matrix*vec cublas with internal allocation:
4
6

matrix*vec cublas without internal allocation:
Failing in Thread:1
call to cuMemcpyDtoHAsync returned error 700: Illegal address during kernel execution

Failing in Thread:1
call to cuMemFreeHost returned error 700: Illegal address during kernel execution

与pgi编译器链接和编译main.cpp时,我没有得到此错误:

pgc++ -fast -Minfo=opt -ta:tesla:cc60,managed,nordc -Mcudalib=cublas -Minfo=accel -fPIC pgiCudaCode.o main.cpp -o mainPGI

这里mainPGI的输出是正确的:

Vector 1:
1
1

Matrix:
        1       3
        2       4

matrix*vec pure openACC:
4
6

matrix*vec cublas with internal allocation:
4
6

matrix*vec cublas without internal allocation:
4
6

所以有趣的部分是:

  • [在函数matmulPureOpenACC中使用g ++分配的内存时,它不会引起任何问题,而在使用cublas的函数matmul中出现错误。
  • 当您在函数matmul_internAlloc中使用pgi分配的内存时,cublas可以与g ++一起使用。

这使我想到我的问题:

[如何在函数matmul中使用g ++分配的内存来防止此错误?

这里是相应的.cpp和.h文件。

main.cpp:

#include <iostream>
#include "pgiCudaCode.h"

void printVec(int N, double* vec)
{
    for(int i = 0; i < N; i++)
    {
        std::cout << vec[i] << std::endl;
    }
}

void printMatrix(int N, double* matr)
{
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            std::cout << '\t' << matr[i + j * N];
        }
        std::cout << std::endl;
    }
}

int main()
{
    int N        = 2;
    double* vec1 = new double[N];
    vec1[0]      = 1.0;
    vec1[1]      = 1.0;
    double* vec2 = new double[N];
    vec2[0]      = 0.0;
    vec2[1]      = 0.0;
    double* matr = new double[N*N];
    matr[0]      = 1.0;
    matr[1]      = 2.0;
    matr[2]      = 3.0;
    matr[3]      = 4.0;

    std::cout << "Vector 1:" << std::endl;
    printVec(N, vec1);
    std::cout << std::endl;

    std::cout << "Matrix:" << std::endl;
    printMatrix(N, matr);
    std::cout << std::endl;

    std::cout << "matrix*vec pure openACC:" << std::endl;
    matmulPureOpenACC(N, matr, vec1, vec2);
    printVec(N, vec2);
    std::cout << std::endl;

    vec2[0]      = 0.0;
    vec2[1]      = 0.0;

    std::cout << "matrix*vec cublas with internal allocation:" << std::endl;
    matmul_internAlloc(N, matr, vec1, vec2);
    printVec(N, vec2);
    std::cout << std::endl;

    vec2[0]      = 0.0;
    vec2[1]      = 0.0;

    std::cout << "matrix*vec cublas without internal allocation:" << std::endl;
    matmul(N, matr, vec1, vec2);
    printVec(N, vec2);
    std::cout << std::endl;

    delete [] vec1;
    delete [] vec2;
    delete [] matr;
    return 0;
}

pgiCudaCode.h:

#ifndef PGICUDACODE_H
#define PGICUDACODE_H


bool matmul(int n, const double* matr, const double* b, double* c);

bool matmul_internAlloc(int n, const double* matr, const double* b, double* c);

bool matmulPureOpenACC(int n, const double* matr, const double* b, double* c);

#endif

pgiCudaCode.cpp:

#include <iostream>
#include <cublas_v2.h>

void matmul(int n, const double* matr, const double* b, double* c)
{
    #pragma acc data pcopyin(n , matr[0:n*n], b[0:n]) pcopy(c[0:n])
    {
        #pragma acc host_data use_device(matr, b, c)
        {
            cublasHandle_t handle;
            cublasStatus_t stat = cublasCreate(&handle);
            if ( CUBLAS_STATUS_SUCCESS != stat ) {
                std::cerr<<"CUBLAS initialization failed"<<std::endl;
            }

            if ( CUBLAS_STATUS_SUCCESS == stat )
            {
                const double alpha = 1.0;
                const double beta  = 1.0;
                stat = cublasDgemv_v2(handle, CUBLAS_OP_N, n,n, &alpha, matr, n, b, 1, &beta, c, 1);
                if (stat != CUBLAS_STATUS_SUCCESS) {
                    std::cerr<<"cublasDgemm failed"<<std::endl;
                }
            }
            cublasDestroy(handle);
        }
    }
}

void matmul_internAlloc(int n2, const double* matr2, const double* b2, double* c2)
{
    int n         = n2;
    double* matr  = new double[n*n];
    double* b     = new double[n];
    double* c     = new double[n];

    std::copy(&matr2[0], &matr2[n*n], &matr[0]);
    std::copy(&b2[0], &b2[n], &b[0]);
    std::copy(&c2[0], &c2[n], &c[0]);

    #pragma acc data pcopyin(n , matr[0:n*n], b[0:n]) pcopy(c[0:n])
    {
        #pragma acc host_data use_device(matr, b, c)
        {
            cublasHandle_t handle;
            cublasStatus_t stat = cublasCreate(&handle);
            if ( CUBLAS_STATUS_SUCCESS != stat ) {
                std::cerr<<"CUBLAS initialization failed"<<std::endl;
            }

            if ( CUBLAS_STATUS_SUCCESS == stat )
            {
                const double alpha = 1.0;
                const double beta  = 1.0;
                stat = cublasDgemv_v2(handle, CUBLAS_OP_N, n,n, &alpha, matr, n, b, 1, &beta, c, 1);
                if (stat != CUBLAS_STATUS_SUCCESS) {
                    std::cerr<<"cublasDgemm failed"<<std::endl;
                }
            }
            cublasDestroy(handle);
        }
    }
    std::copy(&c[0], &c[n], &c2[0]);
    delete [] matr;
    delete [] b;
    delete [] c;
}

void matmulPureOpenACC(int n, const double* matr, const double* b, double* c)
{
    #pragma acc data pcopyin(n, matr[0:n*n], b[0:n]) pcopy(c[0:n])
    {
        #pragma acc parallel loop
        for(int i = 0; i < n; i++)
        {
            #pragma acc loop seq
            for(int j = 0; j < n; j++)
            {
                c[i] += matr[i + j*n]*b[j];
            }
        }
    }
}
c++ g++ cublas openacc pgi
1个回答
0
投票

最好使用pgc ++进行链接。使用g ++编译main.cpp很好,但是在链接时PGI编译器将隐式包含一些OpenACC和CUDA互操作性所需的初始化例程。没有这种初始化,您将看到像这样的运行时错误。

% pgc++ -fast -ta:tesla:cc70 pgiCudaCode.cpp -c pgiCudaCode.o
pgiCudaCode.cpp:
% g++ -c main.cpp
% pgc++ -fast -ta:tesla:cc70 -Mcudalib=cublas -Mcuda pgiCudaCode.o main.o -o mainGCC 
% ./mainGCC
Vector 1:
1
1

Matrix:
        1       3
        2       4

matrix*vec pure openACC:
4
6

matrix*vec cublas with internal allocation:
4
6

matrix*vec cublas without internal allocation:
4
6
© www.soinside.com 2019 - 2024. All rights reserved.