Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a CUDA equivalent to std::numeric_limits?

Tags:

c++

cuda

I would like to determine the maximum int value in a CUDA kernel. Unfortunately I can't find anything similar to std::numeric_limits for CUDA. Trying to use the ::std function results in a error:

error : calling a __host__ function("std::numeric_limits<int> ::max") from a __global__ function("xyz_kernel") is not allowed C:\cuda.cu(153) (col. 10)

Is there a way to determine the desired value from withing a kernel, or should I just pass it as a parameter?

like image 411
wondering Avatar asked Jun 30 '14 20:06

wondering


2 Answers

It exists but it is not as generic as std::numeric_limits. See this page for the list.

For example, you can have NPP_MAX_32U but this is specific to 32-bit unsigned rather than to the int type, whose width is system-dependent.

like image 178
merlin2011 Avatar answered Sep 22 '22 17:09

merlin2011


I've written something like that:

CUDA-compatible version of <limits> (named limits.cuh)

Essentially this means adding __host__ __device__ to a bunch of functions, and putting all of the structures within a namespace other than std.

Notes:

  1. Licensing. It's based on <limits> from libstdc++, so GPL with the standard library exception.
  2. This has undergone some, but not extensive, testing, with device-side code (only).
  3. There may be some macro clashes with the actual <limits> (I'm not sure).
like image 39
einpoklum Avatar answered Sep 21 '22 17:09

einpoklum