OpenACC interoperability with thrust, linking error


I try to use thrust::sort to sort an array of floats that is generated by OpenACC. I found an example that pretty much does what I wanted:
I tried it and it works fine with the pgcc compiler. However I use the pgc++ and there I cannot get it to link.

The thrust code is this:

#include <thrust/sort.h>

extern "C" void sort(float *values, int num, void *stream)
                            values, values + num);

compiled with “nvcc -c” (cuda-10.0)

the openacc code is here:

#include <iostream>
#include <openacc.h>

void sort(float *values, int num, void *stream);

int main(int argc, char* argv[])
    int N = 13;
    float *a = new float[N];

    void* stream = acc_get_cuda_stream(acc_async_sync); 

    #pragma acc enter data create(a[0:N])
    #pragma acc parallel loop present(a)
    for(int i = 0; i < N; ++i)
        a[i] = (N/2-i)*(N/2-i);

    #pragma acc data present(a)
    #pragma acc host_data use_device(a)
        sort(a, N, stream);

    #pragma acc update host(a[0:N])
    for(int i = 0; i < N; ++i)
        std::cout << a[i] <<"\n";

compiling with: pgc++ -acc -ta=tesla:cuda10.0 -Mcuda=cuda10.0 test_thrust2.cpp sortGPU.o -Minline

and get the error “undefined reference to `sort(float*, int, void*)’”

(sorry for the double post, I couldnt post all at once due to some strange error).

Hi Rob,

The problem is because you’re using extern “C” inconsistently. By default, C++ will name-mangle symbol names but use C-style symbol names when extern “C” is used. Here “sort” gets name-mangled when compiling test_thrust2.cpp but not in Hence the linker can’t match-up the symbol names.

The solution is to either remove extern “C” from, or add it to sort’s prototype in test_thrust2.cpp so the symbol naming is consistent.

Hope this helps,

Thanks, that did the trick.