Skip to content

Commit

Permalink
[Fix] Suppress error message while checking for GPUs in Python workfl…
Browse files Browse the repository at this point in the history
…ow (#995)

* Issue: On systems without GPU, while using the Python interface, the check GPU shows errors `sh: line 1: nvidia-smi: command not found`.
* Fix: Modified the utility function to suppress error.
* Using 'mkstemp' utility to create temporary file.
  • Loading branch information
khalatepradnya authored Dec 5, 2023
1 parent d23d892 commit 03b1be5
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions python/utils/LinkedLibraryHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,35 @@ constexpr static const char NVQIR_SIMULATION_BACKEND[] =
"NVQIR_SIMULATION_BACKEND=";
constexpr static const char TARGET_DESCRIPTION[] = "TARGET_DESCRIPTION=";

/// @brief A utility function to check availability of Nvidia GPUs and return
/// their count
int countGPUs() {
char buffer[1024];
std::string output;
FILE *fp1, *fp2;

fp1 = popen("nvidia-smi", "r");
if (!fp1) {
int retCode = std::system("nvidia-smi >/dev/null 2>&1");
if (0 != retCode) {
cudaq::info("nvidia-smi: command not found");
return -1;
}
pclose(fp1);

fp2 = popen("nvidia-smi -L | wc -l", "r");
if (!fp2) {
cudaq::info("nvidia-smi: command not working");
char tmpFile[] = "/tmp/.cmd.capture.XXXXXX";
int fileDescriptor = mkstemp(tmpFile);
if (-1 == fileDescriptor) {
cudaq::info("Failed to create a temporary file to capture output");
return -1;
}
while (fgets(buffer, sizeof buffer, fp2)) {
output += buffer;

std::string command = "nvidia-smi -L 2>/dev/null | wc -l >> ";
command.append(tmpFile);
retCode = std::system(command.c_str());
if (0 != retCode) {
cudaq::info("Encountered error while invoking 'nvidia-smi'");
return -1;
}
pclose(fp2);
return std::stoi(output);

std::stringstream buffer;
buffer << std::ifstream(tmpFile).rdbuf();
close(fileDescriptor);
unlink(tmpFile);
return std::stoi(buffer.str());
}

std::size_t RuntimeTarget::num_qpus() {
Expand Down

0 comments on commit 03b1be5

Please sign in to comment.