New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OpenMP Parallelization for CascadedUnion [WIP] #468
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
92a18b4
Parallel processing of cascaded union using OpenMP
pramsey 7b2f643
Ug, set the OMP iterator to signed for MSVC then cast it back to size…
pramsey 2e84518
Fix off-by-something error
pramsey 70bf61f
Add comment note about Windows OpenMP and int for loops
pramsey File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,21 +42,23 @@ namespace operation { // geos.operation | |
namespace geounion { // geos.operation.geounion | ||
|
||
|
||
// //////////////////////////////////////////////////////////////////////////// | ||
/* public static */ | ||
std::unique_ptr<geom::Geometry> | ||
CascadedPolygonUnion::Union(std::vector<geom::Polygon*>* polys) | ||
{ | ||
CascadedPolygonUnion op(polys); | ||
return op.Union(); | ||
} | ||
|
||
/* public static */ | ||
std::unique_ptr<geom::Geometry> | ||
CascadedPolygonUnion::Union(std::vector<geom::Polygon*>* polys, UnionStrategy* unionFun) | ||
{ | ||
CascadedPolygonUnion op(polys, unionFun); | ||
return op.Union(); | ||
} | ||
|
||
/* public static */ | ||
std::unique_ptr<geom::Geometry> | ||
CascadedPolygonUnion::Union(const geom::MultiPolygon* multipoly) | ||
{ | ||
|
@@ -70,6 +72,7 @@ CascadedPolygonUnion::Union(const geom::MultiPolygon* multipoly) | |
return op.Union(); | ||
} | ||
|
||
/* public */ | ||
std::unique_ptr<geom::Geometry> | ||
CascadedPolygonUnion::Union() | ||
{ | ||
|
@@ -94,13 +97,61 @@ CascadedPolygonUnion::Union() | |
// TODO avoid creating this vector and run binaryUnion off the iterators directly | ||
std::vector<const geom::Geometry*> geoms(index.items().begin(), index.items().end()); | ||
|
||
return binaryUnion(geoms, 0, geoms.size()); | ||
// #ifdef GEOS_OPENMP | ||
return pairUnion(geoms); | ||
// #else | ||
// return binaryUnion(geoms, 0, geoms.size()); | ||
// #endif | ||
} | ||
|
||
#ifdef GEOS_OPENMP | ||
#include <omp.h> | ||
#endif | ||
|
||
/* private */ | ||
std::unique_ptr<geom::Geometry> | ||
CascadedPolygonUnion::binaryUnion(const std::vector<const geom::Geometry*> & geoms, | ||
std::size_t start, std::size_t end) | ||
CascadedPolygonUnion::pairUnion( | ||
std::vector<const geom::Geometry*>& inply) const | ||
{ | ||
/* Windows OpenMP requires loop iterators to be int */ | ||
/* so we do a lot of dumb casting up here to allow ints */ | ||
/* in the loops further down */ | ||
int sz = (int)(inply.size()); | ||
int nsz = (sz / 2) + (sz % 2); | ||
std::vector<std::unique_ptr<geom::Geometry>> outply((std::size_t)nsz); | ||
|
||
#pragma omp parallel for | ||
for (int i = 0; i < sz; i += 2) { | ||
/* Work backwards to preserve an old regression result */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hum I don't see this backward effect in the binaryUnion() implementation. At least not in an obvious way |
||
const geom::Geometry* g0 = inply[(std::size_t)(sz - i - 1)]; | ||
const geom::Geometry* g1 = i+1 < sz ? inply[(std::size_t)(sz - i - 2)] : nullptr; | ||
outply[(std::size_t)(i/2)] = unionSafe(g0, g1); | ||
} | ||
|
||
int step = 1; | ||
while(step < nsz) { | ||
|
||
#pragma omp parallel for | ||
/* Place resultant output into position 1 of the inputs */ | ||
/* This keeps threads from stomping on each other as they */ | ||
/* work through different parts of the processing */ | ||
for (int i0 = 0; i0 < nsz; i0 += 2*step) { | ||
int i1 = i0 + step; | ||
const geom::Geometry* g0 = outply[(std::size_t)(i0)].release(); | ||
const geom::Geometry* g1 = i1 < nsz ? outply[(std::size_t)(i1)].release() : nullptr; | ||
outply[(std::size_t)(i0)] = unionSafe(g0, g1); | ||
} | ||
step *= 2; | ||
} | ||
return std::move(outply[0]); | ||
} | ||
|
||
|
||
/* private */ | ||
std::unique_ptr<geom::Geometry> | ||
CascadedPolygonUnion::binaryUnion( | ||
const std::vector<const geom::Geometry*> & geoms, | ||
std::size_t start, std::size_t end) const | ||
{ | ||
if(end - start <= 1) { | ||
return unionSafe(geoms[start], nullptr); | ||
|
@@ -117,8 +168,10 @@ CascadedPolygonUnion::binaryUnion(const std::vector<const geom::Geometry*> & geo | |
} | ||
} | ||
|
||
/* private */ | ||
std::unique_ptr<geom::Geometry> | ||
CascadedPolygonUnion::unionSafe(const geom::Geometry* g0, const geom::Geometry* g1) const | ||
CascadedPolygonUnion::unionSafe( | ||
const geom::Geometry* g0, const geom::Geometry* g1) const | ||
{ | ||
if(g0 == nullptr && g1 == nullptr) { | ||
return nullptr; | ||
|
@@ -134,8 +187,11 @@ CascadedPolygonUnion::unionSafe(const geom::Geometry* g0, const geom::Geometry* | |
return unionActual(g0, g1); | ||
} | ||
|
||
/* private */ | ||
std::unique_ptr<geom::Geometry> | ||
CascadedPolygonUnion::unionSafe(std::unique_ptr<geom::Geometry> && g0, std::unique_ptr<geom::Geometry> && g1) | ||
CascadedPolygonUnion::unionSafe( | ||
std::unique_ptr<geom::Geometry> && g0, | ||
std::unique_ptr<geom::Geometry> && g1) const | ||
{ | ||
if(g0 == nullptr && g1 == nullptr) { | ||
return nullptr; | ||
|
@@ -151,22 +207,29 @@ CascadedPolygonUnion::unionSafe(std::unique_ptr<geom::Geometry> && g0, std::uniq | |
return unionActual(std::move(g0), std::move(g1)); | ||
} | ||
|
||
/* private */ | ||
std::unique_ptr<geom::Geometry> | ||
CascadedPolygonUnion::unionActual(const geom::Geometry* g0, const geom::Geometry* g1) const | ||
CascadedPolygonUnion::unionActual( | ||
const geom::Geometry* g0, | ||
const geom::Geometry* g1) const | ||
{ | ||
std::unique_ptr<geom::Geometry> ug; | ||
ug = unionFunction->Union(g0, g1); | ||
return restrictToPolygons(std::move(ug)); | ||
} | ||
|
||
/* private */ | ||
std::unique_ptr<geom::Geometry> | ||
CascadedPolygonUnion::unionActual(std::unique_ptr<geom::Geometry> && g0, std::unique_ptr<geom::Geometry> && g1) const | ||
CascadedPolygonUnion::unionActual( | ||
std::unique_ptr<geom::Geometry> && g0, | ||
std::unique_ptr<geom::Geometry> && g1) const | ||
{ | ||
std::unique_ptr<geom::Geometry> ug; | ||
ug = unionFunction->Union(std::move(g0), std::move(g1)); | ||
return restrictToPolygons(std::move(ug)); | ||
} | ||
|
||
/* private static */ | ||
std::unique_ptr<geom::Geometry> | ||
CascadedPolygonUnion::restrictToPolygons(std::unique_ptr<geom::Geometry> g) | ||
{ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you used size_t here and in other places, I guess you could avoid the explicit casts below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Annoyingly, OpenMP on Windows insists that the loop iterators be
int
, which then cascades into all these wonderful casts. 🤷There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, a comment in the code would help other readers then