-
Notifications
You must be signed in to change notification settings - Fork 0
/
matadd.cpp
176 lines (143 loc) · 3.98 KB
/
matadd.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* In His Exalted Name
* Matrix Addition - Sequential Code
* Ahmad Siavashi, Email: siavashi@aut.ac.ir
* 15/04/2018
*/
// Let it be.
#define _CRT_SECURE_NO_WARNINGS
#define RUN_COUNT 10
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <omp.h>
#include <cmath>
typedef struct {
int *A, *B, *C;
int n, m;
} DataSet;
void fillDataSet(DataSet *dataSet);
void printDataSet(DataSet dataSet);
void closeDataSet(DataSet dataSet);
void add(DataSet dataSet);
void add2D(DataSet dataSet);
int main(int argc, char *argv[]) {
DataSet dataSet;
if (argc < 3) {
printf("[-] Invalid No. of arguments.\n");
printf("[-] Try -> <n> <m> \n");
printf(">>> ");
scanf("%d %d", &dataSet.n, &dataSet.m);
}
else {
dataSet.n = atoi(argv[1]);
dataSet.m = atoi(argv[2]);
}
printf("[-] Dataset size is: %d bytes\n\n", dataSet.n * dataSet.m * sizeof(int));
#ifndef _OPENMP
printf("OpenMP is not supported.\n");
return 0;
#endif
// omp_set_num_threads(8);
double starttime, elapsedtime;
double times_sum = 0;
for (int i = 0; i < RUN_COUNT; i++)
{
// get starting time
fillDataSet(&dataSet);
starttime = omp_get_wtime();
add(dataSet);
// get ending time and use it to determine elapsed time
elapsedtime = omp_get_wtime() - starttime;
// printDataSet(dataSet);
closeDataSet(dataSet);
// report elapsed time
printf("[-] Time Elapsed: %f Secs\n", elapsedtime);
times_sum += elapsedtime;
}
printf("\n[-] The average running time was: %lf\n", times_sum / RUN_COUNT);
system("PAUSE");
return EXIT_SUCCESS;
}
void fillDataSet(DataSet *dataSet) {
int i, j;
dataSet->A = (int *)malloc(sizeof(int) * dataSet->n * dataSet->m);
dataSet->B = (int *)malloc(sizeof(int) * dataSet->n * dataSet->m);
dataSet->C = (int *)malloc(sizeof(int) * dataSet->n * dataSet->m);
srand(time(NULL));
#pragma omp parallel for private(i, j) num_threads(8)
for (i = 0; i < dataSet->n; i++) {
for (j = 0; j < dataSet->m; j++) {
dataSet->A[i*dataSet->m + j] = rand() % 100;
dataSet->B[i*dataSet->m + j] = rand() % 100;
}
}
}
void printDataSet(DataSet dataSet) {
int i, j;
printf("[-] Matrix A\n");
for (i = 0; i < dataSet.n; i++) {
for (j = 0; j < dataSet.m; j++) {
printf("%-4d", dataSet.A[i*dataSet.m + j]);
}
putchar('\n');
}
printf("[-] Matrix B\n");
for (i = 0; i < dataSet.n; i++) {
for (j = 0; j < dataSet.m; j++) {
printf("%-4d", dataSet.B[i*dataSet.m + j]);
}
putchar('\n');
}
printf("[-] Matrix C\n");
for (i = 0; i < dataSet.n; i++) {
for (j = 0; j < dataSet.m; j++) {
printf("%-4d", dataSet.C[i*dataSet.m + j]);
}
putchar('\n');
}
}
void closeDataSet(DataSet dataSet) {
free(dataSet.A);
free(dataSet.B);
free(dataSet.C);
}
void add(DataSet dataSet) {
int i, j;
#pragma omp parallel for private(i, j) num_threads(1)
for (i = 0; i < dataSet.n; i++) {
for (j = 0; j < dataSet.m; j++) {
dataSet.C[i * dataSet.m + j] = dataSet.A[i * dataSet.m + j] + dataSet.B[i * dataSet.m + j];
// dataSet.C[i * dataSet.m + j] = omp_get_thread_num();
}
}
}
void add2D(DataSet dataSet) {
#pragma omp parallel num_threads(8)
{
int i, j;
int n_threads = omp_get_num_threads();
double n_th_sqrt = sqrt(n_threads);
int row_blocks, col_blocks;
if (abs((int)n_th_sqrt - n_th_sqrt) < 1e-4) // sqrt(n) is integer
{
row_blocks = n_th_sqrt;
col_blocks = n_th_sqrt;
}
else {
row_blocks = sqrt(n_threads / 2) * 2;
col_blocks = sqrt(n_threads / 2);
}
int row_block_size = ceil((double)dataSet.n / row_blocks);
int col_block_size = ceil((double)dataSet.m / col_blocks);
int curr_thread = omp_get_thread_num();
int row = curr_thread / col_blocks;
int col = curr_thread % col_blocks;
for (i = row * row_block_size; (i < (row + 1) * row_block_size) && (i < dataSet.n); i++) {
for (j = col * col_block_size; (j < (col + 1) * col_block_size) && (j < dataSet.m); j++) {
dataSet.C[i * dataSet.m + j] = dataSet.A[i * dataSet.m + j] + dataSet.B[i * dataSet.m + j];
// dataSet.C[i * dataSet.m + j] = omp_get_thread_num();
}
}
}
}