diff --git a/include/polylabel.h b/include/polylabel.h index 6ce44f16..30de7a6d 100644 --- a/include/polylabel.h +++ b/include/polylabel.h @@ -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; } diff --git a/include/tile_coordinates_set.h b/include/tile_coordinates_set.h index 3d45c50e..3bfab6e1 100644 --- a/include/tile_coordinates_set.h +++ b/include/tile_coordinates_set.h @@ -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& f) const; }; @@ -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& f) const override; + // Add the tiles set in other, a set of the same zoom. + void merge(const PreciseTileCoordinatesSet& other); private: unsigned int zoom_; diff --git a/include/tile_data.h b/include/tile_data.h index 5bc9fa67..8905f72c 100644 --- a/include/tile_data.h +++ b/include/tile_data.h @@ -2,10 +2,15 @@ #ifndef _TILE_DATA_H #define _TILE_DATA_H +#include +#include #include +#include #include #include #include +#include +#include #include "append_vector.h" #include "clip_cache.h" #include "mmap_allocator.h" @@ -31,77 +36,134 @@ template void finalizeObjects( typename std::vector>::iterator end, typename std::vector>& 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 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(indexZoom, threadNum, it->begin(), it->end()); + sortOutputObjects(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 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(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 lock(budgetMutex); + budgetFreed.wait(lock, [&]() { return inFlight == 0 || inFlight + n <= budget; }); + inFlight += n; + } + finalizeShard(i, 1); + { + std::lock_guard lock(budgetMutex); + inFlight -= n; + } + budgetFreed.notify_all(); + reportDone(1); + }); + } + pool.join(); } std::cout << std::endl; } -template void collectTilesWithObjectsAtZoomTemplate( +// Set the tiles holding the objects of z6 tile i (shard), and their parent tiles. +template void collectTilesWithObjectsInShardTemplate( const unsigned int& indexZoom, - const typename std::vector>::iterator objects, - const size_t size, + AppendVectorNS::AppendVector& shard, + const size_t i, std::vector>& 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 void collectTilesWithObjectsAtZoomTemplate( + const unsigned int& indexZoom, + const typename std::vector>::iterator objects, + const size_t size, + std::vector>& zooms +) { + for (size_t i = 0; i < size; i++) + collectTilesWithObjectsInShardTemplate(indexZoom, objects[i], i, zooms); +} + template OutputObjectID outputObjectWithId(const OO& input) { return OutputObjectID({ input.oo, 0 }); @@ -308,6 +370,8 @@ class TileDataSource { TileDataSource(size_t threadNum, unsigned int indexZoom, bool includeID); void collectTilesWithObjectsAtZoom(std::vector>& zooms); + // The same for the objects of one z6 tile (0 <= shard < CLUSTER_ZOOM_AREA) + void collectTilesWithObjectsInShard(size_t shard, std::vector>& zooms); void collectTilesWithLargeObjectsAtZoom(std::vector>& zooms); @@ -428,7 +492,8 @@ class TileDataSource { void populateTilesAtZoom( const std::vector& sources, - std::vector>& zooms + std::vector>& zooms, + size_t threadNum = 1 ); #endif //_TILE_DATA_H diff --git a/src/tile_coordinates_set.cpp b/src/tile_coordinates_set.cpp index e6d6e78e..10bb8f86 100644 --- a/src/tile_coordinates_set.cpp +++ b/src/tile_coordinates_set.cpp @@ -40,6 +40,12 @@ void TileCoordinatesSet::forEachSet(const std::function> 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(maxX, dim - 1); + maxY = std::min(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& 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 diff --git a/src/tile_data.cpp b/src/tile_data.cpp index 3fff009a..860a74aa 100644 --- a/src/tile_data.cpp +++ b/src/tile_data.cpp @@ -165,6 +165,13 @@ void TileDataSource::collectTilesWithObjectsAtZoom(std::vector(indexZoom, objectsWithIds.begin(), objectsWithIds.size(), zooms); } +void TileDataSource::collectTilesWithObjectsInShard(size_t shard, std::vector>& zooms) { + if (shard < objects.size()) + collectTilesWithObjectsInShardTemplate(indexZoom, objects[shard], shard, zooms); + if (shard < objectsWithIds.size()) + collectTilesWithObjectsInShardTemplate(indexZoom, objectsWithIds[shard], shard, zooms); +} + void addCoveredTilesToOutput(const uint indexZoom, std::vector>& zooms, const Box& box) { size_t maxZoom = zooms.size() - 1; // std::cout << "addCoveredTilesToOutput maxZoom=" << maxZoom << ", indexZoom - maxZoom = " << (indexZoom - maxZoom) << std::endl; @@ -173,16 +180,11 @@ void addCoveredTilesToOutput(const uint indexZoom, std::vector= 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; } } @@ -481,15 +483,46 @@ void TileDataSource::reportSize() const { void populateTilesAtZoom( const std::vector& sources, - std::vector>& zooms + std::vector>& 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; icollectTilesWithObjectsAtZoom(zooms); - sources[i]->collectTilesWithLargeObjectsAtZoom(zooms); + std::vector precise; + for (const auto& zoom : zooms) + precise.push_back(dynamic_cast(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 nextColumn(0); + std::vector>> 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(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(*mine[zoom])); } + + for (size_t i = 0; i < sources.size(); i++) + sources[i]->collectTilesWithLargeObjectsAtZoom(zooms); } void sortOutputObjectIDs( diff --git a/src/tilemaker.cpp b/src/tilemaker.cpp index 7a7ae1e5..57eacc49 100644 --- a/src/tilemaker.cpp +++ b/src/tilemaker.cpp @@ -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; diff --git a/test/tile_coordinates_set.test.cpp b/test/tile_coordinates_set.test.cpp index 87a054b8..545e19ef 100644 --- a/test/tile_coordinates_set.test.cpp +++ b/test/tile_coordinates_set.test.cpp @@ -47,8 +47,53 @@ MU_TEST(test_tile_coordinates_set) { } } +static bool sameTiles(const TileCoordinatesSet& a, const TileCoordinatesSet& b) { + std::vector> ta, tb; + a.forEachSet([&](TileCoordinate x, TileCoordinate y) { ta.emplace_back(x, y); }); + b.forEachSet([&](TileCoordinate x, TileCoordinate y) { tb.emplace_back(x, y); }); + return ta == tb && a.size() == b.size(); +} + +MU_TEST(test_tile_coordinates_set_box) { + // setBox must set exactly the tiles that set() would, including boxes that + // start and end mid-word, span several words, or are clipped at the edge + const TileCoordinate boxes[][4] = { + { 0, 0, 0, 0 }, { 3, 9, 5, 70 }, { 100, 101, 0, 1023 }, { 7, 7, 60, 200 }, + { 1000, 1023, 990, 1023 }, { 1020, 1023, 1000, 1023 } + }; + for (const auto& b : boxes) { + PreciseTileCoordinatesSet bySet(10), byBox(10); + for (TileCoordinate x = b[0]; x <= b[1]; x++) + for (TileCoordinate y = b[2]; y <= b[3]; y++) + bySet.set(x, y); + byBox.setBox(b[0], b[1], b[2], b[3]); + mu_check(sameTiles(bySet, byBox)); + } + // Partly outside the 1024x1024 grid: the part inside is set + PreciseTileCoordinatesSet edge(10); + edge.setBox(1020, 1030, 1022, 1030); + mu_check(edge.size() == 4 * 2); + mu_check(edge.test(1023, 1023)); +} + +MU_TEST(test_tile_coordinates_set_merge) { + PreciseTileCoordinatesSet a(12), b(12), both(12); + for (TileCoordinate i = 0; i < 4096; i += 37) { + a.set(i, (i * 7) % 4096); + both.set(i, (i * 7) % 4096); + } + for (TileCoordinate i = 0; i < 4096; i += 53) { + b.set((i * 3) % 4096, i); + both.set((i * 3) % 4096, i); + } + a.merge(b); + mu_check(sameTiles(a, both)); +} + MU_TEST_SUITE(test_suite_tile_coordinates_set) { MU_RUN_TEST(test_tile_coordinates_set); + MU_RUN_TEST(test_tile_coordinates_set_box); + MU_RUN_TEST(test_tile_coordinates_set_merge); } int main() {