2014-09-19 61 views
0

我试图使用MPI_Wtime()来测试总运行时间,但是我得到不正确的输出。MPI运行时给出不正确的输出

#include "mpi.h" 
#include <stdio.h> 
#include <stdlib.h> 
#include <Windows.h> 

int main(int argc, char *argv[]) 
{ 
    int rank, size, len; 
    double start, end, runtime; 
    char name[MPI_MAX_PROCESSOR_NAME]; 

    MPI_Init(&argc, &argv); 
    MPI_Barrier(MPI_COMM_WORLD); 

    MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
    MPI_Comm_size(MPI_COMM_WORLD, &size); 
    MPI_Get_processor_name(name, &len); 
    start = MPI_Wtime(); 

    printf("Hello World! I am %d of %d on %s\n", rank, size, name); 
    fflush(stdout); 
    Sleep(1000); 
    MPI_Barrier(MPI_COMM_WORLD); 
    end = MPI_Wtime(); 

    MPI_Finalize(); 

    if (rank == 0) 
    { 
     runtime = end - start; 
     printf("Start time = %d\n", start); 
     printf("End time = %d\n\n", end); 
     printf("Runtime = %d\n", runtime); 
    } 

    system("pause"); 
    return 0; 
} 

我得到的输出是这样的:

的Hello World!我在测试
开始时间是1 0 = 505400631
结束时间= 521122106

运行时间= -1878261760
按任意键继续。 。 。

我期待得到秒,但我得到这些长输出。任何想法为什么?

+0

下次您提交问题时,请将实际文字替换为您程序输出的图像。在很多情况下很难看到这个图像,只有真正的输出才更好。 – 2014-09-20 17:21:47

回答

2

MPI_Wtime()返回doublestartendruntime是双。通过做printf("Start time = %d\n", start);您正在打印start作为一个整数。

main.c:34:9: attention : format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat] 

解决的办法是打印这些数字作为double

printf("Runtime = %g\n", runtime); 

我用gcc(并通过<unistd.h>sleep更换的<Windows.h>Sleep),并出现以下警告符合你的代码

system("pause")不是必需的,我认为在打印操作完成后,最好在程序的最后部分移动MPI_Finalize()