<p dir="auto">I am using torch1.5 + cuda10, call cuda-function with c++ code and use it in python by pybind.<br>
1、paste my c++ code, name bind_cudevice.cc<br>
2、compile bind_cudevice.cc with my g++ compile code<br>
3、paste my python code named test.py<br>
4、Finally you will see core dump, this core only with cuda10+torch1.5</p>
<p dir="auto">My Environment</p>
<ul dir="auto">
<li>PyTorch Version (e.g., 1.0):1.5</li>
<li>OS (e.g., Linux):Linux</li>
<li>How you installed PyTorch (<code class="notranslate">conda</code>, <code class="notranslate">pip</code>, source):source</li>
<li>Build command you used (if compiling from source):yes</li>
<li>Python version:3.6.8</li>
<li>CUDA/cuDNN version:Cuda10.0</li>
<li>GPU models and configuration:K40</li>
<li>Any other relevant information:</li>
</ul>
<p dir="auto">The c++ code as below:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#include &lt;iostream&gt;
#include &lt;cublas_v2.h&gt;
#include &lt;cuda.h&gt;
#include &lt;cuda_runtime_api.h&gt;
#include &lt;cusparse.h&gt;
#include &lt;cusolverDn.h&gt;
#include &lt;curand.h&gt;
#include &lt;pybind11/pybind11.h&gt;

class CuDevice {
public:
    static CuDevice&amp; Instantiate() {
        CuDevice &amp;ans = this_thread_device_;
        if (!ans.initialized_)
            ans.Initialize();
        return ans;
    }

private:
    CuDevice(){};
public:
    void Initialize() {
        initialized_ = true;
        cusolverDnCreate(&amp;cusolverdn_handle_);
        cusolverDnSetStream(cusolverdn_handle_, cudaStreamPerThread);
      }
      ~CuDevice() {
        std::cout &lt;&lt; &quot;in ~CuDevice&quot; &lt;&lt; std::endl;
        if (cusolverdn_handle_) {
            std::cout &lt;&lt; &quot;in ~CuDevice and cusolverdn_handle_&quot; &lt;&lt; std::endl;
            cusolverDnDestroy(cusolverdn_handle_);
       }
    }
private:
    bool initialized_;
    cublasHandle_t cublas_handle_;
    cusparseHandle_t cusparse_handle_;
    curandGenerator_t curand_handle_;
    cusolverDnHandle_t cusolverdn_handle_;
    static thread_local CuDevice this_thread_device_;
    //static CuDevice this_thread_device_;
};

thread_local CuDevice CuDevice::this_thread_device_ = CuDevice();

int init_cudevice(){
    CuDevice::Instantiate();
    std::cout &lt;&lt; &quot;hello world&quot; &lt;&lt; std::endl;
    return 0;
}

PYBIND11_MODULE(example, m) {
    m.def(&quot;init_cudevide&quot;,
    &amp;init_cudevice,
    &quot;select gpu id&quot;);
}"><pre class="notranslate"><code class="notranslate">#include &lt;iostream&gt;
#include &lt;cublas_v2.h&gt;
#include &lt;cuda.h&gt;
#include &lt;cuda_runtime_api.h&gt;
#include &lt;cusparse.h&gt;
#include &lt;cusolverDn.h&gt;
#include &lt;curand.h&gt;
#include &lt;pybind11/pybind11.h&gt;

class CuDevice {
public:
    static CuDevice&amp; Instantiate() {
        CuDevice &amp;ans = this_thread_device_;
        if (!ans.initialized_)
            ans.Initialize();
        return ans;
    }

private:
    CuDevice(){};
public:
    void Initialize() {
        initialized_ = true;
        cusolverDnCreate(&amp;cusolverdn_handle_);
        cusolverDnSetStream(cusolverdn_handle_, cudaStreamPerThread);
      }
      ~CuDevice() {
        std::cout &lt;&lt; "in ~CuDevice" &lt;&lt; std::endl;
        if (cusolverdn_handle_) {
            std::cout &lt;&lt; "in ~CuDevice and cusolverdn_handle_" &lt;&lt; std::endl;
            cusolverDnDestroy(cusolverdn_handle_);
       }
    }
private:
    bool initialized_;
    cublasHandle_t cublas_handle_;
    cusparseHandle_t cusparse_handle_;
    curandGenerator_t curand_handle_;
    cusolverDnHandle_t cusolverdn_handle_;
    static thread_local CuDevice this_thread_device_;
    //static CuDevice this_thread_device_;
};

thread_local CuDevice CuDevice::this_thread_device_ = CuDevice();

int init_cudevice(){
    CuDevice::Instantiate();
    std::cout &lt;&lt; "hello world" &lt;&lt; std::endl;
    return 0;
}

PYBIND11_MODULE(example, m) {
    m.def("init_cudevide",
    &amp;init_cudevice,
    "select gpu id");
}
</code></pre></div>
<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>
<p dir="auto">I am using cusolverDn in c++ and import it to python with pybind11.<br>
Segment Fault after use cusolverDnCreate() and cusolverDnDestroy() with torch.</p>
<p dir="auto">NOTE:</p>
<p dir="auto">###<strong>1: When the demo run with Torch1.2,it run success, the SegFault disappear, does it mean Torch 1.5 has some bug?</strong><br>
###<strong>2: This SegFault only appear with "thread_local", Does it provide some information for this bug?</strong><br>
###<strong>3: Please see test.py below, if we "import torch" after "import example", SegFault disappear.</strong><br>
###<strong>4: Torch have not link libcusolver.so, so we just link it once.</strong></p>
<p dir="auto"><a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ezyang/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/ezyang">@ezyang</a> say that upgarde cuda may fix this, but after i upgrade cuda to 10.2, SegFault as usual.</p>
<p dir="auto">cc please <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ezyang/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/ezyang">@ezyang</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/gchanan/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/gchanan">@gchanan</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/zou3519/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/zou3519">@zou3519</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/yf225/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/yf225">@yf225</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/glaringlee/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/glaringlee">@glaringlee</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ngimel/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/ngimel">@ngimel</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/mcarilli/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/mcarilli">@mcarilli</a></p>

<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Steps to reproduce the behavior:</p>
<p dir="auto">1.paste the c++ code, name bind_cudevice.cc<br>
2.compile bind_cudevice.cc with my g++ compile code<br>
3.paste my python code named test.py<br>
4. just run test.py and see</p>
<p dir="auto">bind_cudevice.cc</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#include &lt;iostream&gt;

#include &lt;cublas_v2.h&gt;
#include &lt;cuda.h&gt;
#include &lt;cuda_runtime_api.h&gt;

#include &lt;cusolverDn.h&gt;
#include &lt;cusolverSp.h&gt;
#include &lt;cusolverRf.h&gt;

#include &lt;pybind11/pybind11.h&gt;

class CuDevice {

public:
     CuDevice() {
        initialized_ = true;

        cudaSetDevice(0);

        cusolverDnCreate(&amp;cusolverdn_handle_);
        cusolverDnSetStream(cusolverdn_handle_, cudaStreamPerThread);
    }

    ~CuDevice() {
        std::cout &lt;&lt; &quot;in ~CuDevice&quot; &lt;&lt; std::endl;
        if (cusolverdn_handle_) {
            std::cout &lt;&lt; &quot;in ~CuDevice and cusolverdn_handle_&quot; &lt;&lt; std::endl;
            cusolverDnDestroy(cusolverdn_handle_);
            std::cout &lt;&lt; &quot;Destory success&quot; &lt;&lt; std::endl;
       }
    }


private:
    bool initialized_;
    cusolverDnHandle_t cusolverdn_handle_;

};

int init_cudevice(){
    thread_local CuDevice a = CuDevice();
    std::cout &lt;&lt; &quot;hello world&quot; &lt;&lt; std::endl;
    return 0;
}

PYBIND11_MODULE(example, m) {
    m.def(&quot;init_cudevide&quot;,
    &amp;init_cudevice,
    &quot;select gpu id&quot;);
}"><pre class="notranslate"><code class="notranslate">#include &lt;iostream&gt;

#include &lt;cublas_v2.h&gt;
#include &lt;cuda.h&gt;
#include &lt;cuda_runtime_api.h&gt;

#include &lt;cusolverDn.h&gt;
#include &lt;cusolverSp.h&gt;
#include &lt;cusolverRf.h&gt;

#include &lt;pybind11/pybind11.h&gt;

class CuDevice {

public:
     CuDevice() {
        initialized_ = true;

        cudaSetDevice(0);

        cusolverDnCreate(&amp;cusolverdn_handle_);
        cusolverDnSetStream(cusolverdn_handle_, cudaStreamPerThread);
    }

    ~CuDevice() {
        std::cout &lt;&lt; "in ~CuDevice" &lt;&lt; std::endl;
        if (cusolverdn_handle_) {
            std::cout &lt;&lt; "in ~CuDevice and cusolverdn_handle_" &lt;&lt; std::endl;
            cusolverDnDestroy(cusolverdn_handle_);
            std::cout &lt;&lt; "Destory success" &lt;&lt; std::endl;
       }
    }


private:
    bool initialized_;
    cusolverDnHandle_t cusolverdn_handle_;

};

int init_cudevice(){
    thread_local CuDevice a = CuDevice();
    std::cout &lt;&lt; "hello world" &lt;&lt; std::endl;
    return 0;
}

