使用MPI时计算CPU时间
Calculating CPU Time when using MPI
我正在MPI中开发一个并行矩阵矩阵乘法器。我已经让计算部分工作,但我也想计算CPU时间。我被卡住了,因为看起来有些进程报告的开始和结束时间都是0,而对于一个应该需要不到一秒的时间(小矩阵)的任务,程序报告的CPU时间超过1000秒(尽管我知道它的运行时间不到一秒钟)。以下是我目前正在做的事情:
#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
首先,man 3 clock
表示"clock()函数返回程序使用的处理器时间的近似值"。因此,要确定时间,不需要计算差值。这种误解是错误的根源。您只需要在密集计算后调用它,而忽略setup stuff
所消耗的时间。
如果你不想考虑设置时间,那么你真的需要差异。因此,只需使用简单而强大的MPI_Wtime函数,即可获得自过去固定时刻以来的精确秒数。
从最大结束时间减去最小开始时间得到的值不是公认的总CPU时间(即time
实用程序)。那个时间是real
时间。要获得真正的CPU时间,您应该将所有处理时间相加,即调用具有时间差的MPI_Reduce
和MPI_SUM
操作。
相关文章:
- 分别测量每个线程上花费的 CPU 时间(C++)
- 超过CPU时间限制:当MPI_Sent一个非常大的int*时
- 如何测量一组特定线程的 CPU 时间?
- 如何以C++而不是 CPU 时间测量挂钟时间
- 将返回 CPU 时间提升为 0
- C++中的双感叹号(!!)会花费更多的CPU时间吗
- 即使使用睡眠,线程也不会放弃CPU时间
- 以C++为单位测量程序的 CPU 时间和挂钟时间
- 在 Windows 上以C++计算 CPU 时间
- 时间短函数与 CPU 时间使用 RTEMS 操作系统
- PDH 无法通过 PdhAddCounter() 访问总 CPU 时间
- 二叉搜索真的是在 0 时钟 CPU 时间内执行的吗?
- 我应该检查什么:cpu时间还是墙时间
- 使用MPI时计算CPU时间
- 堆排序CPU时间
- rusage的进程/线程的Cpu时间不可能
- 如何在 Windows 上以 C++ 为单位测量 CPU 时间并包括 system() 的调用
- 在 Ubuntu 中获取 CPU 时间
- 为什么使用 clock() 在测量 CPU 时间时得到减号
- 使用 GetProcessTimes 测量 Windows 上的 CPU 时间