0% found this document useful (0 votes)
24 views

As 3

The document contains 3 questions related to parallel programming. Question 1 involves a MPI program that distributes an array among processes, performs addition in parallel, and collects results on the master. Question 2 discusses one-to-all broadcast and scatter algorithms in a hypercube network. Question 3 shows an OpenMP program that performs matrix multiplication in parallel using a parallel for loop.

Uploaded by

syedahmads302
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

As 3

The document contains 3 questions related to parallel programming. Question 1 involves a MPI program that distributes an array among processes, performs addition in parallel, and collects results on the master. Question 2 discusses one-to-all broadcast and scatter algorithms in a hypercube network. Question 3 shows an OpenMP program that performs matrix multiplication in parallel using a parallel for loop.

Uploaded by

syedahmads302
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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