我正在研究MPI中的並行矩陣 - 矩陣乘法器。我有計算部分工作,但我也想計算CPU時間。由於看起來有些進程報告的開始和結束時間爲0,並且對於應該在第二個(小型矩陣)下執行的任務,程序報告了1000多秒的CPU時間(即使我知道它運行了在觀察的第二位)。這是我目前正在做的:使用MPI計算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()'。你可以在C++ 11中使用'chrono'或者在C++ 11中使用它的Boost實現。 –