【发布时间】:2016-04-06 10:06:26
【问题描述】:
我正在研究 MPI 中的并行矩阵-矩阵乘法器。我已经完成了计算部分,但我也想计算 CPU 时间。我被卡住了,因为看起来有些进程报告的开始和结束时间为 0,而对于一个应该在一秒钟内完成的任务(小矩阵),程序报告了 1000+ 秒的 CPU 时间(即使我知道它运行从观察不到一秒钟)。这是我目前正在做的事情:
#include <time.h>
#include "mpi.h"
// other includes
int main()
{
int start, end, min_start, min_end;
if (rank == 0)
{
// setup stuff
start = clock();
MPI_Reduce(&min_start, &start, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
// master computation stuff
end = clock();
MPI_Reduce(&max_end, &end, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
cout << "CPU time was "
<< (double)(max_end - min_start) / CLOCKS_PER_SEC
<< " seconds" << endl;
}
else if (rank != 0)
{
// setup stuff
start = clock();
MPI_Reduce(&min_start, &start, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
// slave computation stuff
end = clock();
MPI_Reduce(&max_end, &end, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
}
}
我不确定错误的根源是什么。当我在这个调试输出中添加时(在if (rank == 0) 和else if (rank != 0) 语句之后)
MPI_Barrier(MPI_COMM_WORLD);
for (int i=0; i<size; i++)
{
if (rank == i)
cout << "(" << i << ") CPU time = "
<< end << " - " << start
<< " = " << end - start << endl;
MPI_Barrier(MPI_COMM_WORLD);
}
我得到以下输出
CPU time was 1627.91 seconds
(1) CPU time = 0 - 0 = 0
(2) CPU time = 0 - 0 = 0
(0) CPU time = 1627938704 - 32637 = 1627906067
(3) CPU time = 10000 - 0 = 10000
【问题讨论】:
-
首先,我根本不会使用
clock()。您可以将chrono与 C++11 一起使用,或者在 C++11 之前使用它的 Boost 实现。
标签: c++ parallel-processing mpi cpu-time