快速解决深度学习推理过程cuda或tensorRT推理速度变慢的办法,记录一下方便自己以后查看。
一、显卡性能设置:
低延时模式——超高、最大帧速度——1000每秒帧数、电源管理模式——最高性能优先
二、管理员权限(命令提示符以管理员身份运行)终端输入nvidia-smi -q -d SUPPORTED_ClOCKS
三、终端输入nvidia-smi -lgc 10501(根据实际大小来输入)
四、终端输入nvidia-smi -lgc 5000,10501 显存锁定在5000-10501直接波动,低版本最好直接设置最大值
五、终端输入nvidia-smi -rgc 解除锁定
六、程序初始化自动设置
int AmplifyGpu()
{
STARTUPINFOW si = { sizeof(si) }; // Use STARTUPINFOW for wide character support
PROCESS_INFORMATION pi;
// Command 1: nvidia-smi -lgc 99999999999
std::wstring cmd1 = L"cmd.exe /c nvidia-smi -lgc 99999999999";
// CreateProcessW to run the first command
if (CreateProcessW(NULL, &cmd1[0], NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi))
{
// Wait for a short time to make sure the window is created
Sleep(1000);
// Get the handle of the cmd window (using FindWindowW for wide strings)
HWND hwndCmd = FindWindowW(NULL, L"Administrator: C:\\Windows\\System32\\cmd.exe");
if (hwndCmd != NULL)
{
LockCmdWindow(hwndCmd); // Lock the cmd window to prevent hiding or closing
}
// Wait for the process to finish
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else
{
std::cerr << "Failed to execute first command as Administrator!" << std::endl;
return -1;
}
// Command 2: nvidia-smi -lmc 99999999999
std::wstring cmd2 = L"cmd.exe /c nvidia-smi -lmc 99999999999";
// CreateProcessW to run the second command
if (CreateProcessW(NULL, &cmd2[0], NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi))
{
// Wait for a short time to make sure the window is created
Sleep(1000);
HWND hwndCmd = FindWindowW(NULL, L"Administrator: C:\\Windows\\System32\\cmd.exe");
if (hwndCmd != NULL)
{
LockCmdWindow(hwndCmd); // Lock the cmd window to prevent hiding or closing
}
// Wait for the process to finish
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else
{
std::cerr << "Failed to execute second command as Administrator!" << std::endl;
return -1;
}
std::cout << "Commands executed successfully!" << std::endl;
return 0;
Command 1: nvidia-smi -lgc 99999999999
//LPCWSTR cmd1 = L"nvidia-smi -lgc 99999999999";
Command 2: nvidia-smi -lmc 99999999999
//LPCWSTR cmd2 = L"nvidia-smi -lmc 99999999999";
Use ShellExecute to run the command as Administrator
//HINSTANCE result1 = ShellExecuteW(
// NULL, // No parent window
// L"runas", // Run as administrator
// L"cmd.exe", // Program to open (cmd)
// (L"/c " + std::wstring(cmd1)).c_str(), // Arguments: run command 1
// NULL, // Default working directory
// SW_HIDE // Hide the command window
//);
//if ((int)result1 <= 32)
//{
// std::cerr << "Failed to execute first command as Administrator!" << std::endl;
// return -1;
//}
Use ShellExecute to run the second command as Administrator
//HINSTANCE result2 = ShellExecuteW(
// NULL, // No parent window
// L"runas", // Run as administrator
// L"cmd.exe", // Program to open (cmd)
// (L"/c " + std::wstring(cmd2)).c_str(), // Arguments: run command 2
// NULL, // Default working directory
// SW_HIDE // Hide the command window
//);
//if ((int)result2 <= 32)
//{
// std::cerr << "Failed to execute second command as Administrator!" << std::endl;
// return -1;
//}
//std::cout << "Commands executed successfully!" << std::endl;
//return 0;
}