0

When declare a static thrust::device_vector in a function, wrapped into dll, it cause cudaErrorCudartUnloading (error 29) during the end of the program. It seems that the cuda context is destroyed before the deconstructor of device_vector. When declared in a normal function, there is no error. Could someone explain the reason for this?

I tried creating a c++ class dll in project 1, then add reference of project 1 to porject 2, where the main.cpp is. Finally I instantiate the class in main.cpp and call the function, the function works fine but the program has trouble during termination while it's trying to release the static devive_vector defined in dll.

// Project 1: Foo.h
#include <thrust/device_vector.h>
class __declspec(dllexport) Foo {
public:
    Foo() {};
    ~Foo() {};
    void FooFcn();
};

// Project 1: Foo.cu
void Foo::FooFcn() {
    static thrust::device_vector<int> a;
    a.resize(500000);
}

// Project 2: main.cpp
#include "Foo.h"
int main()
{
    Foo foo;
    foo.FooFcn();
    return 0;
}

cudaErrorCudartUnloading (error 29) is the asserted error if you tried to run this.

  • ' It seems that the cuda context is destroyed before the deconstructor of device_vector.' -- that is exactly what is happening – talonmies Apr 23 '19 at 22:04
  • @talonmies yes, that's what error 29 usually means, I'm more interested in why it happens and is there a work around? – Xú Jiāfēng Apr 23 '19 at 22:14
  • 2
    see [here](https://stackoverflow.com/questions/24869167/trouble-launching-cuda-kernels-from-static-initialization-code). Your question is effectively a duplicate of that one and I a inclined to mark it as a duplicate of that one. That explains why it happens. There is no work around, it is a broken design pattern, prohibited by CUDA. You are required to make sure that any CUDA runtime API calls that take place even in any constructor or destructor, take place within the confines of `main`. – Robert Crovella Apr 24 '19 at 04:26
  • @RobertCrovella thank you for your answers! I always wonder if wrapping cuda code into DLL is a good idea. – Xú Jiāfēng Apr 24 '19 at 05:11
  • @talonmies thank you for your answer in the other question, dynamically link kernel code seems to be a bad idea. – Xú Jiāfēng Apr 24 '19 at 05:12
  • You can put CUDA code in a DLL, that is not a problem. The problem is when constructors/destructors are called outside of main scope. That is a problem with or without a DLL. – Robert Crovella Apr 24 '19 at 11:22

0 Answers0