0

I am writing code with mpi in c++, and I have done this:

#include <stdio.h>
#include "mpi.h"
#define NMAX 100
#define NTIMES 10
int main(int argc, char **argv)
{
int rank, size, i, n, lmax;
double time_start, time, bandwidth, max, a[NMAX];
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
time_start = MPI_Wtime();
n = 0;
max = 0.0;
lmax = 0;
while(n<=NMAX){
  time_start = MPI_Wtime();
  for(i = 0; i<NTIMES; i++){
     if(rank==0){
        MPI_Send(a, n, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD);
        MPI_Recv(a, n, MPI_DOUBLE, 1, 1, MPI_COMM_WORLD, &status);
     }
     if(rank==1){
        MPI_Recv(a, n, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD, &status);
        MPI_Send(a, n, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD);
     }
  }
  time = (MPI_Wtime()-time_start)/(2*NTIMES);
  bandwidth = (sizeof(double)*n*1.0/(1024*1024*time));
  if(max<bandwidth){
     max = bandwidth;
     lmax = sizeof(double)*n;
  }
  if(rank==0)
     if(n==0) printf("latency = %lf seconds\n", time);
     else printf("%d bytes, bandwidth = %lf Mb/s\n", (int)sizeof(double)*n, bandwidth);
  if(n==0) n = 1;
  else n = 2*n;
  }
  if(rank==0) printf("max bandwidth = %lf Mb/s length = %d bytes\n", max, lmax);
  MPI_Finalize();
   }

It shows no errors, but when i am trying to run the code, this what i have.. [1]: https://i.stack.imgur.com/tl0HF.png

Maybe someone knows hov can i fix it?

  • 1
    It seems you have to run your code using 2 processes. How did you run your code? – MikeCAT Nov 27 '20 at 15:45
  • I am using codeblocks, so just by "build and run". – Justyna Nov 27 '20 at 15:49
  • 1
    @Justyna You cannot "just run" an MPI program. You need to use an MPI launcher (mpiexec, mpirun, aprun, etc.). Check the documentation of your MPI implementation (such as OpenMPI, MPICH, Intel MPI, Cray MPI, ...). – Daniel Langr Nov 27 '20 at 15:54
  • @Daniel Langr i have MPICH2, but i do not know how to run this code using 2 processes – Justyna Nov 27 '20 at 16:00
  • @Justyna Then read MPICH2 documentation. – Daniel Langr Nov 27 '20 at 16:03
  • Related question: [Setting Code Block to run MPICH2](https://stackoverflow.com/q/5141042/580083). – Daniel Langr Nov 27 '20 at 16:05
  • The related question doesn't actually deal with running an MPI project under Code::Blocks. [This one](https://stackoverflow.com/q/24356286/1374437) does. – Hristo Iliev Nov 28 '20 at 22:42
  • What's happening is that when your simply run an MPI executable, it does the so-called *singleton MPI_INIT* and behaves as a 1-rank MPI job, i.e., as if launched with `mpiexec -n 1 `. You need to tell Code::Blocks to not run the executable directly but rather run it through `mpiexec`. – Hristo Iliev Nov 28 '20 at 22:47

0 Answers0