In this problem, you need to use MPI to parallelize the following serial program (http://www. cs.nctu.edu.tw/~ypyou/courses/PP-f19/assignments/HW3/conduction.c). The program simluates heat conduction on an isolated 2D long strip and finds the minimum temperature until heat balance or reaching specified iterations, given random temperatures on the strip. There are three int arguments (L, which controls the length of the strip, iteration and seed, which seeds the random number generator) from command line arguments taken as inputs. For convenience, L is assumed to be divisible by the number of threads.
#include <stdio.h
#include <stdlib.h
#ifndef W
#define W 20 // Width
#endif
int main(int argc, char **argv) {
int L = atoi(argv[1]); // Length
int iteration = atoi(argv[2]); // Iteration srand(atoi(argv[3])); // Seed
float d = (float) random() / RAND_MAX * 0.2; // Diffusivity int *temp = malloc(L*W*sizeof(int)); // Current temperature int *next = malloc(L*W*sizeof(int)); // Next time step
for (int i = 0; i < L; i++) { for (int j = 0; j < W; j++) { temp[i*W+j] = random()3;
}
}
int count = 0, balance = 0;
while (iteration--) { // Compute with up, left, right, down points
balance = 1; count++;
for (int i = 0; i < L; i++) { for (int j = 0; j < W; j++) {
float t = temp[i*W+j] / d; t += temp[i*W+j] * -4;
t += temp[(i - 1 < 0 ? 0 : i - 1) * W + j]; t += temp[(i + 1 = L ? i : i + 1)*W+j]; t += temp[i*W+(j - 1 < 0 ? 0 : j - 1)]; t += temp[i*W+(j + 1 = W ? j : j + 1)]; t *= d; next[i*W+j] = t ;
if (next[i*W+j] != temp[i*W+j]) {
balance = 0; }
} }
if (balance) { break;
}
int *tmp = temp; temp = next; next = tmp;
}
int min = temp[0]; for (int i = 0; i < L; i++) { for (int j = 0; j < W; j++) { if (temp[i*W+j] < min) {