Skip to content

Instantly share code, notes, and snippets.

@gunavaran
Created December 13, 2020 04:09
Show Gist options
  • Save gunavaran/812888a0f687a82a326426a2e8b3784c to your computer and use it in GitHub Desktop.
Save gunavaran/812888a0f687a82a326426a2e8b3784c to your computer and use it in GitHub Desktop.
//
// Created by gunavaran on 12/8/20.
//
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#define n 1024
#define s 512
#define NUM_THREADS 2
double A[n][n];
double B[n][n];
double C[n][n];
double D[n][n];
void compare_matrix(double *matrixA, double *matrixB) {
for (int j = 0; j < n; j++)
for (int i = 0; i < n; i++)
if (matrixA[i + j * n] != matrixB[i + j * n]) {
printf("FAILED \n");
}
}
void *parallel_matrixmul(void *threadarg) {
int threadId = (int) threadarg;
int lower_limit = (n / NUM_THREADS) * threadId;
int upper_limt = lower_limit + n / NUM_THREADS;
for (int ih = lower_limit; ih < upper_limt; ih += s) {
for (int jh = 0; jh < n; jh += s) {
for (int kh = 0; kh < n; kh += s) {
for (int il = 0; il < s; il++) {
for (int kl = 0; kl < s; kl++) {
for (int jl = 0; jl < s; jl++) {
C[ih + il][jh + jl] += A[ih + il][kh + kl] * B[kh + kl][jh + jl];
}
}
}
}
}
}
pthread_exit(NULL);
}
int main() {
int rc;
//populate the matrices with random values between 0.0 and 1.0
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = (double) rand() / (double) RAND_MAX;
B[i][j] = (double) rand() / (double) RAND_MAX;
C[i][j] = 0;
}
}
struct timespec start, end;
double time_spent;
// for (int i = 0; i < n; i++) {
// for (int k = 0; k < n; k++) {
// for (int j = 0; j < n; j++) {
// D[i][j] += A[i][k] * B[k][j];
// }
// }
// }
clock_gettime(CLOCK_REALTIME, &start);
//initialize threads
pthread_t threads[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++) {
rc = pthread_create(&threads[i], NULL, parallel_matrixmul, (void *) i);
if (rc) {
printf("ERROR; return code from pthread_join() is %d\n", rc);
exit(-1);
}
}
for (int i = 0; i < NUM_THREADS; i++) {
rc = pthread_join(threads[i], NULL);
if (rc) {
printf("ERROR; return code from pthread_join() is %d\n", rc);
exit(-1);
}
}
clock_gettime(CLOCK_REALTIME, &end);
time_spent = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.0;
printf("Elapsed time in seconds: %f \n", time_spent);
// compare_matrix(C, D);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment