我有一个程序(用加农子算法计算矩阵乘法),在MPI中为C实现,我设置了一个时钟来查看这个程序的总时间,总我指的是所有的进程和。
但结果是,我得到了每个过程的时间。
在我的主代码的开头部分代码:
clock_t begin, end;
double time_spent;
begin = clock();
/* Initializing */
MPI_Init (&argc, &argv);然后,在我的代码末尾,我有:
MPI_Finalize();
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("\n\nTIME: %f SECONDS\n\n", time_spent);发布于 2015-10-21 13:11:36
假设您想要的是每个流程的单个时间之和,则需要:
MPI_Finalize()之前,移动对时间结束的测量,因为您需要额外的沟通;MPI_Reduce()收集单个时间以处理#0 (例如);最后除此之外,除非您有一个令人信服的理由不这样做,否则我鼓励您使用MPI_Wtime()作为计时器,而不是某种误导的clock()计时器。
然后,代码可以如下所示:
int main( int *argc, char* argv[] ) {
MPI_Init( &argc, &argv );
double tbeg = MPI_Wtime();
// a lot of stuff here
// you might want a barrier here...
// MPI_Barrier( MPI_COMM_WORLD );
double elapsedTime = MPI_Wtime() - tbeg;
double totalTime;
MPI_Reduce( &elapsedTime, &totalTime, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD );
if ( rank == 0 ) {
printf( "Total time spent in seconds id %f\n", totalTime );
}
MPI_Finalize();
return 0;
}发布于 2015-10-21 12:45:19
是的,你得到的当然是正确的。
如果您想要总时间,即在每个进程中花费的时间之和,然后发送主节点中每个节点的本地time_spent,执行本地time_spent的求和并打印出来。
https://stackoverflow.com/questions/33259582
复制相似问题