(fix:cuda) CUDA portability improved\nbuild system now checks for valid compiler compatible with nvcc and fails if that is not found and cuda is selected. Further, compiler choices are propegated throughout build along with CFLAGS and CXXFLAGS

This commit is contained in:
2026-09-07 10:51:34 -04:00
parent 323555b722
commit 7c99debf2f
6 changed files with 365 additions and 36 deletions

View File

@@ -16,6 +16,8 @@ import sys
import tarfile
from typing import Iterable, Mapping, Sequence
from check_cuda_toolchain import check_toolchain
CMAKE_FEATURES = {
"mpi": "MFEM_USE_MPI",
@@ -89,6 +91,7 @@ def parse_args() -> argparse.Namespace:
parser.add_argument("--host-system", required=True)
parser.add_argument("--cross-build", choices=("true", "false"), required=True)
parser.add_argument("--library-kind", choices=("static", "shared"), required=True)
parser.add_argument("--cuda-compiler", default="")
parser.add_argument("--cuda-arch", default="native")
parser.add_argument("--hip-arch", default="")
parser.add_argument("--precision", choices=("single", "double"), default="double")
@@ -356,6 +359,18 @@ def common_cmake_args(
"-DCMAKE_FIND_USE_PACKAGE_REGISTRY=OFF",
f"-DBUILD_SHARED_LIBS={'ON' if args.library_kind == 'shared' else 'OFF'}",
]
if args.cuda_compiler:
cuda_standard = 20 if any(feature in args.feature for feature in ("raja=ON", "umpire=ON")) else 17
result.extend([
f"-DCUDAToolkit_ROOT={Path(args.cuda_compiler).parent.parent}",
f"-DCMAKE_CUDA_COMPILER={args.cuda_compiler}",
f"-DCMAKE_CUDA_HOST_COMPILER={args.cxx}",
f"-DCUDA_HOST_COMPILER={args.cxx}",
f"-DCUDA_NVCC_EXECUTABLE={args.cuda_compiler}",
f"-DCMAKE_CUDA_STANDARD={cuda_standard}",
"-DCMAKE_CUDA_STANDARD_REQUIRED=ON",
"-DCMAKE_CXX_STANDARD=17",
])
if c_compiler:
result.append(f"-DCMAKE_C_COMPILER={c_compiler}")
if c_compiler == args.cc and args.cc_launcher:
@@ -502,6 +517,25 @@ def prepare_mpich_source(args: argparse.Namespace) -> Path:
return source
def mpich_cuda_architectures(architecture: str, nvcc: str) -> str:
"""Translate CMake architecture syntax without Yaksa's obsolete default list."""
if architecture == "native":
return "auto"
if architecture in {"all", "all-major"}:
supported = re.findall(r"sm_([0-9]+[a-z]?)", output([nvcc, "--list-gpu-code"]))
if architecture == "all-major":
supported = [sm for i, sm in enumerate(supported)
if i == 0 or sm.endswith("0")]
if not supported:
raise RuntimeError("nvcc did not report any supported GPU architectures")
return ",".join(dict.fromkeys(supported))
architectures = [re.sub(r"-(real|virtual)$", "", sm)
for sm in architecture.split(";")]
if not all(re.fullmatch(r"[0-9]+[a-z]?", sm) for sm in architectures):
raise RuntimeError(f"Invalid CUDA architecture list: {architecture}")
return ",".join(dict.fromkeys(architectures))
def build_mpich(
args: argparse.Namespace,
features: Mapping[str, bool],
@@ -560,7 +594,9 @@ def build_mpich(
nvcc = find_program_in_environment("nvcc", env)
if not nvcc:
raise RuntimeError("CUDA-enabled MPI requested but nvcc is unavailable")
configure.append(f"--with-cuda={Path(nvcc).resolve().parent.parent}")
configure.append("--with-cuda-sm=" + mpich_cuda_architectures(args.cuda_arch, args.cuda_compiler))
local_env["NVCC"] = shlex.join([args.cuda_compiler, "-ccbin", args.cxx, "-std=c++17"])
configure.append(f"--with-cuda={Path(args.cuda_compiler).resolve().parent.parent}")
elif features["hip"]:
hipcc = find_program_in_environment("hipcc", env)
if not hipcc:
@@ -638,6 +674,79 @@ def build_metis(
shutil.copy2(source / "LICENSE.txt", legal_dir / "metis-LICENSE.txt")
def prepare_hypre_source(args: argparse.Namespace, features: Mapping[str, bool]) -> Path:
if not features["cuda"]:
return args.hypre_source
source = args.work_dir / "hypre-source"
shutil.copytree(args.hypre_source, source, dirs_exist_ok=True, symlinks=True)
# CCCL 3 no longer supplies these public aliases through transitive includes.
for name in ("_hypre_utilities.hpp", "device_utils.h"):
header = source / "src" / "utilities" / name
text = header.read_text()
anchor = "#include <thrust/execution_policy.h>"
if anchor not in text:
raise RuntimeError(f"Hypre Thrust include anchor missing in {header}")
header.write_text(text.replace(anchor, anchor + "\n#include <thrust/tuple.h>\n#include <thrust/pair.h>\n#include <thrust/iterator/reverse_iterator.h>"))
# Recent CCCL releases removed these deprecated Thrust helper types.
# Keep the older typed predicate contract,
# including the typedefs required by older Thrust's not1 adaptor.
identity = """
#ifndef HYPRE_MESON_THRUST_IDENTITY
#define HYPRE_MESON_THRUST_IDENTITY
template <typename T, typename R> struct hypre_meson_unary_function
{
typedef T argument_type;
typedef R result_type;
};
template <typename T, typename U, typename R> struct hypre_meson_binary_function
{
typedef T first_argument_type;
typedef U second_argument_type;
typedef R result_type;
};
template <typename T> struct hypre_meson_identity
{
typedef T argument_type;
typedef T result_type;
__host__ __device__ const T& operator()(const T& value) const { return value; }
};
#endif
"""
for name in ("_hypre_utilities.hpp", "device_utils.h"):
header = source / "src" / "utilities" / name
text = header.read_text()
header.write_text(text.replace("using namespace thrust::placeholders;",
identity + "using namespace thrust::placeholders;"))
for file in (source / "src").rglob("*"):
if file.suffix not in {".c", ".h", ".hpp"}:
continue
text = file.read_text()
patched = text.replace("thrust::identity<", "hypre_meson_identity<")
patched = patched.replace("thrust::unary_function<", "hypre_meson_unary_function<")
patched = patched.replace("thrust::binary_function<", "hypre_meson_binary_function<")
if file.suffix == ".c":
patched = patched.replace("thrust::not1(", "HYPRE_THRUST_NOT(")
if patched != text:
file.write_text(patched)
memory = source / "src" / "utilities" / "memory.c"
text = memory.read_text()
for device, location_type in (("hypre_HandleDevice(hypre_handle())", "cudaMemLocationTypeDevice"),
("cudaCpuDeviceId", "cudaMemLocationTypeHost")):
call = (f"HYPRE_CUDA_CALL( cudaMemPrefetchAsync(ptr, size, {device},\n"
" hypre_HandleComputeStream(hypre_handle())) );")
if call not in text:
raise RuntimeError("Hypre CUDA prefetch patch anchor missing")
device_id = device if location_type == "cudaMemLocationTypeDevice" else "0"
replacement = (f"#if CUDART_VERSION >= 13000\n"
f" cudaMemLocation destination = {{{location_type}, {device_id}}};\n"
" HYPRE_CUDA_CALL( cudaMemPrefetchAsync(ptr, size, destination, 0,\n"
" hypre_HandleComputeStream(hypre_handle())) );\n"
"#else\n " + call + "\n#endif")
text = text.replace(call, replacement)
memory.write_text(text)
return source
def build_hypre(
args: argparse.Namespace,
features: Mapping[str, bool],
@@ -651,6 +760,7 @@ def build_hypre(
args,
build_type,
c_compiler=args.cc,
cxx_compiler=args.cxx,
) + [
f"-DHYPRE_ENABLE_MPI={'ON' if features['mpi'] else 'OFF'}",
f"-DHYPRE_ENABLE_OPENMP={'ON' if features['openmp'] else 'OFF'}",
@@ -678,7 +788,7 @@ def build_hypre(
options.append(f"-DCMAKE_HIP_ARCHITECTURES={args.hip_arch}")
cmake_build_install(
args,
args.hypre_source / "src",
prepare_hypre_source(args, features) / "src",
args.work_dir / "hypre",
options,
env,
@@ -729,8 +839,8 @@ def build_sundials(
options = common_cmake_args(
args,
build_type,
c_compiler=mpicc if features["mpi"] else args.cc,
cxx_compiler=mpicxx if features["mpi"] else args.cxx,
c_compiler=args.cc,
cxx_compiler=args.cxx,
) + [
f"-DSUNDIALS_ENABLE_MPI={'ON' if features['mpi'] else 'OFF'}",
f"-DSUNDIALS_ENABLE_OPENMP={'ON' if features['openmp'] else 'OFF'}",
@@ -882,6 +992,15 @@ def build_libceed(
f"EMSCRIPTEN={'1' if args.host_system == 'emscripten' else ''}",
]
)
if features["cuda"]:
nvcc_flags = ['-ccbin', args.cxx, '-std=c++17',
'-Xcompiler', optimization, '-Xcompiler', '-fPIC']
if ceed_cuda_arch:
nvcc_flags.append('-arch=' + ceed_cuda_arch)
command.extend([
f"NVCC={args.cuda_compiler}",
f"NVCCFLAGS={shlex.join(nvcc_flags)}",
])
if args.host_system not in {"darwin", "emscripten"}:
command.append("LDLIBS=-ldl")
run(command, env=local_env)
@@ -2204,6 +2323,13 @@ def main() -> int:
"dependency_prefix and the private output prefix must be disjoint"
)
features = parse_features(args.feature)
if features["cuda"]:
args.cuda_compiler = str(Path(args.cuda_compiler or shutil.which("nvcc") or "nvcc").resolve())
args.cc = shutil.which(args.cc) or args.cc
args.cxx = shutil.which(args.cxx) or args.cxx
print(check_toolchain(args.cuda_compiler, args.cc, args.cxx,
standard="20" if features["raja"] or features["umpire"] else "17",
c_args=args.c_arg, cxx_args=args.cxx_arg), flush=True)
build_type = cmake_build_type(args.buildtype)
compiler_identity = {
"cc": output([*args.cc_launcher, args.cc, "--version"]).splitlines()[0],
@@ -2218,6 +2344,10 @@ def main() -> int:
(args.source / "CMakeLists.txt").read_bytes()
).hexdigest(),
}
if features["cuda"]:
fingerprint_data["cuda_version"] = output([args.cuda_compiler, "--version"])
fingerprint_data["cuda_check_sha256"] = hashlib.sha256(
Path(__file__).with_name("check_cuda_toolchain.py").read_bytes()).hexdigest()
if features["ceed"]:
ceed_jit_source = Path(__file__).with_name(
"ceed_jit_source_root_relocatable.c"
@@ -2249,9 +2379,17 @@ def main() -> int:
args.prefix.mkdir(parents=True, exist_ok=True)
env = dict(os.environ)
if features["cuda"]:
env["CUDA_HOME"] = str(Path(args.cuda_compiler).parent.parent)
env["CUDA_PATH"] = env["CUDA_HOME"]
env["CUDACXX"] = args.cuda_compiler
env["CUDAHOSTCXX"] = args.cxx
env["NVCC_CCBIN"] = args.cxx
path_entries = [os.fspath(args.prefix / "bin")]
if args.dependency_prefix:
path_entries.append(os.fspath(Path(args.dependency_prefix) / "bin"))
if features["cuda"]:
path_entries.append(str(Path(args.cuda_compiler).parent))
path_entries.append(env.get("PATH", ""))
env["PATH"] = os.pathsep.join(path_entries)
env["CMAKE_PREFIX_PATH"] = os.pathsep.join(

View File

@@ -0,0 +1,72 @@
#!/usr/bin/env python3
"""Probe the installed toolkit instead of maintaining a CUDA/compiler version table."""
from __future__ import annotations
import argparse
import os
from pathlib import Path
import shlex
import subprocess
import tempfile
def check_toolchain(nvcc: str, cc: str, cxx: str, *, standard: str = '17',
c_args: list[str] | None = None,
cxx_args: list[str] | None = None) -> str:
version = subprocess.run([nvcc, '--version'], text=True, capture_output=True, check=True).stdout.strip()
# Do not permit ambient flags to bypass the installed toolkit's version checks.
env = dict(os.environ)
for key in ('NVCC_PREPEND_FLAGS', 'NVCC_APPEND_FLAGS', 'NVCC_CCBIN'):
env.pop(key, None)
with tempfile.TemporaryDirectory(prefix='mfem-cuda-check-') as directory:
root = Path(directory)
source = root / 'probe.cu'
source.write_text('''#include <cuda_runtime.h>
#include <thrust/device_vector.h>
#include <cmath>
__global__ void probe(int *p) { *p = 1; }
int main() { int *p = nullptr; cudaMalloc(&p, sizeof(int));
probe<<<1, 1>>>(p); return (int)cudaFree(p); }
''')
for label, host, flags in [('C', cc, c_args or []), ('C++', cxx, cxx_args or [])]:
command = [nvcc, '--compiler-bindir', host, '--std=c++' + standard,
str(source), '-o', str(root / ('probe.exe' if os.name == 'nt' else 'probe'))]
if label == 'C':
command.append('-c')
for flag in flags:
command += ['-Xcompiler', flag]
result = subprocess.run(command, env=env, text=True, capture_output=True)
if result.returncode:
raise RuntimeError(
f'CUDA compatibility check failed for the selected {label} compiler: {host}\n'
f'{version}\nCommand: {shlex.join(command)}\n'
f'{result.stdout}{result.stderr}\n'
'Select a C/C++ toolchain supported by this CUDA toolkit using CC and CXX '
'(for example CC=gcc-15 CXX=g++-15), or install a compatible toolkit. '
'Use a fresh Meson build directory when changing compilers; --reconfigure '
'does not change cached compilers. For a CPU build use -Dmfem_cuda=disabled. '
'The probe compiles and links but does not require a running GPU.'
)
return f'CUDA compatibility checks passed: C={cc}, C++={cxx}, CUDA C++{standard}\n{version}'
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--nvcc', required=True)
parser.add_argument('--cc', required=True)
parser.add_argument('--cxx', required=True)
parser.add_argument('--standard', default='17')
parser.add_argument('--c-arg', action='append', default=[])
parser.add_argument('--cxx-arg', action='append', default=[])
args = parser.parse_args()
try:
print(check_toolchain(args.nvcc, args.cc, args.cxx, standard=args.standard,
c_args=args.c_arg, cxx_args=args.cxx_arg))
except (RuntimeError, OSError, subprocess.SubprocessError) as error:
print(error)
return 1
return 0
if __name__ == '__main__':
raise SystemExit(main())