Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Addition and Subtraction of Matrix using pthreads in C/C++
Matrix addition and subtraction using pthreads allows us to perform operations on large matrices efficiently by utilizing multiple threads. Each thread handles a portion of the matrix, enabling parallel computation and improved performance.
To compile and run pthread programs, you need to link with the pthread library using: gcc -pthread program.c -o program
Syntax
pthread_create(&thread_id, NULL, function_name, (void*)argument); pthread_join(thread_id, NULL);
Example: Matrix Operations with Pthreads
This example demonstrates matrix addition and subtraction using multiple threads. Each thread processes one row of the matrices −
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdint.h>
#define MAX 3
#define THREADS 3
int AMat[MAX][MAX] = {{10, 20, 30},
{40, 50, 60},
{70, 80, 50}};
int BMat[MAX][MAX] = {{80, 60, 20},
{30, 20, 15},
{10, 14, 35}};
int add[MAX][MAX], sub[MAX][MAX];
pthread_t thread[THREADS * 2];
void* addMatrices(void* arg) {
intptr_t row = (intptr_t)arg;
for (int j = 0; j < MAX; j++) {
add[row][j] = AMat[row][j] + BMat[row][j];
}
return NULL;
}
void* subtraction(void* arg) {
intptr_t row = (intptr_t)arg;
for (int j = 0; j < MAX; j++) {
sub[row][j] = AMat[row][j] - BMat[row][j];
}
return NULL;
}
void display() {
printf("Matrix A:\n");
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
printf("%d ", AMat[i][j]);
}
printf("\n");
}
printf("\nMatrix B:\n");
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
printf("%d ", BMat[i][j]);
}
printf("\n");
}
}
void displayResults() {
printf("\nAddition Result:\n");
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
printf("%d ", add[i][j]);
}
printf("\n");
}
printf("\nSubtraction Result:\n");
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
printf("%d ", sub[i][j]);
}
printf("\n");
}
}
int main() {
display();
/* Create threads for addition and subtraction */
for (int i = 0; i < THREADS; i++) {
pthread_create(&thread[i], NULL, addMatrices, (void*)(intptr_t)i);
pthread_create(&thread[i + THREADS], NULL, subtraction, (void*)(intptr_t)i);
}
/* Wait for all threads to complete */
for (int i = 0; i < THREADS * 2; i++) {
pthread_join(thread[i], NULL);
}
displayResults();
return 0;
}
Matrix A: 10 20 30 40 50 60 70 80 50 Matrix B: 80 60 20 30 20 15 10 14 35 Addition Result: 90 80 50 70 70 75 80 94 85 Subtraction Result: -70 -40 10 10 30 45 60 66 15
How It Works
- Each thread is assigned a specific row to process
- Addition threads compute
add[i][j] = AMat[i][j] + BMat[i][j] - Subtraction threads compute
sub[i][j] = AMat[i][j] - BMat[i][j] -
pthread_join()ensures main thread waits for all worker threads to complete
Key Points
- Using row-wise thread distribution ensures balanced workload
- The
intptr_tcast safely passes integer values as void pointers - Each thread operates on different memory locations, avoiding race conditions
Conclusion
Pthread-based matrix operations provide efficient parallel computation by distributing workload across multiple threads. This approach significantly improves performance for large matrices while maintaining code simplicity and thread safety.
Advertisements
