2014-09-19 60 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()