-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathintro_parallel_for.cpp
More file actions
148 lines (126 loc) · 4.43 KB
/
Copy pathintro_parallel_for.cpp
File metadata and controls
148 lines (126 loc) · 4.43 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
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
/*
Copyright (c) 2025 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <iostream>
#include <vector>
#include <tbb/tbb.h>
#include "intro_examples.h"
using ImagePtr = std::shared_ptr<ch01::Image>;
void writeImage(ImagePtr image_ptr);
ImagePtr applyGamma(ImagePtr image_ptr, double gamma) {
auto output_image_ptr =
std::make_shared<ch01::Image>(image_ptr->name() + "_gamma",
ch01::IMAGE_WIDTH, ch01::IMAGE_HEIGHT);
auto in_rows = image_ptr->rows();
auto out_rows = output_image_ptr->rows();
const int height = in_rows.size();
const int width = in_rows[1] - in_rows[0];
tbb::parallel_for( 0, height,
[&in_rows, &out_rows, width, gamma](int i) {
for ( int j = 0; j < width; ++j ) {
const ch01::Image::Pixel& p = in_rows[i][j];
double v = 0.3*p.bgra[2] + 0.59*p.bgra[1] + 0.11*p.bgra[0];
double res = pow(v, gamma);
if(res > ch01::MAX_BGR_VALUE) res = ch01::MAX_BGR_VALUE;
out_rows[i][j] = ch01::Image::Pixel(res, res, res);
}
}
);
return output_image_ptr;
}
ImagePtr applyTint(ImagePtr image_ptr, const double *tints) {
auto output_image_ptr =
std::make_shared<ch01::Image>(image_ptr->name() + "_tinted",
ch01::IMAGE_WIDTH, ch01::IMAGE_HEIGHT);
auto in_rows = image_ptr->rows();
auto out_rows = output_image_ptr->rows();
const int height = in_rows.size();
const int width = in_rows[1] - in_rows[0];
tbb::parallel_for( 0, height,
[&in_rows, &out_rows, width, tints](int i) {
for ( int j = 0; j < width; ++j ) {
const ch01::Image::Pixel& p = in_rows[i][j];
std::uint8_t b = (double)p.bgra[0] +
(ch01::MAX_BGR_VALUE-p.bgra[0])*tints[0];
std::uint8_t g = (double)p.bgra[1] +
(ch01::MAX_BGR_VALUE-p.bgra[1])*tints[1];
std::uint8_t r = (double)p.bgra[2] +
(ch01::MAX_BGR_VALUE-p.bgra[2])*tints[2];
out_rows[i][j] =
ch01::Image::Pixel(
(b > ch01::MAX_BGR_VALUE) ? ch01::MAX_BGR_VALUE : b,
(g > ch01::MAX_BGR_VALUE) ? ch01::MAX_BGR_VALUE : g,
(r > ch01::MAX_BGR_VALUE) ? ch01::MAX_BGR_VALUE : r
);
}
}
);
return output_image_ptr;
}
void myfuncPF(std::vector<ImagePtr>& image_vector) {
const double tint_array[] = {0.75, 0, 0};
tbb::flow::graph g;
int i = 0;
tbb::flow::input_node<ImagePtr> src( g, [&]( tbb::flow_control &fc ) -> ImagePtr
{
if ( i < image_vector.size() )
{
return image_vector[i++];
}
else
{
fc.stop();
return nullptr;
}
});
tbb::flow::function_node<ImagePtr, ImagePtr> gamma(g,
tbb::flow::unlimited,
[] (ImagePtr img) -> ImagePtr {
return applyGamma(img, 1.4);
}
);
tbb::flow::function_node<ImagePtr, ImagePtr> tint(g,
tbb::flow::unlimited,
[tint_array] (ImagePtr img) -> ImagePtr {
return applyTint(img, tint_array);
}
);
tbb::flow::function_node<ImagePtr> write(g,
tbb::flow::unlimited,
[] (ImagePtr img) {
writeImage(img);
}
);
tbb::flow::make_edge(src, gamma);
tbb::flow::make_edge(gamma, tint);
tbb::flow::make_edge(tint, write);
src.activate();
g.wait_for_all();
}
void writeImage(ImagePtr image_ptr) {
image_ptr->write( (image_ptr->name() + ".bmp").c_str());
}
int main(int argc, char* argv[]) {
std::vector<ImagePtr> image_vector;
for ( int i = 2000; i < 20000000; i *= 10 )
image_vector.push_back(ch01::makeFractalImage(i));
// warmup the scheduler
tbb::parallel_for(0, tbb::this_task_arena::max_concurrency(), [](int) {
tbb::tick_count t0 = tbb::tick_count::now();
while ((tbb::tick_count::now() - t0).seconds() < 0.01);
});
tbb::tick_count t0 = tbb::tick_count::now();
myfuncPF(image_vector);
std::cout << "Time : " << (tbb::tick_count::now()-t0).seconds()
<< " seconds" << std::endl;
return 0;
}