summaryrefslogtreecommitdiff
path: root/thirdparty/embree/common/algorithms/parallel_partition.h
blob: a1cbdc8e0478ad24a2cf3c291015255ee6782f0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "parallel_for.h"
#include "../math/range.h"

namespace embree
{
  /* serial partitioning */
  template<typename T, typename V, typename IsLeft, typename Reduction_T>
    __forceinline size_t serial_partitioning(T* array, 
                                             const size_t begin,
                                             const size_t end, 
                                             V& leftReduction,
                                             V& rightReduction,
                                             const IsLeft& is_left, 
                                             const Reduction_T& reduction_t)
  {
    T* l = array + begin;
    T* r = array + end - 1;
    
    while(1)
    {
      /* *l < pivot */
      while (likely(l <= r && is_left(*l) )) 
      {
        //prefetchw(l+4); // FIXME: enable?
        reduction_t(leftReduction,*l);
        ++l;
      }
      /* *r >= pivot) */
      while (likely(l <= r && !is_left(*r)))
      {
        //prefetchw(r-4); FIXME: enable?
        reduction_t(rightReduction,*r);
        --r;
      }
      if (r<l) break;
      
      reduction_t(leftReduction ,*r);
      reduction_t(rightReduction,*l);
      xchg(*l,*r);
      l++; r--;
    }
    
    return l - array;        
  }

  template<typename T, typename V, typename Vi, typename IsLeft, typename Reduction_T, typename Reduction_V>
    class __aligned(64) parallel_partition_task
  {
    ALIGNED_CLASS_(64);
  private:

    static const size_t MAX_TASKS = 64;

    T* array;
    size_t N;
    const IsLeft& is_left;
    const Reduction_T& reduction_t;
    const Reduction_V& reduction_v;
    const Vi& identity;

    size_t numTasks; 
    __aligned(64) size_t counter_start[MAX_TASKS+1]; 
    __aligned(64) size_t counter_left[MAX_TASKS+1];  
    __aligned(64) range<ssize_t> leftMisplacedRanges[MAX_TASKS];  
    __aligned(64) range<ssize_t> rightMisplacedRanges[MAX_TASKS]; 
    __aligned(64) V leftReductions[MAX_TASKS];           
    __aligned(64) V rightReductions[MAX_TASKS];    

  public:
     
    __forceinline parallel_partition_task(T* array, 
                                          const size_t N, 
                                          const Vi& identity, 
                                          const IsLeft& is_left, 
                                          const Reduction_T& reduction_t, 
                                          const Reduction_V& reduction_v,
                                          const size_t BLOCK_SIZE) 

      : array(array), N(N), is_left(is_left), reduction_t(reduction_t), reduction_v(reduction_v), identity(identity),
      numTasks(min((N+BLOCK_SIZE-1)/BLOCK_SIZE,min(TaskScheduler::threadCount(),MAX_TASKS))) {}

    __forceinline const range<ssize_t>* findStartRange(size_t& index, const range<ssize_t>* const r, const size_t numRanges)
    {
      size_t i = 0;
      while(index >= (size_t)r[i].size())
      {
        assert(i < numRanges);
        index -= (size_t)r[i].size();
        i++;
      }	    
      return &r[i];
    }

    __forceinline void swapItemsInMisplacedRanges(const size_t numLeftMisplacedRanges,
                                                  const size_t numRightMisplacedRanges,
                                                  const size_t startID,
                                                  const size_t endID)
    {
      size_t leftLocalIndex  = startID;
      size_t rightLocalIndex = startID;
      const range<ssize_t>* l_range = findStartRange(leftLocalIndex,leftMisplacedRanges,numLeftMisplacedRanges);
      const range<ssize_t>* r_range = findStartRange(rightLocalIndex,rightMisplacedRanges,numRightMisplacedRanges);
      
      size_t l_left = l_range->size() - leftLocalIndex;
      size_t r_left = r_range->size() - rightLocalIndex;
      T *__restrict__ l = &array[l_range->begin() + leftLocalIndex];
      T *__restrict__ r = &array[r_range->begin() + rightLocalIndex];
      size_t size = endID - startID;
      size_t items = min(size,min(l_left,r_left)); 
     
      while (size)
      {
        if (unlikely(l_left == 0))
        {
          l_range++;
          l_left = l_range->size();
          l = &array[l_range->begin()];
          items = min(size,min(l_left,r_left));
        }

        if (unlikely(r_left == 0))
        {		
          r_range++;
          r_left = r_range->size();
          r = &array[r_range->begin()];          
          items = min(size,min(l_left,r_left));
        }

        size   -= items;
        l_left -= items;
        r_left -= items;

        while(items) {
          items--;
          xchg(*l++,*r++);
        }
      }
    }

