[英]Communication cost of MPI send and receive
我是MPI的新手,想衡量兩個節點之間MPI_Send和MPI_Recv的通信成本。 為此,我編寫了以下代碼:
/*==============================================================
* print_elapsed (prints timing statistics)
*==============================================================*/
void print_elapsed(const char* desc, struct timeval* start, struct timeval* end, int numiterations) {
struct timeval elapsed;
/* calculate elapsed time */
if(start->tv_usec > end->tv_usec) {
end->tv_usec += 1000000;
end->tv_sec--;
}
elapsed.tv_usec = end->tv_usec - start->tv_usec;
elapsed.tv_sec = end->tv_sec - start->tv_sec;
printf("\n%s total elapsed time = %ld (usec)\n",
desc, (elapsed.tv_sec*1000000 + elapsed.tv_usec)/numiterations );
}
int main(int argc, char **argv) {
int nprocs, nElements; /* command line args */
int my_id;
long double* buffer, *rec_buffer;
/* gettimeofday stuff */
struct timeval start, end; /* gettimeofday stuff */
struct timezone tzp;
MPI_Status status; /* Status variable for MPI operations */
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_id); /* Getting the ID for this process */
/*---------------------------------------------------------
* Read Command Line
* - check usage and parse args
*---------------------------------------------------------*/
if(argc < 2) {
if(my_id == 0)
printf("Usage: %s [nElements]\n\n", argv[0]);
MPI_Finalize();
exit(1);
}
nElements = atoi(argv[1]);
int numiterations = 64;
MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* Get number of processors */
if(my_id == 0)
printf("\nExecuting %s: numElements=%d \n",
argv[0], nElements);
buffer = (long double *) malloc(sizeof(long double)*nElements);
rec_buffer = (long double *) malloc(sizeof(long double)*nElements);
if(buffer == NULL) {
printf("Processor %d - unable to malloc()\n", my_id);
MPI_Finalize();
exit(1);
}
MPI_Barrier(MPI_COMM_WORLD);
if(my_id == 1)
gettimeofday(&start, &tzp);
for(int i = 0 ; i < numiterations ; ++i)
{
if(my_id == 0)
MPI_Send(buffer, nElements, MPI_LONG, 1, 0, MPI_COMM_WORLD);
if(my_id == 1)
MPI_Recv(rec_buffer, nElements, MPI_LONG, 0, 0, MPI_COMM_WORLD, &status);
}
if(my_id == 1) {
gettimeofday(&end,&tzp);
}
MPI_Barrier(MPI_COMM_WORLD);
if(my_id == 1) {
print_elapsed("Summation", &start, &end, numiterations);
}
free(buffer);
MPI_Finalize();
return 0;
} /* main() */
我重復發送和接收numiteration
次,但我不知道初始化成本和實際的通信時間的信息。 我想知道是否有更好的方法或工具來更詳細地衡量通信成本。
如果需要描述的詳細程度,則可能必須深入了解MPI庫本身的實現。 您要衡量的是通信對應用程序的影響。 但是,與您的基礎架構相比,通信中涉及的可能更多。 一些網絡可以在不涉及應用程序的情況下取得進展,而某些MPI庫也可以使用線程來異步處理消息。
您如何衡量這些事情將取決於您的系統和上述約束。 如果您所關心的只是您的應用程序在通信通話中花費了多少時間,那么您或多或少就已經做到了。 您可以使用其他跟蹤工具來完成類似的事情(HPCtoolkit是我過去使用過的一個工具)。
如果要獲取有關幕后情況的更多詳細信息,則必須在實現內部進行深入研究,並開始進行內部檢測(假設您使用的是MPICH或Open MPI之類的開源實現)。 這是一個涉及更多的過程,其機制將從一個實現變為另一個實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.