lundi 25 juillet 2016

CUDA: Invalid instantiation of std::cref

While including a header only lib (that uses c++11 threads) within a cuda project I encountered a problem with the template instantiation of std::cref.

The following example shows the usage of std::cref within the library and compiles warning free with gcc but fails with nvcc (for compilation the file extension has been changed to cu or cpp respectively).

#include <iostream>
#include <thread>

struct Foo {
    int bar = 73;
};

void f(Foo const & foo) {
    std::cout << foo.bar << std::endl;
}

int main(int, char * []) {
    Foo foo;
    std::thread other(f, std::cref(foo));
    other.join();
    return EXIT_SUCCESS;
}

If the argument is passed by value (std::thread other(f, foo);) it compiles fine with nvcc as well.

It appears to me that some something in the pre-processing of the cu file causes the instantiation of std::reference_wrapper, or more specifically the base std::_Reference_wrapper_base_impl, not only as an object but also as a function wrapper. And while the instantiation of the object wrapper succeeds the instantiation as a function wrapper fails because argument and return types are undefined. However, I could not find anything spurious within the generated (--keep) file (test.cpp1.ii).

Any ideas why the nvcc compilation fails?

Do you think this is a pre-processing failure that should be reported to the nvcc developers?

Appendix

Operating System: Ubuntu 16.04 LTS (xenial)

Compiler:

#nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2015 NVIDIA Corporation
Built on Tue_Aug_11_14:27:32_CDT_2015
Cuda compilation tools, release 7.5, V7.5.17

#gcc --version 
gcc (Ubuntu 5.3.1-14ubuntu2.1) 5.3.1 20160413
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Compilation output gcc (successful):

#g++ --std=c++11 test.cpp -pthread -Wall -Wextra --verbose
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.3.1-14ubuntu2.1' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2.1) 
COLLECT_GCC_OPTIONS='-std=c++11' '-pthread' '-Wall' '-Wextra' '-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/5/cc1plus -quiet -v -imultiarch x86_64-linux-gnu -D_GNU_SOURCE -D_REENTRANT test.cpp -quiet -dumpbase test.cpp -mtune=generic -march=x86-64 -auxbase test -Wall -Wextra -std=c++11 -version -fstack-protector-strong -Wformat-security -o /tmp/ccqwyOjk.s
GNU C++11 (Ubuntu 5.3.1-14ubuntu2.1) version 5.3.1 20160413 (x86_64-linux-gnu)
compiled by GNU C version 5.3.1 20160413, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "/usr/include/x86_64-linux-gnu/c++/5"
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/5
 /usr/include/x86_64-linux-gnu/c++/5
 /usr/include/c++/5/backward
 /usr/lib/gcc/x86_64-linux-gnu/5/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C++11 (Ubuntu 5.3.1-14ubuntu2.1) version 5.3.1 20160413 (x86_64-linux-gnu)
compiled by GNU C version 5.3.1 20160413, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 9bfec9bd675e3d566f69dbd512b33d18
COLLECT_GCC_OPTIONS='-std=c++11' '-pthread' '-Wall' '-Wextra' '-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 as -v --64 -o /tmp/cc9HVsQq.o /tmp/ccqwyOjk.s
GNU assembler version 2.26 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.26
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-std=c++11' '-pthread' '-Wall' '-Wextra' '-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/5/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/5/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper -plugin-opt=-fresolution=/tmp/ccESdprx.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lpthread -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/5/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/5 -L/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/5/../../.. /tmp/cc9HVsQq.o -lstdc++ -lm -lgcc_s -lgcc -lpthread -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/5/crtend.o /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crtn.o

Compilation output nvcc (unsuccessful):

