2015-09-27 48 views
0

目標是測量運行時間與#個進程。MPI中的一般運行時間測量C

我只是MPI的初學者,遇到困難。

我寫了一個hello world程序,想測試全局運行時。

我試過使用屏障,以確保所有進程在測量系統時間之前終止,但我得到了分段錯誤。

我的代碼:

#include <mpi.h> 
#include <stdio.h> 
int main(int argc, char *argv[]) { 
    double time1, time2; 
    double duration=0.0000; 
    int npes, myrank; 
    time1 = clock(); 
    MPI_Init(&argc, &argv); 
    MPI_Comm_size(MPI_COMM_WORLD, &npes); 
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank); 
    printf("From process %d out of %d, Hello World!\n", myrank, npes); 
    time2 = clock(); 
    if (time2-time1>duration) { 
    duration = time2-time1; 
    } 
    duration = time2-time1; 
    MPI_BARRIER(MPI_COMM_WORLD); 
    printf("runtime is %f ", duration); 
    MPI_Finalize(); 
    return 0; 
} 

幫我找出爲什麼我收到分段錯誤?

+3

C是區分大小寫的。 MPI_BARRIER將在鏈接時抓取Fortran符號。 – Jeff

+0

僅供參考這是用mpicc編譯並由mpirun運行 –

回答

1

我可以從代碼中注意到的第一件事是,您已經測量了MPI_Barrier之前的時間,這意味着甚至可以在所有進程打印「hello world"」之前測量運行時間。爲確保正確性,一個MPI_Barrier

你也可能要使用MPI_Wtime()來衡量一個MPI過程經歷的時間。

您的代碼將只打印運行在每一臺機器,計算全球運行時,你將不得不使用MPI_Reduce。這個函數將計算指定的操作(在這種情況下是MAX)和stor e根源上的結果。

因此,這裏是你的代碼應該是什麼樣子:

#include <mpi.h> 
#include <stdio.h> 
int main(int argc, char *argv[]) { 
    double time1, time2,duration,global; 
    int npes, myrank; 
    MPI_Init(&argc, &argv); 
    time1 = MPI_Wtime(); 
    MPI_Comm_size(MPI_COMM_WORLD, &npes); 
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank); 
    printf("From process %d out of %d, Hello World!\n", myrank, npes); 
    MPI_Barrier(MPI_COMM_WORLD); 
    time2 = MPI_Wtime(); 
    duration = time2 - time1; 
    MPI_Reduce(&duration,&global,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD); 
    if(myrank == 0) { 
     printf("Global runtime is %f\n",global); 
    } 
    printf("Runtime at %d is %f \n", myrank,duration); 
    MPI_Finalize(); 
    return 0; 
} 
+0

謝謝@Pooja,我完全明白你的觀點,你是一位好老師! –