I have one problem at that point when I tried to split an array into some subarrays.
To be more exactly I have an array, let's say int a[10]={1,3,2,7,8,12,5,7,68,10}
and I'm running my program on X process (in this moment I'm using 8 but could be more or less).
And I want to sent to each process on part of this array, for example for my array in this moment each process will receive something like process0 = {1, 3}
, process2 = {2, 7}
and so on.. until process7 = 68, 10
.
After I've send each subarray I will do some operations on each subarray and after I want to merge all my subarrays into one back.
I've search on google a lot and I saw some example using MPI_Send
and MPI_Recv
or MPI_Scatter
and MPI_Gather
and I've tried some methods but everything I've tried... was without success and I receive errors or
null pointer...
My Code:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define N 32
int A[N];
int main(int argc, char *argv[]) {
int size;
int rank;
const int ROOT = 0;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int count = N / (size - 1);
int *localArray = (int *) malloc(count * sizeof(int));
if (rank == ROOT) {
for (int i = 0; i < N; i++) {
A[i] = rand() % 10;
}
for (int dest = 1; dest < size; ++dest) {
MPI_Send(&A[(dest - 1) * count], count, MPI_INT, dest, tag, MPI_COMM_WORLD);
printf("P0 sent a %d elements to P%d.\n", count, dest);
}
for (int source = 1; source < size; source++) {
MPI_Recv(localArray, count, MPI_INT, source, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
//--------------------------------MERGE THE ALL RESULTS INTO A SORTED ARRAY-------------------------------------
printf("Received results from task %d\n", source);
}
}
else {
MPI_Recv(localArray, count, MPI_INT, ROOT, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
//---------------SORT THE localArray-------------------
MPI_Send(localArray, count, MPI_INT, ROOT, tag, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}
What ever I've tried I can't get results where I've put the comment, what I'm doing wrong