Updated README, added rayon and tests for comparision

This commit is contained in:
Jack Yu 2021-04-30 19:24:15 -04:00
commit e36141a7cf
7 changed files with 308 additions and 0 deletions

55
omp/Makefile Normal file
View file

@ -0,0 +1,55 @@
#
# File: Makefile
# Author: Jack Yu (yyu57)
# Simple generic makefile for my C projects
# Run 'make build' to compile binary
#
CC ?= gcc
CFLAGS := -std=gnu17 -Werror -Wall -O3
CFLAGS_SEQ := $(CFLAGS)
CFLAGS_OMP := $(CFLAGS) -fopenmp -DPAR
CFLAGS_OMP_SAFE := $(CFLAGS_OMP) -DCRIT
BIN := matmul
BIN_SEQ := seqc_$(BIN)
BIN_OMP := omp_$(BIN)
BIN_OMP_SAFE := ompsafe_$(BIN)
SRCDIR := src
BLDDIR := build
SRCS := $(shell find $(SRCDIR) -name '*.c')
# === Recipes ===
.DEFAULT_GOAL := all
all: $(BIN_SEQ) $(BIN_OMP) $(BIN_OMP_SAFE)
$(BIN_SEQ): $(BLDDIR)/$(BIN_SEQ)
@cp $(BLDDIR)/$(BIN_SEQ) $(BIN_SEQ)
$(BIN_OMP): $(BLDDIR)/$(BIN_OMP)
@cp $(BLDDIR)/$(BIN_OMP) $(BIN_OMP)
$(BIN_OMP_SAFE): $(BLDDIR)/$(BIN_OMP_SAFE)
@cp $(BLDDIR)/$(BIN_OMP_SAFE) $(BIN_OMP_SAFE)
$(BLDDIR)/$(BIN_SEQ): $(BLDDIR) $(SRCS)
$(CC) $(CFLAGS_SEQ) $(SRCS) -o $@
$(BLDDIR)/$(BIN_OMP): $(BLDDIR) $(SRCS)
$(CC) $(CFLAGS_OMP) $(SRCS) -o $@
$(BLDDIR)/$(BIN_OMP_SAFE): $(BLDDIR) $(SRCS)
$(CC) $(CFLAGS_OMP_SAFE) $(SRCS) -o $@
build: all
$(BLDDIR):
@mkdir -p $(BLDDIR)
clean:
@rm -rf $(BLDDIR)
@rm -f $(BIN_SEQ) $(BIN_OMP) $(BIN_OMP_SAFE)
.PHONY: build run clean all

100
omp/src/main.c Normal file
View file

@ -0,0 +1,100 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/sysinfo.h>
#include <time.h>
#include <limits.h>
#include <unistd.h>
void
print_time_diff (struct timespec *start, struct timespec *end)
{
time_t sec;
long nsec;
sec = end->tv_sec - start->tv_sec;
nsec = end->tv_nsec - start->tv_nsec;
if (nsec < 0)
{
sec--;
nsec += 1000000000;
}
printf ("Elapsed time: %ld.%09ld sec\n", sec, nsec);
}
void
generate_matrices (double ***matrix, double ***result, size_t nsize)
{
size_t i, j;
*matrix = (double **) malloc (nsize * sizeof (double *));
*result = (double **) malloc (nsize * sizeof (double *));
srand (time (NULL));
for (i = 0; i < nsize; i++) {
(*matrix)[i] = (double *) malloc (nsize * sizeof (double));
(*result)[i] = (double *) malloc (nsize * sizeof (double));
for (j = 0; j < nsize; j++) {
(*matrix)[i][j] = 255.0f * ((double) rand () - ((double) RAND_MAX/2.0f));
(*result)[i][j] = 0;
}
}
}
void
free_all (double **matrix, double **result, size_t nsize)
{
size_t i;
for (i = 0; i < nsize; i++) {
free (matrix[nsize]);
free (result[nsize]);
}
free (matrix);
free (result);
}
int main (int argc, char *argv[])
{
struct timespec start, end;
size_t i, j, k, nsize;
long raw_size;
double sum;
double **matrix, **result;
if (argc != 2) {
printf ("Usage: %s <nsize>\n", argv[0]);
exit(-1);
}
raw_size = atol (argv[1]);
if (raw_size < 1) {
printf ("Usage: %s <nsize>\n", argv[0]);
exit(-1);
}
nsize = raw_size;
generate_matrices (&matrix, &result, nsize);
clock_gettime (CLOCK_MONOTONIC, &start);
#ifdef PAR
#pragma omp parallel for default(shared) private(j, k, sum)
#endif
for (i = 0; i < nsize; i++) {
for (j = 0; j < nsize; j++) {
sum = 0;
for (k = 0; k < nsize; k++) {
sum += matrix[i][k] * matrix[k][j];
}
#ifdef PAR
#ifdef CRIT
#pragma omp critical
{
#endif
#endif
result[i][j] = sum;
#ifdef PAR
#ifdef CRIT
}
#endif
#endif
}
}
clock_gettime (CLOCK_MONOTONIC, &end);
print_time_diff (&start, &end);
free_all (matrix, result, nsize);
}