Move range splitting higher to also parallelize 'box_intersection_d()'

This commit is contained in:
Mael Rouxel-Labbé 2019-11-19 14:40:30 +01:00
parent 9ef1c976c6
commit b7d20e0033
1 changed files with 158 additions and 133 deletions

View File

@ -26,17 +26,103 @@
#include <CGAL/Box_intersection_d/box_limits.h>
#include <CGAL/use.h>
#include <CGAL/tags.h>
#ifdef CGAL_LINKED_WITH_TBB
#include <tbb/parallel_invoke.h>
#endif
#include <iterator>
#include <vector>
namespace CGAL {
namespace internal {
// Generic call with custom predicate traits parameter.
template< class RandomAccessIter1, class RandomAccessIter2,
template< class ConcurrencyTag,
class RandomAccessIter1, class RandomAccessIter2,
class Callback, class Traits >
void box_intersection_segment_tree_d(
RandomAccessIter1 begin1, RandomAccessIter1 end1,
RandomAccessIter2 begin2, RandomAccessIter2 end2,
Callback callback,
const Traits& traits,
const std::ptrdiff_t cutoff,
const bool in_order)
{
typedef typename Traits::NT NT;
CGAL_assertion(Traits::dimension() > 0);
const int dim = Traits::dimension() - 1;
const NT inf = Box_intersection_d::box_limits<NT>::inf();
const NT sup = Box_intersection_d::box_limits<NT>::sup();
#ifndef CGAL_LINKED_WITH_TBB
CGAL_static_assertion_msg (!(boost::is_convertible<ConcurrencyTag, Parallel_tag>::value),
"Parallel_tag is enabled but TBB is unavailable.");
#else // CGAL_LINKED_WITH_TBB
if(boost::is_convertible<ConcurrencyTag, Parallel_tag>::value)
{
typename std::iterator_traits<RandomAccessIter1>::difference_type r1hs = std::distance(begin1, end1) / 2;
typename std::iterator_traits<RandomAccessIter2>::difference_type r2hs = std::distance(begin2, end2) / 2;
// TODO: Check which of the two following approaches is better
// The first one makes three copies of the vector and performs four parallel tasks
// The second one makes one copy, but must perform two times two parallel tasks (can't
// do all four at the same time otherwise the sort / split will conflict)
#ifdef CGAL_BOX_INTER_FOUR_RANGES
typedef typename std::iterator_traits<RandomAccessIter1>::value_type val_t;
std::vector< val_t> r3( begin1, end1);
std::vector< val_t> r4( begin2, end2);
RandomAccessIter1 mid1 = begin1;
std::advance(mid1, r1hs);
RandomAccessIter2 mid2 = begin2;
std::advance(mid2, r2hs);
typename std::vector< val_t>::iterator mid3 = r3.begin();
std::advance(mid3, r1hs);
typename std::vector< val_t>::iterator mid4 = r4.begin();
std::advance(mid4, r2hs);
tbb::parallel_invoke([&]{ Box_intersection_d::segment_tree( begin1, mid1, begin2, mid2, inf, sup,
callback, traits, cutoff, dim, in_order); },
[&]{ Box_intersection_d::segment_tree( mid1, end1, mid2, end2, inf, sup,
callback, traits, cutoff, dim, in_order); },
[&]{ Box_intersection_d::segment_tree( r3.begin(), mid3, mid4, r4.end(), inf, sup,
callback, traits, cutoff, dim, in_order); },
[&]{ Box_intersection_d::segment_tree( mid3, r3.end(), r4.begin(), mid4, inf, sup,
callback, traits, cutoff, dim, in_order); } );
#else // CGAL_BOX_INTER_FOUR_RANGES
RandomAccessIter1 mid1 = begin1;
std::advance(mid1, r1hs);
RandomAccessIter2 mid2 = begin2;
std::advance(mid2, r2hs);
tbb::parallel_invoke([&]{ Box_intersection_d::segment_tree( begin1, mid1, begin2, mid2, inf, sup,
callback, traits, cutoff, dim, in_order); },
[&]{ Box_intersection_d::segment_tree( mid1, end1, mid2, end2, inf, sup,
callback, traits, cutoff, dim, in_order); } );
tbb::parallel_invoke([&]{ Box_intersection_d::segment_tree( begin1, mid1, mid2, end2, inf, sup,
callback, traits, cutoff, dim, in_order); },
[&]{ Box_intersection_d::segment_tree( mid1, end1, begin2, mid2, inf, sup,
callback, traits, cutoff, dim, in_order); } );
#endif // CGAL_BOX_INTER_FOUR_RANGES
}
else
#endif // CGAL_LINKED_WITH_TBB
{
Box_intersection_d::segment_tree(begin1, end1, begin2, end2, inf, sup, callback, traits, cutoff, dim, in_order);
}
}
} // namespace internal
// Generic call with custom predicate traits parameter.
template< class ConcurrencyTag = Sequential_tag,
class RandomAccessIter1, class RandomAccessIter2,
class Callback, class BoxPredicateTraits >
void box_intersection_custom_predicates_d(
RandomAccessIter1 begin1, RandomAccessIter1 end1,
@ -46,24 +132,15 @@ void box_intersection_custom_predicates_d(
std::ptrdiff_t cutoff = 10,
Box_intersection_d::Setting setting = Box_intersection_d::BIPARTITE)
{
typedef BoxPredicateTraits Traits;
typedef typename Traits::NT NT;
CGAL_assertion( Traits::dimension() > 0 );
const int dim = Traits::dimension() - 1;
const NT inf = Box_intersection_d::box_limits<NT>::inf();
const NT sup = Box_intersection_d::box_limits<NT>::sup();
Box_intersection_d::segment_tree(begin1, end1, begin2, end2,
inf, sup, callback, traits, cutoff, dim, true);
if(setting == Box_intersection_d::BIPARTITE)
Box_intersection_d::segment_tree(begin2, end2, begin1, end1,
inf, sup, callback, traits, cutoff, dim, false);
internal::box_intersection_segment_tree_d<ConcurrencyTag>(begin1, end1, begin2, end2, callback, traits, cutoff, true);
if(setting == Box_intersection_d::BIPARTITE)
internal::box_intersection_segment_tree_d<ConcurrencyTag>(begin2, end2, begin1, end1, callback, traits, cutoff, false);
}
// Generic call with box traits parameter.
// - make all default parameters explicit overloads (workaround)
template< class RandomAccessIter1, class RandomAccessIter2,
template< class ConcurrencyTag = Sequential_tag,
class RandomAccessIter1, class RandomAccessIter2,
class Callback, class BoxTraits >
void box_intersection_d(
RandomAccessIter1 begin1, RandomAccessIter1 end1,
@ -74,18 +151,19 @@ void box_intersection_d(
Box_intersection_d::Topology topology,
Box_intersection_d::Setting setting)
{
if (topology == Box_intersection_d::CLOSED) {
typedef Box_intersection_d::Predicate_traits_d<BoxTraits,true> Traits;
box_intersection_custom_predicates_d(begin1, end1, begin2, end2,
callback, Traits(), cutoff, setting);
} else {
typedef Box_intersection_d::Predicate_traits_d<BoxTraits,false> Traits;
box_intersection_custom_predicates_d(begin1, end1, begin2, end2,
callback, Traits(), cutoff, setting);
}
if (topology == Box_intersection_d::CLOSED) {
typedef Box_intersection_d::Predicate_traits_d<BoxTraits,true> Traits;
box_intersection_custom_predicates_d<ConcurrencyTag>(begin1, end1, begin2, end2,
callback, Traits(), cutoff, setting);
} else {
typedef Box_intersection_d::Predicate_traits_d<BoxTraits,false> Traits;
box_intersection_custom_predicates_d<ConcurrencyTag>(begin1, end1, begin2, end2,
callback, Traits(), cutoff, setting);
}
}
template< class RandomAccessIter1, class RandomAccessIter2,
template< class ConcurrencyTag = Sequential_tag,
class RandomAccessIter1, class RandomAccessIter2,
class Callback, class BoxTraits >
void box_intersection_d(
RandomAccessIter1 begin1, RandomAccessIter1 end1,
@ -93,35 +171,39 @@ void box_intersection_d(
Callback callback, BoxTraits box_traits, std::ptrdiff_t cutoff,
Box_intersection_d::Topology topology)
{
box_intersection_d( begin1, end1, begin2, end2, callback, box_traits,
cutoff, topology, Box_intersection_d::BIPARTITE);
box_intersection_d<ConcurrencyTag>( begin1, end1, begin2, end2, callback, box_traits,
cutoff, topology, Box_intersection_d::BIPARTITE);
}
template< class RandomAccessIter1, class RandomAccessIter2,
template< class ConcurrencyTag = Sequential_tag,
class RandomAccessIter1, class RandomAccessIter2,
class Callback, class BoxTraits >
void box_intersection_d(
RandomAccessIter1 begin1, RandomAccessIter1 end1,
RandomAccessIter2 begin2, RandomAccessIter2 end2,
Callback callback, BoxTraits box_traits, std::ptrdiff_t cutoff)
{
box_intersection_d( begin1, end1, begin2, end2, callback, box_traits,
cutoff, Box_intersection_d::CLOSED,
Box_intersection_d::BIPARTITE);
box_intersection_d<ConcurrencyTag>( begin1, end1, begin2, end2, callback, box_traits,
cutoff, Box_intersection_d::CLOSED,
Box_intersection_d::BIPARTITE);
}
template< class RandomAccessIter1, class RandomAccessIter2,
template< class ConcurrencyTag = Sequential_tag,
class RandomAccessIter1, class RandomAccessIter2,
class Callback, class BoxTraits >
void box_intersection_d(
RandomAccessIter1 begin1, RandomAccessIter1 end1,
RandomAccessIter2 begin2, RandomAccessIter2 end2,
Callback callback, BoxTraits box_traits)
{
box_intersection_d( begin1, end1, begin2, end2, callback, box_traits,
10, Box_intersection_d::CLOSED,
Box_intersection_d::BIPARTITE);
box_intersection_d<ConcurrencyTag>( begin1, end1, begin2, end2, callback, box_traits,
10, Box_intersection_d::CLOSED,
Box_intersection_d::BIPARTITE);
}
// Specialized call with default box traits.
// - make all default parameters explicit overloads (workaround)
template< class RandomAccessIter1, class RandomAccessIter2, class Callback >
template< class ConcurrencyTag = Sequential_tag,
class RandomAccessIter1, class RandomAccessIter2, class Callback >
void box_intersection_d(
RandomAccessIter1 begin1, RandomAccessIter1 end1,
RandomAccessIter2 begin2, RandomAccessIter2 end2,
@ -129,52 +211,58 @@ void box_intersection_d(
Box_intersection_d::Topology topology,
Box_intersection_d::Setting setting)
{
typedef typename std::iterator_traits<RandomAccessIter1>::value_type val_t;
typedef Box_intersection_d::Box_traits_d< val_t> Box_traits;
box_intersection_d( begin1, end1, begin2, end2, callback, Box_traits(),
cutoff, topology, setting);
typedef typename std::iterator_traits<RandomAccessIter1>::value_type val_t;
typedef Box_intersection_d::Box_traits_d< val_t> Box_traits;
box_intersection_d<ConcurrencyTag>( begin1, end1, begin2, end2, callback, Box_traits(),
cutoff, topology, setting);
}
template< class RandomAccessIter1, class RandomAccessIter2, class Callback >
template< class ConcurrencyTag = Sequential_tag,
class RandomAccessIter1, class RandomAccessIter2, class Callback >
void box_intersection_d(
RandomAccessIter1 begin1, RandomAccessIter1 end1,
RandomAccessIter2 begin2, RandomAccessIter2 end2,
Callback callback, std::ptrdiff_t cutoff,
Box_intersection_d::Topology topology)
{
typedef typename std::iterator_traits<RandomAccessIter1>::value_type val_t;
typedef Box_intersection_d::Box_traits_d< val_t> Box_traits;
box_intersection_d( begin1, end1, begin2, end2, callback, Box_traits(),
cutoff, topology, Box_intersection_d::BIPARTITE);
typedef typename std::iterator_traits<RandomAccessIter1>::value_type val_t;
typedef Box_intersection_d::Box_traits_d< val_t> Box_traits;
box_intersection_d<ConcurrencyTag>( begin1, end1, begin2, end2, callback, Box_traits(),
cutoff, topology, Box_intersection_d::BIPARTITE);
}
template< class RandomAccessIter1, class RandomAccessIter2, class Callback >
template< class ConcurrencyTag = Sequential_tag,
class RandomAccessIter1, class RandomAccessIter2, class Callback >
void box_intersection_d(
RandomAccessIter1 begin1, RandomAccessIter1 end1,
RandomAccessIter2 begin2, RandomAccessIter2 end2,
Callback callback, std::ptrdiff_t cutoff)
{
typedef typename std::iterator_traits<RandomAccessIter1>::value_type val_t;
typedef Box_intersection_d::Box_traits_d< val_t> Box_traits;
box_intersection_d( begin1, end1, begin2, end2, callback, Box_traits(),
cutoff, Box_intersection_d::CLOSED,
Box_intersection_d::BIPARTITE);
typedef typename std::iterator_traits<RandomAccessIter1>::value_type val_t;
typedef Box_intersection_d::Box_traits_d< val_t> Box_traits;
box_intersection_d<ConcurrencyTag>( begin1, end1, begin2, end2, callback, Box_traits(),
cutoff, Box_intersection_d::CLOSED,
Box_intersection_d::BIPARTITE);
}
template< class RandomAccessIter1, class RandomAccessIter2, class Callback >
template< class ConcurrencyTag = Sequential_tag,
class RandomAccessIter1, class RandomAccessIter2, class Callback >
void box_intersection_d(
RandomAccessIter1 begin1, RandomAccessIter1 end1,
RandomAccessIter2 begin2, RandomAccessIter2 end2,
Callback callback)
{
typedef typename std::iterator_traits<RandomAccessIter1>::value_type val_t;
typedef Box_intersection_d::Box_traits_d< val_t> Box_traits;
box_intersection_d( begin1, end1, begin2, end2, callback, Box_traits(),
10, Box_intersection_d::CLOSED,
Box_intersection_d::BIPARTITE);
typedef typename std::iterator_traits<RandomAccessIter1>::value_type val_t;
typedef Box_intersection_d::Box_traits_d< val_t> Box_traits;
box_intersection_d<ConcurrencyTag>( begin1, end1, begin2, end2, callback, Box_traits(),
10, Box_intersection_d::CLOSED,
Box_intersection_d::BIPARTITE);
}
// Generic call with box traits parameter, specialized for self-intersection.
// - make all default parameters explicit overloads (workaround)
template< class ConcurrencyTag = Sequential_tag, class RandomAccessIter, class Callback, class BoxTraits >
template< class ConcurrencyTag = Sequential_tag,
class RandomAccessIter, class Callback, class BoxTraits >
void box_self_intersection_d(
RandomAccessIter begin, RandomAccessIter end,
Callback callback,
@ -182,83 +270,19 @@ void box_self_intersection_d(
std::ptrdiff_t cutoff,
Box_intersection_d::Topology topology)
{
#ifndef CGAL_LINKED_WITH_TBB
CGAL_static_assertion_msg (!(boost::is_convertible<ConcurrencyTag, Parallel_tag>::value),
"Parallel_tag is enabled but TBB is unavailable.");
#else // CGAL_LINKED_WITH_TBB
if(boost::is_convertible<ConcurrencyTag, Parallel_tag>::value)
{
// Copying rather than calling 'box_intersection_d(begin, end, begin, end, ...'
// is necessary because the 'std::partition' and range splits on the first range
// would be messed up by sorts on the second range otherwise.
typedef typename std::iterator_traits<RandomAccessIter>::value_type val_t;
std::vector< val_t> i( begin, end);
// Copying rather than calling 'box_intersection_d(begin, end, begin, end, ...'
// is necessary because the 'std::partition' and range splits on the first range
// would be messed up by sorts on the second range otherwise.
typedef typename std::iterator_traits<RandomAccessIter>::value_type val_t;
std::vector< val_t> i( begin, end);
// TODO: Check which of the two following approaches is better
// The first one makes three copies of the vector and performs four parallel tasks
// The second one makes one copy, but must perform two times two parallel tasks (can't
// do all four at the same time otherwise the sort / split will conflict)
#ifdef CGAL_BOX_INTER_FOUR_RANGES
std::vector< val_t> j( begin, end);
std::vector< val_t> k( begin, end);
RandomAccessIter mid = begin;
std::advance(mid, std::distance(begin,end)/2);
typename std::vector< val_t>::iterator midi = i.begin();
std::advance(midi, std::distance(begin,end)/2);
typename std::vector< val_t>::iterator midj = j.begin();
std::advance(midj, std::distance(begin,end)/2);
typename std::vector< val_t>::iterator midk = k.begin();
std::advance(midk, std::distance(begin,end)/2);
tbb::parallel_invoke([&]{ box_intersection_d( begin, mid, i.begin(), midi,
callback, box_traits, cutoff, topology,
Box_intersection_d::COMPLETE); },
[&]{ box_intersection_d( mid, end, midi, i.end(),
callback, box_traits, cutoff, topology,
Box_intersection_d::COMPLETE); },
[&]{ box_intersection_d( j.begin(), midj, midk, k.end(),
callback, box_traits, cutoff, topology,
Box_intersection_d::COMPLETE); },
[&]{ box_intersection_d( midj, j.end(), k.begin(), midk,
callback, box_traits, cutoff, topology,
Box_intersection_d::COMPLETE); } );
#else // CGAL_BOX_INTER_FOUR_RANGES
RandomAccessIter mid = begin;
std::advance(mid, std::distance(begin,end)/2);
typename std::vector< val_t>::iterator midi = i.begin();
std::advance(midi, std::distance(begin,end)/2);
tbb::parallel_invoke([&]{ box_intersection_d( begin, mid, i.begin(), midi,
callback, box_traits, cutoff, topology,
Box_intersection_d::COMPLETE); },
[&]{ box_intersection_d( mid, end, midi, i.end(),
callback, box_traits, cutoff, topology,
Box_intersection_d::COMPLETE); } );
tbb::parallel_invoke([&]{box_intersection_d( begin, mid, midi, i.end(),
callback, box_traits, cutoff, topology,
Box_intersection_d::COMPLETE); },
[&]{ box_intersection_d( mid, end, i.begin(), midi,
callback, box_traits, cutoff, topology,
Box_intersection_d::COMPLETE); } );
#endif // CGAL_BOX_INTER_FOUR_RANGES
}
else
#endif // CGAL_LINKED_WITH_TBB
{
// Copying rather than calling 'box_intersection_d(begin, end, begin, end, ...'
// is necessary because the 'std::partition' and range splits on the first range
// would be messed up by sorts on the second range otherwise.
typedef typename std::iterator_traits<RandomAccessIter>::value_type val_t;
std::vector< val_t> i( begin, end);
box_intersection_d( begin, end, i.begin(), i.end(),
callback, box_traits, cutoff, topology,
Box_intersection_d::COMPLETE);
}
box_intersection_d<ConcurrencyTag>( begin, end, i.begin(), i.end(),
callback, box_traits, cutoff, topology,
Box_intersection_d::COMPLETE);
}
template< class ConcurrencyTag = Sequential_tag, class RandomAccessIter, class Callback, class BoxTraits >
template< class ConcurrencyTag = Sequential_tag,
class RandomAccessIter, class Callback, class BoxTraits >
void box_self_intersection_d(
RandomAccessIter begin, RandomAccessIter end,
Callback callback,
@ -269,7 +293,8 @@ void box_self_intersection_d(
Box_intersection_d::CLOSED);
}
template< class ConcurrencyTag = Sequential_tag, class RandomAccessIter, class Callback, class BoxTraits >
template< class ConcurrencyTag = Sequential_tag,
class RandomAccessIter, class Callback, class BoxTraits >
void box_self_intersection_d(
RandomAccessIter begin, RandomAccessIter end,
Callback callback,