-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmult_matrix.c
75 lines (60 loc) · 1.32 KB
/
mult_matrix.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
64
65
66
67
68
69
70
#include <math.h>
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define NROWS 2000
#define NCOLS 2000
/*
*Multiplicação de matrizes
*/
void mult_matrix(int *matA, int *matB);
int main(int argc, char **argv){
int *matA = malloc(NROWS * NCOLS * sizeof(int));
int *matB = malloc(NROWS * NCOLS * sizeof(int));
int numThread = 4;
srand(time(NULL));
for (int i = 0; i < NROWS; i++)
{
for (int j = 0; j < NCOLS; j++)
{
matA[(i*NROWS)+j] = rand() % 100;
matB[(i*NROWS)+j] = rand() % 100;
}
}
struct timeval start, end;
gettimeofday(&start, NULL);
mult_matrix(matA,matB);
gettimeofday(&end, NULL);
printf("Tempo total:%lu\n",((end.tv_sec * 1000000 + end.tv_usec)
- (start.tv_sec * 1000000 + start.tv_usec)));
return 0;
}
void mult_matrix(int *matA, int *matB){
int *result = malloc(NROWS * NCOLS * sizeof(int));
int count = 0;
for (int i = 0; i < NROWS; i++)
{
for (int j = 0; j < NROWS; j++)
{
count = 0;
for (int k = 0; k < NCOLS ; k++)
{
count += (matA[(i*NROWS)+k]*matB[(k*NROWS)+j]);
}
result[(i*NROWS)+j] = count;
}
}
//printf("Número de Threads:%d\n",omp_get_num_threads());
/*
for (int i = 0; i < 100; i++)
{
printf("|");
for (int j = 0; j < 100; j++)
{
printf("%d\t", result[i][j]);
}
printf("|\n");
}
*/
}