Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Assignment 3

Syed Ahmad Rasool

Question #1:
```c
#include <stdio.h>
#include <mpi.h>

int main(int argc, char *argv[]) {


MPI_Init(&argc, &argv);

int rank, size;


MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

if (size % 4 != 0) {
if (rank == 0) {
printf("Number of MPI processes must be divisible by 4.\n");
}
MPI_Finalize();
return 0;
}

// Master process initializes array


if (rank == 0) {
// Initialize and distribute array
// ...

// Collect sums from other processes


// ...

// Display the sum of all array elements


// ...
} else {
// Receive array portion
// Perform addition
// Send back sum to master
// ...
}

MPI_Finalize();
return 0;
}
```

Question #2:
a) The iteration for one-to-all broadcast in a hypercube involves sending the message to all
neighboring nodes. The minimum number of iterations is the logarithm base 2 of the number of
nodes.

b) The steps for one-to-all scatter operation in a hypercube:


1. Identify the source node that possesses the array.
2. In each iteration, the source node sends its corresponding element to the destination node.
3. Repeat this process until all nodes receive their portion.

Question #3:
```c
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

#define N 10

int main() {
double A[N][N], B[N][N], C[N][N];
int i, j, k;

// Initialize input matrices A and B with random values


// ...

double start_time = omp_get_wtime();

// Perform matrix multiplication using OpenMP


#pragma omp parallel for private(i, j, k) shared(A, B, C)
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
C[i][j] = 0.0;
for (k = 0; k < N; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}

double end_time = omp_get_wtime();


printf("Execution time: %f seconds\n", end_time - start_time);

// Display resulting matrix C


// ...

return 0;
}
```

You might also like