Skip to content

Commit

Permalink
Move local static for device count to a host-only function
Browse files Browse the repository at this point in the history
A future release of NVC++ will report an error for local static variables
that are defined in device functions and host/device functions, even if
the variable is defined within a host-only region of the function.  To
avoid errors, move the local static variable that stores the device count
from cub::DeviceCount() to a separate host-only function.

Fix NVIDIA#1217
  • Loading branch information
dkolsen-pgi authored and alliepiper committed Jul 3, 2020
1 parent 33c4743 commit c37a493
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions cub/util_device.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ struct ValueCache

#endif

#if CUB_CPP_DIALECT >= 2011
// Host code, only safely usable in C++11 or newer, where thread-safe
// initialization of static locals is guaranteed. This is a separate function
// to avoid defining a local static in a host/device function.
__host__ __forceinline__ int DeviceCountCachedValue()
{
static ValueCache<int, DeviceCountUncached> cache;
return cache.value;
}
#endif

/**
* \brief Returns the number of CUDA devices available.
*
Expand All @@ -217,10 +228,7 @@ CUB_RUNTIME_FUNCTION __forceinline__ int DeviceCount()
#if CUB_INCLUDE_HOST_CODE
#if CUB_CPP_DIALECT >= 2011
// Host code and C++11.
// C++11 guarantees that initialization of static locals is thread safe.
static ValueCache<int, DeviceCountUncached> cache;

result = cache.value;
result = DeviceCountCachedValue();
#else
// Host code and C++98.
result = DeviceCountUncached();
Expand Down

0 comments on commit c37a493

Please sign in to comment.