Skip to content

Instantly share code, notes, and snippets.

@Suvink
Last active September 30, 2022 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Suvink/71fd4445000fd9c018ccbed845f45728 to your computer and use it in GitHub Desktop.
Save Suvink/71fd4445000fd9c018ccbed845f45728 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <sys/time.h>
#define MATRIX_SIZE 1024
int main()
{
int i,j,k,l,p,q,r;
static int a[MATRIX_SIZE][MATRIX_SIZE], b[MATRIX_SIZE][MATRIX_SIZE], result[MATRIX_SIZE][MATRIX_SIZE];
struct timeval start, stop;
int chunk = 32;
omp_set_num_threads(omp_get_num_procs());
// Create Matrixes
for (i = 0; i < MATRIX_SIZE; i++){
for (j = 0; j < MATRIX_SIZE; j++){
a[i][j] = rand();
b[i][j] = rand();
result[i][j] =0;
}
}
printf("Created Matrixes\n");
gettimeofday(&start, 0);
#pragma omp parallel private(i,j,k) shared(a, b, result)
#pragma omp for schedule(dynamic,chunk) nowait
for (i = 0; i < MATRIX_SIZE; ++i) {
for (j = 0; j < MATRIX_SIZE; ++j) {
for (k = 0; k < MATRIX_SIZE; ++k) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
//Calculate elapsed time
gettimeofday(&stop, 0);
printf("OpenMP\n");
printf("Elapsed time = %f\n", (stop.tv_sec+stop.tv_usec*1e-6)-(start.tv_sec+start.tv_usec*1e-6));
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <assert.h>
#include <pthread.h>
#define MATRIX_SIZE 1024
#define THREADS 2
void *slave(void *myid);
/*Shared Data*/
static int a[MATRIX_SIZE][MATRIX_SIZE], b[MATRIX_SIZE][MATRIX_SIZE], result[MATRIX_SIZE][MATRIX_SIZE];
void *slave(void *myid)
{
int x, low, high;
if (MATRIX_SIZE >= THREADS){
x = MATRIX_SIZE / THREADS;
low = (int)myid * x;
high = low + x;
}
else{
x = 1;
low = (int)myid;
if (low >= MATRIX_SIZE)
{
high = low;
}
else
{
high = low + 1;
}
}
int i, j, k;
for (i = low; i < high; i++)
{
for (j = 0; j < MATRIX_SIZE; j++)
{
result[i][j] = 0.0;
for (k = 0; k < MATRIX_SIZE; k++)
{
result[i][j] = result[i][j] + a[i][k] * b[k][j];
}
}
}
}
int main(int argc, char *argv[])
{
struct timeval start, stop;
int i, j;
pthread_t tid[THREADS];
//Create Matrixes
for(i = 0; i<MATRIX_SIZE; i++){
for(j = 0; j<MATRIX_SIZE; j++){
a[i][j] = 1 + (rand() % 20);
b[i][j] = 1 + (rand() % 20);
result[i][j] =0;
}
}
printf("Created Matrixes\n");
//Measure the start time
gettimeofday(&start, 0);
/*Create Threads*/
for (i = 0; i < THREADS; i++)
if (pthread_create(&tid[i], NULL, slave, (void *)i) != 0)
perror("Pthread create fails");
/*Join Threads*/
for (i = 0; i < THREADS; i++)
if (pthread_join(tid[i], NULL) != 0)
perror("Pthread join fails");
/*End Timing*/
gettimeofday(&stop, 0);
printf("PThread\n");
printf("Elapsed time = %f\n", (stop.tv_sec+stop.tv_usec*1e-6)-(start.tv_sec+start.tv_usec*1e-6));
return 0;
}
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#define MATRIX_SIZE 1024
int main(){
int i,j,l;
int sum = 0;
static int a[MATRIX_SIZE][MATRIX_SIZE], b[MATRIX_SIZE][MATRIX_SIZE], result[MATRIX_SIZE][MATRIX_SIZE];
struct timeval start, stop;
//Create Matrixes
for(i = 0; i<MATRIX_SIZE; i++){
for(j = 0; j<MATRIX_SIZE; j++){
a[i][j] = 1 + (rand() % 20);
b[i][j] = 1 + (rand() % 20);
result[i][j] =0;
}
}
printf("Created Matrixes\n");
//Measure the start time
gettimeofday(&start, 0);
//Multiply the matrices and input the values to the result matrix
for(i=0; i<MATRIX_SIZE; i++){
for(j=0; j<MATRIX_SIZE; j++){
for(l=0; l<MATRIX_SIZE; l++){
result[i][j] += a[i][l] * b[l][j];
}
}
}
//Calculate elapsed time
gettimeofday(&stop, 0);
printf("Sequencial\n");
printf("Elapsed time = %f\n", (stop.tv_sec+stop.tv_usec*1e-6)-(start.tv_sec+start.tv_usec*1e-6));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment