Skip to content

Instantly share code, notes, and snippets.

@keisukefukuda
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keisukefukuda/f91eda2cf1fffdb31e0c to your computer and use it in GitHub Desktop.
Save keisukefukuda/f91eda2cf1fffdb31e0c to your computer and use it in GitHub Desktop.
計算機システム MPIサンプル [7]
/*
* 計算機システム MPIサンプル [7]
* MPI_Barrierとgettimeofdayの時間計測の例
* 各ノードで独立に整数のソートを行って、MPI_Barrier() で同期を行って時間を計測する
* (結果的に、「一番時間がかかったプロセスの時間 + 同期コスト」の時間を計測することになる)
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <mpi.h>
#include <unistd.h>
#include <sys/time.h>
#define LEN ((int)1e7)
int comp(const void *a, const void *b) {
return *(int*)a - *(int*)b;
}
int main(int argc, char **argv) {
int i;
int *data = NULL;
struct timeval beg, end;
int rank;
srand(time(NULL));
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
data = malloc(sizeof(data[0]) * LEN);
for (i = 0; i < LEN; i++) {
data[i] = rand();
}
/* MPI_Barrierして計測開始。Barrierとgettimeofdayの位置関係に注意。
同期の待ち時間は含めず、計算時間は全て含める必要がある */
MPI_Barrier(MPI_COMM_WORLD);
gettimeofday(&beg, NULL);
qsort(data, LEN, sizeof(data[0]), comp);
MPI_Barrier(MPI_COMM_WORLD);
gettimeofday(&end, NULL);
if (rank == 0) {
double elapsed_time = end.tv_sec - beg.tv_sec + (end.tv_usec - beg.tv_usec) * 1e-6;
printf("Elapsed time: %.3f [s]\n", elapsed_time);
}
MPI_Finalize();
free(data);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment