<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><div dir="ltr">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 ?<br><div> </div><div>program:</div><div><div>#include <malloc.h></div><div>#include <stdlib.h></div><div>#include <stdio.h></div><div>#include <time.h></div><div>#include "math.h"</div><div>#include "mpi.h"</div><div><br></div><div>int main(int argc, char** argv)</div><div>{</div><div><span class="" style="white-space:pre">   </span>int          taskid, ntasks;</div><div><span class="" style="white-space:pre">      </span>int          ierr, i, itask;</div><div><span class="" style="white-space:pre">      </span>int<span class="" style="white-space:pre">       </span>         sendcounts[2048], displs[2048], recvcount;</div><div><span class="" style="white-space:pre">    </span>double       **sendbuff, *recvbuff,*afteradd, buffsum, buffsums[2048];</div><div><span class="" style="white-space:pre">      </span>double       inittime, totaltime;</div><div><span class="" style="white-space:pre">   </span>const int nbr_etat = 10;</div><div><span class="" style="white-space:pre">   </span>double tab[nbr_etat];</div><div><span class="" style="white-space:pre">      </span>for (int i = 0; i < nbr_etat; i++)</div><div><span class="" style="white-space:pre">              </span>tab[i] = i;</div><div><span class="" style="white-space:pre">        </span>int nbr_elm[2] = { 4, 6 };</div><div><span class="" style="white-space:pre"> </span>int dpl[2] = { 0, 4 };</div><div><span class="" style="white-space:pre">     </span>MPI_Init(&argc, &argv);</div><div><span class="" style="white-space:pre">    </span>MPI_Comm_rank(MPI_COMM_WORLD, &taskid);</div><div><span class="" style="white-space:pre">        </span>MPI_Comm_size(MPI_COMM_WORLD, &ntasks);</div><div><span class="" style="white-space:pre">        </span>recvbuff = (double *)malloc(sizeof(double)*nbr_etat);</div><div><span class="" style="white-space:pre">      </span>if (taskid == 0)</div><div><span class="" style="white-space:pre">   </span>{</div><div><span class="" style="white-space:pre">          </span>sendbuff = (double **)malloc(sizeof(double *)*ntasks);// run for 2 proc</div><div><span class="" style="white-space:pre">            </span>sendbuff[0] = (double *)malloc(sizeof(double)*ntasks*nbr_etat);</div><div><span class="" style="white-space:pre">            </span>for (i = 1; i < ntasks; i++)</div><div><span class="" style="white-space:pre">            </span>{</div><div><span class="" style="white-space:pre">                  </span>sendbuff[i] = sendbuff[i - 1] + nbr_etat;</div><div><span class="" style="white-space:pre">              </span>}</div><div><span class="" style="white-space:pre">  </span>}</div><div><span class="" style="white-space:pre">  </span>else</div><div><span class="" style="white-space:pre">       </span>{</div><div><span class="" style="white-space:pre">          </span>sendbuff = (double **)malloc(sizeof(double *)* 1);</div><div><span class="" style="white-space:pre">         </span>sendbuff[0] = (double *)malloc(sizeof(double)* 1);</div><div><span class="" style="white-space:pre"> </span>}</div><div><br></div><div><span class="" style="white-space:pre"> </span>if (taskid == 0){</div><div><br></div><div><span class="" style="white-space:pre">         </span>srand((unsigned)time(NULL) + taskid);</div><div><span class="" style="white-space:pre">          </span>for (itask = 0; itask<ntasks; itask++)</div><div><span class="" style="white-space:pre">          </span>{</div><div><span class="" style="white-space:pre">                  </span>int k;</div><div><span class="" style="white-space:pre">                     </span>displs[itask] = itask*nbr_etat;</div><div><span class="" style="white-space:pre">                    </span>int s = dpl[itask];</div><div><span class="" style="white-space:pre">                        </span>sendcounts[itask] = nbr_elm[itask];</div><div><br></div><div><br></div><div><span class="" style="white-space:pre">                      </span>for (i = 0; i<sendcounts[itask]; i++)</div><div><span class="" style="white-space:pre">                   </span>{</div><div><span class="" style="white-space:pre">                          </span>k = i + s;</div><div><span class="" style="white-space:pre">                             </span>sendbuff[itask][i] = tab[k];</div><div><span class="" style="white-space:pre">                               </span>printf("+ %0.0f  ", sendbuff[itask][i]);</div><div><span class="" style="white-space:pre">                        </span>}</div><div><span class="" style="white-space:pre">                  </span>printf("\n");</div><div><span class="" style="white-space:pre">            </span>}</div><div><span class="" style="white-space:pre">  </span>}</div><div><br></div><div><span class="" style="white-space:pre"> </span>recvcount = nbr_elm[taskid];</div><div><br></div><div><span class="" style="white-space:pre">      </span>inittime = MPI_Wtime();</div><div><br></div><div><span class="" style="white-space:pre">   </span>ierr = MPI_Scatterv(sendbuff[0], sendcounts, displs, MPI_DOUBLE,</div><div><span class="" style="white-space:pre">           </span>recvbuff, recvcount, MPI_DOUBLE,</div><div><span class="" style="white-space:pre">           </span>0, MPI_COMM_WORLD);</div><div><br></div><div><span class="" style="white-space:pre">       </span>totaltime = MPI_Wtime() - inittime;</div><div><br></div><div><span class="" style="white-space:pre">       </span></div><div><span class="" style="white-space:pre">   </span>buffsum = 0.0;</div><div><span class="" style="white-space:pre">     </span>int add = 3;</div><div><span class="" style="white-space:pre">       </span>afteradd = (double *)malloc(sizeof(double)*nbr_etat);</div><div><span class="" style="white-space:pre">      </span>for (i = 0; i<recvcount; i++)</div><div><span class="" style="white-space:pre">   </span>{</div><div><span class="" style="white-space:pre">          </span>afteradd [i]= add + recvbuff[i];</div><div><span class="" style="white-space:pre">       </span>}</div><div><span class="" style="white-space:pre">  </span>for (i = 0; i<recvcount; i++)</div><div><span class="" style="white-space:pre">   </span>{</div><div><span class="" style="white-space:pre">          </span>printf("* %0.0f<span class="" style="white-space:pre">      </span>", afteradd[i]);</div><div><span class="" style="white-space:pre">      </span>}</div><div><span class="" style="white-space:pre">  </span>printf("\n");<span class="" style="white-space:pre">   </span>                                    </div><div><span class="" style="white-space:pre">     </span>if (taskid == 0)</div><div><span class="" style="white-space:pre">   </span>{</div><div><span class="" style="white-space:pre">          </span>free(sendbuff[0]);</div><div><span class="" style="white-space:pre">         </span>free(sendbuff);</div><div><span class="" style="white-space:pre">    </span>}</div><div><span class="" style="white-space:pre">  </span>else</div><div><span class="" style="white-space:pre">       </span>{</div><div><span class="" style="white-space:pre">          </span>free(sendbuff[0]);</div><div><span class="" style="white-space:pre">         </span>free(sendbuff);</div><div><span class="" style="white-space:pre">            </span>free(recvbuff);</div><div><span class="" style="white-space:pre">    </span>}</div><div><span class="" style="white-space:pre">  </span>MPI_Finalize();</div><div><br></div><div>}</div><div><br></div></div></div>