Precision loss conversion warnings #242

kuninl opened this Issue Oct 12, 2012 · 4 comments


None yet

3 participants


When compiling my Thrust 1.6 based code using nvcc/gcc 4.4.6 with -Wconversion flag in order to find possible precision loss due to long to int, etc. conversions, I get numerous warning in the thrust code related to this. Could you consider eliminating those in the future version of Thrust?
For example:
/usr/local/cuda/include/thrust/iterator/iterator_adaptor.h:119: warning: conversion to ‘int’ from ‘long int’ may alter its value
/usr/local/cuda/include/thrust/system/cuda/detail/reduce.inl:244: warning: conversion to ‘unsigned int’ from ‘size_t’ may alter its value

The sample program (Thrust doc example) below illustrates the issues with reduce but those seem to exist in other algorithms.

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/generate.h>
#include <thrust/reduce.h>
#include <thrust/functional.h>
#include <algorithm>
#include <cstdlib>

int main(void)
// generate random data serially
thrust::host_vector<int> h_vec(100);
std::generate(h_vec.begin(), h_vec.end(), rand);

// transfer to device and compute sum
thrust::device_vector<int> d_vec = h_vec;
int x = thrust::reduce(d_vec.begin(), d_vec.end(), 0, thrust::plus<int>());

std::cout << "x = " << x << std::endl;
return 0;

Compile as:
nvcc -O3 -o test_warning -Xcompiler -Wconversion -I /usr/lib/gcc/x86_64-redhat-linux6E/4.4.6/include -I/usr/local/cuda/include -I/usr/local/ -L/usr/local/cuda/lib64 -lcuda -L/usr/lib/gcc/x86_64-redhat-linux6E/4.4.6 -lstdc++ -lm

A Parallel Algorithms Library member

These look related to converting long integers related to iterator_difference to small integers related to things like num_blocks or block_size. Marking Release1.7.

A Parallel Algorithms Library member

Many of these warnings occur due to the fact that we compute grid & block sizes in terms of size_t. We should avoid this, as launches are specified with dim3, which is internally represented by unsigned int.

To fix this comprehensively, we should define the typedefs:

typedef unsigned int grid_size_t;
typedef unsigned int block_size_t;
typedef size_t smem_size_t; // yes, cudaDeviceProp.sharedMemPerBlock and cudaFuncAttributes.sharedSizeBytes are size_t

And do arithmetic using these types instead of size_t or unsigned int.


I would like to add that there are other warnings related to conversion loss. I just included the thrust\device_ptr.h and I got a whole bunch of these

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include\thrust/system/cuda/detail/cuda_launch_config.h(338) : see reference to function template instantiation 'size_t thrust::system::cuda::detail::block_size_with_maximum_potential_occupancy<thrust::system::cuda::detail::cuda_launch_config_detail::util::zero_function<T>>(const thrust::system::cuda::detail::function_attributes_t &,const thrust::system::cuda::detail::device_properties_t &,UnaryFunction)' being compiled
1> with
1> [
1> T=size_t,
1> UnaryFunction=thrust::system::cuda::detail::cuda_launch_config_detail::util::zero_function<size_t>
1> ]
1>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include\thrust/system/cuda/detail/cuda_launch_config.h(147): warning C4267: 'return' : conversion from 'size_t' to 'int', possible loss of data
1> C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include\thrust/system/cuda/detail/cuda_launch_config.h(159) : see reference to function template instantiation 'L thrust::system::cuda::detail::cuda_launch_config_detail::util::divide_ri<L,R>(const L,const R)' being compiled
1> with
1> [
1> L=int,
1> R=size_t
1> ]
1> C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include\thrust/system/cuda/detail/cuda_launch_config.h(272) : see reference to function template instantiation 'L thrust::system::cuda::detail::cuda_launch_config_detail::util::round_i<L,size_t>(const L,const R)' being compiled
1> with
1> [
1> L=int,
1> R=size_t
1> ]

CUDA 5.5 + VS 2012

Could this be verified and corrected those messages are annoying. Best regards !

A Parallel Algorithms Library member

These should be fixed in master now. Closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment