I have a table "tab[]" with 10 elements "0 | 1 | 2
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |" I am sending with the Scatterv
function; 4 elements to the process 0 "|0 | 1 | 2 | 3 | 4| "and
6 elements to process 1 "| 5 | 6 | 7 | 8 | 9 | ", after I did
the summation of the value" add " to each element of the
received table which gives me a new table "afteradd[] "for each
process, I want this time collecting the both tables
"afteradd[]" and send them to the process 0, after a little
research I found that there's a GATHERV function which do that
but I do not know how ?
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "math.h"
#include "mpi.h"
int main(int argc, char** argv)
{
int
taskid, ntasks;
int
ierr, i, itask;
int
sendcounts[2048], displs[2048], recvcount;
double
**sendbuff, *recvbuff,*afteradd, buffsum,
buffsums[2048];
double
inittime, totaltime;
const int
nbr_etat = 10;
double
tab[nbr_etat];
for (int
i = 0; i < nbr_etat; i++)
tab[i] =
i;
int
nbr_elm[2] = { 4, 6 };
int
dpl[2] = { 0, 4 };
MPI_Init(&argc,
&argv);
MPI_Comm_rank(MPI_COMM_WORLD,
&taskid);
MPI_Comm_size(MPI_COMM_WORLD,
&ntasks);
recvbuff
= (double *)malloc(sizeof(double)*nbr_etat);
if
(taskid == 0)
{
sendbuff
= (double **)malloc(sizeof(double *)*ntasks);// run for 2
proc
sendbuff[0]
= (double *)malloc(sizeof(double)*ntasks*nbr_etat);
for (i =
1; i < ntasks; i++)
{
sendbuff[i]
= sendbuff[i - 1] + nbr_etat;
}
}
else
{
sendbuff
= (double **)malloc(sizeof(double *)* 1);
sendbuff[0]
= (double *)malloc(sizeof(double)* 1);
}
if
(taskid == 0){
srand((unsigned)time(NULL)
+ taskid);
for
(itask = 0; itask<ntasks; itask++)
{
int k;
displs[itask]
= itask*nbr_etat;
int s =
dpl[itask];
sendcounts[itask]
= nbr_elm[itask];
for (i =
0; i<sendcounts[itask]; i++)
{
k = i +
s;
sendbuff[itask][i]
= tab[k];
printf("+
%0.0f ", sendbuff[itask][i]);
}
printf("\n");
}
}
recvcount
= nbr_elm[taskid];
inittime
= MPI_Wtime();
ierr =
MPI_Scatterv(sendbuff[0], sendcounts, displs, MPI_DOUBLE,
recvbuff,
recvcount, MPI_DOUBLE,
0,
MPI_COMM_WORLD);
totaltime
= MPI_Wtime() - inittime;
buffsum =
0.0;
int add =
3;
afteradd
= (double *)malloc(sizeof(double)*nbr_etat);
for (i =
0; i<recvcount; i++)
{
afteradd
[i]= add + recvbuff[i];
}
for (i =
0; i<recvcount; i++)
{
printf("*
%0.0f ",
afteradd[i]);
}
printf("\n");
if
(taskid == 0)
{
free(sendbuff[0]);
free(sendbuff);
}
else
{
free(sendbuff[0]);
free(sendbuff);
free(recvbuff);
}
MPI_Finalize();
}