Skip to content
Merged
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
1 change: 1 addition & 0 deletions include/sharded_node_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ShardedNodeStore : public NodeStore {
void reopen() override;
void finalize(size_t threadNum) override;
LatpLon at(NodeID i) const override;
void at(const NodeID* ids, size_t n, LatpLon* out) const override;
size_t size() const override;
void batchStart() override;
void insert(const std::vector<element_t>& elements) override;
Expand Down
6 changes: 5 additions & 1 deletion src/mmap_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ mmap_file::mmap_file(std::string const &filename, std::size_t offset)
, mapping(filename.c_str(), boost::interprocess::read_write)
, region(mapping, boost::interprocess::read_write)
, buffer(boost::interprocess::create_only, reinterpret_cast<uint8_t *>(region.get_address()) + offset, region.get_size() - offset)
{ }
{
// The mapped region stays valid without the file handle. Release it, so large --store runs
// with many threads (one ~1 GB file each, more as they fill) don't exhaust the open-files limit.
mapping = boost::interprocess::file_mapping();
}

mmap_file::~mmap_file()
{
Expand Down
31 changes: 24 additions & 7 deletions src/pbf_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,17 @@ bool PbfProcessor::ReadRelations(
TagMap tags;
std::vector<RelationStore::element_t> relations;

// Passes skip shards with no ways, so the first pass is for the first non-empty shard
uint firstWayShard = 0;
while (firstWayShard + 1 < effectiveShards && osmStore.ways.shard(firstWayShard).size() == 0)
firstWayShard++;
auto wayInOtherShard = [&](WayID wayId, uint thisShard) {
for (uint s = 0; s < effectiveShards; s++)
if (s != thisShard && osmStore.ways.contains(s, wayId))
return true;
return false;
};

int typeKey = findStringPosition(pb, "type");
int mpKey = findStringPosition(pb, "multipolygon");
int boundaryKey = findStringPosition(pb, "boundary");
Expand All @@ -309,24 +320,30 @@ bool PbfProcessor::ReadRelations(
WayVec outerWayVec, innerWayVec;
bool isInnerOuter = isBoundary || isMultiPolygon;
bool skipToNext = false;
bool firstWay = true;
// With shards, a relation is read in the pass for the shard of its first member way
// that is in the store. Member ways missing from the extract don't decide the shard,
// so relations crossing the extract boundary aren't dropped; a relation with no stored
// member ways is read in the first pass.
bool shardDecided = effectiveShards <= 1;
for (int n = 0; n < pbfRelation.memids.size(); n++) {
uint64_t lastID = pbfRelation.memids[n];
if (pbfRelation.types[n] != PbfReader::Relation::MemberType::WAY) { continue; }
int32_t role = pbfRelation.roles_sid[n];
if (role==innerKey || role==outerKey) isInnerOuter=true;
WayID wayId = static_cast<WayID>(lastID);

if (firstWay && effectiveShards > 1 && !osmStore.ways.contains(shard, wayId)) {
skipToNext = true;
break;
if (!shardDecided) {
if (osmStore.ways.contains(shard, wayId)) {
shardDecided = true;
} else if (wayInOtherShard(wayId, shard)) {
skipToNext = true;
break;
}
}
if (firstWay)
firstWay = false;
(role == innerKey ? innerWayVec : outerWayVec).push_back(wayId);
}

if (skipToNext)
if (skipToNext || (!shardDecided && shard != firstWayShard))
continue;

try {
Expand Down
24 changes: 24 additions & 0 deletions src/sharded_node_store.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "sharded_node_store.h"
#include <stdexcept>

thread_local size_t lastNodeShard = 0;

Expand Down Expand Up @@ -35,6 +36,29 @@ LatpLon ShardedNodeStore::at(NodeID id) const {
return stores[shards() - 1]->at(id);
}

void ShardedNodeStore::at(const NodeID* ids, size_t n, LatpLon* out) const {
if (n == 0)
return;

// Ways rarely cross shards: look up the whole batch in the shard of its first node,
// and only fall back to per-node lookups if one of the nodes is elsewhere.
for (int i = 0; i < shards(); i++) {
size_t index = (lastNodeShard + i) % shards();

if (stores[index]->contains(0, ids[0])) {
lastNodeShard = index;
try {
stores[index]->at(ids, n, out);
return;
} catch (std::out_of_range &) {
break;
}
}
}

NodeStore::at(ids, n, out);
}

size_t ShardedNodeStore::size() const {
size_t rv = 0;
for (auto& store : stores)
Expand Down
24 changes: 15 additions & 9 deletions src/tile_sorting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ template<typename OO> void sortOutputObjects(
return;
}

// Parallel counting sort on the key: each chunk copies its objects
// into a contiguous buffer and counts its keys, then scatters them
// back into their final positions.
// Parallel counting sort on the key: each chunk counts its keys, then
// scatters its objects into their final positions in a buffer, which is
// copied back in order. Writing the objects (which can live in the
// --store files) sequentially means each page is dirtied only once.
const size_t chunks = std::max<size_t>(1, std::min<size_t>(threadNum, n / countingSortMinSize));
const size_t chunkSize = (n + chunks - 1) / chunks;
// OO has no default constructor, so use raw storage for the copies.
Expand All @@ -165,10 +166,8 @@ template<typename OO> void sortOutputObjects(
runChunks([&](size_t c, size_t from, size_t to) {
size_t* counts = &offsets[c * keyValues];
auto it = begin + from;
for (size_t i = from; i < to; i++, ++it) {
new (&buffer[i]) OO(*it);
counts[key(buffer[i])]++;
}
for (size_t i = from; i < to; i++, ++it)
counts[key(*it)]++;
});

// Turn counts into starting positions: by key, then by chunk.
Expand All @@ -183,8 +182,15 @@ template<typename OO> void sortOutputObjects(

runChunks([&](size_t c, size_t from, size_t to) {
size_t* next = &offsets[c * keyValues];
for (size_t i = from; i < to; i++)
begin[next[key(buffer[i])]++] = buffer[i];
auto it = begin + from;
for (size_t i = from; i < to; i++, ++it)
new (&buffer[next[key(*it)]++]) OO(*it);
});

runChunks([&](size_t c, size_t from, size_t to) {
auto it = begin + from;
for (size_t i = from; i < to; i++, ++it)
*it = buffer[i];
});
}

Expand Down
Loading