Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-02 09:13:51

0001 /**
0002  * @file SEventDeviceConfigTest.cc
0003  * @brief Verifies that each CUDA device has its own photon-slot limit.
0004  *
0005  * Two host threads select different GPUs and check that each new event uses
0006  * its device's limit. After both threads exit, the main thread switches
0007  * between the GPUs and reads back each override. It then checks that clearing
0008  * an override restores the job setting and that automatic sizing uses the
0009  * selected GPU's memory.
0010  *
0011  * The single-device case checks the same settings with one worker. With all
0012  * GPUs hidden, the test checks that the job limit remains available and that
0013  * attempting to set a device override raises an exception.
0014  */
0015 
0016 #include "CUDA_CHECK.h"
0017 #include "NPFold.h"
0018 #include "SEventConfig.hh"
0019 #include "SEvt.hh"
0020 #include "scuda.h"
0021 #include "squad.h"
0022 
0023 #include "sphoton.h"
0024 #include "srec.h"
0025 
0026 #include "sevent.h"
0027 
0028 #include <cuda_runtime_api.h>
0029 
0030 #include <algorithm>
0031 #include <array>
0032 #include <future>
0033 #include <iostream>
0034 #include <latch>
0035 #include <limits>
0036 #include <memory>
0037 #include <stdexcept>
0038 #include <string_view>
0039 
0040 namespace
0041 {
0042 constexpr int64_t optixLimit = int64_t{1} << 30;
0043 
0044 void require(bool condition, const char* message)
0045 {
0046     if (!condition)
0047         throw std::runtime_error(message);
0048 }
0049 
0050 int64_t automaticLimit(int device)
0051 {
0052     cudaDeviceProp properties{};
0053     CUDA_CHECK(cudaGetDeviceProperties(&properties, device));
0054     const size_t raw = SEventConfig::HeuristicMaxSlot(properties.totalGlobalMem, SEventConfig::ModeLite(), SEventConfig::ModeMerge());
0055     return std::min<int64_t>((raw / SEventConfig::M) * SEventConfig::M, optixLimit);
0056 }
0057 
0058 void expectDevice(int device, int64_t limit)
0059 {
0060     require(SEventConfig::DeviceIndex() == device, "wrong CUDA-visible device");
0061     require(SEventConfig::MaxSlot() == limit, "wrong effective launch limit");
0062     sevent event{};
0063     event.init();
0064     require(event.max_slot == static_cast<size_t>(limit), "event initialized with the wrong launch limit");
0065     cudaDeviceProp properties{};
0066     CUDA_CHECK(cudaGetDeviceProperties(&properties, device));
0067     require(SEventConfig::HasDevice() && properties.name == std::string(SEventConfig::DeviceName()), "wrong device name");
0068 }
0069 } // namespace
0070 
0071 int main(int argc, char** argv)
0072 {
0073     const std::string_view mode = argc > 1 ? argv[1] : "";
0074     constexpr int          jobLimit = 3000;
0075     constexpr size_t       gib = size_t{1} << 30;
0076     require(SEventConfig::HeuristicMaxSlot_Rounded(24 * gib, 1, 0) == 784000000, "wrong automatic limit below the OptiX ceiling");
0077     require(SEventConfig::HeuristicMaxSlot_Rounded(48 * gib, 1, 0) == optixLimit, "automatic limit exceeds the OptiX ceiling");
0078     SEventConfig::SetModeLite(1);
0079     SEventConfig::SetMaxSlot(jobLimit);
0080     require(SEventConfig::DeviceIndex() == -1 && SEventConfig::MaxSlot() == jobLimit, "wrong pre-initialization configuration");
0081     try
0082     {
0083         SEventConfig::SetMaxSlot(-1);
0084         throw std::runtime_error("accepted a negative job limit");
0085     }
0086     catch (const std::invalid_argument&)
0087     {
0088         require(SEventConfig::MaxSlot() == jobLimit, "invalid job limit changed the active setting");
0089     }
0090     if (mode == "--no-device")
0091     {
0092         SEventConfig::Initialize();
0093         require(SEventConfig::DeviceIndex() == -1 && !SEventConfig::HasDevice(), "expected all CUDA devices to be hidden");
0094         require(SEventConfig::DeviceName() == nullptr && SEventConfig::MaxSlot() == jobLimit, "wrong device-free configuration");
0095         try
0096         {
0097             SEventConfig::SetDeviceMaxSlot(123);
0098         }
0099         catch (const std::logic_error&)
0100         {
0101             return 0;
0102         }
0103         throw std::runtime_error("accepted a device override without a CUDA device");
0104     }
0105 
0106     const int numDevices = mode == "--single-device" ? 1 : 2;
0107     if (numDevices == 2)
0108         SEventConfig::SetMaxSlot(0);
0109     int  count = 0;
0110     auto status = cudaGetDeviceCount(&count);
0111     if (status == cudaErrorNoDevice || status == cudaErrorInsufficientDriver || (status == cudaSuccess && count < numDevices))
0112         return 77;
0113     CUDA_CHECK(status);
0114 
0115     // Each device inherits the job setting until its worker overrides it.
0116     constexpr std::array<int, 2>     limits{1000, 2000};
0117     std::latch                       start(1);
0118     std::array<std::future<void>, 2> workers;
0119     for (int device = 0; device < numDevices; ++device)
0120         workers[device] = std::async(std::launch::async, [&, device] {
0121             start.wait();
0122             CUDA_CHECK(cudaSetDevice(device));
0123             SEventConfig::Initialize();
0124             expectDevice(device, numDevices == 2 ? automaticLimit(device) : jobLimit);
0125             SEventConfig::SetDeviceMaxSlot(limits[device]);
0126             expectDevice(device, limits[device]);
0127         });
0128     start.count_down();
0129     for (int device = 0; device < numDevices; ++device) workers[device].get();
0130 
0131     SEventConfig::SetMaxSlot(jobLimit);
0132     // A different thread sees the limit owned by each device, including after a switch.
0133     for (int device = 0; device < numDevices; ++device)
0134     {
0135         CUDA_CHECK(cudaSetDevice(device));
0136         expectDevice(device, limits[device]);
0137         std::cout << "CUDA device " << device << " (" << SEventConfig::DeviceName() << "), max slots " << SEventConfig::MaxSlot() << '\n';
0138         SEventConfig::SetDeviceMaxSlot(0);
0139         expectDevice(device, jobLimit);
0140     }
0141 
0142     SEventConfig::SetMaxSlot(0);
0143     for (int device = 0; device < numDevices; ++device)
0144     {
0145         CUDA_CHECK(cudaSetDevice(device));
0146         expectDevice(device, automaticLimit(device));
0147     }
0148 
0149     SEventConfig::SetMaxSlot(std::numeric_limits<int>::max());
0150     expectDevice(numDevices - 1, optixLimit);
0151     SEventConfig::SetMaxSlot(jobLimit);
0152     SEvt* event = SEvt::Create_EGPU();
0153     SEventConfig::SetDeviceMaxSlot(std::numeric_limits<int>::max());
0154     expectDevice(numDevices - 1, optixLimit);
0155     event->addEventConfigArray();
0156     const NP* eventConfig = event->extrafold->get(SEventConfig::NAME);
0157     require(eventConfig && eventConfig->get_meta<int64_t>("MaxSlot", -1) == jobLimit, "event metadata lost its captured launch limit");
0158     const std::unique_ptr<NP> currentConfig(SEventConfig::Serialize());
0159     require(currentConfig->get_meta<int64_t>("MaxSlot", -1) == optixLimit, "standalone metadata lost the current device limit");
0160     SEventConfig::SetDeviceMaxSlot(0);
0161     expectDevice(numDevices - 1, jobLimit);
0162 }