CPLUSPLUC Telegram 1125
πŸš€ Π₯ΠΎΡ‚ΠΈΡ‚Π΅ ΠΌΠΎΠ½ΠΈΡ‚ΠΎΡ€ΠΈΡ‚ΡŒ Π·Π°Π³Ρ€ΡƒΠ·ΠΊΡƒ вашСй NVIDIA GPU прямо ΠΈΠ· C++?

Π’ΠΎΡ‚ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ ΠΏΡ€ΠΈΠΌΠ΅Ρ€ Π½Π° C++, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ nvidia-smi Ρ‡Π΅Ρ€Π΅Π· систСмный Π²Ρ‹Π·ΠΎΠ² для получСния Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ GPU ΠΈ использованной памяти.

πŸ›  ВрСбования:
β€’ УстановлСнный Π΄Ρ€Π°ΠΉΠ²Π΅Ρ€ NVIDIA ΠΈ ΡƒΡ‚ΠΈΠ»ΠΈΡ‚Π° nvidia-smi
β€’ C++17 ΠΈΠ»ΠΈ Π²Ρ‹ΡˆΠ΅

πŸ“„ Код:


#include <iostream>
#include <cstdlib>
#include <memory>
#include <array>

void get_gpu_utilization() {
std::array<char, 128> buffer;
std::string result;

std::unique_ptr<FILE, decltype(&pclose)> pipe(
popen("nvidia-smi --query-gpu=utilization.gpu,memory.used,memory.total --format=csv,nounits,noheader", "r"),
pclose
);

if (!pipe) {
std::cerr << "❌ Ошибка ΠΏΡ€ΠΈ Π²Ρ‹Π·ΠΎΠ²Π΅ nvidia-smi" << std::endl;
return;
}

int gpu_id = 0;
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
std::string line(buffer.data());
size_t pos1 = line.find(',');
size_t pos2 = line.rfind(',');

std::string util = line.substr(0, pos1);
std::string mem_used = line.substr(pos1 + 1, pos2 - pos1 - 1);
std::string mem_total = line.substr(pos2 + 1);

std::cout << "πŸ–₯ GPU " << gpu_id++ << ": " << util << "% load | "
<< mem_used << " MiB / " << mem_total << " MiB";
}
}

int main() {
get_gpu_utilization();
return 0;
}



πŸ“¦ ΠšΠΎΠΌΠΏΠΈΠ»ΡΡ†ΠΈΡ:

bash
ΠšΠΎΠΏΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ
Π Π΅Π΄Π°ΠΊΡ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ
g++ gpu_monitor.cpp -o gpu_monitor -std=c++17
πŸ“Œ Π’Ρ‹Π²ΠΎΠ΄:

πŸ–₯ GPU 0: 17% load | 512 MiB / 8192 MiB

🧠 ΠŸΠΎΠ΄Ρ…ΠΎΠ΄ΠΈΡ‚ для:
β€’ ΠœΠΎΠ½ΠΈΡ‚ΠΎΡ€ΠΈΠ½Π³Π° GPU Π² Ρ€Π΅Π°Π»ΡŒΠ½ΠΎΠΌ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ
β€’ Π˜Π½Ρ‚Π΅Π³Ρ€Π°Ρ†ΠΈΠΈ Π² бэкСнды, Π±ΠΎΡ‚Ρ‹, систСмы Π»ΠΎΠ³Π³ΠΈΠ½Π³Π°
β€’ Π›Ρ‘Π³ΠΊΠΎΠΉ ΠΎΡ‚Π»Π°Π΄ΠΊΠΈ ML/AI-ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠΉ Π½Π° C++



tgoop.com/cpluspluc/1125
Create:
Last Update:

πŸš€ Π₯ΠΎΡ‚ΠΈΡ‚Π΅ ΠΌΠΎΠ½ΠΈΡ‚ΠΎΡ€ΠΈΡ‚ΡŒ Π·Π°Π³Ρ€ΡƒΠ·ΠΊΡƒ вашСй NVIDIA GPU прямо ΠΈΠ· C++?

