Timing in MPI Tarik Booker MPI Presentation May 7, 2003
What we will cover… How to time programs Sample Programs My Samples MPI Pi Program
How to Time Programs Very Easy Very Easy Simply use function Simply use functionMPI_Wtime()
MPI_Wtime() Null input Null input Returns a Long Float (double) Returns a Long Float (double) Not like UNIX “clock” function Not like UNIX “clock” function MPI_Wtime() is somewhat arbitrary Starts depending on node
How to Time Code Must use time blocks Must use time blocks Start time End time Time for your code is simply end_time – start_time.
Example #include<stdio.h>#include<mpi.h> main(int argc, char **argv) { int size, node; int size, node; double start, end; double start, end; MPI_Init(&argc, &argv); MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &node); MPI_Comm_rank(MPI_COMM_WORLD, &node); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_size(MPI_COMM_WORLD, &size); start = MPI_Wtime(); start = MPI_Wtime(); if(node==0) if(node==0) { printf(" Hello From Master. Time = %lf \n", MPI_Wtime() - start); printf(" Hello From Master. Time = %lf \n", MPI_Wtime() - start); //Count number of ticks //Count number of ticks } else else { printf("Hello From Slave #%d %lf \n", node, (MPI_Wtime() - start)); printf("Hello From Slave #%d %lf \n", node, (MPI_Wtime() - start)); } MPI_Finalize(); MPI_Finalize(); }
Example (2) #include<stdio.h>#include<mpi.h> main(int argc, char **argv) { int size, node; int size, node; int serial_counter = 0; int serial_counter = 0; double start, end; double start, end; MPI_Init(&argc, &argv); MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &node); MPI_Comm_rank(MPI_COMM_WORLD, &node); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Barrier(MPI_COMM_WORLD); MPI_Barrier(MPI_COMM_WORLD); start = MPI_Wtime(); start = MPI_Wtime(); MPI_Barrier(MPI_COMM_WORLD); MPI_Barrier(MPI_COMM_WORLD); if(node==0) if(node==0) { printf("Hello From Node #%d %lf \n", node, (MPI_Wtime() - start)); printf("Hello From Node #%d %lf \n", node, (MPI_Wtime() - start)); } else else { printf("Hello From Slave #%d #%lf \n", node, (MPI_Wtime() - start)); printf("Hello From Slave #%d #%lf \n", node, (MPI_Wtime() - start)); } MPI_Finalize(); MPI_Finalize();}
Pi Example Uses MPI to compute value of Pi using formula: Uses MPI to compute value of Pi using formula:
Pi Example (2) start_time = MPI_Wtime(); start_time = MPI_Wtime(); MPI_Bcast(&n, 1, MPI_INT, host_rank, MPI_COMM_WORLD); MPI_Bcast(&n, 1, MPI_INT, host_rank, MPI_COMM_WORLD); end_time = MPI_Wtime(); end_time = MPI_Wtime(); communication_time = end_time - start_time; communication_time = end_time - start_time;
Pi Example (3) start_time = MPI_Wtime(); h = 1.0 / (double) n; h = 1.0 / (double) n; sum = 0.0; for (i = my_rank + 1; i <= n; i += pool_size) { x = h * ((double)i - 0.5); sum += f(x); } mypi = h * sum; end_time = MPI_Wtime(); computation_time = end_time - start_time;
Pi Example (4) start_time = MPI_Wtime(); MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, host_rank, MPI_COMM_WORLD); end_time = MPI_Wtime(); communication_time = communication_time + end_time - start_time; communication_time = communication_time + end_time - start_time;