diff --git a/common/CUDAWrapper.cpp b/common/CUDAWrapper.cpp index 5d3f45ecc..88aeb1800 100644 --- a/common/CUDAWrapper.cpp +++ b/common/CUDAWrapper.cpp @@ -80,6 +80,8 @@ int32_t get_gpu_count() { return 0; } +void hide_gpus() {} + std::vector get_gpu_names() { return {}; } diff --git a/common/CUDAWrapper.cu b/common/CUDAWrapper.cu index f12c984a6..4df5a9daa 100644 --- a/common/CUDAWrapper.cu +++ b/common/CUDAWrapper.cu @@ -18,7 +18,17 @@ inline void cuda_err(cudaError_t val) { throw JFJochException(JFJochExceptionCategory::GPUCUDAError, cudaGetErrorString(val)); } +namespace { + std::atomic gpus_hidden{false}; +} + +void hide_gpus() { + gpus_hidden = true; +} + int32_t get_gpu_count() { + if (gpus_hidden) + return 0; int device_count; cudaError_t val = cudaGetDeviceCount(&device_count); switch (val) { @@ -137,11 +147,13 @@ void set_gpu_blocking_sync() { // cudaSetDevice creates the device's context, serially, and on four cards that was most of a // second before the run had started. The function is taken from the driver at run time, so the // program does not link against it and still starts on a machine without one. + // Without a device nothing is called at all: even cudaGetLastError() would load the driver. + if (get_gpu_count() == 0) + return; void *fn = nullptr; cudaDriverEntryPointQueryResult found{}; - if (get_gpu_count() == 0 - || cudaGetDriverEntryPointByVersion("cuDevicePrimaryCtxSetFlags", &fn, 12000, cudaEnableDefault, - &found) != cudaSuccess + if (cudaGetDriverEntryPointByVersion("cuDevicePrimaryCtxSetFlags", &fn, 12000, cudaEnableDefault, &found) + != cudaSuccess || found != cudaDriverEntryPointSuccess) { cudaGetLastError(); return; @@ -175,10 +187,13 @@ void release_gpu_contexts() { } void cuda_clear_error() { - cudaGetLastError(); + if (!gpus_hidden) // hidden, nothing has used CUDA, and this call would still load the driver + cudaGetLastError(); } void cuda_throw_if_context_lost() { + if (gpus_hidden) + return; // cudaFree(nullptr) frees nothing, but a lost context fails it. The last error cannot say the same: // once cudaGetLastError() has returned a sticky error, it and cudaPeekAtLastError() report success. const cudaError_t err = cudaFree(nullptr); diff --git a/common/CUDAWrapper.h b/common/CUDAWrapper.h index a61937f5a..0b895f1fb 100644 --- a/common/CUDAWrapper.h +++ b/common/CUDAWrapper.h @@ -11,6 +11,12 @@ int32_t get_gpu_count(); +// From here on get_gpu_count() is 0 without asking CUDA, so nothing in the process creates a context or +// touches a device and every GPU path takes its CPU path, as on a machine without a GPU: what +// CUDA_VISIBLE_DEVICES= does, asked for by the program (rugnux --no-gpu). Call it before anything uses +// CUDA; there is no way back. +void hide_gpus(); + // Names of the visible GPUs, in device order and one entry per device, so repeated cards repeat. // Empty without CUDA and on a machine with no device, which is also what get_gpu_count() == 0 says. std::vector get_gpu_names(); diff --git a/docs/RUGNUX_ADVANCED.md b/docs/RUGNUX_ADVANCED.md index 4bcf8edd9..5510b61c8 100644 --- a/docs/RUGNUX_ADVANCED.md +++ b/docs/RUGNUX_ADVANCED.md @@ -330,6 +330,7 @@ General: | `-e, --end-image ` | Last image to process (default: all) | | `-t, --stride ` | Process every *n*-th image (default: 1) | | `-v, --verbose` | Show the whole log on the terminal in place of the steps; `_log.txt` is written either way | +| `--no-gpu` | Run on the CPU only, as on a machine without a GPU, even where one is present: CUDA is not touched and every GPU step takes its CPU path (`-X Auto` indexes with FFTW; `-X FFT` and `-X FFBIDX` are refused) | Mode — `--mode ` (default `mx`): diff --git a/docs/RUGNUX_INSTALL.md b/docs/RUGNUX_INSTALL.md index 650b2153c..eb5b0a61b 100644 --- a/docs/RUGNUX_INSTALL.md +++ b/docs/RUGNUX_INSTALL.md @@ -124,7 +124,7 @@ fits and rigid-body placement of the validation and the rotation and translation molecular replacement. The CPU implementations are the path where there is no GPU, and the two agree to rounding rather than bit for bit (parts of the scaling and the model fits round differently on the card), so a GPU and a CPU run can differ in the last digits of the merged data. The choice is automatic; -a CUDA build runs on the CPU when the GPU is hidden from it (`CUDA_VISIBLE_DEVICES= rugnux ...`). +a CUDA build runs on the CPU when told to (`rugnux --no-gpu ...`). The thread count (`-N`) governs the CPU side of all of it. The released CUDA builds need only an NVIDIA **driver** on the host, no CUDA toolkit: 525.60.13 or @@ -189,7 +189,7 @@ a big cell. One sweep of a crystal with an axis of about 640 Å predicted 236 mi peaked at **17.5 GB of host memory on a GPU build and 72.5 GB on a CPU-only build**. A CPU-only build keeps in host RAM what a GPU build keeps on the card, so it is the one that needs the large machine. When such a set is too large for GPU scaling on the card, the run stops and says to run it on the -CPU (`CUDA_VISIBLE_DEVICES= rugnux ...`, or a CPU-only build) — which then wants that much host memory. +CPU (`rugnux --no-gpu ...`, or a CPU-only build) — which then wants that much host memory. ### When host memory runs out diff --git a/image_analysis/indexing/CUDAMemHelpers.h b/image_analysis/indexing/CUDAMemHelpers.h index 0da8b4719..0aa741556 100644 --- a/image_analysis/indexing/CUDAMemHelpers.h +++ b/image_analysis/indexing/CUDAMemHelpers.h @@ -32,7 +32,7 @@ inline std::string GPUMemoryFailure(const char *what, size_t bytes, bool out_of_ msg += buf; } return msg + ". Either another program is using this GPU, or this data set needs more GPU memory than " - "the card has - free the GPU and run again, or run on the CPU (CUDA_VISIBLE_DEVICES= rugnux " + "the card has - free the GPU and run again, or run on the CPU (rugnux --no-gpu " "..., or a CPU-only build)"; } diff --git a/image_analysis/scale_merge/RotationScaleMergeGPU.cu b/image_analysis/scale_merge/RotationScaleMergeGPU.cu index a4d52a8d7..2582589b5 100644 --- a/image_analysis/scale_merge/RotationScaleMergeGPU.cu +++ b/image_analysis/scale_merge/RotationScaleMergeGPU.cu @@ -1127,7 +1127,7 @@ namespace { "Scaling %lld partial observations needs more GPU memory than this card has: %.1f GB for " "their per-observation arrays alone (%zu bytes each), before the merge's other buffers, " "on a %.1f GB card. This data set is too large for GPU scaling on this card - run it on " - "the CPU (CUDA_VISIBLE_DEVICES= rugnux ..., or a CPU-only build) on a machine with plenty of " + "the CPU (rugnux --no-gpu ..., or a CPU-only build) on a machine with plenty of " "host memory: the CPU path keeps all of it in RAM, which for a set this large can be " "tens of GB", static_cast(n_obs), double(n_obs) * OBS_BYTES / 1e9, OBS_BYTES, @@ -1146,7 +1146,7 @@ namespace { "Scaling %lld partial observations ran out of GPU memory: a buffer of %.1f MB did not fit, " "with %.1f GB of the %.1f GB card free (the per-observation arrays take %.1f GB). Either " "another program is using this GPU, or this data set is too large for GPU scaling on this " - "card - run it on the CPU (CUDA_VISIBLE_DEVICES= rugnux ..., or a CPU-only build) on a " + "card - run it on the CPU (rugnux --no-gpu ..., or a CPU-only build) on a " "machine with plenty of host memory", static_cast(n_obs), double(bytes) / 1e6, double(free_bytes) / 1e9, double(total_bytes) / 1e9, double(n_obs) * OBS_BYTES / 1e9); @@ -1162,7 +1162,7 @@ namespace { snprintf(msg, sizeof(msg), "GPU busy: scaling %lld partial observations waited %lld minutes for GPU work running " "beside it in this process to give back its memory, and it did not. The data set fits " - "this card; run it again, or on the CPU (CUDA_VISIBLE_DEVICES= rugnux ...)", + "this card; run it again, or on the CPU (rugnux --no-gpu ...)", static_cast(n_obs), static_cast(std::chrono::duration_cast(GPU_BUSY_TIMEOUT).count())); throw JFJochException(JFJochExceptionCategory::GPUCUDAError, msg); diff --git a/rugnux/rugnux_cli.cpp b/rugnux/rugnux_cli.cpp index 714bd73d1..4853c5119 100644 --- a/rugnux/rugnux_cli.cpp +++ b/rugnux/rugnux_cli.cpp @@ -118,6 +118,8 @@ void print_usage() { "(debug level); an empty prefix writes nothing at all" << std::endl; std::cout << " -N, --threads Number of threads (default: every CPU the run may use - its affinity and cgroup quota - of which the " "per-image loop uses at most 16 per GPU; an explicit value is used as given)" << std::endl; + std::cout << " --no-gpu Run on the CPU only, as on a machine without a GPU: CUDA is not " + "touched and every GPU step takes its CPU path (-X Auto indexes with FFTW)" << std::endl; std::cout << " -s, --start-image Start image number (default: 0)" << std::endl; std::cout << " -e, --end-image End image number (default: all)" << std::endl; std::cout << " -t, --stride Image stride (default: 1)" << std::endl; @@ -318,6 +320,7 @@ enum { OPT_FINALIST_LEDGER, OPT_SIMULATE_LIVE, OPT_DEVELOPER, + OPT_NO_GPU, OPT_FORCE_STILL, OPT_AZIM_MIN_Q, OPT_AZIM_MAX_Q, @@ -375,6 +378,7 @@ static option long_options[] = { {"finalist-ledger", no_argument, nullptr, OPT_FINALIST_LEDGER}, {"simulate-live", required_argument, nullptr, OPT_SIMULATE_LIVE}, {"developer", no_argument, nullptr, OPT_DEVELOPER}, + {"no-gpu", no_argument, nullptr, OPT_NO_GPU}, {"smooth-g", optional_argument, nullptr, OPT_SMOOTH_G}, {"relative-b", optional_argument, nullptr, OPT_RELATIVE_B}, {"no-scaling-corrections", no_argument, nullptr, OPT_NO_SCALING_CORRECTIONS}, @@ -852,7 +856,7 @@ extern "C" void fftwf_assertion_failed(const char *s, int line, const char *file // cannot reject on its own (a resolution limit of zero, a distance of zero, a polarisation above one, // an unreadable file), so main wraps this and reports rather than letting the exception terminate the // process with no diagnostic and exit code 134. -static int RunRugnux(int argc, char **argv) { +static int RunRugnux(int argc, char **argv, bool no_gpu) { // The whole invocation, reported at the end and written into the results report. Started before // anything else so it covers reading the file and setting up as well as the processing. const auto invocation_start = std::chrono::steady_clock::now(); @@ -898,8 +902,8 @@ static int RunRugnux(int argc, char **argv) { : fmt::format(" (git {} {})", jfjoch_git_sha1().substr(0, 6), jfjoch_git_date()), build); logger.Info("Build flags: {}", jfjoch_build_cxx_flags().empty() ? "none" : jfjoch_build_cxx_flags()); logger.Info("Command line: {}", provenance.command_line); - logger.Info("GPU: {}", provenance.gpu_count > 0 - ? fmt::format("{} ({} visible)", provenance.gpu_description, provenance.gpu_count) + logger.Info("GPU: {}", no_gpu ? std::string("not used (--no-gpu) - running on the CPU") + : provenance.gpu_count > 0 ? fmt::format("{} ({} visible)", provenance.gpu_description, provenance.gpu_count) : std::string("none visible - running on the CPU")); RegisterHDF5Filter(); @@ -1361,6 +1365,8 @@ static int RunRugnux(int argc, char **argv) { case OPT_DEVELOPER: provenance.developer = true; break; + case OPT_NO_GPU: // acted on in main, before anything asks CUDA for the devices + break; case OPT_SMOOTH_G: smooth_g_deg_arg = optarg ? parse_double_arg(optarg, "--smooth-g", logger) : SMOOTH_G_DEFAULT_DEG; break; @@ -1639,7 +1645,8 @@ static int RunRugnux(int argc, char **argv) { // Said before the run rather than after it: the GPUs are what makes rugnux fast, and a machine // that turns out to have none - a driver mismatch, a CUDA_VISIBLE_DEVICES left over from another // job - is worth knowing about while there is still time to stop and fix it. - console->Field("Compute", provenance.gpu_count > 0 + console->Field("Compute", no_gpu ? fmt::format("CPU only (--no-gpu), {} threads", nthreads) + : provenance.gpu_count > 0 ? fmt::format("{}{}, {} threads", provenance.gpu_count > 1 ? fmt::format("{} x ", provenance.gpu_count) : "", provenance.gpu_description, nthreads) : fmt::format("no GPU visible - {} CPU threads, which is much slower", nthreads)); @@ -3297,9 +3304,16 @@ int main(int argc, char **argv) { // Before anything creates a CUDA context, which fixes the flags for the device. A thread // waiting on the GPU then sleeps instead of spinning on a core - measured on a 16M rotation // run, a fifth of all CPU time went to that spinning, with no change in wall time either way. + // --no-gpu is looked for here, ahead of the parser, because that already asks CUDA for the devices. + bool no_gpu = false; + for (int i = 1; i < argc; i++) + if (std::strcmp(argv[i], "--no-gpu") == 0) + no_gpu = true; + if (no_gpu) + hide_gpus(); set_gpu_blocking_sync(); enable_gpu_numa_binding(); - const int ret = RunRugnux(argc, argv); + const int ret = RunRugnux(argc, argv, no_gpu); // Everything that used a card has been destroyed with the run. release_gpu_contexts(); return ret;