Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions include/polylabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,13 @@ Cell getCentroidCell(const Polygon& polygon) {
return Cell(area == 0 ? ring.at(0) : Point { ox + cx / area, oy + cy / area }, 0, polygon);
}

// Upper bound on pointToPolygonDist(p, polygon), from the edge nearest to `near.c`. The full scan
// Upper bound on pointToPolygonDist(p, polygon), from the edge nearest to `nearCell.c`. The full scan
// computes min over all edges of getSegDistSq(p, a, b) with the same arguments, so its result is
// <= this edge's value; the margin covers FP contraction differences between call sites.
inline double edgeDistBound(const Point& p, const Cell& near, const Polygon& polygon) {
const auto& ring = near.kr < 0 ? polygon.outer() : polygon.inners()[near.kr];
const Point& a = ring[near.ki];
const Point& b = ring[near.ki == 0 ? ring.size() - 1 : near.ki - 1];
inline double edgeDistBound(const Point& p, const Cell& nearCell, const Polygon& polygon) {
const auto& ring = nearCell.kr < 0 ? polygon.outer() : polygon.inners()[nearCell.kr];
const Point& a = ring[nearCell.ki];
const Point& b = ring[nearCell.ki == 0 ? ring.size() - 1 : nearCell.ki - 1];
return std::sqrt(getSegDistSq(p, a, b)) * (1 + 1e-12) + 1e-11;
}

Expand Down
5 changes: 5 additions & 0 deletions include/tile_coordinates_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class TileCoordinatesSet {
virtual void set(TileCoordinate x, TileCoordinate y) = 0;
virtual size_t size() const = 0;
virtual size_t zoom() const = 0;
// Set every tile with minX <= x <= maxX and minY <= y <= maxY.
virtual void setBox(TileCoordinate minX, TileCoordinate maxX, TileCoordinate minY, TileCoordinate maxY);
// Visit every set tile in x-major, y-minor order.
virtual void forEachSet(const std::function<void(TileCoordinate x, TileCoordinate y)>& f) const;
};
Expand All @@ -30,7 +32,10 @@ class PreciseTileCoordinatesSet : public TileCoordinatesSet {
size_t size() const override;
size_t zoom() const override;
void set(TileCoordinate x, TileCoordinate y) override;
void setBox(TileCoordinate minX, TileCoordinate maxX, TileCoordinate minY, TileCoordinate maxY) override;
void forEachSet(const std::function<void(TileCoordinate x, TileCoordinate y)>& f) const override;
// Add the tiles set in other, a set of the same zoom.
void merge(const PreciseTileCoordinatesSet& other);

private:
unsigned int zoom_;
Expand Down
149 changes: 107 additions & 42 deletions include/tile_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
#ifndef _TILE_DATA_H
#define _TILE_DATA_H

#include <algorithm>
#include <condition_variable>
#include <map>
#include <mutex>
#include <set>
#include <vector>
#include <memory>
#include <boost/asio/thread_pool.hpp>
#include <boost/asio/post.hpp>
#include "append_vector.h"
#include "clip_cache.h"
#include "mmap_allocator.h"
Expand All @@ -31,77 +36,134 @@ template<typename OO> void finalizeObjects(
typename std::vector<AppendVectorNS::AppendVector<OO>>::iterator end,
typename std::vector<std::vector<OO>>& lowZoom
) {
size_t z6OffsetDivisor = indexZoom >= CLUSTER_ZOOM ? (1 << (indexZoom - CLUSTER_ZOOM)) : 1;
#ifdef CLOCK_MONOTONIC
timespec startTs, endTs;
clock_gettime(CLOCK_MONOTONIC, &startTs);
#endif

int i = -1;
for (auto it = begin; it != end; it++) {
i++;
if (it->size() > 0 || i % 50 == 0 || i == 4095) {
std::cout << "\r" << name << ": finalizing z6 tile " << (i + 1) << "/" << CLUSTER_ZOOM_AREA;

const size_t shards = end - begin;
std::mutex progressMutex;
size_t done = 0;
auto reportDone = [&](size_t n) {
std::lock_guard<std::mutex> lock(progressMutex);
done += n;
std::cout << "\r" << name << ": finalizing z6 tile " << done << "/" << CLUSTER_ZOOM_AREA;
#ifdef CLOCK_MONOTONIC
clock_gettime(CLOCK_MONOTONIC, &endTs);
uint64_t elapsedNs = 1e9 * (endTs.tv_sec - startTs.tv_sec) + endTs.tv_nsec - startTs.tv_nsec;
std::cout << " (" << std::to_string((uint32_t)(elapsedNs / 1e6)) << " ms)";
clock_gettime(CLOCK_MONOTONIC, &endTs);
uint64_t elapsedNs = 1e9 * (endTs.tv_sec - startTs.tv_sec) + endTs.tv_nsec - startTs.tv_nsec;
std::cout << " (" << std::to_string((uint32_t)(elapsedNs / 1e6)) << " ms)";
#endif
std::cout << std::flush;
}
if (it->size() == 0)
continue;
std::cout << std::flush;
};

auto finalizeShard = [&](size_t i, size_t sortThreads) {
auto it = begin + i;
// We track a separate copy of low zoom objects to avoid scanning large
// lists of objects that may be on slow disk storage.
for (auto objectIt = it->begin(); objectIt != it->end(); objectIt++)
if (objectIt->oo.minZoom < CLUSTER_ZOOM)
lowZoom[i].push_back(*objectIt);

sortOutputObjects<OO>(indexZoom, threadNum, it->begin(), it->end());
sortOutputObjects<OO>(indexZoom, sortThreads, it->begin(), it->end());
};

// Shards are independent. A shard with more than 1/threadNum of all objects
// (e.g. one of the few populated z6 tiles of a small extract) is sorted on its
// own using all threads. The others are sorted in parallel, one thread each,
// largest first.
size_t total = 0, empty = 0;
for (auto it = begin; it != end; it++) total += it->size();
std::vector<size_t> big, small;
for (size_t i = 0; i < shards; i++) {
const size_t n = (begin + i)->size();
if (n == 0) empty++;
else if (threadNum <= 1 || n * threadNum > total) big.push_back(i);
else small.push_back(i);
}
std::sort(small.begin(), small.end(), [&](size_t a, size_t b) { return (begin + a)->size() > (begin + b)->size(); });
reportDone(empty);

for (size_t i : big) {
finalizeShard(i, threadNum);
reportDone(1);
}

if (!small.empty()) {
// Each sort copies its shard into a buffer, so cap the objects being
// sorted at once (a single shard may always run).
const size_t budget = std::max<size_t>(total / 8, (begin + small.front())->size());
size_t inFlight = 0;
std::mutex budgetMutex;
std::condition_variable budgetFreed;
boost::asio::thread_pool pool(threadNum);
for (size_t i : small) {
boost::asio::post(pool, [&, i]() {
const size_t n = (begin + i)->size();
{
std::unique_lock<std::mutex> lock(budgetMutex);
budgetFreed.wait(lock, [&]() { return inFlight == 0 || inFlight + n <= budget; });
inFlight += n;
}
finalizeShard(i, 1);
{
std::lock_guard<std::mutex> lock(budgetMutex);
inFlight -= n;
}
budgetFreed.notify_all();
reportDone(1);
});
}
pool.join();
}

std::cout << std::endl;
}

template<typename OO> void collectTilesWithObjectsAtZoomTemplate(
// Set the tiles holding the objects of z6 tile i (shard), and their parent tiles.
template<typename OO> void collectTilesWithObjectsInShardTemplate(
const unsigned int& indexZoom,
const typename std::vector<AppendVectorNS::AppendVector<OO>>::iterator objects,
const size_t size,
AppendVectorNS::AppendVector<OO>& shard,
const size_t i,
std::vector<std::shared_ptr<TileCoordinatesSet>>& zooms
) {
size_t maxZoom = zooms.size() - 1;
uint16_t z6OffsetDivisor = indexZoom >= CLUSTER_ZOOM ? (1 << (indexZoom - CLUSTER_ZOOM)) : 1;
int64_t lastX = -1;
int64_t lastY = -1;
for (size_t i = 0; i < size; i++) {
const size_t z6x = i / CLUSTER_ZOOM_WIDTH;
const size_t z6y = i % CLUSTER_ZOOM_WIDTH;

for (size_t j = 0; j < objects[i].size(); j++) {
// Compute the x, y at the base zoom level
TileCoordinate baseX = z6x * z6OffsetDivisor + objects[i][j].x;
TileCoordinate baseY = z6y * z6OffsetDivisor + objects[i][j].y;

// Translate the x, y at the requested zoom level
TileCoordinate x = baseX / (1 << (indexZoom - maxZoom));
TileCoordinate y = baseY / (1 << (indexZoom - maxZoom));

if (lastX != x || lastY != y) {
lastX = x;
lastY = y;

for (int zoom = maxZoom; zoom >= 0; zoom--) {
zooms[zoom]->set(x, y);
x /= 2;
y /= 2;
}
const size_t z6x = i / CLUSTER_ZOOM_WIDTH;
const size_t z6y = i % CLUSTER_ZOOM_WIDTH;

for (size_t j = 0; j < shard.size(); j++) {
// Compute the x, y at the base zoom level
TileCoordinate baseX = z6x * z6OffsetDivisor + shard[j].x;
TileCoordinate baseY = z6y * z6OffsetDivisor + shard[j].y;

// Translate the x, y at the requested zoom level
TileCoordinate x = baseX / (1 << (indexZoom - maxZoom));
TileCoordinate y = baseY / (1 << (indexZoom - maxZoom));

if (lastX != x || lastY != y) {
lastX = x;
lastY = y;

for (int zoom = maxZoom; zoom >= 0; zoom--) {
zooms[zoom]->set(x, y);
x /= 2;
y /= 2;
}
}
}
}

template<typename OO> void collectTilesWithObjectsAtZoomTemplate(
const unsigned int& indexZoom,
const typename std::vector<AppendVectorNS::AppendVector<OO>>::iterator objects,
const size_t size,
std::vector<std::shared_ptr<TileCoordinatesSet>>& zooms
) {
for (size_t i = 0; i < size; i++)
collectTilesWithObjectsInShardTemplate<OO>(indexZoom, objects[i], i, zooms);
}

template<typename OO>
OutputObjectID outputObjectWithId(const OO& input) {
return OutputObjectID({ input.oo, 0 });
Expand Down Expand Up @@ -308,6 +370,8 @@ class TileDataSource {
TileDataSource(size_t threadNum, unsigned int indexZoom, bool includeID);

void collectTilesWithObjectsAtZoom(std::vector<std::shared_ptr<TileCoordinatesSet>>& zooms);
// The same for the objects of one z6 tile (0 <= shard < CLUSTER_ZOOM_AREA)
void collectTilesWithObjectsInShard(size_t shard, std::vector<std::shared_ptr<TileCoordinatesSet>>& zooms);

void collectTilesWithLargeObjectsAtZoom(std::vector<std::shared_ptr<TileCoordinatesSet>>& zooms);

Expand Down Expand Up @@ -428,7 +492,8 @@ class TileDataSource {

void populateTilesAtZoom(
const std::vector<class TileDataSource *>& sources,
std::vector<std::shared_ptr<TileCoordinatesSet>>& zooms
std::vector<std::shared_ptr<TileCoordinatesSet>>& zooms,
size_t threadNum = 1
);

#endif //_TILE_DATA_H
35 changes: 35 additions & 0 deletions src/tile_coordinates_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ void TileCoordinatesSet::forEachSet(const std::function<void(TileCoordinate x, T
f(x, y);
}

void TileCoordinatesSet::setBox(TileCoordinate minX, TileCoordinate maxX, TileCoordinate minY, TileCoordinate maxY) {
for (TileCoordinate x = minX; x <= maxX; x++)
for (TileCoordinate y = minY; y <= maxY; y++)
set(x, y);
}

PreciseTileCoordinatesSet::PreciseTileCoordinatesSet(unsigned int zoom):
zoom_(zoom),
numBits((uint64_t)(1 << zoom) * (1 << zoom)),
Expand Down Expand Up @@ -99,6 +105,35 @@ void PreciseTileCoordinatesSet::set(TileCoordinate x, TileCoordinate y) {
summary[w >> 12] |= (uint64_t)1 << ((w >> 6) & 63);
}

// Fills each row of the box word by word, rather than tile by tile.
void PreciseTileCoordinatesSet::setBox(TileCoordinate minX, TileCoordinate maxX, TileCoordinate minY, TileCoordinate maxY) {
const uint64_t dim = 1ull << zoom_;
if (minX > maxX || minY > maxY || minX >= dim || minY >= dim)
return;
maxX = std::min<uint64_t>(maxX, dim - 1);
maxY = std::min<uint64_t>(maxY, dim - 1);
for (uint64_t x = minX; x <= maxX; x++) {
const uint64_t first = x * dim + minY, last = x * dim + maxY;
const uint64_t w0 = first >> 6, w1 = last >> 6;
for (uint64_t w = w0; w <= w1; w++) {
uint64_t mask = ~0ull;
if (w == w0) mask &= ~0ull << (first & 63);
if (w == w1) mask &= ~0ull >> (63 - (last & 63));
tiles[w] |= mask;
}
for (uint64_t block = w0 >> 6; block <= (w1 >> 6); block++)
summary[block >> 6] |= (uint64_t)1 << (block & 63);
}
}

void PreciseTileCoordinatesSet::merge(const PreciseTileCoordinatesSet& other) {
if (other.zoom_ != zoom_)
throw std::invalid_argument("PreciseTileCoordinatesSet::merge: zooms differ");
forEachMarkedWord(other.summary, numWords, [&](size_t w) { tiles[w] |= other.tiles[w]; });
for (size_t s = 0; s < summary.size(); s++)
summary[s] |= other.summary[s];
}

void PreciseTileCoordinatesSet::forEachSet(const std::function<void(TileCoordinate x, TileCoordinate y)>& f) const {
// loc = x * 2^zoom + y, so ascending loc order is x-major, y-minor.
// Only blocks marked in the summary can hold set bits; visiting them in
Expand Down
61 changes: 47 additions & 14 deletions src/tile_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ void TileDataSource::collectTilesWithObjectsAtZoom(std::vector<std::shared_ptr<T
collectTilesWithObjectsAtZoomTemplate<OutputObjectXYID>(indexZoom, objectsWithIds.begin(), objectsWithIds.size(), zooms);
}

void TileDataSource::collectTilesWithObjectsInShard(size_t shard, std::vector<std::shared_ptr<TileCoordinatesSet>>& zooms) {
if (shard < objects.size())
collectTilesWithObjectsInShardTemplate<OutputObjectXY>(indexZoom, objects[shard], shard, zooms);
if (shard < objectsWithIds.size())
collectTilesWithObjectsInShardTemplate<OutputObjectXYID>(indexZoom, objectsWithIds[shard], shard, zooms);
}

void addCoveredTilesToOutput(const uint indexZoom, std::vector<std::shared_ptr<TileCoordinatesSet>>& zooms, const Box& box) {
size_t maxZoom = zooms.size() - 1;
// std::cout << "addCoveredTilesToOutput maxZoom=" << maxZoom << ", indexZoom - maxZoom = " << (indexZoom - maxZoom) << std::endl;
Expand All @@ -173,16 +180,11 @@ void addCoveredTilesToOutput(const uint indexZoom, std::vector<std::shared_ptr<T
TileCoordinate maxx = box.max_corner().x() / scale;
TileCoordinate miny = box.min_corner().y() / scale;
TileCoordinate maxy = box.max_corner().y() / scale;
for (int x=minx; x<=maxx; x++) {
for (int y=miny; y<=maxy; y++) {
size_t zx = x, zy = y;

for (int zoom = maxZoom; zoom >= 0; zoom--) {
zooms[zoom]->set(zx, zy);
zx /= 2;
zy /= 2;
}
}
// The parents of the tiles in the box are the tiles in the box scaled down
for (int zoom = maxZoom; zoom >= 0; zoom--) {
zooms[zoom]->setBox(minx, maxx, miny, maxy);
minx /= 2; maxx /= 2;
miny /= 2; maxy /= 2;
}
}

Expand Down Expand Up @@ -481,15 +483,46 @@ void TileDataSource::reportSize() const {

void populateTilesAtZoom(
const std::vector<class TileDataSource *>& sources,
std::vector<std::shared_ptr<TileCoordinatesSet>>& zooms
std::vector<std::shared_ptr<TileCoordinatesSet>>& zooms,
size_t threadNum
) {
if (zooms.size() > 15)
throw std::out_of_range("populateTilesAtZoom: expected at most z14 zooms (15), but found " + std::to_string(zooms.size()) + " vectors");

for(size_t i=0; i<sources.size(); i++) {
sources[i]->collectTilesWithObjectsAtZoom(zooms);
sources[i]->collectTilesWithLargeObjectsAtZoom(zooms);
std::vector<PreciseTileCoordinatesSet*> precise;
for (const auto& zoom : zooms)
precise.push_back(dynamic_cast<PreciseTileCoordinatesSet*>(zoom.get()));
const bool allPrecise = std::find(precise.begin(), precise.end(), nullptr) == precise.end();

if (threadNum <= 1 || !allPrecise) {
for (size_t i = 0; i < sources.size(); i++)
sources[i]->collectTilesWithObjectsAtZoom(zooms);
} else {
// Objects are kept per z6 tile. Each worker takes whole columns of z6
// tiles (so that it touches only its part of the bitmaps), sets bits in
// bitmaps of its own, and these are merged at the end.
std::atomic<size_t> nextColumn(0);
std::vector<std::vector<std::shared_ptr<TileCoordinatesSet>>> local(threadNum);
boost::asio::thread_pool pool(threadNum);
for (size_t t = 0; t < threadNum; t++) {
boost::asio::post(pool, [&, t]() {
auto& mine = local[t];
for (size_t zoom = 0; zoom < zooms.size(); zoom++)
mine.push_back(std::make_shared<PreciseTileCoordinatesSet>(zoom));
for (size_t column; (column = nextColumn++) < CLUSTER_ZOOM_WIDTH; )
for (size_t row = 0; row < CLUSTER_ZOOM_WIDTH; row++)
for (const auto source : sources)
source->collectTilesWithObjectsInShard(column * CLUSTER_ZOOM_WIDTH + row, mine);
});
}
pool.join();
for (const auto& mine : local)
for (size_t zoom = 0; zoom < mine.size(); zoom++)
precise[zoom]->merge(static_cast<const PreciseTileCoordinatesSet&>(*mine[zoom]));
}

for (size_t i = 0; i < sources.size(); i++)
sources[i]->collectTilesWithLargeObjectsAtZoom(zooms);
}

void sortOutputObjectIDs(
Expand Down
2 changes: 1 addition & 1 deletion src/tilemaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ int main(const int argc, const char* argv[]) {
clock_gettime(CLOCK_MONOTONIC, &start);
#endif
std::cout << "collecting tiles" << std::flush;
populateTilesAtZoom(sources, zoomResults);
populateTilesAtZoom(sources, zoomResults, options.threadNum);
#ifdef CLOCK_MONOTONIC
clock_gettime(CLOCK_MONOTONIC, &end);
uint64_t tileNs = 1e9 * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
Expand Down
Loading
Loading