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

  • Home
  • SEARCH
  • 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 8992285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:55:06+00:00 2026-06-15T22:55:06+00:00

I’m attempting to parallelize a script I wrote. Each process needs to do a

  • 0

I’m attempting to parallelize a script I wrote. Each process needs to do a calculation and store the data to a specific part of an array (list of lists). Each process is calculating and storing its data alright, but I can’t figure out how to get the data from the non-root processes to the root process so that it can print the data out to file. I created a minimum working example of my script—this one is designed to run on 2 cores only for simplicity:

from mpi4py import MPI 
import pdb 
import os

comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()

# Declare the array that will store all the temp results
temps = [[0 for x in xrange(5)] for x in xrange(4)]

# Loop over all directories
if rank==0:
   counter = 0 
   for i in range(2):
      for j in range(5):
         temps[i][j] = counter
     counter = counter + 1 

else:
   counter = 20
   for i in range(2,4):
      for j in range(5):
         temps[i][j] = counter
         counter = counter + 1 

temps = comm.bcast(temps,root=0)

if rank==0:

   print temps

I execute the script using:

mpiexec -n 2 python mne.py

When the case finishes, the output is:

[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

So you can see that the data sharing is not working as I want. Can someone please show me the correct way to get data back to the root process?

  • 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-15T22:55:07+00:00Added an answer on June 15, 2026 at 10:55 pm

    The code is working correctly, just not doing what you’d like.

    This line

    temps = comm.bcast(temps,root=0)
    

    broadcasts processor 0’s temps variable to all processors (including rank 0), which of course gives exactly the results above. You want to use gather (or allgather, if you want all of the processors to have the answer). That would look something more like this:

    from mpi4py import MPI
    import pdb
    import os
    
    comm = MPI.COMM_WORLD
    size = comm.Get_size()
    rank = comm.Get_rank()
    
    assert size == 2
    
    # Declare the array that will store all the temp results
    temps = [[0 for x in xrange(5)] for x in xrange(4)]
    
    # declare the array that holds the local results
    locals =[[0 for x in xrange(5)] for x in xrange(2)]
    
    # Loop over all directories
    if rank==0:
       counter = 0
       for i in range(2):
          for j in range(5):
             locals[i][j] = counter
             counter = counter + 1
    
    else:
       counter = 20
       for i in range(2):
          for j in range(5):
             locals[i][j] = counter
             counter = counter + 1
    
    temps = comm.gather(locals,temps,root=0)
    
    if rank==0:
       print temps
    

    If you really want to do the collection in-place, and you know (say) that all the real data is going to be larger than the zero you’ve initialized the data with, you can use a reduction operation, but that goes easier with numpy arrays:

    from mpi4py import MPI
    import numpy
    
    comm = MPI.COMM_WORLD
    size = comm.Get_size()
    rank = comm.Get_rank()
    
    assert size == 2
    
    # Declare the array that will store all the temp results
    temps = numpy.zeros((4,5))
    
    # Loop over all directories
    if rank==0:
       counter = 0
       for i in range(2):
          for j in range(5):
             temps[i,j] = counter
             counter = counter + 1
    
    else:
       counter = 20
       for i in range(2,4):
          for j in range(5):
             temps[i,j] = counter
             counter = counter + 1
    
    comm.Allreduce(MPI.IN_PLACE,temps,op=MPI.MAX)
    
    if rank==0:
       print temps
    
    • 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
Basically, what I'm trying to create is a page of div tags, each has
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.