I created an array and I want to send it using MPI. The first part works well (I think?), But I have problems with the second part. I assume this is part of the trick and how can I allocate an array? Please see:
if (myrank > 1)
{
int rows = 5;
int cols = 4;
int realcount = 0;
int (*sendarray)[cols] = malloc(sizeof *sendarray * rows);
for (int j = 0; j < 100; ++j)
{
for (int k = 0; k < 100; ++k)
{
for (int l = 0; l < 100; ++l)
{
if(realcount==rows){
int newnum = (rows + 2) * 2;
int (*newptr)[cols] = realloc(sendarray, sizeof *newptr * newnum);
rows = newnum;
sendarray = newptr;
}
if(checks)
{
sendarray[realcount][0] = j;
sendarray[realcount][1] = k;
sendarray[realcount][2] = l;
sendarray[realcount][3] = max;
++realcount;
}
}
}
}
MPI_Request req;
MPI_Isend(sendarray, realcount, MPI_INT, 1, 1, MPI_COMM_WORLD, &req);
MPI_Wait(&req, MPI_STATUS_IGNORE);
}else if(myrank == 1){
for () {
i = task thats going to send data
int amount = 0;
int cols = 4;
MPI_Status status;
MPI_Probe(i, 1, MPI_COMM_WORLD, &status);
MPI_Get_count(&status, MPI_INT, &amount);
int (*recv_buf)[cols] = malloc(sizeof *recv_buf * amount);
MPI_Recv(recv_buf, amount, MPI_INT, i, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for (int var = 0; var < amount; ++var) {
fprintf(fp, "{ \"x\": %d, \"y\": %d, \"z\": %d, \"value\": %d },", recv_buf[var][0], recv_buf[var][1], recv_buf[var][2], recv_buf[var][3]);
}
free(recv_buf);
}
}
Thank you very much for your help. I have taken a look at https://stackoverflow.com/a/166164/169 and ...
source
share