jsan
jsan

Reputation: 1057

MPI runtime giving incorrect output

I'm trying to test the total runtime using MPI_Wtime() however I'm getting incorrect outputs.

#include "mpi.h"
#include <stdio.h>  
#include <stdlib.h>
#include <Windows.h>

int main(int argc, char *argv[])
{
   int rank, size, len;
   double start, end, runtime;
   char name[MPI_MAX_PROCESSOR_NAME];

   MPI_Init(&argc, &argv);
   MPI_Barrier(MPI_COMM_WORLD);

   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
   MPI_Comm_size(MPI_COMM_WORLD, &size);
   MPI_Get_processor_name(name, &len);
   start = MPI_Wtime();

   printf("Hello World! I am %d of %d on %s\n", rank, size, name);
   fflush(stdout);
   Sleep(1000);
   MPI_Barrier(MPI_COMM_WORLD);
   end = MPI_Wtime();

   MPI_Finalize();

   if (rank == 0)
   {
        runtime = end - start;
        printf("Start time = %d\n", start);
        printf("End time = %d\n\n", end);
        printf("Runtime = %d\n", runtime);
   }

   system("pause");
   return 0;
}

I get outputs like this:

Hello World! I am 0 of 1 on test
Start time = 505400631
End time = 521122106

Runtime = -1878261760
Press any key to continue . . .

I'm expecting to get seconds, but I'm getting these long outputs instead. Any idea why?

Upvotes: 0

Views: 426

Answers (1)

francis
francis

Reputation: 9817

MPI_Wtime() returns a double and start , end and runtime are double. By doing printf("Start time = %d\n", start); you are printing start as an integer.

I complied your code using gcc (and replacing Sleep of <Windows.h> by sleep of <unistd.h>) and the following warning appears :

main.c:34:9: attention : format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]

The solution is to print these numbers as double :

 printf("Runtime = %g\n", runtime);

The system("pause") is not necessary and i think it would advisable to move MPI_Finalize() at the very end of your program, after the printing operations.

Upvotes: 2

Related Questions