forked from mapnik/python-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_encoding.cpp
More file actions
110 lines (94 loc) · 3.22 KB
/
Copy pathparallel_encoding.cpp
File metadata and controls
110 lines (94 loc) · 3.22 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
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2015 Artem Pavlenko, Jean-Francois Doyon
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#include <mapnik/config.hpp>
#pragma GCC diagnostic push
#include <mapnik/warning_ignore.hpp>
#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/stl_iterator.hpp>
#pragma GCC diagnostic pop
// mapnik
#include <mapnik/image_util.hpp>
#include <mapnik/image_view_any.hpp>
#include <mapnik/util/parallelize.hpp>
unsigned jobs_by_chunks(unsigned chunks, unsigned max_concurrency=0)
{
unsigned max_jobs = max_concurrency ? max_concurrency :
std::max(1u, std::thread::hardware_concurrency() / 2);
return std::max(1u, std::min(chunks, max_jobs));
}
struct encoding_chunk
{
boost::python::object key;
mapnik::image_view_any const & img;
std::string encoded_img;
};
struct encoding_func
{
std::vector<encoding_chunk> & chunks;
std::string const & format;
void operator()(unsigned begin, unsigned end)
{
for (unsigned i = begin; i < end; ++i)
{
encoding_chunk & chunk = chunks[i];
chunk.encoded_img = std::move(mapnik::save_to_string(chunk.img, format));
}
}
};
void encode_parallel(boost::python::dict & tiles, std::string const & format)
{
using namespace boost::python;
std::vector<encoding_chunk> chunks;
auto tiles_iterator = tiles.items();
stl_input_iterator<tuple> it(tiles_iterator), end;
for (; it != end; ++it)
{
extract<mapnik::image_view_any const &> img((*it)[1]);
if (img.check())
{
chunks.emplace_back(encoding_chunk{ (*it)[0], img() });
}
}
unsigned jobs = jobs_by_chunks(chunks.size());
encoding_func enc_func{chunks, format};
mapnik::util::parallelize(enc_func, jobs, chunks.size());
for (auto const & chunk : chunks)
{
// TODO: move the data instead of copy, if possible
tiles[chunk.key] = boost::python::object(
boost::python::handle<>(
PyBytes_FromStringAndSize(
chunk.encoded_img.data(),
chunk.encoded_img.size())));
}
}
void export_encode_parallel()
{
using namespace boost::python;
def("encode_parallel", &encode_parallel,
(arg("tiles"),
arg("format")
)
);
}