Skip to content

Commit 1e2ecfb

Browse files
authored
Merge pull request #1336 from proberts-cinesite/PrimitiveBoundUseParallelReduce
Primitive : Use tbb parallel_reduce to accelerate bounds compute
2 parents 97754ec + 0953512 commit 1e2ecfb

File tree

3 files changed

+70
-23
lines changed

3 files changed

+70
-23
lines changed

Changes

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
10.4.x.x (relative to 10.4.5.0)
22
========
33

4+
Improvements
5+
------------
6+
7+
- PointsPrimitive, Primitive : Accelerated bounds computation using `tbb::parallel_reduce`.
8+
49
Fixes
510
-----
611

src/IECoreScene/PointsPrimitive.cpp

+41-19
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
#include "IECore/MurmurHash.h"
4141
#include "IECore/SimpleTypedData.h"
4242

43+
#include <tbb/parallel_reduce.h>
44+
#include <tbb/blocked_range.h>
45+
4346
using namespace std;
4447
using namespace Imath;
4548
using namespace IECore;
@@ -193,26 +196,45 @@ Imath::Box3f PointsPrimitive::bound() const
193196
// Compute the bounding box from the gathered data.
194197

195198
Box3f result;
196-
for( size_t i = 0; i < count; ++i )
197-
{
198-
float r = constantWidth * *width / 2.0f;
199-
width += widthStep;
200-
if( aspectRatio )
201-
{
202-
// Type is patch - the diagonal will be
203-
// longer than either the width or the
204-
// height, so derive a new radius from that.
205-
float hh = r; // half the height
206-
if( *aspectRatio != 0.0f )
199+
using RangeType = tbb::blocked_range< const V3f* >;
200+
tbb::this_task_arena::isolate( [ =, & result ]{
201+
tbb::task_group_context taskGroupContext( tbb::task_group_context::isolated );
202+
result = tbb::parallel_reduce( RangeType( p, p + count, 1000 ), Imath::Box3f(),
203+
[ = ]( const RangeType& range, const Imath::Box3f& value ) -> Imath::Box3f
207204
{
208-
hh /= *aspectRatio;
209-
}
210-
r = sqrtf( r * r + hh * hh );
211-
aspectRatio += aspectRatioStep;
212-
}
213-
result.extendBy( Box3f( *p - V3f( r ), *p + V3f( r ) ) );
214-
p++;
215-
}
205+
Imath::Box3f b( value );
206+
const size_t offset = range.begin() - p;
207+
const float* wIt = offset * widthStep + width;
208+
const float* aIt = offset * aspectRatioStep + aspectRatio;
209+
for( const V3f& pos : range )
210+
{
211+
float r = constantWidth * ( *wIt ) / 2.0f;
212+
wIt += widthStep;
213+
if( aspectRatio )
214+
{
215+
// Type is patch - the diagonal will be
216+
// longer than either the width or the
217+
// height, so derive a new radius from that.
218+
float hh = r; // half the height
219+
if( ( *aIt ) != 0.0f )
220+
{
221+
hh /= ( *aIt );
222+
}
223+
r = sqrtf( r * r + hh * hh );
224+
aIt += aspectRatioStep;
225+
}
226+
b.extendBy( Box3f( pos - V3f( r ), pos + V3f( r ) ) );
227+
}
228+
return b;
229+
},
230+
[]( const Imath::Box3f& lhs, const Imath::Box3f& rhs ) -> Imath::Box3f
231+
{
232+
Imath::Box3f b( lhs );
233+
b.extendBy( rhs );
234+
return b;
235+
},
236+
taskGroupContext );
237+
} );
216238

217239
return result;
218240
}

src/IECoreScene/Primitive.cpp

+24-4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141

4242
#include "boost/algorithm/string.hpp"
4343

44+
#include <tbb/parallel_reduce.h>
45+
#include <tbb/blocked_range.h>
46+
4447
#include <cassert>
4548

4649
using namespace IECore;
@@ -78,10 +81,27 @@ Imath::Box3f Primitive::bound() const
7881
if( p )
7982
{
8083
const vector<V3f> &pp = p->readable();
81-
for( size_t i=0; i<pp.size(); i++ )
82-
{
83-
result.extendBy( pp[i] );
84-
}
84+
using RangeType = tbb::blocked_range< const V3f* >;
85+
tbb::this_task_arena::isolate( [ & result, & pp ]{
86+
tbb::task_group_context taskGroupContext( tbb::task_group_context::isolated );
87+
result = tbb::parallel_reduce( RangeType( pp.data(), pp.data() + pp.size(), 1000 ), Imath::Box3f(),
88+
[]( const RangeType& range, const Imath::Box3f& value ) -> Imath::Box3f
89+
{
90+
Imath::Box3f b( value );
91+
for( const V3f& pos : range )
92+
{
93+
b.extendBy( pos );
94+
}
95+
return b;
96+
},
97+
[]( const Imath::Box3f& lhs, const Imath::Box3f& rhs ) -> Imath::Box3f
98+
{
99+
Imath::Box3f b( lhs );
100+
b.extendBy( rhs );
101+
return b;
102+
},
103+
taskGroupContext );
104+
} );
85105
}
86106
}
87107
return result;

0 commit comments

Comments
 (0)