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

Lab5 Mat Ops Pthreads 11

Uploaded by

thomasriley35420
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab5 Mat Ops Pthreads 11

Uploaded by

thomasriley35420
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment Number: 5

Problem Statement: Pthread Implementation


NAME: Aayush Chavan ROLLNO: 1
CLASS: TY – IT A BATCH: 1
DATE OF PERFORMANCE: Sept 24
______________________________________________________________________________________
_____

PROGARM: Implement multithreading for Matrix Operations


using Pthreads.
Problem Statement:
Multithreading allows a program to execute multiple threads
concurrently, improving performance by utilizing multiple CPU cores. For
matrix operations, which involve repetitive and independent calculations
(like matrix multiplication), multithreading can significantly speed up the
process.
Pthreads (POSIX threads) is a standardized C library for creating and
managing threads on Unix-like systems. It provides a set of API functions
for thread creation, synchronization, and management.
Code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX 100

int A[MAX][MAX], B[MAX][MAX], C[MAX][MAX];


int M, N, P; // Dimensions of matrices
int thread_count = 4; // Number of threads

typedef struct {
int row_start;
int row_end;
} thread_args;

// Thread function for matrix multiplication


void* multiply_matrices(void* args) {
thread_args* t_args = (thread_args*) args;
int start = t_args->row_start;
int end = t_args->row_end;
for (int i = start; i < end; i++) {
for (int j = 0; j < P; j++) {
C[i][j] = 0;
for (int k = 0; k < N; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
pthread_exit(0);
}

// Thread function for matrix addition


void* add_matrices(void* args) {
thread_args* t_args = (thread_args*) args;
int start = t_args->row_start;
int end = t_args->row_end;

for (int i = start; i < end; i++) {


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

// Thread function for matrix subtraction


void* subtract_matrices(void* args) {
thread_args* t_args = (thread_args*) args;
int start = t_args->row_start;
int end = t_args->row_end;

for (int i = start; i < end; i++) {


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

// Thread function for matrix transposition


void* transpose_matrix(void* args) {
thread_args* t_args = (thread_args*) args;
int start = t_args->row_start;
int end = t_args->row_end;
for (int i = start; i < end; i++) {
for (int j = 0; j < N; j++) {
C[j][i] = A[i][j];
}
}
pthread_exit(0);
}

void create_threads(void* (*operation)(void*), int rows) {


pthread_t threads[thread_count];
thread_args args[thread_count];

int rows_per_thread = rows / thread_count;


for (int i = 0; i < thread_count; i++) {
args[i].row_start = i * rows_per_thread;
args[i].row_end = (i == thread_count - 1) ? rows : (i + 1) *
rows_per_thread;
pthread_create(&threads[i], NULL, operation, &args[i]);
}

for (int i = 0; i < thread_count; i++) {


pthread_join(threads[i], NULL);
}
}

// Function to input matrix


void input_matrix(int matrix[MAX][MAX], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
}

// Function to print matrix


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

printf("Enter dimensions M, N, P (for matrix sizes MxN and NxP): ");


scanf("%d%d%d", &M, &N, &P);

while (1) {
printf("\nMatrix Operations Menu:\n");
printf("1. Matrix Addition (MxN)\n");
printf("2. Matrix Subtraction (MxN)\n");
printf("3. Matrix Multiplication (MxN * NxP)\n");
printf("4. Matrix Transposition (MxN)\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter matrix A (%d x %d):\n", M, N);
input_matrix(A, M, N);
printf("Enter matrix B (%d x %d):\n", M, N);
input_matrix(B, M, N);

create_threads(add_matrices, M);
printf("Resultant matrix C (Addition):\n");
print_matrix(C, M, N);
break;

case 2:
printf("Enter matrix A (%d x %d):\n", M, N);
input_matrix(A, M, N);
printf("Enter matrix B (%d x %d):\n", M, N);
input_matrix(B, M, N);

create_threads(subtract_matrices, M);
printf("Resultant matrix C (Subtraction):\n");
print_matrix(C, M, N);
break;

case 3:
printf("Enter matrix A (%d x %d):\n", M, N);
input_matrix(A, M, N);
printf("Enter matrix B (%d x %d):\n", N, P);
input_matrix(B, N, P);

create_threads(multiply_matrices, M);
printf("Resultant matrix C (Multiplication):\n");
print_matrix(C, M, P);
break;

case 4:
printf("Enter matrix A (%d x %d):\n", M, N);
input_matrix(A, M, N);

create_threads(transpose_matrix, M);
printf("Resultant matrix C (Transposition):\n");
print_matrix(C, N, M);
break;

case 5:
exit(0);

default:
printf("Invalid choice!\n");
break;
}
}

return 0;
}

Output:
Aayush@Chavan:/c/OS/lab projects/C/lab5$ ls
example.txt matrix_ops_pthreads.c system_call system_call.c
Aayush@Chavan:/c/OS/lab projects/C/lab5$ gcc -pthread
matrix_ops_pthreads.c -o mat_ops
Aayush@Chavan:/c/OS/lab projects/C/lab5$ ./mat_ops
Enter dimensions M, N, P (for matrix sizes MxN and NxP): 3
2
3

Matrix Operations Menu:


1. Matrix Addition (MxN)
2. Matrix Subtraction (MxN)
3. Matrix Multiplication (MxN * NxP)
4. Matrix Transposition (MxN)
5. Exit
Enter your choice: 2
Enter matrix A (3 x 2):
12
32
44
65
87
11
Enter matrix B (3 x 2):
65
23
13
87
32
96
Resultant matrix C (Subtraction):
-53 9
31 -22
55 -85

Matrix Operations Menu:


1. Matrix Addition (MxN)
2. Matrix Subtraction (MxN)
3. Matrix Multiplication (MxN * NxP)
4. Matrix Transposition (MxN)
5. Exit
Enter your choice: 5
Aayush@Chavan:/c/OS/lab projects/C/lab5$

You might also like