#nvcc --std=c++11 test.cu --verbose
#$ _SPACE_= 
#$ _CUDART_=cudart
#$ _HERE_=/usr/lib/nvidia-cuda-toolkit/bin
#$ _THERE_=/usr/lib/nvidia-cuda-toolkit/bin
#$ _TARGET_SIZE_=
#$ _TARGET_DIR_=
#$ _TARGET_SIZE_=64
#$ NVVMIR_LIBRARY_DIR=/usr/lib/nvidia-cuda-toolkit/libdevice
#$ PATH=/usr/lib/nvidia-cuda-toolkit/bin:/gcc/home/nmoehrle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
#$ LIBRARIES=  -L/usr/lib/x86_64-linux-gnu/stubs
#$ gcc -std=c++11 -D__CUDA_ARCH__=200 -E -x c++        -DCUDA_DOUBLE_MATH_FUNCTIONS  -D__CUDACC__ -D__NVCC__  -D"__CUDACC_VER__=70517" -D"__CUDACC_VER_BUILD__=17" -D"__CUDACC_VER_MINOR__=5" -D"__CUDACC_VER_MAJOR__=7" -include "cuda_runtime.h" -m64 "test.cu" > "/tmp/tmpxft_00007d30_00000000-9_test.cpp1.ii" 
#$ cudafe --allow_managed --m64 --gnu_version=50301 --c++11 -tused --no_remove_unneeded_entities --gen_c_file_name "/tmp/tmpxft_00007d30_00000000-4_test.cudafe1.c" --stub_file_name "/tmp/tmpxft_00007d30_00000000-4_test.cudafe1.stub.c" --gen_device_file_name "/tmp/tmpxft_00007d30_00000000-4_test.cudafe1.gpu" --nv_arch "compute_20" --gen_module_id_file --module_id_file_name "/tmp/tmpxft_00007d30_00000000-3_test.module_id" --include_file_name "tmpxft_00007d30_00000000-2_test.fatbin.c" "/tmp/tmpxft_00007d30_00000000-9_test.cpp1.ii" 
/usr/include/c++/5/functional(78): error: class "Foo" has no member "result_type"
          detected during:
            instantiation of class "std::_Maybe_get_result_type<_Functor, void> [with _Functor=Foo]" 
(86): here
            instantiation of class "std::_Weak_result_type_impl<_Functor> [with _Functor=Foo]" 
(184): here
            instantiation of class "std::_Weak_result_type<_Functor> [with _Functor=Foo]" 
(264): here
            instantiation of class "std::_Reference_wrapper_base_impl<true, true, _Tp> [with _Tp=Foo]" 
(283): here
            instantiation of class "std::_Reference_wrapper_base<_Tp> [with _Tp=Foo]" 
(399): here
            instantiation of class "std::reference_wrapper<_Tp> [with _Tp=const Foo]" 
test.cu(14): here

/usr/include/c++/5/functional(266): error: class "Foo" has no member "argument_type"
          detected during:
            instantiation of class "std::_Reference_wrapper_base_impl<true, true, _Tp> [with _Tp=Foo]" 
(283): here
            instantiation of class "std::_Reference_wrapper_base<_Tp> [with _Tp=Foo]" 
(399): here
            instantiation of class "std::reference_wrapper<_Tp> [with _Tp=const Foo]" 
test.cu(14): here

/usr/include/c++/5/functional(267): error: class "Foo" has no member "first_argument_type"
          detected during:
            instantiation of class "std::_Reference_wrapper_base_impl<true, true, _Tp> [with _Tp=Foo]" 
(283): here
            instantiation of class "std::_Reference_wrapper_base<_Tp> [with _Tp=Foo]" 
(399): here
            instantiation of class "std::reference_wrapper<_Tp> [with _Tp=const Foo]" 
test.cu(14): here

/usr/include/c++/5/functional(268): error: class "Foo" has no member "second_argument_type"
          detected during:
            instantiation of class "std::_Reference_wrapper_base_impl<true, true, _Tp> [with _Tp=Foo]" 
(283): here
            instantiation of class "std::_Reference_wrapper_base<_Tp> [with _Tp=Foo]" 
(399): here
            instantiation of class "std::reference_wrapper<_Tp> [with _Tp=const Foo]" 
test.cu(14): here

4 errors detected in the compilation of "/tmp/tmpxft_00007d30_00000000-9_test.cpp1.ii".
# --error 0x2 --

Aucun commentaire:

Enregistrer un commentaire