看来 GCC 和 Clang 对有符号整数和无符号整数之间的加法的解释不同,具体取决于它们的大小。为什么会这样?所有编译器和平台上的转换是否一致?
举个例子:
#include <cstdint>
#include <iostream>
int main()
{
    std::cout <<"16 bit uint 2 - int 3 = "<<uint16_t(2)+int16_t(-3)<<std::endl;
    std::cout <<"32 bit uint 2 - int 3 = "<<uint32_t(2)+int32_t(-3)<<std::endl;
    return 0;
}
结果:
$ ./out.exe   
16 bit uint 2 - int 3 = -1
32 bit uint 2 - int 3 = 4294967295
在这两种情况下,我们都得到 -1,但其中一个被解释为无符号整数并下溢。我本以为两者都会以相同的方式进行转换。
那么,为什么编译器对它们的转换如此不同,这能保证一致吗?我用 g++ 11.1.0、clang 12.0 对此进行了测试。以及 Arch Linux 和 Debian 上的 g++ 11.2.0,得到相同的结果。
我最近通过系统的包管理器在我的 arch-Linux 机器上安装了 Cuda,我一直试图通过运行一个简单的向量加法程序来测试它是否工作。
我只是将本教程中的代码(使用一个或多个内核的代码)复制粘贴到一个名为cuda_test.cu并运行的文件中
> nvcc cuda_test.cu -o cuda_test
在任何一种情况下,程序都可以运行,并且我没有收到任何错误(因为程序没有崩溃并且输出是没有错误)。但是当我尝试在程序上运行 Cuda 分析器时:
> sudo nvprof ./cuda_test
我得到结果:
==3201== NVPROF is profiling process 3201, command: ./cuda_test
Max error: 0
==3201== Profiling application: ./cuda_test
==3201== Profiling result:
No kernels were profiled.
No API activities were profiled.
==3201== Warning: Some profiling data are not recorded. Make sure cudaProfilerStop() or cuProfilerStop() is called before application exit to flush profile data.
后一个警告不是我的主要问题或我的问题的主题,我的问题是消息说没有分析内核并且没有分析 API 活动。
这是否意味着该程序完全在我的 CPU 上运行?还是 nvprof 中的错误?
我已经找到了大约相同的错误的讨论 …