Skip to content

Instantly share code, notes, and snippets.

@auriza
Last active November 24, 2020 13:52
Show Gist options
  • Save auriza/8120103 to your computer and use it in GitHub Desktop.
Save auriza/8120103 to your computer and use it in GitHub Desktop.
Matrix addition using multithreading.
#include <stdio.h>
#include <pthread.h>
#define T 2
#define N 4
int A[N][N] = {{1,0,1,0},{0,1,1,0},{1,0,1,0},{0,1,1,0}};
int B[N][N] = {{0,2,2,0},{2,0,2,0},{0,2,2,0},{2,0,2,0}};
int C[N][N] = {{0}};
void *matrix_add(void *arg) {
int id = *(int*)arg;
int i, j;
for (i = id; i < N; i += T)
for (j = 0; j < N; j++)
C[i][j] = A[i][j] + B[i][j];
pthread_exit(NULL);
}
int main()
{
pthread_t thread[T];
int tid[T];
int i, j;
for (i = 0; i < T; i++) {
tid[i] = i;
pthread_create(&thread[i], NULL, matrix_add, &tid[i]);
}
for (i = 0; i < T; i++)
pthread_join(thread[i], NULL);
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
printf((j < N-1) ? "%d " : "%d\n", C[i][j]);
return 0;
}
@Rayanagoud
Copy link

sir i want to implement the add two matrices of order assuming 100x 100

@Rayanagoud
Copy link

sir using multi-thread and and sequential approach

@Rayanagoud
Copy link

Design and implement a program to add two m x n matrices (n>=100 and m>=100). In order to perform the above, two versions of the program need to be implemented, one, a sequential version and the other a concurrent version. The sequential version implements a function to add the two matrices. The concurrent version of the program spawns threads, each thread to add the assigned number of rows. The main thread computes the consolidated output matrix.
Perform the following using file management system calls:
2.1 Design and implement the application using sequential approach with functions
2.2 Design and implement the application using multi threaded approach

@Rayanagoud
Copy link

sir how to solve it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment