Question: Task 3 : Open MPI Helpful resources: Lab 7 , Discussion 1 0 , Homework 1 0 Up until this point, you've been using the

Task 3: Open MPI
Helpful resources: Lab 7, Discussion 10, Homework 10
Up until this point, you've been using the provided coordinator_naive.c as the coordinator with a small number of tasks. However, coordinator_naive.c does not have any PLP capabilities and we can optimize it for a workload that includes a larger number of tasks.
Implement an Open MPI coordinator in coordinator_mpi.c.
The structure of the code will be similar to the Open MPI coordinator you wrote in homework 10 and discussion 10.
Copy your existing compute functions in compute_optimized.c to compute_optimized_mpi.c, and make any changes necessary to further improve the speedup.
"coordinator_mpi.c"
#include
#include "coordinator.h"
#define READY 0
#define NEW_TASK 1
#define TERMINATE -1
int main(int argc, char *argv[]){
if (argc <2){
printf("Error: not enough arguments
");
printf("Usage: %s [path_to_task_list]
", argv[0]);
return -1;
}
// TODO: implement Open MPI coordinator
}
#include "compute.h"
2
3// Computes the convolution of two matrices
4 int convolve(matrix_t *a_matrix, matrix_t *b_matrix, matrix_t **output_matrix){
5// TODO: convolve matrix a and matrix b, and store the resulting matrix in
6// output_matrix
7
8//Flip Matrix B Horiztonally
9 for (int i =0; i < b_matrix->rows; i++){
10 int start = i * b_matrix->cols;
11 int end = start + b_matrix->cols -1;
12 while (start < end){
13 int temp = b_matrix->data[start];
14 b_matrix->data[start]= b_matrix->data[end];
15 b_matrix->data[end]= temp;
16 start++;
17 end--;
18}
19}
20
21//Flip Matrix B Vertically
22 for (int i =0; i < b_matrix->rows /2; i++){
23 int startRow = i * b_matrix->cols;
24 int endRow =(b_matrix->rows - i -1)* b_matrix->cols;
25 for (int j =0; j < b_matrix->cols; j++){
26 int temp = b_matrix->data[startRow + j];
27 b_matrix->data[startRow + j]= b_matrix->data[endRow + j];
28 b_matrix->data[endRow + j]= temp;
29}
30}
31
32//Determine Output Matrix Dimensions
33 int output_rows = a_matrix->rows - b_matrix->rows +1;
34 int output_cols = a_matrix->cols - b_matrix->cols +1;
35
36//Allocate Output Matrix Memory
37*output_matrix = malloc(sizeof(matrix_t));
38 if (*output_matrix == NULL){
39 return -1;
40}
41(*output_matrix)->rows = output_rows;
42(*output_matrix)->cols = output_cols;
43(*output_matrix)->data = malloc(output_rows*output_cols*sizeof(int));
44 if ((*output_matrix)->data == NULL){
45 free(*output_matrix);
46 return -1;
47}
48
49//Perform Convultion
50 for (int i =0; i < output_rows; i++){
51 for (int j =0; j < output_cols; j++){
52 int sum =0;
53 for (int k =0; k < b_matrix->rows; k++){
54 for (int l =0; l < b_matrix->cols; l++){
55 int ik = i + k;
56 int jl = j + l;
57 sum += a_matrix->data[ik * a_matrix->cols + jl]* b_matrix->data[k * b_matrix->cols + l];
58}
59}
60(*output_matrix)->data[i * output_cols + j]= sum;
61}
62}
63
64 return 0;
65}
66
67// Executes a task
68 int execute_task(task_t *task){
69 matrix_t *a_matrix, *b_matrix, *output_matrix;
70
71 char *a_matrix_path = get_a_matrix_path(task);
72 if (read_matrix(a_matrix_path, &a_matrix)){
73 printf("Error reading matrix from %s
", a_matrix_path);
74 return -1;
75}
76 free(a_matrix_path);
77
78 char *b_matrix_path = get_b_matrix_path(task);
79 if (read_matrix(b_matrix_path, &b_matrix)){
80 printf("Error reading matrix from %s
", b_matrix_path);
81 return -1;
82}
83 free(b_matrix_path);
84
85 if (convolve(a_matrix, b_matrix, &output_matrix)){
86 printf("convolve returned a non-zero integer
");
87 return -1;
88}
89
90 char *output_matrix_path = get_output_matrix_path(task);
91 if (write_matrix(output_matrix_path, output_matrix)){
92 printf("Error writing matrix to %s
", output_matrix_path);
93 return -1;
94}
95 free(output_matrix_path);
96
97 free(a_matrix->data);
98 free(b_matrix->data);
99 free(output_matrix->data);
100 free(a_matrix);
101 free(b_matrix);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!