sandbox/bugs/cuda_fp64_dvec_undef.c
CUDA
DOUBLE_PRECISION=1 leaves
dvec2/dvec3 undefined and downcasts
cos/sin/exp/pow
When compiling a 2D or 3D CUDA program with
DOUBLE_PRECISION=1, any GPU foreach() kernel
that declares a coord fails NVRTC compilation with
identifier "dvec3" is undefined (or dvec2 in
2D) and falls back to CPU. In addition, cos,
sin, exp, and pow are downcast to
float(x) even on CUDA where native FP64 transcendentals
exist.
Reproduce directly via:
make cuda_fp64_dvec_undef.tstObserved output on NVIDIA GeForce RTX 3070 (CUDA/NVRTC):
cuda_fp64_dvec_undef.c:41: CUDA: error: identifier "dvec3" is undefined
cuda_fp64_dvec_undef.c:41: CUDA: !coord v = {x, y, z};
cuda_fp64_dvec_undef.c:41: CUDA: !^
cuda_fp64_dvec_undef.c:40: warning: foreach() done on CPU (see CUDA errors above)
val = 0 ref = 1.117388885112192 diff = 1.117e+00
Assertion `fabs (val - ref) < 1e-12' failed.
#define DOUBLE_PRECISION 1
#include "grid/cuda/multigrid3D.h"
int main()
{
init_grid (4);
scalar a[]; foreach() {
coord v = {x, y, z};
a[] = v.x/L0 + cos (0.123456789012345);
}
double ref = 0.125 + cos (0.123456789012345);
double val = interpolate (a, 0.125, 0.125, 0.125);
fprintf (stderr, "val = %.16g ref = %.16g diff = %.3e\n",
val, ref, fabs (val - ref));
assert (fabs (val - ref) < 1e-12);
}Root cause
Lines 485–491 (
#if _CUDA) definestruct vec2andstruct vec3withfloatmembers, but never definestruct dvec2orstruct dvec3:"struct vec2 { float x, y; };\n" "struct vec3 { float x, y, z; };\n"Lines 520–528 (
#else // DOUBLE_PRECISION) then aliasGPU_COORD2_TYPE "dvec2",GPU_COORD3_TYPE "dvec3", and downcastcos(x),sin(x),exp(x),pow(x,y)tofloat(x)even when_CUDAis defined (that downcast is only needed for GLSL).
Proposed fix
In
/src/grid/gpu/grid.hunder#if _CUDA, also define:"struct dvec2 { double x, y; };\n" "struct dvec3 { double x, y, z; };\n"In
/src/grid/gpu/grid.hunder#else // DOUBLE_PRECISION, guard thefloat(x)transcendental downcasts with#if !_CUDA(leaving native FP64cos,sin,exp,powactive on CUDA).
