当我使用MPI_Wtime()记录计算时间时,它返回几个奇怪的数字,这似乎不正确。我的代码非常简单:
#include <stdio.h>
#include "mpi.h"
int main(int argc,char *argv[])
{
int i,sum,total;
int size,rank,start,end,sumGlobal;
double t1,t2;
total = 10000;
t1 = MPI_Wtime();
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
start = rank*(total/size)+1;
if (rank==(size-1))
{
end = total;
}
else
{
end = start+(total/size)-1;
}
sumGlobal = 0;
sum = 0;
for (i=start;i<=end;i++)
{
sum += i;
}
MPI_Reduce(&sum,&sumGlobal,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
t2 = MPI_Wtime();
printf("rank = %d,sum = %d,sumGlobal = %d,time=%f\n",rank,sum,sumGlobal,t2-t1);
MPI_Finalize();
return 0;
}
运行结果是(使用16个核心):
rank = 0,sum = 195625,sumGlobal = 50005000,time = 1520044462.143606
rank = 1,sum = 586250,sumGlobal = 0,time = 1520044462.143662
rank = 3,sum = 1367500,sumGlobal = 0,time = 1520044462.143665
rank = 4,sum = 1758125,sumGlobal = 0,time = 1520044462.143610
rank = 5,sum = 2148750,sumGlobal = 0,time = 1520044462.143651
rank = 6,sum = 2539375,sumGlobal = 0,time = 1520044462.143608
rank = 7,sum = 2930000,sumGlobal = 0,time = 1520044462.143604
rank = 9,sum = 3711250,sumGlobal = 0,time = 1520044462.143647
rank = 10,sum = 4101875,sumGlobal = 0,time = 1520044462.143655
rank = 11,sum = 4492500,sumGlobal = 0,time = 1520044462.143606
rank = 12,sum = 4883125,sumGlobal = 0,time = 1520044462.143614
rank = 13,sum = 5273750,sumGlobal = 0,time = 1520044462.143672
rank = 14,sum = 5664375,sumGlobal = 0,time = 1520044462.143643
rank = 15,sum = 6055000,sumGlobal = 0,time = 1520044462.143605
rank = 2,sum = 976875,sumGlobal = 0,time = 1520044462.143665
rank = 8,sum = 3320625,sumGlobal = 0,time = 1520044462.143652
正如您所看到的,“时间”值非常奇怪。那么如何解决呢?谢谢。
答案 0 :(得分:1)
您的程序与MPI标准不一致:
您无法在 MPI_Wtime()
之前使用MPI_Init()
(也不能在MPI_Finalize()
fwiw之后使用。
移动t1
初始化应解决问题。