Skip to content

Commit

Permalink
Improve how CUDAEnsemble reports failure to find a CUDA device.
Browse files Browse the repository at this point in the history
This aligns closer to CUDASimulation's behaviour.

Closes #858
  • Loading branch information
Robadob authored and mondus committed Jun 1, 2022
1 parent 620e904 commit 376f37d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/flamegpu/gpu/CUDAEnsemble.cu
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,26 @@ unsigned int CUDAEnsemble::simulate(const RunPlanVector &plans) {
run_logs.clear();
run_logs.resize(plans.size());
// Workout how many devices and runner we will be executing
int ct = -1;
gpuErrchk(cudaGetDeviceCount(&ct));
int device_count = -1;
cudaError_t cudaStatus = cudaGetDeviceCount(&device_count);
if (cudaStatus != cudaSuccess) {
THROW exception::InvalidCUDAdevice("Error finding CUDA devices! Do you have a CUDA-capable GPU installed?, in CUDAEnsemble::simulate()");
}
if (device_count == 0) {
THROW exception::InvalidCUDAdevice("Error no CUDA devices found!, in CUDAEnsemble::simulate()");
}
for (const int &id : config.devices) {
if (id >= device_count) {
THROW exception::InvalidCUDAdevice("Requested CUDA device %d is not valid, only %d CUDA devices available!, in CUDAEnsemble::simulate()", id, device_count);
}
}

std::set<int> devices;
if (config.devices.size()) {
devices = config.devices;
} else {
for (int i = 0; i < ct; ++i) {
devices.emplace(i);
for (int i = 0; i < device_count; ++i) {
devices.emplace(i);
}
}
// Check that each device is capable, and init cuda context
Expand Down
10 changes: 10 additions & 0 deletions tests/test_cases/gpu/test_cuda_ensemble.cu
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ TEST(TestCUDAEnsemble, initialise_devices) {
ensemble.initialise(sizeof(argv) / sizeof(char*), argv);
EXPECT_EQ(ensemble.getConfig().devices, std::set<int>({0}));
}
TEST(TestCUDAEnsemble, initialise_devices_wrong) {
flamegpu::ModelDescription model("test");
flamegpu::CUDAEnsemble ensemble(model);
int device_ct = -1;
EXPECT_EQ(cudaGetDeviceCount(&device_ct), cudaSuccess);
ensemble.Config().devices = { device_ct };
RunPlanVector plan(model, 1);
// Sim with out of bounds device ID, get exception
EXPECT_THROW(ensemble.simulate(plan), exception::InvalidCUDAdevice);
}
TEST(TestCUDAEnsemble, initialise_quiet) {
// Create a model
flamegpu::ModelDescription model("test");
Expand Down

0 comments on commit 376f37d

Please sign in to comment.