Page 1 of 1

Hi, I need you to look at the code and you must add only one line to Add the necessary MPI_Reduce to both programs to fi

Posted: Sat May 14, 2022 7:24 pm
by answerhappygod
Hi, I need you to look at the code and you must add only one
line to Add the necessary MPI_Reduce to both programs to find the
total sum.
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <math.h>

#define max_rows 100000 //change this to the maximum
number without segmentation fault
#define send_data_tag 1
int array[max_rows];
int array2[max_rows];

int main(int argc, char **argv)
{
long int sum, partial_sum;
MPI_Status status;
int my_id, root_process, i,
num_rows, num_procs,

num_rows_to_receive, avg_rows_per_process,

num_rows_received, start_row;
/* begining of MPI Code.
* From this point on, every
process executes a seperate (local) copy
* of this program */
MPI_Init(&argc,
&argv);

root_process = 0;

/* find out MY process ID, and how
many processes were started. */

MPI_Comm_rank(MPI_COMM_WORLD,
&my_id);
MPI_Comm_size(MPI_COMM_WORLD,
&num_procs);
if(my_id == root_process) {

/* I must be the
root process, so I will query the user
* to
determine the size of the array */
printf("please
enter the array size: ");
scanf("%i",
&num_rows);

if(num_rows >
max_rows) {

printf("Too many numbers.\n");

exit(1);
}
avg_rows_per_process = floor(num_rows / num_procs);
/* initialize
an array */
for(i = 0; i
< num_rows; i++) {

array = 1;
}
/* distribute a
portion of the array to each slave process */

for (i = 1; i <
num_procs - 1; i++) {

start_row = i * avg_rows_per_process;


MPI_Send(&avg_rows_per_process,

1, MPI_INT, i, send_data_tag,

MPI_COMM_WORLD);

MPI_Send(&array[start_row],

avg_rows_per_process,

MPI_INT, i, send_data_tag,

MPI_COMM_WORLD);

}


// last process adds remaining elements

start_row = i * avg_rows_per_process;

int elements_left = num_rows - start_row;


MPI_Send(&elements_left,

1, MPI_INT,

i, send_data_tag,

MPI_COMM_WORLD);

MPI_Send(&array[start_row],

elements_left,

MPI_INT, i, send_data_tag,

MPI_COMM_WORLD);
/* and
calculate the sum of the values in the segment assigned
* to the
root process */

partial_sum =
0;
for(i = 0; i <
avg_rows_per_process ; i++) {

partial_sum += array;
}
printf("partial_sum %ld calculated by root process\n",
partial_sum);
}
else {
/* I must be a
slave process, so I must receive my array segment,
* storing it
in a "local" array, array2. */
MPI_Recv(
&num_rows_to_receive, 1, MPI_INT,

root_process, send_data_tag, MPI_COMM_WORLD, &status);

MPI_Recv(
&array2, num_rows_to_receive, MPI_INT,

root_process, send_data_tag, MPI_COMM_WORLD, &status);
num_rows_received = num_rows_to_receive;
/* Calculate
the sum of my portion of the array */
partial_sum =
0;
for(i = 0; i <
num_rows_received; i++) {

partial_sum += array2;
}

printf("partial_sum %ld calculated by process
%d\n", partial_sum, my_id );
}
/* and finally, sum-up all partial sums using
MPI_Reduce */


/***********************************************************/

//add MPI_Reduce here

/***********************************************************/
/* the master process prints the sum */

if(my_id == root_process) {


printf("The grand total is: %ld\n", sum);

}

MPI_Finalize();

}