Click here to Skip to main content
15,798,066 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an problem in a parallelised code where I need to reduce values from all processors to the root process, summing them and then assigning the sum to an array element. I would like to do this in a loop such that each MPI_Reduce() call reduces to consecutive elements along an array. I would like to know how to point to the specific location in the array.

The part of the for-loop code is shown below:
for(int i = 1; i < N_t; i++) {

            loc_EK = valEK * (cblas_ddot(loc_N, loc_vx, 1, loc_vx, 1) + 
            cblas_ddot(loc_N, loc_vy, 1, loc_vy, 1));
            loc_EP = valEP * cblas_dasum(loc_N, loc_y, 1);
            loc_ET = loc_EP + loc_EK;

            // Gather and sum loc_E components in root process
            MPI_Reduce(&loc_EK, (EK + 1 + i), 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
            MPI_Reduce(&loc_EP, (EP + 1 + i), 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
            MPI_Reduce(&loc_ET, (ET + 1 + i), 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);

What I have tried:

I am currectly pointing the the array positions using (EK+i+1), where EK is the pointer to a dynamic array. I have tried using EK[i+1] but as expected this did not work. How should I fix this so that I can assign each reduced sum to elements of the array pointed to by EK?
Updated 24-Mar-21 6:44am
Rick York 24-Mar-21 12:15pm    
I can't give you an exact solution but I worked recently on a reduction algorithm in CUDA and it was really, really fast. I compared two elements and put the result in the same slot as the first one. You will have to look at the documentation of MPI_Reduce to find how this should be called.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900