MPI 計時器函數 MPI_Wtime()

▶ 計時器函數 MPI_Wtime() 以及頭文件 timer.h 中的宏函數 GET_TIME(),相似 time.h 中函數 clock() 的用法。注意 函數 clock() 記錄的是CPU時間(包含用戶代碼,庫函數,系統調用耗時,不包括空閒等待時間),而 MPI_Wtime() 和 GET_TIME()記錄的是牆上時間(在 CPU 時間的基礎上包括了空閒等待時間)。函數

● MPI_Wtime() 使用範例spa

 1 {  2     const int globalSize = 8;  3     int globalDataInt[globalSize], i, comSize, comRank, sumInt;  4     double time;  5 
 6     MPI_Init(&argc, &argv);  7     MPI_Comm_size(MPI_COMM_WORLD, &comSize);  8     MPI_Comm_rank(MPI_COMM_WORLD, &comRank);  9 
10     if (comRank == 0) 11         for (i = 0; i < globalSize; globalDataInt[i] = i, i++); 12 
13     time = MPI_Wtime();         // 開始計時
14     MPI_Bcast(globalDataInt, globalSize, MPI_DOUBLE, 0, MPI_COMM_WORLD); 15     for (i = sumInt = 0; i < globalSize; sumInt += globalDataInt[i++]); 16     time = MPI_Wtime() - time;  // 終止計時
17     
18     printf("Size = %d, rank = %d, resultInt = %d, time = %f ms\n", comSize, comRank, sumInt, time * 1000); 19  MPI_Finalize(); 20     return 0; 21 }

● 輸出結果:code

D:\Code\MPI\MPIProjectTemp\x64\Debug>mpiexec -n 8 -l MPIProjectTemp.exe [3]Size = 8, rank = 3, resultInt = 28, time = 0.497859 ms [5]Size = 8, rank = 5, resultInt = 28, time = 0.466163 ms [2]Size = 8, rank = 2, resultInt = 28, time = 0.459093 ms [1]Size = 8, rank = 1, resultInt = 28, time = 0.442270 ms [0]Size = 8, rank = 0, resultInt = 28, time = 0.385950 ms [4]Size = 8, rank = 4, resultInt = 28, time = 0.437638 ms [7]Size = 8, rank = 7, resultInt = 28, time = 0.574171 ms [6]Size = 8, rank = 6, resultInt = 28, time = 0.542719 ms

 

● GET_TIME() 使用範例blog

 1 #include <stdio.h>
 2 #include "timer.h"
 3 #include <mpi.h>
 4 
 5 int main(int argc, char *argv[])  6 {  7     const int globalSize = 8;  8     int globalDataInt[globalSize], i, comSize, comRank, sumInt;  9     double timeStart,timeEnd; 10 
11     MPI_Init(&argc, &argv); 12     MPI_Comm_size(MPI_COMM_WORLD, &comSize); 13     MPI_Comm_rank(MPI_COMM_WORLD, &comRank); 14 
15     if (comRank == 0) 16         for (i = 0; i < globalSize; globalDataInt[i] = i, i++); 17 
18     GET_TIME(timeStart);         // 開始計時
19     MPI_Bcast(globalDataInt, globalSize, MPI_DOUBLE, 0, MPI_COMM_WORLD); 20     for (i = sumInt = 0; i < globalSize; sumInt += globalDataInt[i++]); 21     GET_TIME(timeEnd);          // 終止計時
22 
23     printf("Size = %d, rank = %d, resultInt = %d, time = %f ms\n", comSize, comRank, sumInt, (timeEnd - timeStart) * 1000); 24  MPI_Finalize(); 25     return 0; 26 }

● timer.h 的主要內容,個人 time.h 中沒有結構體 struct timeval,沒法運行上面的代碼get

1 #ifndef _TIMER_H_ 2 #define _TIMER_H_
3 
4 #include <time.h>
5 
6 // 注意參數 now 是 double 類型而不是 double* 類型
7 #define GET_TIME(now) {struct timeval t; gettimeofday(&t, NULL); now = t.tv_sec + t.tv_usec/1000000.0; }
8 
9 #endif
相關文章
相關標籤/搜索