thrust icon indicating copy to clipboard operation
thrust copied to clipboard

CPU->GPU copy of a default-initialized vector [windows/vs2019]

Open mfoco opened this issue 6 years ago • 7 comments

In Windows, with VS2019 16.7.2 (debug mode) and the default thrust provided with CUDA 11 package, I found this problem:

int main()
{
    thrust::device_vector<int> gpu;
    std::vector<int> cpu;
    gpu = cpu; // assertion in debug
}

The code triggers an assertion in vector:46 (standard vector header, provided by Microsoft) coming from thrust/system/cuda/detail/internal/copy_cross_system.h:106 (it's inside the function cross_system_copy_n, and the assert is triggering because the code is dereferencing the begin() iterator, which is invalid (being the vector empty), and more specifically is nullptr (being uninitialized).

The problem disappears if the gpu vector is allocated (e.g. by pushing back and clearing) before the assignment:

#include <thrust/device_vector.h>

int main()
{
    thrust::device_vector<int> gpu;
    std::vector<int> cpu;
    gpu.push_back(1);
    gpu.clear();
    gpu = cpu; // works! (actually, the CUDA11 version triggers a different assertion, but that has been fixed already)
}

I think this might be loosely related to #938 and #939 .

The other assertion I mentioned above has been fixed by @allisonvacanti here (https://github.com/thrust/thrust/commit/b9a20738fd68ce4af4dd2df7b1820bf9a2fc69dc)

mfoco avatar Sep 16 '20 18:09 mfoco

This looks like an easy enough fix -- wrapping the call to trivial_device_copy with if (n > 0) { ... } should do the trick.

alliepiper avatar Sep 16 '20 19:09 alliepiper

This looks like an easy enough fix -- wrapping the call to trivial_device_copy with if (n > 0) { ... } should do the trick.

Great! I'm looking forward to 1.11.0! (attn @nvibd to include this patch into our temporary working version too)

mfoco avatar Sep 17 '20 16:09 mfoco

I'm having trouble reproducing this. What arguments are being passed to nvcc?

alliepiper avatar Sep 17 '20 20:09 alliepiper

I created a default cuda project in Visual Studio 2019 (using the "Cuda 11.0 Runtime" template) and then changed the "kernel.cu" file to match the code above. The assertion is triggered on run in debug|x64 configuration.

This is the entire command line from the build window:

"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.27.29110\bin\HostX86\x64" -x cu   -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\include" -I"C:\vcpkg\installed\x64-windows\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\include"  -G   --keep-dir x64\Debug -maxrregcount=0  --machine 64 --compile -cudart static  -g   -DWIN32 -DWIN64 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Fdx64\Debug\vc142.pdb /FS /Zi /RTC1 /MDd " -o x64\Debug\kernel.cu.obj "C:\Users\pan\source\repos\cuda-assign-repro\cuda-assign-repro\kernel.cu"

This is exactly what comes out of the VS template, as I didn't change any of the default options.

FYI, I was a bit puzzled by the additional include directory C:\vcpkg\installed\x64-windows\include so I wanted to check its content, but the directory doesn't even exists (nor C:\vcpkg\installed\x64-windows exists). That shouldn't be a problem.

mfoco avatar Sep 18 '20 14:09 mfoco

Thanks -- I can reproduce it here with those flags.

alliepiper avatar Sep 18 '20 15:09 alliepiper

I've fixed this one, though compiling the test suite requires disabling warnings-as-errors to compile with MSVC in debug due to F:\code\thrust\thrust/detail/allocator/allocator_traits.inl(369): warning #3056-D: calling a __host__ function("std::_Iterator_base12::_Iterator_base12") from a __host__ __device__ function("std::_Vector_const_iterator< ::std::_Vector_val< ::std::_Simple_types<signed char> > > ::_Vector_const_iterator") is not allowed

bjude avatar Oct 24 '20 06:10 bjude

Thanks!

The MSVC debug warnings is a known bug (#1273 for context). I'm planning to work-around that in the build system for 1.11.0.

alliepiper avatar Oct 24 '20 14:10 alliepiper