Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8173331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:06:33+00:00 2026-06-06T22:06:33+00:00

I’m working on a project which contains several slave nodes and one master node.

  • 0

I’m working on a project which contains several slave nodes and one master node. At some point I need to gather data from different slave nodes(master node can be treated as a slave node too) to master node. The data can be any type but let’s suppose it is unsigned int. And that is how the data looks on slave nodes:

node0: |chunk01|chunk02|chunk03|chunk04|….

node1: |chunk11|chunk12|chunk13|chunk14|….

…

noden: |chunkn1|chunkn2|chunkn3|chunkn4|….

The data should be all gathered to node0 and look like this:

node0: |chunk01|chunk11|chunk21|….|chunkn1|chunk02|chunk12|…|chunkn2|…|chunknm|

Which means we concatenate the first chunk from each node together, then second chunk from each node together…

I don’t know how to use MPI_Gatherv to implement this because each chunkij has different size, also each node only knows its own chunk size and start index, but not other node’s info.

I’m not so familiar with MPI so I’m wondering is there any API which can gather data of different size from various nodes to one node?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:06:35+00:00Added an answer on June 6, 2026 at 10:06 pm

    Here’s an example that you can edit that should work. It is almost certainly not the most optimal way to solve the problem – I’d need more details of your code to comment on that. I haven’t checked whether it compiles, but if you fix up any typos I’m happy to try to fix any outstanding bugs.

    I also don’t know how important efficiency is to you – is this op going to be done hundreds of times per second or once a day? If it’s the latter, then this code is probably fine. I’m also assuming C / C++.

    // Populate this on each node from MPI_Comm_rank.
    int myRank; 
    // Populate this on each node from MPI_Comm_size.
    int P; 
    // Num chunks per core.
    const int M = 4;  
    
    // I'm assuming 0 is the master.
    int masterNodeRank = 0; 
    
    // Populate this. 
    // It only needs to have meaningful data on the master node. 
    //If master node doesn't have the data, fill with MPI_GATHER.
    int* sizeOfEachChunkOnEachRank[M]; 
    // Populate this. 
    //It needs to exist on every 'slave' node.
    int sizeOfMyChunks[M]; 
    
    // Assuming you already have this array
    // it should be the contiguous store of each core's data.
    unsigned* myData; 
    // This is what we'll gather all the data into on master node only.
    unsigned* gatheredData = new unsigned[totalDataSize];
    // This array will keep all of the displacements from each sending node.
    int* displacements = new int[P];
    
    // This keeps track of how many unsigneds we've received so far.
    int totalCountSoFar = 0;
    
    // We'll work through all the first chunks on each node at once, then all
    // the second chunks, etc.
    for(int localChunkNum = 0; localChunkNum < M; ++localChunkNum)
    {
      // On the receiving node we need to calculate all the displacements
      // for the received data to go into the array
      if (myRank == masterNodeRank)
      {
        displacements[0] = 0;
    
        for(int otherCore = 1; otherCore < P; ++otherCore)
        {
          displacements[otherCore] = displacements[otherCore-1] + sizeOfEachChunkOnEachRank[localChunkNum][otherCore-1];
        }
      }
    
      // On all cores, we'll need to calculate how far into our local array
      // to start the sending from.      
      int myFirstIndex = 0;
    
      for(int previousChunk=0; previousChunk < localChunkNum; previousChunk++)
      {
        myFirstIndex += sizeOfMyChunks[previousChunk];
      }
    
      // Do the variable gather
      MPI_Gatherv(&myData[myFirstIndex], // Start address to send from
                  sizeOfMyChunks[localChunkNum], // Number to send
                  MPI_UNSIGNED, // Type to send
                  &gatheredData[totalCountSoFar], // Start address to receive into
                  sizeOfEachChunkOnEachRank[localChunkNum], // Number expected from each core
                  displacements, // Displacements to receive into from each core
                  MPI_UNSIGNED, // Type to receive
                  masterNodeRank, // Receiving core rank
                  MPI_COMM_WORLD); // MPI communicator.
    
      // If this is the receiving rank, update the count we've received so far
      // so that we don't overwrite data the next time we do the gather.
      // Note that the total received is the displacement to the receive from the
      // last core + the total received from that core.
      if(myRank == masterNodeRank)
      {
        totalCountSoFar += displacements[P-1] + sizeOfEachChunkOnEachRank[localChunkNum][P-1];
      }
    }
    
    delete[] displacements;
    delete[] gatheredData;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.