armando
armando

Reputation: 1480

How to use the same array on different processors using MPI

I would like to have the same array called hist(1:1000) on different processors using OpenMPI, such that when one processor modifies hist this modification is updated in the rest of the processors.

I have written a code and declared hist(1:1000) but the problem is that hist is updated independently on each processor, then what I do is to send the data to a "master node" in order to update another histogram based on the information of the slaves but this is what I don't want. I would like to have the same histogram updated on each of the processors.

Is it possible to do this with OpenMPI?

Upvotes: 0

Views: 2190

Answers (3)

haraldkl
haraldkl

Reputation: 3819

You can share an array on one process with others with the help of one-sided communications. However this is regarded by many people as a sub-optimal solution. Maybe you would be better off, rethinking your algorithm and trying formulate it more along the message passing paradigm.

Upvotes: 0

lamba
lamba

Reputation: 1651

I don't exactly know your application but you might be interested in the MPI_Bcast function, it will broadcast an array to all of your processors. However, it is usually not a good idea to use a lot of MPI_Bcast because a long time will be used for communicating rather than processing.

Upvotes: 1

mgilson
mgilson

Reputation: 310227

To my knowledge, that sort of thing isn't possible with MPI. The purpose of MPI is to implement message passing from one process to another (hence the name -- Message Passing Interface). If you want to use shared memory, your best bet is to use something like OpenMP (I think it's probably possible there?) which is supported by at least gcc/gfortran and probably a host of other compilers. You could use threads ... But, ultimately, none of these scales to 1000's of processors like MPI. Depending on what you're doing, you could try MPI's collective operations -- As you mentioned, hist is storing a histogram, so you probably just want the sum of all the hists you're using and your calculation doesn't actually depend on hist. MPI can sum all the arrays for you after they've been changed...

see this for example

http://mpi.deino.net/mpi_functions/MPI_Allreduce.html

(The operation you want is probably MPI_SUM)

Upvotes: 6

Related Questions