mb0
mb0

Reputation: 139

How to distribute a number range to each process?

I am doing a learning exercise. I want to calculate the number of primes in a range from 0 to N. What function of mpi can I use to distribute ranges of numbers to each process? In other words, each process calculates the number of primes within a number range of the main range.

Upvotes: 1

Views: 192

Answers (1)

herisson
herisson

Reputation: 128

You could simply use a for loop and MPI_Send on the root (and MPI_Recv on receivers) to send to each process the number at which it should start and how many numbers it should check.

Another possibility, even better, is to send N to each process with MPI_Bcast (On root and receivers) and let each process compute which numbers it should check using it's own rank. (something like start=N/MPI_Comm_size*MPI_Comm_rank and length=N/MPI_Comm_size, and some adequate rounding etc.)

You can probably optimize load balancing even more but you should get it working first.

At the end you should call MPI_Reduce with a sum.

Upvotes: 2

Related Questions