2

I'm trying to send a number to p-1 processes. Process 0 sends this value to all other processes. I use an MPI_SEND Command to do this. When I explicitly write out MPI_SEND commands for 3 processes, it works fine. But when I want to put it in a loop, it gives me the output as well as a segmentation fault code. Here is my code:

#include <stdlib.h>
#include <mpi.h>
#include "a1.h"

//AUTHORS
//LAKSHAN SIVANANTHAN - 1150161
//RAZMIG PAPISSIAN - 1152517

int main(int argc, char** argv)
{
  RGB *image;
  int width, height, max;
  int windowLength = atoi(argv[3]);
  int my_rank, p, local_height, source, i;

  int dest;

  MPI_Status status;

  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD, &p);
  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

  int *processorRows;
  processorRows = (int*)malloc(sizeof(int)*(p+1));

  if (my_rank == 0) {

    printf("Process %d is reading...\n", my_rank);
    image = readPPM(argv[1], &width, &height, &max); 

    //calculate rows to each process

    for (i=0; i<p; i++) {

      processorRows[i] = height/p;

    }

    for (i=0; i< height%p; i++){

      processorRows[i]++;
    }

    for (dest=1; dest<p; dest++) {

      MPI_Send(processorRows + dest, 1, MPI_INT, dest, 0, MPI_COMM_WORLD);
      //MPI_Send(processorRows + 2, 1, MPI_INT, 2, 0, MPI_COMM_WORLD);
      //MPI_Send(processorRows + 3, 1, MPI_INT, 3, 0, MPI_COMM_WORLD);
    }

  }
  else {

    MPI_Recv(processorRows, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
    printf("I am Process %d and will run %d rows...\n", my_rank, *processorRows);

  } 

  //processImage(width, height, image, windowLength);
  //writePPM(argv[2], width, height, max, image);

  free(image);
  free(processorRows);

  MPI_Finalize();
  return(0);

}

If I were to remove the for loop, replace "dest" with 1, and uncomment the other 2 MPI_SEND lines, it works completely fine when running mpirun -np 4 ./program

Not sure what's going on here...

2 Answers2

0

I'm not exactly sure what you are trying to accomplish. But, from the statement

Process 0 sends this value to all other processes.

and from the part of the code, I would expect you to do a scatter from Process-0 to all other PEs rather than this send-receive loop tricks.

Remove all the send-receive pairs and remove the loops, just use a single scatter operation. Here is the link for MPI_Scatter operation https://www.open-mpi.org/doc/v1.8/man3/MPI_Scatter.3.php. If you are unsure about the scatter operation, have a look at this neat explanation http://mpitutorial.com/tutorials/mpi-scatter-gather-and-allgather/

It looks like, the size of processorRows array is the size of the total number of Process used. And, you are trying to send each element of this processorRows array to all other ranks. Hence, your code should look something like this one below:

int *processorRows;
processorRows = (int*)malloc(sizeof(int)*(p+1));

if (my_rank == 0) {
    printf("Process %d is reading...\n", my_rank);
    image = readPPM(argv[1], &width, &height, &max); 

    for (i=0; i<p; i++) {
        processorRows[i] = height/p;
    }

    for (i=0; i< height%p; i++){
        processorRows[i]++;
    }
}   
MPI_Scatter(processorRows, 1, MPI_INT, processorRows, 1, MPI_INT, 0, MPI_COMM_WORLD);
naveen-rn
  • 98
  • 1
  • 6
0

I removed the

#include "a.h"

and

image = readPPM(argv[1], &width, &height, &max); 

since I do not have these classes, set the height manually to 10 and the code worked. Maybe the problem is with height variable?

Vyacheslav
  • 113
  • 1
  • 9
  • Though this suggests us the possible source of this error; there are other things in terms of using MPI, are fundamentally unnecessary in the first place; – naveen-rn Feb 09 '16 at 17:57
  • 1. Missing to use the MPI_Collectives(scatter in this case) and trying to implement a hand-coded algorithm with send-receive 2. I don't even know why he should maintain an array of *processorRows*, essentially he just need to send the height from *rank==0* to every PEs and each PE can calculate its own *local_height*. These two suggestions can completely remove the cause for this error. – naveen-rn Feb 09 '16 at 18:03
  • And, also we should remember that the author says *When I explicitly write out MPI_SEND commands for 3 processes, it works fine.*, so if this statement is actually correct, then we getting *height* from *image* should also be correct !!! – naveen-rn Feb 09 '16 at 18:06