# MPI Sorting and Prefix Sum

This is a discussion on MPI Sorting and Prefix Sum within the C Programming forums, part of the General Programming Boards category; New to MPI and C, I have used the pseudo code for the sorting algorithms and this is what I ...

1. ## MPI Sorting and Prefix Sum

New to MPI and C, I have used the pseudo code for the sorting algorithms and this is what I have constructed. I know its not perfect but I'm searching for help on how to fix it.

Code:
```#include <stdio.h>
#include <mpi.h>
void PrefixSum();
int id, rank, nprocs;
int size;
int main(int argc, char** argv)
{
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Status status;

size = rank/nprocs;

if (id == 0)
{
for(i=0;i<nprocs;i++)
MPI_Send(id, size, MPI_INT, i, 0, MPI_COMM_WORLD);
}
else
{
MPI_Recv(id, size, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
}
MPI_Barrier(MPI_COMM_WORLD);

size = rank/nprocs;
partner_id = id + size(log(2));
MPI_Reduce(&result, &rank, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (id == 0)
MPI_Barrier(MPI_COMM_WORLD);

return 0;
MPI_Finalize();

}
void PrefixSum(int id, int number, partner_id, result)
{
int d, partner_id, id, number, result;

int size;
result = number;
message = result;

for (i=0; i<=(size/log(2)); i++)
{
MPI_Recv(partner_id, 1, MPI_INT, predec, 0, MPI_COMM_WORLD, &status);

if (partner_id == 0)
{
MPI_Send(partner_id, message, MPI_INT, i, 0, MPI_COMM_WORLD);

else (partner_id == 1)
{
MPI_Recv(number, partner_id, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
}
}

message += number;

if(partner_id < id)
{
result += number;
MPI_Send(partner_id, result, MPI_INT, i, 0, MPI_COMM_WORLD);
}
}
}```

2. Originally Posted by Newby2012
New to MPI and C
How about learning the basics of C before trying to learn MPI?

Even without knowing anything about MPI, I've noticed several syntax errors. Take a look at a C Tutorial (e.g. the one on this site) and come back with specific questions.

Include all the errors/warnings you get when compiling the code in your posts too.

Bye, Andreas

Popular pages Recent additions