我尝试使用nvcc
来构建以下多线程程序,该程序由" gcc -pthread a.c
"之前:
$ cat a.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *myThreadFun(void *vargp)
{
printf("myThreadFun \n");
return NULL;
}
int main()
{
pthread_t tid;
printf("Before Thread\n");
pthread_create(&tid, NULL, myThreadFun, NULL);
pthread_join(tid, NULL);
printf("After Thread\n");
exit(0);
}
执行&#34; nvcc -pthread a.c
&#34;:
$ nvcc -pthread a.c
nvcc fatal : Unknown option 'pthread'
此topic表示nvcc
支持在不使用-pthread
选项的情况下构建多线程程序。我的测试似乎也是正确的:
$ nvcc a.c
nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
$ ldd a.out
linux-vdso.so.1 (0x00007ffcff79e000)
librt.so.1 => /usr/lib/librt.so.1 (0x00007fd4f5a43000)
libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007fd4f5825000)
libdl.so.2 => /usr/lib/libdl.so.2 (0x00007fd4f5621000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007fd4f5299000)
libm.so.6 => /usr/lib/libm.so.6 (0x00007fd4f4f86000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007fd4f4d6f000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007fd4f49cb000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd4f5c4b000)
但我无法从nvcc
官方document找到证据。任何人都可以帮忙确认一下吗?
答案 0 :(得分:2)
没有nvcc不支持pthread选项。事实上,它对pthreads一无所知。 pthread依赖性来自CUDA运行时库中的依赖项。它与代码中的内容无关。 nvcc甚至无法编译该代码,它会传递给您的主机编译器。 nvcc是一个编译器驱动程序。它只是使用其他编译器来控制编译。在这种情况下,主机C ++编译器。
你可以看到实际发生的事情:
$ nvcc -arch=sm_52 -v pthread_confusion.c
#$ _SPACE_=
#$ _CUDART_=cudart
#$ _HERE_=/opt/cuda-8.0/bin
#$ _THERE_=/opt/cuda-8.0/bin
#$ _TARGET_SIZE_=
#$ _TARGET_DIR_=
#$ _TARGET_SIZE_=64
#$ TOP=/opt/cuda-8.0/bin/..
#$ NVVMIR_LIBRARY_DIR=/opt/cuda-8.0/bin/../nvvm/libdevice
#$ LD_LIBRARY_PATH=/opt/cuda-8.0/bin/../lib:/opt/cuda-8.0/lib64:/usr/lib/nx/X11/Xinerama:/usr/lib/nx/X11
#$ PATH=/opt/cuda-8.0/bin/../open64/bin:/opt/cuda-8.0/bin/../nvvm/bin:/opt/cuda-8.0/bin:/usr/local/bin:/usr/bin:/bin:/opt/cuda-8.0/bin
#$ INCLUDES="-I/opt/cuda-8.0/bin/..//include"
#$ LIBRARIES= "-L/opt/cuda-8.0/bin/..//lib64/stubs" "-L/opt/cuda-8.0/bin/..//lib64"
#$ CUDAFE_FLAGS=
#$ PTXAS_FLAGS=
#$ gcc -c -x c -D__NVCC__ "-I/opt/cuda-8.0/bin/..//include" -D"__CUDACC_VER__=80044" -D"__CUDACC_VER_BUILD__=44" -D"__CUDACC_VER_MINOR__=0" -D"__CUDACC_VER_MAJOR__=8" -m64 -o "/tmp/tmpxft_000069ca_00000000-4_pthread_confusion.o" "pthread_confusion.c"
#$ nvlink --arch=sm_52 --register-link-binaries="/tmp/tmpxft_000069ca_00000000-2_a_dlink.reg.c" -m64 "-L/opt/cuda-8.0/bin/..//lib64/stubs" "-L/opt/cuda-8.0/bin/..//lib64" -cpu-arch=X86_64 "/tmp/tmpxft_000069ca_00000000-4_pthread_confusion.o" -lcudadevrt -o "/tmp/tmpxft_000069ca_00000000-5_a_dlink.sm_52.cubin"
#$ fatbinary --create="/tmp/tmpxft_000069ca_00000000-3_a_dlink.fatbin" -64 -link "--image=profile=sm_52,file=/tmp/tmpxft_000069ca_00000000-5_a_dlink.sm_52.cubin" --embedded-fatbin="/tmp/tmpxft_000069ca_00000000-3_a_dlink.fatbin.c"
#$ rm /tmp/tmpxft_000069ca_00000000-3_a_dlink.fatbin
#$ gcc -c -x c++ -DFATBINFILE="\"/tmp/tmpxft_000069ca_00000000-3_a_dlink.fatbin.c\"" -DREGISTERLINKBINARYFILE="\"/tmp/tmpxft_000069ca_00000000-2_a_dlink.reg.c\"" -I. "-I/opt/cuda-8.0/bin/..//include" -D"__CUDACC_VER__=80044" -D"__CUDACC_VER_BUILD__=44" -D"__CUDACC_VER_MINOR__=0" -D"__CUDACC_VER_MAJOR__=8" -m64 -o "/tmp/tmpxft_000069ca_00000000-6_a_dlink.o" "/opt/cuda-8.0/bin/crt/link.stub"
#$ g++ -m64 -o "a.out" -Wl,--start-group "/tmp/tmpxft_000069ca_00000000-6_a_dlink.o" "/tmp/tmpxft_000069ca_00000000-4_pthread_confusion.o" "-L/opt/cuda-8.0/bin/..//lib64/stubs" "-L/opt/cuda-8.0/bin/..//lib64" -lcudadevrt -lcudart_static -lrt -lpthread -ldl -Wl,--end-group
此处,样板链接阶段包括CUDA运行时库和pthreads库。
如果您希望明确主机编译器,请传递-Xcompiler="-pthread"