PYBIND11_MODULE(example, m) {
    m.def("init_cudevide",
    &amp;init_cudevice,
    "select gpu id");
}
</code></pre></div>
<p dir="auto">g++ compile code</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="
g++ -O3 -Wall -shared -std=c++11 -fPIC -I/usr/include/python3.6m -I./third-party/pybind11-2.2.4/include -I/usr/local/cuda/include -Wl,-rpath=/usr/local/cuda/lib64 -L/usr/local/cuda/lib64 -lcudart -lcusolver `python3 -m pybind11 --includes` binding_cudevice.cc -o example`python3-config --extension-suffix`
"><pre class="notranslate"><code class="notranslate">
g++ -O3 -Wall -shared -std=c++11 -fPIC -I/usr/include/python3.6m -I./third-party/pybind11-2.2.4/include -I/usr/local/cuda/include -Wl,-rpath=/usr/local/cuda/lib64 -L/usr/local/cuda/lib64 -lcudart -lcusolver `python3 -m pybind11 --includes` binding_cudevice.cc -o example`python3-config --extension-suffix`

</code></pre></div>
<p dir="auto">test.py</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import torch
import example
example.init_cudevide()"><pre class="notranslate"><code class="notranslate">import torch
import example
example.init_cudevide()
</code></pre></div>
<h2 dir="auto">Core Dump Info</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(gdb) bt
#0  0x00007f78b6a391de in ?? () from /usr/local/cuda/lib64/libcusolver.so.10.0
#1  0x00007f78b6a5b4ca in ?? () from /usr/local/cuda/lib64/libcusolver.so.10.0
#2  0x00007f78b5db6e49 in ?? () from /usr/local/cuda/lib64/libcusolver.so.10.0
#3  0x00007f78b5b854c1 in cusolverDnDestroy () from /usr/local/cuda/lib64/libcusolver.so.10.0
#4  0x00007f78be143952 in kaldi::CuDevice::~CuDevice (this=0x2f18a38, __in_chrg=&lt;optimized out&gt;) at cu-device.cc:683
#5  0x00007f79880d7ad1 in (anonymous namespace)::run (p=&lt;optimized out&gt;) at /home/nwani/m3/conda-bld/compilers_linux-64_1560109574129/work/.build/x86_64-conda_cos6-linux-gnu/src/gcc/libstdc++-v3/libsupc++/atexit_thread.cc:75
#6  0x00007f79b752cc99 in __run_exit_handlers () from /usr/lib64/libc.so.6
#7  0x00007f79b752cce7 in exit () from /usr/lib64/libc.so.6
#8  0x00007f79b751550c in __libc_start_main () from /usr/lib64/libc.so.6
#9  0x0000000000400c20 in _start ()"><pre class="notranslate"><code class="notranslate">(gdb) bt
#0  0x00007f78b6a391de in ?? () from /usr/local/cuda/lib64/libcusolver.so.10.0
#1  0x00007f78b6a5b4ca in ?? () from /usr/local/cuda/lib64/libcusolver.so.10.0
#2  0x00007f78b5db6e49 in ?? () from /usr/local/cuda/lib64/libcusolver.so.10.0
#3  0x00007f78b5b854c1 in cusolverDnDestroy () from /usr/local/cuda/lib64/libcusolver.so.10.0
#4  0x00007f78be143952 in kaldi::CuDevice::~CuDevice (this=0x2f18a38, __in_chrg=&lt;optimized out&gt;) at cu-device.cc:683
#5  0x00007f79880d7ad1 in (anonymous namespace)::run (p=&lt;optimized out&gt;) at /home/nwani/m3/conda-bld/compilers_linux-64_1560109574129/work/.build/x86_64-conda_cos6-linux-gnu/src/gcc/libstdc++-v3/libsupc++/atexit_thread.cc:75
#6  0x00007f79b752cc99 in __run_exit_handlers () from /usr/lib64/libc.so.6
#7  0x00007f79b752cce7 in exit () from /usr/lib64/libc.so.6
#8  0x00007f79b751550c in __libc_start_main () from /usr/lib64/libc.so.6
#9  0x0000000000400c20 in _start ()
</code></pre></div>

<h2 dir="auto">Expected behavior</h2>
<p dir="auto">No error happen</p>

<h2 dir="auto">Environment</h2>
<ul dir="auto">
<li>PyTorch Version (e.g., 1.5):1.5</li>
<li>OS (e.g., Linux):Centos7</li>
<li>How you installed PyTorch (<code class="notranslate">conda</code>, <code class="notranslate">pip</code>, source):source</li>
<li>Build command you used (if compiling from source):</li>
<li>Python version:3.6.8</li>
<li>CUDA/cuDNN version:10.0</li>
<li>GPU models and configuration:K40</li>
<li>Any other relevant information:</li>
</ul>
<h2 dir="auto">Additional context</h2>

<p dir="auto">cuSolver is base cublas and cusparse, reference about cuSolver：<br>
<a href="https://docs.nvidia.com/cuda/archive/10.0/cusolver/index.html#cuSolverDNhandle" rel="nofollow">https://docs.nvidia.com/cuda/archive/10.0/cusolver/index.html#cuSolverDNhandle</a></p>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ngimel/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/ngimel">@ngimel</a></p>