    __forceinline size_t partition(V& leftReduction, V& rightReduction)
    {
      /* partition the individual ranges for each task */
      parallel_for(numTasks,[&] (const size_t taskID) {
          const size_t startID = (taskID+0)*N/numTasks;
          const size_t endID   = (taskID+1)*N/numTasks;
          V local_left(identity);
          V local_right(identity);
          const size_t mid = serial_partitioning(array,startID,endID,local_left,local_right,is_left,reduction_t);
          counter_start[taskID] = startID;
          counter_left [taskID] = mid-startID;
          leftReductions[taskID]  = local_left;
          rightReductions[taskID] = local_right;
        });
      counter_start[numTasks] = N;
      counter_left[numTasks]  = 0;
      
      /* finalize the reductions */
      for (size_t i=0; i<numTasks; i++) {
        reduction_v(leftReduction,leftReductions[i]);
        reduction_v(rightReduction,rightReductions[i]);
      }

      /* calculate mid point for partitioning */
      size_t mid = counter_left[0];
      for (size_t i=1; i<numTasks; i++)
        mid += counter_left[i];
      const range<ssize_t> globalLeft (0,mid);
      const range<ssize_t> globalRight(mid,N);

      /* calculate all left and right ranges that are on the wrong global side */
      size_t numMisplacedRangesLeft  = 0;
      size_t numMisplacedRangesRight = 0;
      size_t numMisplacedItemsLeft   = 0;
      size_t numMisplacedItemsRight  = 0;

      for (size_t i=0; i<numTasks; i++)
      {	    
        const range<ssize_t> left_range (counter_start[i], counter_start[i] + counter_left[i]);
        const range<ssize_t> right_range(counter_start[i] + counter_left[i], counter_start[i+1]);
        const range<ssize_t> left_misplaced  = globalLeft. intersect(right_range);
        const range<ssize_t> right_misplaced = globalRight.intersect(left_range);

        if (!left_misplaced.empty())  
        {
          numMisplacedItemsLeft += left_misplaced.size();
          leftMisplacedRanges[numMisplacedRangesLeft++] = left_misplaced;
        }

        if (!right_misplaced.empty()) 
        {
          numMisplacedItemsRight += right_misplaced.size();
          rightMisplacedRanges[numMisplacedRangesRight++] = right_misplaced;
        }
      }
      assert( numMisplacedItemsLeft == numMisplacedItemsRight );

      /* if no items are misplaced we are done */
      if (numMisplacedItemsLeft == 0)
        return mid;

      /* otherwise we copy the items to the right place in parallel */
      parallel_for(numTasks,[&] (const size_t taskID) {
          const size_t startID = (taskID+0)*numMisplacedItemsLeft/numTasks;
          const size_t endID   = (taskID+1)*numMisplacedItemsLeft/numTasks;
          swapItemsInMisplacedRanges(numMisplacedRangesLeft,numMisplacedRangesRight,startID,endID);	                             
        });

      return mid;
    }
  };

  template<typename T, typename V, typename Vi, typename IsLeft, typename Reduction_T, typename Reduction_V>
    __noinline size_t parallel_partitioning(T* array, 
                                            const size_t begin,
                                            const size_t end, 
                                            const Vi &identity,
                                            V &leftReduction,
                                            V &rightReduction,
                                            const IsLeft& is_left, 
                                            const Reduction_T& reduction_t,
                                            const Reduction_V& reduction_v,
                                            size_t BLOCK_SIZE = 128)
  {
    /* fall back to single threaded partitioning for small N */
    if (unlikely(end-begin < BLOCK_SIZE))
      return serial_partitioning(array,begin,end,leftReduction,rightReduction,is_left,reduction_t);

    /* otherwise use parallel code */
    else {
      typedef parallel_partition_task<T,V,Vi,IsLeft,Reduction_T,Reduction_V> partition_task;
      std::unique_ptr<partition_task> p(new partition_task(&array[begin],end-begin,identity,is_left,reduction_t,reduction_v,BLOCK_SIZE));
      return begin+p->partition(leftReduction,rightReduction);    
    }
  }

  template<typename T, typename V, typename Vi, typename IsLeft, typename Reduction_T, typename Reduction_V>
    __noinline size_t parallel_partitioning(T* array, 
                                            const size_t begin,
                                            const size_t end, 
                                            const Vi &identity,
                                            V &leftReduction,
                                            V &rightReduction,
                                            const IsLeft& is_left, 
                                            const Reduction_T& reduction_t,
                                            const Reduction_V& reduction_v,
                                            size_t BLOCK_SIZE,
                                            size_t PARALLEL_THRESHOLD)
  {
    /* fall back to single threaded partitioning for small N */
    if (unlikely(end-begin < PARALLEL_THRESHOLD))
      return serial_partitioning(array,begin,end,leftReduction,rightReduction,is_left,reduction_t);

    /* otherwise use parallel code */
    else {
      typedef parallel_partition_task<T,V,Vi,IsLeft,Reduction_T,Reduction_V> partition_task;
      std::unique_ptr<partition_task> p(new partition_task(&array[begin],end-begin,identity,is_left,reduction_t,reduction_v,BLOCK_SIZE));
      return begin+p->partition(leftReduction,rightReduction);    
    }
  }


  template<typename T, typename IsLeft>
    inline size_t parallel_partitioning(T* array, 
                                        const size_t begin,
                                        const size_t end, 
                                        const IsLeft& is_left, 
                                        size_t BLOCK_SIZE = 128)
  {
    size_t leftReduction = 0;
    size_t rightReduction = 0;
    return parallel_partitioning(
      array,begin,end,0,leftReduction,rightReduction,is_left,
      [] (size_t& t,const T& ref) {  },
      [] (size_t& t0,size_t& t1) { },
      BLOCK_SIZE);
  }

}