sandbox/bugs/cuda_fp64_abi_and_reset.c
libbuda.a
and libgpu.a hardcode FP32 (SINGLE_PRECISION),
breaking DOUBLE_PRECISION=1
When compiling a CUDA or OpenGL GPU program with
DOUBLE_PRECISION=1, the application compiles with
sizeof(real) == 8, while the prebuilt runtime libraries
libbuda.a (grid/cuda/cuda.c) and
libgpu.a (grid/gpu/opengl.c) are always
compiled with -DSINGLE_PRECISION and hardcoded
typedef float real; (sizeof(real) == 4).
This produces a silent host/device ABI mismatch (sum = 0
instead of 384 on both CUDA and OpenGL). In addition,
cuda.c leaves reset_scalar() unimplemented for
DOUBLE_PRECISION and hardcodes float inside
cuda_reduce().
Reproduce directly via:
make cuda_fp64_abi_and_reset.tstObserved output on NVIDIA GeForce RTX 3070 (CUDA):
cuda_fp64_abi_and_reset.c:36: warning: CUDA: found FP64 assembly in single precision mode
sum = 0 (expected 384, diff = 3.840e+02)
cuda_fp64_abi_and_reset.c:41: Assertion `fabs (sum - expected) < 1e-12' failed.
#define DOUBLE_PRECISION 1
#include "grid/cuda/multigrid.h"
int main()
{
init_grid (16);
scalar a[];
reset ({a}, 1.5);
double sum = 0.; foreach (reduction(+:sum))
sum += a[];
double expected = 1.5*sq(N);
fprintf (stderr, "sum = %.16g (expected %.16g, diff = %.3e)\n",
sum, expected, fabs (sum - expected));
assert (fabs (sum - expected) < 1e-12);
}Root cause
- /src/grid/cuda/Makefile (line
3) and /src/grid/gpu/Makefile (line
3) hardcode
-DSINGLE_PRECISION. - /src/grid/cuda/cuda.c (line 42)
and /src/grid/gpu/opengl.c (line
20) hardcode
typedef float real;(andcuda.comits#define HUGE 1e30f/1e30, which also fails to compile on newer glibc). - /src/grid/cuda/cuda.c (lines
345–347) leaves
reset_scalar()unimplemented underDOUBLE_PRECISION("error: not implemented yet"), andcuda_reduce()(lines 596–739) hardcodesfloatbuffers, kernel types, and return type.
Proposed fix
- Guard
typedef float real;andHUGEwith#if SINGLE_PRECISION ... #else typedef double real; #define HUGE 1e30 #endifin bothcuda.candopengl.c. - Implement the
DOUBLE_PRECISIONbranch ofreset_scalar()and switchcuda_reduce()fromfloattorealincuda.c. - Build companion FP64 libraries (
libbuda64.a,libgpu64.a) without-DSINGLE_PRECISIONand select them via#if DOUBLE_PRECISIONin#pragma autolinkingrid/{cuda,gpu}/multigrid{,3D}.h.