Π’ΠΎΡ‚ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ ΠΏΡ€ΠΈΠΌΠ΅Ρ€ Π½Π° C++, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ nvidia-smi Ρ‡Π΅Ρ€Π΅Π· систСмный Π²Ρ‹Π·ΠΎΠ² для получСния Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ GPU ΠΈ использованной памяти.

πŸ›  ВрСбования:
β€’ УстановлСнный Π΄Ρ€Π°ΠΉΠ²Π΅Ρ€ NVIDIA ΠΈ ΡƒΡ‚ΠΈΠ»ΠΈΡ‚Π° nvidia-smi
β€’ C++17 ΠΈΠ»ΠΈ Π²Ρ‹ΡˆΠ΅

πŸ“„ Код:


#include <iostream>
#include <cstdlib>
#include <memory>
#include <array>

void get_gpu_utilization() {
std::array<char, 128> buffer;
std::string result;

std::unique_ptr<FILE, decltype(&pclose)> pipe(
popen("nvidia-smi --query-gpu=utilization.gpu,memory.used,memory.total --format=csv,nounits,noheader", "r"),
pclose
);

if (!pipe) {
std::cerr << "❌ Ошибка ΠΏΡ€ΠΈ Π²Ρ‹Π·ΠΎΠ²Π΅ nvidia-smi" << std::endl;
return;
}

int gpu_id = 0;
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
std::string line(buffer.data());
size_t pos1 = line.find(',');
size_t pos2 = line.rfind(',');

std::string util = line.substr(0, pos1);
std::string mem_used = line.substr(pos1 + 1, pos2 - pos1 - 1);
std::string mem_total = line.substr(pos2 + 1);

std::cout << "πŸ–₯ GPU " << gpu_id++ << ": " << util << "% load | "
<< mem_used << " MiB / " << mem_total << " MiB";
}
}

int main() {
get_gpu_utilization();
return 0;
}



πŸ“¦ ΠšΠΎΠΌΠΏΠΈΠ»ΡΡ†ΠΈΡ:

bash
ΠšΠΎΠΏΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ
Π Π΅Π΄Π°ΠΊΡ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ
g++ gpu_monitor.cpp -o gpu_monitor -std=c++17
πŸ“Œ Π’Ρ‹Π²ΠΎΠ΄:

πŸ–₯ GPU 0: 17% load | 512 MiB / 8192 MiB

🧠 ΠŸΠΎΠ΄Ρ…ΠΎΠ΄ΠΈΡ‚ для:
β€’ ΠœΠΎΠ½ΠΈΡ‚ΠΎΡ€ΠΈΠ½Π³Π° GPU Π² Ρ€Π΅Π°Π»ΡŒΠ½ΠΎΠΌ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ
β€’ Π˜Π½Ρ‚Π΅Π³Ρ€Π°Ρ†ΠΈΠΈ Π² бэкСнды, Π±ΠΎΡ‚Ρ‹, систСмы Π»ΠΎΠ³Π³ΠΈΠ½Π³Π°
β€’ Π›Ρ‘Π³ΠΊΠΎΠΉ ΠΎΡ‚Π»Π°Π΄ΠΊΠΈ ML/AI-ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠΉ Π½Π° C++

BY C++ Academy


Share with your friend now:
tgoop.com/cpluspluc/1125

View MORE
Open in Telegram


Telegram News

Date: |

best-secure-messaging-apps-shutterstock-1892950018.jpg A Hong Kong protester with a petrol bomb. File photo: Dylan Hollingsworth/HKFP. Done! Now you’re the proud owner of a Telegram channel. The next step is to set up and customize your channel. The main design elements of your Telegram channel include a name, bio (brief description), and avatar. Your bio should be: More>>
from us


Telegram C++ Academy
FROM American