-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlect4.cpp
159 lines (139 loc) · 3.31 KB
/
lect4.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
#include <vector>
#include <thread>
#include <stdio.h>
#include <exception>
#include <locale.h>
#include <cilk/cilk.h>
#include "cilk/reducer_opadd.h"
enum class eprocess_type
{
by_rows = 0,
by_cols
};
void InitMatrix(double** matrix, const size_t numb_rows, const size_t numb_cols)
{
cilk_for (size_t i = 0; i < numb_rows; ++i)
{
cilk_for (size_t j = 0; j < numb_cols; ++j)
{
matrix[i][j] = rand() % 5 + 1;
}
}
}
void PrintMatrix(double** matrix, const size_t numb_rows, const size_t numb_cols)
{
printf("Generated matrix:\n");
for (size_t i = 0; i < numb_rows; ++i)
{
for (size_t j = 0; j < numb_cols; ++j)
{
printf("%lf ", matrix[i][j]);
}
printf("\n");
}
}
void FindAverageValues(eprocess_type proc_type, double** matrix, const size_t numb_rows, const size_t numb_cols, double* average_vals)
{
switch (proc_type)
{
case eprocess_type::by_rows:
{
cilk_for (size_t i = 0; i < numb_rows; ++i)
{
cilk::reducer_opadd<double> sum(0.0);
cilk_for (size_t j = 0; j < numb_cols; ++j)
{
sum += matrix[i][j];
}
average_vals[i] = sum.get_value() / numb_cols;
}
break;
}
case eprocess_type::by_cols:
{
cilk_for (size_t j = 0; j < numb_cols; ++j)
{
cilk::reducer_opadd<double> sum(0.0);
cilk_for(size_t i = 0; i < numb_rows; ++i)
{
sum += matrix[i][j];
}
average_vals[j] = sum.get_value() / numb_rows;
}
break;
}
default:
{
throw("Incorrect value for parameter 'proc_type' in function FindAverageValues() call!");
}
}
}
void PrintAverageVals(eprocess_type proc_type, double* average_vals, const size_t dimension)
{
switch (proc_type)
{
case eprocess_type::by_rows:
{
printf("\nAverage values in rows:\n");
for (size_t i = 0; i < dimension; ++i)
{
printf("Row %u: %lf\n", i, average_vals[i]);
}
break;
}
case eprocess_type::by_cols:
{
printf("\nAverage values in columns:\n");
for (size_t i = 0; i < dimension; ++i)
{
printf("Column %u: %lf\n", i, average_vals[i]);
}
break;
}
default:
{
throw("Incorrect value for parameter 'proc_type' in function PrintAverageVals() call!");
}
}
}
int main()
{
const unsigned ERROR_STATUS = -1;
const unsigned OK_STATUS = 0;
unsigned status = OK_STATUS;
try
{
srand((unsigned)time(0));
const size_t numb_rows = 5;
const size_t numb_cols = 5;
double** matrix = new double*[numb_rows];
for (size_t i = 0; i < numb_rows; ++i)
{
matrix[i] = new double[numb_cols];
}
double* average_vals_in_rows = new double[numb_rows];
double* average_vals_in_cols = new double[numb_cols];
InitMatrix(matrix, numb_rows, numb_cols);
PrintMatrix(matrix, numb_rows, numb_cols);
std::thread first_thr(FindAverageValues, eprocess_type::by_rows, matrix, numb_rows, numb_cols, average_vals_in_rows);
std::thread second_thr(FindAverageValues, eprocess_type::by_cols, matrix, numb_rows, numb_cols, average_vals_in_cols);
first_thr.join();
second_thr.join();
PrintAverageVals(eprocess_type::by_rows, average_vals_in_rows, numb_rows);
PrintAverageVals(eprocess_type::by_cols, average_vals_in_cols, numb_cols);
delete[] average_vals_in_rows;
delete[] average_vals_in_cols;
for (size_t i = 0; i < numb_rows; ++i)
{
delete[] matrix[i];
}
delete[] matrix;
}
catch (std::exception& except)
{
printf("Error occured!\n");
except.what();
status = ERROR_STATUS;
}
return status;
}