0

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?

1
  • Next time you submit a question, please put in the actual text instead of an image with the output of your program. It's very hard to view that image in a lot of situations and it's much better to just have the real output. Commented Sep 20, 2014 at 17:21

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.