0% found this document useful (0 votes)
11 views4 pages

Exericses 2 Solution Matrix Multlpication

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

Exericses 2 Solution Matrix Multlpication

ji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Bouchibane Mohamed Abdelwahab Exercise 2

1 Solution
1.1 Code
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

#define N 4 // Size of matrices (NxN)

// Matrices
int A[N][N], B[N][N], C[N][N];

// Semaphore for synchronization


sem_t semaphore;

// Function to initialize matrices


void initialize_matrices() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
A[i][j] = rand() % 10;
B[i][j] = rand() % 10;
C[i][j] = 0; // Initialize result matrix
}
}
}

// Function to print a matrix


void print_matrix(int matrix[N][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

// Thread function for matrix multiplication


void *compute_rows(void *arg) {
int thread_id = *(int *)arg;
int start_row = thread_id * (N / 2);
int end_row = start_row + (N / 2);

for (int i = start_row; i < end_row; i++) {


for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
C[i][j] += A[i][k] * B[k][j];
}

1
Bouchibane Mohamed Abdelwahab Exercise 2

}
}

// Signal completion
sem_post(&semaphore);
pthread_exit(NULL);
}

int main() {
pthread_t threads[2];
int thread_ids[2] = {0, 1};

// Initialize matrices and semaphore


initialize_matrices();
sem_init(&semaphore, 0, 0);

printf("Matrix A:\n");
print_matrix(A);
printf("Matrix B:\n");
print_matrix(B);

// Create threads
for (int i = 0; i < 2; i++) {
pthread_create(&threads[i], NULL, compute_rows, &thread_ids[i]);
}

// Wait for threads to complete


for (int i = 0; i < 2; i++) {
sem_wait(&semaphore);
}

printf("Resulting Matrix C:\n");


print_matrix(C);

// Clean up
sem_destroy(&semaphore);
return 0;
}

1.2 Report
1.2.1 Understanding the Problem
• Matrix multiplication is a computationally expensive operation that can benefit from
parallel processing. The task requires multiplying two matrices A and B to produce a
∑N −1 matrix C Each element C[i][j] of the resulting matrix is computed by this formula
result
k=0 A[i][k].B[k][j]

2
Bouchibane Mohamed Abdelwahab Exercise 2

• Matrix multiplication is a computationally expensive operation that can benefit from


parallel processing. The task requires multiplying two matrices A and B to produce a
result matrix C Each element C[i][j] of the resulting matrix is computed by this formula
∑ N −1
k=0 A[i][k].B[k][j]

1.2.2 Steps and Logic


• The matrices A and B are initialized with random values, and the result matrix C is
initialized to zero.

• The computation of the matrix product is split between two threads. Each thread is
responsible for calculating a subset of the rows of matrix C.

• The semaphore is used for synchronization, ensuring that the main thread waits for both
threads to finish before printing the result matrix C.

• Each thread calculates the dot product of the corresponding row of matrix A with all
columns of matrix B, and stores the result in the matrix C.

1.2.3 Use of Semaphores


• Semaphores are used to ensure that both threads synchronize properly and that the main
thread does not proceed until both threads have finished their work.

• The semaphore is initialized with a value of 0. Each thread signals (increments the
semaphore) once it finishes its computation using sem_post.

• The main thread waits for the completion of both threads using sem_wait, ensuring that
the final result matrix C is printed only after both threads have completed their tasks.

1.2.4 Challenges Faced and Solutions


• Thread Synchronization: One of the challenges was ensuring proper synchronization
between threads to prevent race conditions. This was resolved using semaphores to ensure
that both threads completed their computations before the main thread proceeded.

• Matrix Indexing: Ensuring that each thread worked on different rows without overlap-
ping was another challenge. The matrix rows were evenly divided between the threads
by assigning specific ranges to each thread, thus avoiding conflicts.

• Dynamic Thread Allocation: The current solution is limited to two threads. For
larger matrices or more threads, this approach could be modified to dynamically assign
rows based on the number of available threads. This would require adjusting the work
division and possibly using more advanced synchronization techniques.

1.2.5 Conclusion
• The solution successfully implements parallel matrix multiplication using semaphores to
ensure thread safety and synchronization.

• The parallel approach significantly reduces the computation time compared to sequential
matrix multiplication, especially for larger matrices.

3
Bouchibane Mohamed Abdelwahab Exercise 2

• Future work could involve scaling the solution to handle more threads dynamically, im-
proving load balancing, and optimizing the performance for larger matrices.

You might also like