As Hristo Iliev pointed out, the array sendBuf should be the argument of MPI_Send. It works the same way for table[i].
Another fact : MPI_Send and MPI_Recv do not allocate memory. These functions just copy a message from one place to another. Both sendBuff and table[i] should be allocated previously. And writting table[0]=sendBuff would therefore trigger a memory leak.
Here is a code that may help you :
#include <iostream>
#include <mpi.h>
using namespace std;
int main(int argc, char* argv[])
{
int *sendBuff;
int **table;
int size, rank;
MPI_Status stat;
int pass = 1;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
sendBuff = new int[10];
printf("firts task %d passed %d\n", rank, pass); //1
pass++;
if (rank == 0)
{
table = new int*[size];
}
for (int i = 0; i < 10; i++)
{
sendBuff[i] = rank;
}
printf("second task %d passed %d\n", rank, pass); //2
pass++;
if (rank != 0)
{
MPI_Send(sendBuff, 10, MPI_INT, 0, rank, MPI_COMM_WORLD);
}
printf("thrid task %d passed %d\n", rank, pass); //3
pass++;
if (rank == 0)
{
table[0]=new int[10];
for(int i=0;i<10;i++){
table[0][i]=sendBuff[i];
}
// table[0] = sendBuff;
for (int i = 1; i < size; i++)
{
table[i]=new int[10];
MPI_Recv(table[i], 10, MPI_INT, i, i, MPI_COMM_WORLD, &stat);
}
}
printf("fourth task %d passed %d\n", rank, pass); //4
pass++;
if (rank == 0)
{
for (int i = 0; i < size; i++)
{
delete [] table[i];
table[i]=NULL;
}
delete [] table;
}
delete [] sendBuff;
MPI_Finalize();
return 0;
}
A function that may help you : MPI_Gather(...). It seems to be what you are looking for ! Watch for memory allocation if you want to use it : all the values of table should be allocated as one contiguous chunk of memory.
http://www.mcs.anl.gov/research/projects/mpi/www/www3/MPI_Gather.html
Bye,
Francis