🎉 First commit

This commit is contained in:
Nicolas Rojas 2025-05-29 20:26:11 -05:00
commit 3b8d21dde2
Signed by: nicolas
SSH key fingerprint: SHA256:gi4v1tDcXHbV+fkvqqs9b5rkFlo4Q9DHXp90MifkZK0
17 changed files with 571 additions and 0 deletions

86
MatMult/MM1c.c Normal file
View file

@ -0,0 +1,86 @@
/************************************************************************
* Autor: J. Corredor
* Fecha: Octubre 2023
* Computación de Alto Rendimiento
* Maestría en Inteligencia Artificial
* Tema: Programa de Multiplicación de Matrices usando hilos OpenMP
* -Algorimo Clásico filasXcolumnas
*************************************************************************/
#include "sample.h"
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif
#define DATA_SZ (1024 * 1024 * 64 * 3)
static double MEM_CHUNK[DATA_SZ];
void Matrix_Init_col(int SZ, double *a, double *b, double *c) {
int j, k;
for (j = 0; j < SZ; j++) {
a[j + k * SZ] = 2.0 * (j + k);
b[j + k * SZ] = 3.2 * (j - k);
c[j + k * SZ] = 1.0;
}
}
int main(int argc, char **argv) {
int N;
if (argc < 2) {
printf("MM1c MatrixSize [Sample arguments ...]\n");
return -1;
}
N = (int)atof(argv[1]);
argc--;
argv++;
if (N > 1024 * 10) {
printf("Unvalid MatrixSize\n");
return -1;
}
Sample_Init(argc, argv);
#pragma omp parallel
{
int NTHR, THR, SZ;
int i, j, k;
double *a, *b, *c;
SZ = N;
THR = Sample_PAR_install();
NTHR = omp_get_num_threads();
a = MEM_CHUNK;
b = a + SZ * SZ;
c = b + SZ * SZ;
#pragma omp master
Matrix_Init_col(SZ, a, b, c);
Sample_Start(THR);
#pragma omp for
for (i = 0; i < SZ; i++)
for (j = 0; j < SZ; j++) {
double *pA, *pB, S;
S = 0.0;
pA = a + (i * SZ);
pB = b + j;
for (k = SZ; k > 0; k--, pA++, pB += SZ)
S += (*pA * *pB);
c[i * SZ + j] = S;
}
Sample_Stop(THR);
}
Sample_End(&N);
}

88
MatMult/MM1r.c Normal file
View file

@ -0,0 +1,88 @@
/************************************************************************
* Autor: N Rojas
* Fecha: Noviembre 2023
* Computación de Alto Rendimiento
* Maestría en Inteligencia Artificial
* Tema: Programa de Multiplicación de Matrices usando hilos OpenMP
* -Algorimo filasXfilas
*************************************************************************/
#include "sample.h"
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif
#define DATA_SZ (1024 * 1024 * 64 * 3)
static double MEM_CHUNK[DATA_SZ];
void Matrix_Init_col(int SZ, double *a, double *b, double *c) {
int j, k;
for (j = 0; j < SZ; j++) {
a[j + k * SZ] = 2.0 * (j + k);
b[j + k * SZ] = 3.2 * (j - k);
c[j + k * SZ] = 0.0;
}
}
int main(int argc, char **argv) {
int N;
if (argc < 2) {
printf("MM1r MatrixSize [Sample arguments ...]\n");
return -1;
}
N = (int)atof(argv[1]);
argc--;
argv++;
if (N > 1024 * 10) {
printf("Unvalid MatrixSize\n");
return -1;
}
Sample_Init(argc, argv);
#pragma omp parallel
{
int NTHR, THR, SZ;
int i, j, k;
double *a, *b, *c;
SZ = N;
THR = Sample_PAR_install();
NTHR = omp_get_num_threads();
a = MEM_CHUNK;
b = a + SZ * SZ;
c = b + SZ * SZ;
#pragma omp master
Matrix_Init_col(SZ, a, b, c);
Sample_Start(THR);
#pragma omp for
for (i = 0; i < SZ; ++i) {
double *pA, *pB, S;
pA = a + (i * SZ);
for (j = 0; j < SZ; ++j) {
pB = b + (j * SZ);
for (k = 0; k < SZ; ++k, ++pB) {
S = *pA * *pB;
c[i * SZ + k] += S;
}
++pA;
}
}
Sample_Stop(THR);
}
Sample_End(&N);
}

20
MatMult/Makefile Normal file
View file

@ -0,0 +1,20 @@
GCC = gcc
oT = -fopenmp -O3
CFLAGS = -lm
oL= Otime.c
BINDIR = ../
PROGS = $(BINDIR)MM1c $(BINDIR)MM1r
all: MM1c MM1r
clean:
$(RM) $(PROGS)
MM1c:
$(GCC) $(oT) $(oL) $@.c -o $(BINDIR)$@ $(CFLAGS)
MM1r:
$(GCC) $(oT) $(oL) $@.c -o $(BINDIR)$@ $(CFLAGS)

63
MatMult/Otime.c Normal file
View file

@ -0,0 +1,63 @@
#include <errno.h>
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#define MAX_THREADS 20
struct timeval start[MAX_THREADS];
struct timeval stop[MAX_THREADS];
static int N_THREADS;
void Sample_Start(int THR) {
#pragma omp barrier
gettimeofday(start + THR, (void *)0);
}
void Sample_Stop(int THR) { gettimeofday(&(stop[THR]), (void *)0); }
void Sample_Init(int argc, char *argv[]) {
if (argc < 3) {
printf("Sample parameters: NumberThreads \n");
exit(1);
}
N_THREADS = (int)atof(argv[1]);
if (!N_THREADS || N_THREADS > MAX_THREADS) {
printf("Number of Threads is not valid\n");
exit(1);
}
omp_set_num_threads(N_THREADS);
}
int Sample_PAR_install() {
int THR;
THR = omp_get_thread_num();
return THR;
}
void Sample_End(const int *SZ) {
int THR, i;
for (THR = 0; THR < N_THREADS; THR++) {
printf("%1.0f,", (double)*SZ);
printf("%1.0f,", (double)N_THREADS);
printf("%1.0f,", (double)THR);
stop[THR].tv_usec -= start[THR].tv_usec;
if (stop[THR].tv_usec < 0) {
stop[THR].tv_usec += 1000000;
stop[THR].tv_sec--;
}
stop[THR].tv_sec -= start[THR].tv_sec;
printf("%1.0f\n", (double)(stop[THR].tv_sec * 1000000 + stop[THR].tv_usec));
}
}

5
MatMult/sample.h Normal file
View file

@ -0,0 +1,5 @@
extern void Sample_Init(int argc, char *argv[]);
extern void Sample_Start(int THR);
void Sample_Stop(int THR);
int Sample_PAR_install(void);
void Sample_End(const int *SZ);