-
Notifications
You must be signed in to change notification settings - Fork 0
/
MM1fDynamic.c
63 lines (51 loc) · 1.8 KB
/
MM1fDynamic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
#include "interfaz.h"
int main(int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "%s <matrix size> <number of threads>\n", argv[0]);
return -1;
}
int MATRIX_SIZE = atoi(argv[1]);
argc--;
argv++;
int NUM_THREADS = atoi(argv[1]);
argc--;
argv++;
double **matrixA = NULL, **matrixB = NULL, **result = NULL, **transposeMatrixB = NULL;
if ((matrixA = (double **)malloc(MATRIX_SIZE * sizeof(double *))) == NULL ||
(matrixB = (double **)malloc(MATRIX_SIZE * sizeof(double *))) == NULL ||
(transposeMatrixB = (double **)malloc(MATRIX_SIZE * sizeof(double *))) == NULL ||
(result = (double **)malloc(MATRIX_SIZE * sizeof(double *))) == NULL)
{
printf("Not enough memory\n");
return -1;
}
for (int i = 0; i < MATRIX_SIZE; i++)
{
if ((matrixA[i] = (double *)malloc(MATRIX_SIZE * sizeof(double))) == NULL ||
(matrixB[i] = (double *)malloc(MATRIX_SIZE * sizeof(double))) == NULL ||
(transposeMatrixB[i] = (double *)malloc(MATRIX_SIZE * sizeof(double))) == NULL ||
(result[i] = (double *)malloc(MATRIX_SIZE * sizeof(double))) == NULL)
{
printf("Not enough memory\n");
return -1;
}
}
omp_set_num_threads(NUM_THREADS); // Sets # of threads to be used
#pragma omp parallel // Declaration of Open MP pragma
{
#pragma omp master
initDynamicMatrixTranspose(MATRIX_SIZE, matrixA, matrixB, result, transposeMatrixB);
transposeMatrix(MATRIX_SIZE, matrixB, transposeMatrixB); // Transpose matrix execution
sample_start();
multiplyMatrix(MATRIX_SIZE, matrixA, transposeMatrixB, result);
}
sample_stop();
sample_end();
freeReservedMemoryTranspose(MATRIX_SIZE, matrixA, matrixB, result, transposeMatrixB);
return 0;
}