-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixOP.c
More file actions
55 lines (52 loc) · 1.27 KB
/
MatrixOP.c
File metadata and controls
55 lines (52 loc) · 1.27 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <sys/time.h>
double **A;
double **B;
double **C;
int main(int argc, char*argv[])
{
int i,j,k;
struct timeval tv1, tv2;
struct timezone tz;
double elapsed;
int size=atoi(argv[1]);
int numTh=atoi(argv[2]);
int N=size;
double sum=0.0;
//printf("Size is %d",N);
A=(double **)calloc(sizeof(double *), size);
B=(double **)calloc(sizeof(double *), size);
C=(double **)calloc(sizeof(double *), size);
for(i=0;i<size;i++)
{
A[i]=(double *)calloc(sizeof(double), size);
B[i]=(double *)calloc(sizeof(double), size);
C[i]=(double *)calloc(sizeof(double), size);
}
omp_set_num_threads(numTh);
for (i= 0; i< N; i++)
for (j= 0; j< N; j++)
{
A[i][j] = 2;
B[i][j] = 2;
}
//printf("Number of Threads: %d",omp_get_num_procs());
gettimeofday(&tv1, &tz);
#pragma omp parallel for private(i,j,k) shared(A,B,C)
for (i = 0; i < N; ++i) {
for (j = 0; j < N; ++j) {
for (k = 0; k < N; ++k) { C[i][j]+=A[i][k] * B[k][j]; }
}
}
gettimeofday(&tv2, &tz);
elapsed = (double) (tv2.tv_sec-tv1.tv_sec) + (double) (tv2.tv_usec-tv1.tv_usec) * 1.e-6;
printf("elapsed time = %4.2lf seconds.\n", elapsed);
/*for (i= 0; i< N; i++) {
for (j= 0; j< N; j++) {
printf("%lf\t",C[i][j]);
}
printf("\n");
}*/
}