安装 Ubuntu 20.04 后,一切都运行良好。但是,在安装和使用一些应用程序(Matlab 和 Matlab/Dynare)后,Ubuntu 似乎限制了我的 CPU 使用率。我使用 openMP 和一个曾经实现 100% CPU 使用率的代码,但现在速度慢了很多,top 显示以下内容: 从顶部输出 我的处理器总共有 32 个内核和 64 个线程。此代码使用了所有线程,之前 CPU 使用率达到 100%。但是,现在总 CPU 使用率仅为 18.9%。还请注意,主作业几乎使用了 6400% 的 CPU=64*100。与以前相比,现在的代码非常慢。以下是 htop 显示的内容: htop 的输出 线程只是在进行独立计算,没有任何交互(只是在最后把所有东西放在一起)。过去我遇到过同样的问题,我通过重新安装 Ubuntu 解决了它。有没有办法在不重新安装 Ubuntu 的情况下解决问题?在 C++ 中,我使用一个非常简单的循环,线程之间没有交互(完全并行):
// nproc is the number of threads to be used
// NIT is the total number of random draws to be obtained
// NITper is the number of random draws per thread,
// NIT= nproc*NITper
arma::vec keepn=arma::zeros(NIT,1); // vector to keep the random draws
// Ydep is a matrix of data, it is to be read (but not modified) by threads. In reality I have more of these, but this is a simplified version.
omp_set_num_threads(nproc);
#pragma omp parallel for schedule(static)
for (int ii=0; ii<=nproc-1; ii++){
arma::vec Lkeepn=arma::zeros(NITper,1);
Gibbs_Inte(Ydep, Lkeepn, NITper);
// Ask thread ii to obtain NITper random values, which are stored in Lkeepn
int l1=ii*NITper;
int l2=(ii+1)*NITper-1;
keepn.rows(l1,l2)=Lkeepn; // write random draws in keepn
}
我检查了 gcc 的 spec 文件,它与全新安装 ubuntu 后的文件相同,所以问题不存在。是否有可能某些应用程序更改了限制我的 CPU 使用率的系统范围设置?我该如何解决这个问题?
如果我执行“strace -p PID”,我会得到以下结果:
sched_yield() = 0
futex(0x7fa75e2e6a10, FUTEX_WAIT_PRIVATE, 2, NULL) = -1 EAGAIN (Resource temporarily unavailable)
futex(0x7fa75e2e6a10, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x7fa75e2e5c80, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x7fa75e2e5c80, FUTEX_WAIT_PRIVATE, 2, NULL) = 0
futex(0x7fa75e2e5c80, FUTEX_WAIT_PRIVATE, 2, NULL) = -1 EAGAIN (Resource temporarily unavailable)
futex(0x7fa75e2e5c80, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x7fa75e2e5c80, FUTEX_WAKE_PRIVATE, 1) = 1
futex(0x7fa75e2e5c80, FUTEX_WAIT_PRIVATE, 2, NULL) = 0
futex(0x7fa75e2e5c80, FUTEX_WAIT_PRIVATE, 2, NULL) = 0
futex(0x7fa75e2e5c80, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x7fa75e2e5c80, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x7fa75e2e5c80, FUTEX_WAIT_PRIVATE, 2, NULL) = -1 EAGAIN (Resource temporarily unavailable)
futex(0x7fa75e2e5c80, FUTEX_WAIT_PRIVATE, 2, NULL) = 0
futex(0x7fa75e2e5c80, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x7fa75e2e5c80, FUTEX_WAIT_PRIVATE, 2, NULL) = 0
futex(0x7fa75e2e5c80, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x7fa75e2e5c80, FUTEX_WAIT_PRIVATE, 2, NULL) = 0
futex(0x7fa75e2e5c80, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x7fa75e2e6d90, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0x7fa75e2e7010, FUTEX_WAKE_PRIVATE, 1) = 0
sched_yield() = 0
futex(0x7fa75e2e6d90, FUTEX_WAKE_PRIVATE, 1) = 0
sched_yield() = 0
我尝试将仅由线程读取的共享变量设置为私有变量,但问题仍然存在。
答案1
问题是 Matlab 或/和 Matlab/Dynare 安装了 OpenBlas,而在 Ubuntu 20.04 中它有一个错误,该错误已在其他地方报告: https://github.com/xianyi/OpenBLAS/issues/2642
解决方案是卸载 OpenBlas。以下方法对我有用:
sudo apt-get remove libopenblas0
sudo apt-get remove libopenblas0-pthread
代码在处理矩阵时变慢。OpenBlas 导致线程相互冲突。在删除 OpenBlas 之前,我尝试了 OPENBLAS_NUM_THREADS=1,但这并没有解决性能问题。删除 OpenBlas 后,我的计算速度与以前一样快(比使用 OpenBlas 快得多)。