-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel.c
80 lines (63 loc) · 2.6 KB
/
parallel.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
71
72
73
74
75
76
77
78
79
80
double parallelMultiplyFloat(floatTYPE** matrixA, floatTYPE** matrixB, floatTYPE** matrixC, int dimension){
struct timeval t0, t1;
gettimeofday(&t0, 0);
#pragma omp parallel for
for(int i=0; i<dimension; i++){
for(int j=0; j<dimension; j++){
for(int k=0; k<dimension; k++){
matrixC[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
gettimeofday(&t1, 0);
return getElapsedTime(t1, t0);
}
double parallelMultiplyDouble(doubleTYPE** matrixA, doubleTYPE** matrixB, doubleTYPE** matrixC, int dimension){
struct timeval t0, t1;
gettimeofday(&t0, 0);
#pragma omp parallel for
for(int i=0; i<dimension; i++){
for(int j=0; j<dimension; j++){
for(int k=0; k<dimension; k++){
matrixC[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
gettimeofday(&t1, 0);
return getElapsedTime(t1, t0);
}
void parallelMultiplyTest(int dimension){
FILE* fp;
fp = fopen("parallelMultiplyReport.txt", "a+");
printf("----------------------------------\n");
printf("Operation : Parallel Multiply \n");
printf("----------------------------------\n");
printf("Dimension : %d\n", dimension);
fprintf(fp, "----------------------------------\n");
fprintf(fp, "Operation : Parallel Multiply \n");
fprintf(fp, "----------------------------------\n");
fprintf(fp, "Dimension : %d\n", dimension);
double* opmLatency = malloc(2 * sizeof(double));
// double matrix
doubleTYPE** doubleMatrixA = createDoubleSquareMatrix(dimension, 1.0);
doubleTYPE** doubleMatrixB = createDoubleSquareMatrix(dimension, 1.0);
doubleTYPE** doubleMatrixResult = createDoubleSquareMatrix(dimension, 0.0);
opmLatency[0] = parallelMultiplyDouble(doubleMatrixA, doubleMatrixB, doubleMatrixResult, dimension);
free(doubleMatrixResult);
free(doubleMatrixA);
free(doubleMatrixB);
printf("Duration(double) : %f\n", opmLatency[0]);
fprintf(fp, "Duration(double) : %f\n", opmLatency[0]);
// float matrix
floatTYPE** floatMatrixA = createFloatSquareMatrix(dimension, 1.0);
floatTYPE** floatMatrixB = createFloatSquareMatrix(dimension, 1.0);
floatTYPE** floatMatrixResult = createFloatSquareMatrix(dimension, 0.0);
opmLatency[1] = parallelMultiplyFloat(floatMatrixA, floatMatrixB, floatMatrixResult, dimension);
free(floatMatrixResult);
free(floatMatrixA);
free(floatMatrixB);
printf("Duration(float) : %f\n\n", opmLatency[1]);
fprintf(fp, "Duration(float) : %f\n\n", opmLatency[1]);
fclose(fp);
free(opmLatency);
}