Skip to content

Commit 13d0428

Browse files
authored
Fix(input): require explicit GPU device selection (#7720)
* Fix(input): require explicit GPU device selection * Fix(input): retain explicit auto device option * Fix(input): require explicit GPU device selection
1 parent 1cd141d commit 13d0428

3 files changed

Lines changed: 20 additions & 13 deletions

File tree

source/source_base/module_device/device.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ bool probe_gpu_availability() {
6363
std::string get_device_flag(const std::string &device,
6464
const std::string &basis_type) {
6565
// 1. Validate input string
66-
if (device != "cpu" && device != "gpu" && device != "auto") {
67-
ModuleBase::WARNING_QUIT("device", "Parameter \"device\" can only be set to \"cpu\", \"gpu\", or \"auto\"!");
66+
if (device != "cpu" && device != "gpu") {
67+
ModuleBase::WARNING_QUIT("device", "Parameter \"device\" can only be set to \"cpu\" or \"gpu\"!");
6868
}
6969

7070
// NOTE: This function is called only on rank 0 during input parsing.
@@ -80,15 +80,6 @@ std::string get_device_flag(const std::string &device,
8080
} else {
8181
ModuleBase::WARNING_QUIT("device", "Device is set to 'gpu', but no available GPU was found. Please check your hardware/drivers or set 'device=cpu'.");
8282
}
83-
} else if (device == "auto") {
84-
if (probe_gpu_availability()) {
85-
result = "gpu";
86-
// std::cout << " INFO: 'device=auto' specified. GPU detected and will be used." << std::endl;
87-
} else {
88-
result = "cpu";
89-
// std::cout << " WARNING: 'device=auto' specified, but no GPU was found. Falling back to CPU." << std::endl;
90-
// std::cout << " To suppress this warning, please explicitly set 'device=cpu' in your input." << std::endl;
91-
}
9283
} else { // device == "cpu"
9384
result = "cpu";
9485
// std::cout << " INFO: 'device=cpu' specified. CPU will be used." << std::endl;

source/source_io/module_parameter/input_parameter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct Input_para
6969
std::string kmesh_type = "gamma"; ///< k-point mesh type for kspacing-generated k-point mesh: gamma or mp
7070
double min_dist_coef = 0.2; ///< allowed minimum distance between two atoms
7171

72-
std::string device = "auto";
72+
std::string device = "cpu";
7373
std::string precision = "double";
7474
std::string gint_precision = "double";
7575
bool timer_enable_nvtx = false;

source/source_io/test_serial/read_input_test.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "gtest/gtest.h"
88
#include <cstdio>
99
#include <fstream>
10+
#include <stdexcept>
1011

1112
// mock
1213
namespace GlobalV
@@ -120,6 +121,7 @@ TEST_F(InputTest, Selfconsistent_Read)
120121
Parameter param;
121122
// readinput.read_parameters(param, "./empty_INPUT");
122123
EXPECT_NO_THROW(readinput.read_parameters(param, "./empty_INPUT"));
124+
EXPECT_EQ(param.inp.device, "cpu");
123125
readinput.write_parameters(param, "./my_INPUT1");
124126
readinput.clear();
125127
// readinput.read_parameters(param, "./my_INPUT1");
@@ -152,6 +154,20 @@ TEST_F(InputTest, Selfconsistent_Read)
152154
}
153155
}
154156

157+
TEST_F(InputTest, RejectAutoDevice)
158+
{
159+
std::ofstream input("auto_device_INPUT");
160+
input << "INPUT_PARAMETERS\n"
161+
<< "device auto\n";
162+
input.close();
163+
164+
ModuleIO::ReadInput readinput(0);
165+
readinput.check_ntype_flag = false;
166+
Parameter param;
167+
EXPECT_THROW(readinput.read_parameters(param, "./auto_device_INPUT"), std::runtime_error);
168+
EXPECT_TRUE(std::remove("./auto_device_INPUT") == 0);
169+
}
170+
155171
TEST_F(InputTest, Check)
156172
{
157173
ModuleIO::ReadInput readinput(0);
@@ -175,4 +191,4 @@ TEST_F(InputTest, Check)
175191
std::string output = testing::internal::GetCapturedStdout();
176192
EXPECT_THAT(output, testing::HasSubstr("INPUT parameters have been successfully checked!"));
177193
EXPECT_TRUE(std::remove("./INPUT.ref") == 0);
178-
}
194+
}

0 commit comments

Comments
 (0)