__global__ void foo(int *const a, const int N){
const auto tid = threadIdx.x + blockDim.x * blockIdx.x;
// Implicit downcast/truncation from int64 to int32 - how can we get an error?
a[tid] = tid * 3L;
}
如何获得
a[tid] = tid * 3L
?
的错误 在GCC中会标记类似的情况:
-Wconverion
在clang中<source>:27:9: warning: conversion from ‘int64_t’ {aka ‘long int’} to ‘int’ may change value [-Wconversion]
27 | int y = x;
| ^
将升起:
-Wshorten64-to-32
最小工作示例
<source>:6:11: warning: implicit conversion loses integer precision: 'int64_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
6 | int y=x;
| ~ ^
一种可能的方法:使内核代码的主体成为功能,从内核中调用该函数,然后使用#include <cstdint>
#include <iostream>
#include <limits>
#include <vector>
__global__ void foo(int *const a, const int N){
// Wrong data type: it would be nice to detect this
const int tid = threadIdx.x + blockDim.x * blockIdx.x;
// Implicit downcast/truncation from int64 to int32 - how can we get an error?
a[tid] = tid * 3L;
}
int main(){
constexpr int N = 10000;
constexpr int threads = 128;
int *a_d;
cudaMalloc(&a_d, N * sizeof(int));
foo<<<(N + threads - 1) / 128, threads>>>(a_d, N);
std::vector<int> v(N);
cudaMemcpy(v.data(), a_d, N * sizeof(int), cudaMemcpyDeviceToHost);
for(int i=0;i<10;i++){
std::cout<<v.at(i)<<std::endl;
}
return 0;
}
: