tt_hh
tt_hh

Reputation: 1

How to call cuda libraries from OpenACC C++ code by g++?

I want to use cuda libraries (cublas, cusparse cusolver) in OpenACC code by g++-8.

Can g++-8 use cuda libraries like PGI?

I wrote following code, that needs cublas or cublas_v2.

// #include<cublas.h>
#include<cublas_v2.h>
int main(){

//...allocate and initialize data...//

#pragma acc data copy(x[0:size], y[0:size])                   
        {                                                     
#pragma acc host_data use_device(x,y)                         
                {                                             
                        //dot = cublasDdot(size, x, 1, y, 1); 
                        cublasDdot(h, size, x, 1, y, 1, &dot);
                }
        } 
}

compile command is here: (These libraries are exists.)

 g++-8 -fopenacc -foffload=nvptx-none \
        -foffload="-I/usr/local/cuda-10.1/targets/x86_64-linux/include/ \
        -L/usr/local/cuda-10.1/targets/x86_64-linux/lib/ \
        -L/usr/lib/x86_64-linux-gnu/ \
        -lcuda -lcudart -lcublas" -O3 -std=c++11 acc_cublas.cpp -o acc_cublas.o

It cause a compilation error. Following error is occured:

acc_cublas.cpp:(.text.startup+0x11f): undefined reference to `cublasCreate_v2'
acc_cublas.cpp:(.text.startup+0x1b4): undefined reference to `cublasDdot_v2'

Is this compile command correct? Why can't find functions?

I created my environment based on nvidia/cuda:10.1-devel by executing following commands:

apt install -y gcc-8-offload-nvptx nvptx-tools g++-8
apt install -y cuda-cublas-dev-10-0
apt install -y cuda-cudart-dev-10-0

I got the correct answer in the following program that does not use cuda libraries:

#pragma acc data copy(x[0:size], y[0:size])
        {
#pragma acc kernels
                {
#pragma acc loop reduction ( + : dot)
                        for(int i=0; i<size; i++){
                                dot += x[i] * y[i];
                        }
                }
        }

Upvotes: 0

Views: 387

Answers (1)

jefflarkin
jefflarkin

Reputation: 1279

When I recreated your container it looks like the cublas library is installed in /usr/local/cuda-10.0, rather than cuda-10.1. Can you please try updating your compile line to point to that directory instead? Without a fully compilable example it's hard to confirm for myself that this fixes your problem. Alternatively, you could link against /usr/lib/x86_64-linux-gnu/libcublas.so, which appears to be the 10.2 version of cublas and also appears to have the symbols you need.

Upvotes: 1

Related Questions