0

I'm going to mastering MPI in this summer. I started to read "An Introduction to Parallel Programming" by Peter Pacheco, and solving its exercise. In one of it's MPI exercise he asked for implementing simple prefix sum with MPI_Scan, but I couldn't implement it with MPI_Scan. I found a solution by using MPI_Scatter, and MPI_Gather. Please help me to find an answer for MPI_Scan.

// declaring array and result
// generating random array
MPI_Bcast( &array, n, MPI_FLOAT, 0, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Scan(array, result, n, MPI_FLOAT, MPI_SUM, MPI_COMM_WORLD);

I'm expecting to have prefix sum in result but for some reason it is not working.

Amir
  • 1,087
  • 1
  • 9
  • 26

1 Answers1

3

You are definitely not getting prefix sum using MPI_PROD operation - you should be using MPI_SUM operation. Also note that MPI_Scan and MPI_Reduce work elementwise on array, e.g.

result[i] = array_0[i] op array_1[i] op array_2[i] op ... (i = 0,...,n-1)

where array_0 is the array in process 0, array_1 is the array in process 1, and so on; op is the operation specified by the MPI_Op handle, e.g. MPI_SUM for +, MPI_PROD for *, and so on.

If you have an array with as many elements as the number of procsses then you first need to perform MPI_Scatter with a length of 1 element per process, followed by MPI_Scan. For larger arrays, see this question.

Jeff Hammond
  • 5,374
  • 3
  • 28
  • 45
Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
  • Thank you very much. I got it know. My array should be scattered first and then MPI_Scan will work like MPI_Reduce on each elements of array and the result will be prefix sum. – Amir Aug 06 '12 at 10:50