Skip to content

Commit 8222f23

Browse files
author
AndreySorokin7
committed
fix
1 parent f5f0f14 commit 8222f23

4 files changed

Lines changed: 44 additions & 39 deletions

File tree

include/layers/Layer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Layer {
6666
protected:
6767
int id_ = 0;
6868
LayerType type_;
69-
ParBackend parallel_backend_ = ParBackend::Seq;
69+
ParBackend parallel_backend_ = ParBackend::kSeq;
7070
};
7171

7272
template <typename ValueType>

include/parallel/backends.hpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,25 @@
1717
namespace it_lab_ai {
1818
namespace parallel {
1919

20-
enum class Backend { Seq = 0, Threads = 1, TBB = 2, OMP = 3 };
20+
enum class Backend { kSeq = 0, kThreads = 1, kTbb = 2, kOmp = 3 };
2121

2222
struct Options {
23-
Backend backend = Backend::Seq;
23+
Backend backend = Backend::kSeq;
2424
int max_threads = 0;
2525
std::size_t min_parallel_n = 1000;
2626
std::size_t grain = 1024;
2727
};
2828

29-
inline void impl_seq(std::size_t count, std::function<void(std::size_t)> func) {
29+
inline void impl_seq(std::size_t count,
30+
const std::function<void(std::size_t)>& func) {
3031
for (std::size_t i = 0; i < count; ++i) {
3132
func(i);
3233
}
3334
std::cout << "Seq " << std::endl;
3435
}
3536

3637
inline void impl_threads(std::size_t count,
37-
std::function<void(std::size_t)> func,
38+
const std::function<void(std::size_t)>& func,
3839
const Options& opt) {
3940
int num_threads = opt.max_threads > 0
4041
? opt.max_threads
@@ -73,7 +74,8 @@ inline void impl_threads(std::size_t count,
7374
std::cout << "Stl " << std::endl;
7475
}
7576

76-
inline void impl_tbb(std::size_t count, std::function<void(std::size_t)> func,
77+
inline void impl_tbb(std::size_t count,
78+
const std::function<void(std::size_t)>& func,
7779
const Options& opt) {
7880
std::cout << "tbb " << std::endl;
7981
oneapi::tbb::parallel_for(
@@ -87,30 +89,33 @@ inline void impl_tbb(std::size_t count, std::function<void(std::size_t)> func,
8789
}
8890

8991
#ifdef HAS_OPENMP
90-
inline void impl_omp(std::size_t count, std::function<void(std::size_t)> func,
92+
inline void impl_omp(std::size_t count,
93+
const std::function<void(std::size_t)>& func,
9194
const Options& opt) {
9295
if (count == 0) return;
9396

9497
int num_threads = opt.max_threads > 0
9598
? opt.max_threads
9699
: static_cast<int>(std::thread::hardware_concurrency());
97-
int chunk_size =
98-
static_cast<int>(std::max(opt.grain, count / (num_threads * 8)));
100+
101+
// Убрана неиспользуемая переменная chunk_size
102+
static_cast<void>(std::max(opt.grain, count / (num_threads * 8)));
99103

100104
int int_count = static_cast<int>(count);
101105
if (int_count < 0 || static_cast<std::size_t>(int_count) != count) {
102106
impl_seq(count, func);
103107
return;
104108
}
105109

106-
#pragma omp parallel for schedule(static, chunk_size) num_threads(num_threads)
110+
#pragma omp parallel for schedule(static) num_threads(num_threads)
107111
for (int i = 0; i < int_count; ++i) {
108112
func(static_cast<std::size_t>(i));
109113
}
110114
std::cout << "OMP " << std::endl;
111115
}
112116
#else
113-
inline void impl_omp(std::size_t count, std::function<void(std::size_t)> func,
117+
inline void impl_omp(std::size_t count,
118+
const std::function<void(std::size_t)>& func,
114119
const Options& opt) {
115120
impl_seq(count, func);
116121
}

include/parallel/parallel.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace it_lab_ai {
55
namespace parallel {
66

7-
constexpr bool has_omp =
7+
constexpr bool kHasOmp =
88
#ifdef HAS_OPENMP
99
true;
1010
#else
@@ -13,23 +13,23 @@ constexpr bool has_omp =
1313

1414
inline Backend resolve_default_backend(std::size_t n, const Options& opt) {
1515
if (n < opt.min_parallel_n) {
16-
return Backend::Seq;
16+
return Backend::kSeq;
1717
}
1818

1919
#ifdef HAS_OPENMP
20-
return Backend::OMP;
20+
return Backend::kOmp;
2121
#else
22-
return Backend::TBB;
22+
return Backend::kTbb;
2323
#endif
2424
}
2525

2626
inline Backend select_backend(const Options& opt, std::size_t n) {
27-
if (opt.backend != Backend::Seq && n < opt.min_parallel_n) {
28-
return Backend::Seq;
27+
if (opt.backend != Backend::kSeq && n < opt.min_parallel_n) {
28+
return Backend::kSeq;
2929
}
3030

31-
if (opt.backend == Backend::Seq || opt.backend == Backend::Threads ||
32-
opt.backend == Backend::TBB || opt.backend == Backend::OMP) {
31+
if (opt.backend == Backend::kSeq || opt.backend == Backend::kThreads ||
32+
opt.backend == Backend::kTbb || opt.backend == Backend::kOmp) {
3333
return opt.backend;
3434
}
3535

@@ -44,16 +44,16 @@ inline void parallel_for(std::size_t count, Func&& func,
4444
Backend backend = select_backend(opt, count);
4545

4646
switch (backend) {
47-
case Backend::Seq:
47+
case Backend::kSeq:
4848
impl_seq(count, std::forward<Func>(func));
4949
break;
50-
case Backend::Threads:
50+
case Backend::kThreads:
5151
impl_threads(count, std::forward<Func>(func), opt);
5252
break;
53-
case Backend::TBB:
53+
case Backend::kTbb:
5454
impl_tbb(count, std::forward<Func>(func), opt);
5555
break;
56-
case Backend::OMP:
56+
case Backend::kOmp:
5757
impl_omp(count, std::forward<Func>(func), opt);
5858
break;
5959
}

test/single_layer/test_ewlayer.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,13 @@ TEST(ewlayer, new_ewlayer_can_sigmoid_float_extreme_values) {
218218

219219
TEST(ewlayer, parallel_for_ew) {
220220
EWLayer layer0("relu");
221-
layer0.setParallelBackend(ParBackend::Seq);
221+
layer0.setParallelBackend(ParBackend::kSeq);
222222
EWLayer layer1("relu");
223-
layer1.setParallelBackend(ParBackend::Threads);
223+
layer1.setParallelBackend(ParBackend::kThreads);
224224
EWLayer layer2("relu");
225-
layer2.setParallelBackend(ParBackend::TBB);
225+
layer2.setParallelBackend(ParBackend::kTbb);
226226
EWLayer layer3("relu");
227-
layer3.setParallelBackend(ParBackend::OMP);
227+
layer3.setParallelBackend(ParBackend::kOmp);
228228

229229
std::vector<int> vec(800000000, -1);
230230
Tensor input = make_tensor<int>(vec);
@@ -275,13 +275,13 @@ TEST(ewlayer, parallel_for_ew) {
275275

276276
TEST(ewlayer, parallel_for_ew_sigmoid) {
277277
EWLayer layer0("sigmoid");
278-
layer0.setParallelBackend(ParBackend::Seq);
278+
layer0.setParallelBackend(ParBackend::kSeq);
279279
EWLayer layer1("sigmoid");
280-
layer1.setParallelBackend(ParBackend::Threads);
280+
layer1.setParallelBackend(ParBackend::kThreads);
281281
EWLayer layer2("sigmoid");
282-
layer2.setParallelBackend(ParBackend::TBB);
282+
layer2.setParallelBackend(ParBackend::kTbb);
283283
EWLayer layer3("sigmoid");
284-
layer3.setParallelBackend(ParBackend::OMP);
284+
layer3.setParallelBackend(ParBackend::kOmp);
285285

286286
std::vector<int> vec(800000000, -1);
287287
Tensor input = make_tensor<int>(vec);
@@ -337,7 +337,7 @@ TEST(ewlayer, parallel_for_direct) {
337337
auto start = std::chrono::high_resolution_clock::now();
338338
parallel::parallel_for(
339339
SIZE * SIZE, [&](std::size_t i) { result[i] = matrix1[i] + matrix2[i]; },
340-
ParBackend::Seq);
340+
ParBackend::kSeq);
341341

342342
auto end = std::chrono::high_resolution_clock::now();
343343
auto total_duration =
@@ -350,7 +350,7 @@ TEST(ewlayer, parallel_for_direct) {
350350
start = std::chrono::high_resolution_clock::now();
351351
parallel::parallel_for(
352352
SIZE * SIZE, [&](std::size_t i) { result[i] = matrix1[i] + matrix2[i]; },
353-
ParBackend::Threads);
353+
ParBackend::kThreads);
354354
end = std::chrono::high_resolution_clock::now();
355355
total_duration =
356356
std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
@@ -361,7 +361,7 @@ TEST(ewlayer, parallel_for_direct) {
361361
start = std::chrono::high_resolution_clock::now();
362362
parallel::parallel_for(
363363
SIZE * SIZE, [&](std::size_t i) { result[i] = matrix1[i] + matrix2[i]; },
364-
ParBackend::TBB);
364+
ParBackend::kTbb);
365365
end = std::chrono::high_resolution_clock::now();
366366
total_duration =
367367
std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
@@ -371,7 +371,7 @@ TEST(ewlayer, parallel_for_direct) {
371371
start = std::chrono::high_resolution_clock::now();
372372
parallel::parallel_for(
373373
SIZE * SIZE, [&](std::size_t i) { result[i] = matrix1[i] + matrix2[i]; },
374-
ParBackend::OMP);
374+
ParBackend::kOmp);
375375
end = std::chrono::high_resolution_clock::now();
376376
total_duration =
377377
std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
@@ -392,7 +392,7 @@ TEST(ewlayer, parallel_for_notmatrix) {
392392
auto start = std::chrono::high_resolution_clock::now();
393393
parallel::parallel_for(
394394
SIZE * SIZE, [&](std::size_t i) { result[i] = matrix1[i] + 1; },
395-
ParBackend::Seq);
395+
ParBackend::kSeq);
396396

397397
auto end = std::chrono::high_resolution_clock::now();
398398
auto total_duration =
@@ -405,7 +405,7 @@ TEST(ewlayer, parallel_for_notmatrix) {
405405
start = std::chrono::high_resolution_clock::now();
406406
parallel::parallel_for(
407407
SIZE * SIZE, [&](std::size_t i) { result[i] = matrix1[i] + 1; },
408-
ParBackend::Threads);
408+
ParBackend::kThreads);
409409
end = std::chrono::high_resolution_clock::now();
410410
total_duration =
411411
std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
@@ -416,7 +416,7 @@ TEST(ewlayer, parallel_for_notmatrix) {
416416
start = std::chrono::high_resolution_clock::now();
417417
parallel::parallel_for(
418418
SIZE * SIZE, [&](std::size_t i) { result[i] = matrix1[i] + 1; },
419-
ParBackend::TBB);
419+
ParBackend::kTbb);
420420
end = std::chrono::high_resolution_clock::now();
421421
total_duration =
422422
std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
@@ -427,7 +427,7 @@ TEST(ewlayer, parallel_for_notmatrix) {
427427
start = std::chrono::high_resolution_clock::now();
428428
parallel::parallel_for(
429429
SIZE * SIZE, [&](std::size_t i) { result[i] = matrix1[i] + 1; },
430-
ParBackend::OMP);
430+
ParBackend::kOmp);
431431
end = std::chrono::high_resolution_clock::now();
432432
total_duration =
433433
std::chrono::duration_cast<std::chrono::milliseconds>(end - start);

0 commit comments

Comments
 (0)