Skip to content

Instantly share code, notes, and snippets.

@danchev
Last active September 22, 2025 15:40
Show Gist options
  • Save danchev/3d81fe10b7f15fcd7ce4f95f03367cdb to your computer and use it in GitHub Desktop.
Save danchev/3d81fe10b7f15fcd7ce4f95f03367cdb to your computer and use it in GitHub Desktop.
Scripts to patch CUDA math_functions.h for glibc 2.42 compatibility
--- a/math_functions.h 00:02:30.815134398 +0300
+++ b/math_functions.h 00:03:30.815134398 +0300
@@ -2547,7 +2547,7 @@
*
* \note_accuracy_double
*/
-extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double sinpi(double x);
+extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double sinpi(double x) noexcept (true);
/**
* \ingroup CUDA_MATH_SINGLE
* \brief Calculate the sine of the input argument
@@ -2570,7 +2570,7 @@
*
* \note_accuracy_single
*/
-extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float sinpif(float x);
+extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float sinpif(float x) noexcept (true);
/**
* \ingroup CUDA_MATH_DOUBLE
* \brief Calculate the cosine of the input argument
@@ -2592,7 +2592,7 @@
*
* \note_accuracy_double
*/
-extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double cospi(double x);
+extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double cospi(double x) noexcept (true);
/**
* \ingroup CUDA_MATH_SINGLE
* \brief Calculate the cosine of the input argument
@@ -2614,7 +2614,7 @@
*
* \note_accuracy_single
*/
-extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float cospif(float x);
+extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float cospif(float x) noexcept (true);
/**
* \ingroup CUDA_MATH_DOUBLE
* \brief Calculate the sine and cosine of the first input argument
--- a/math_functions.h 2025-09-22 10:11:36.438660639 -0500
+++ b/math_functions.h 2025-09-22 10:25:37.731368676 -0500
@@ -626,7 +626,7 @@
*
* \note_accuracy_double
*/
-extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double rsqrt(double x);
+extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double rsqrt(double x) noexcept(true);
/**
* \ingroup CUDA_MATH_SINGLE
@@ -650,7 +650,7 @@
*
* \note_accuracy_single
*/
-extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float rsqrtf(float x);
+extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float rsqrtf(float x) noexcept(true);
#if defined(__QNX__) && !defined(_LIBCPP_VERSION)
#!/bin/bash
# Script to patch CUDA math_functions.h for glibc 2.41 compatibility
# Usage: sudo bash patch_cuda_math_functions.sh
PATCH_TARGET="/usr/local/cuda/include/crt/math_functions.h"
cat <<'EOF' > /tmp/cuda_glibc_241_compat.diff
--- a/math_functions.h 00:02:30.815134398 +0300
+++ b/math_functions.h 00:03:30.815134398 +0300
@@ -2547,7 +2547,7 @@
*
* \note_accuracy_double
*/
-extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double sinpi(double x);
+extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double sinpi(double x) noexcept (true);
/**
* \ingroup CUDA_MATH_SINGLE
* \brief Calculate the sine of the input argument
@@ -2570,7 +2570,7 @@
*
* \note_accuracy_single
*/
-extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float sinpif(float x);
+extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float sinpif(float x) noexcept (true);
/**
* \ingroup CUDA_MATH_DOUBLE
* \brief Calculate the cosine of the input argument
@@ -2592,7 +2592,7 @@
*
* \note_accuracy_double
*/
-extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double cospi(double x);
+extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double cospi(double x) noexcept (true);
/**
* \ingroup CUDA_MATH_SINGLE
* \brief Calculate the cosine of the input argument
@@ -2614,7 +2614,7 @@
*
* \note_accuracy_single
*/
-extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float cospif(float x);
+extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float cospif(float x) noexcept (true);
/**
* \ingroup CUDA_MATH_DOUBLE
* \brief Calculate the sine and cosine of the first input argument
EOF
patch $PATCH_TARGET /tmp/cuda_glibc_241_compat.diff
#!/bin/bash
# Define the CUDA installation path
# Adjust this path if your CUDA toolkit is installed elsewhere.
CUDA_PATH="/usr/local/cuda"
HEADER_FILE="$CUDA_PATH/targets/x86_64-linux/include/crt/math_functions.h"
# Check if the header file exists
if [ ! -f "$HEADER_FILE" ]; then
echo "Error: CUDA header file not found at $HEADER_FILE"
echo "Please update the CUDA_PATH variable in the script."
exit 1
fi
echo "Patching $HEADER_FILE for glibc 2.42 compatibility..."
# Use sed to add the "noexcept(true)" specification to the relevant functions.
# The `I` flag makes the change in-place.
# The `-e` flag allows running multiple commands.
sed -i -e 's/extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float sqrtf(float x);/extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float sqrtf(float x) noexcept(true);/g' "$HEADER_FILE"
sed -i -e 's/extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double rsqrt(double x);/extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double rsqrt(double x) noexcept(true);/g' "$HEADER_FILE"
echo "Patching complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment