I have this array [1 2 3 4 5 6 7 8 9] and i am performing scan operation on that.
I have 3 mpi tasks and each task gets 3 elements then each task calculates its scan and returns result to master task
task 0 - [1 2 3] => [1 3 6]
task 1 - [4 5 6 ] => [4 9 15]
task 2 - [7 8 9] => [7 15 24]
Now task 0 gets all the results [1 3 6] [4 9 15] [7 15 24]
How can I combine these results to produce final scan output?
final scan output of array would be [1 3 6 10 15 21 28 36 45]
can anyone help me please?
Are you trying to implement your own scan operation? Since this is not what MPI_SCAN
does. It applies the scan operation elementwise over each i-th element of the input array stored on each node and the result will be more like:
rank 0 - [1 2 3] => [ 1 2 3]
rank 1 - [4 5 6] => [ 5 7 9]
rank 2 - [7 8 9] => [12 15 18]
Nevertheless, in order to obtain the result that you want, you should add 6
(the last element from the first scan in task 0) to all elements in the next scans:
[ 1 3 6][ 4 9 15][ 7 15 24]
+6 -------------->
=
[ 1 3 6][10 15 21][13 21 30]
Then you should add 15
(the last element from the scan in task 1 before 6
was added) to all elements in the next scans and so forth.
[ 1 3 6][10 15 21][13 21 30]
+15 ---->
=
[ 1 3 6][10 15 21][28 36 45]
Alternatively you could add 6
only to the results from the second scan, then add 21
to the results from the third scan and so forth.
Maybe you can find some clever way to do that using MPI operations.