|
1 | 1 | package com.github.coderodde.util; |
| 2 | + |
| 3 | +import java.util.Random; |
| 4 | + |
2 | 5 | /** |
3 | 6 | * |
4 | 7 | * @author Rodion "rodde" Efremov |
@@ -343,6 +346,69 @@ private static void parallelRadixSortImpl( |
343 | 346 |
|
344 | 347 | listOfBucketKeyLists.addBucketKeyList(bucketKeyList); |
345 | 348 | } |
| 349 | + |
| 350 | + // Match each thread to the number of threads it may run in: |
| 351 | + int[] threadCountMap = new int[spawnDegree]; |
| 352 | + |
| 353 | + // ... basic thread counts... |
| 354 | + for (int i = 0; i != spawnDegree; i++) { |
| 355 | + threadCountMap[i] = threads / spawnDegree; |
| 356 | + } |
| 357 | + |
| 358 | + // ... make sure all threads are in use: |
| 359 | + for (int i = 0; i != threads % spawnDegree; i++) { |
| 360 | + threadCountMap[i]++; |
| 361 | + } |
| 362 | + |
| 363 | + BucketKeyList nonEmptyBucketIndices = |
| 364 | + new BucketKeyList(numberOfNonemptyBuckets); |
| 365 | + |
| 366 | + for (int bucketKey = 0; bucketKey != BUCKETS; bucketKey++) { |
| 367 | + if (globalBucketSizeMap[bucketKey] != 0) { |
| 368 | + nonEmptyBucketIndices.addBucketKey(bucketKey); |
| 369 | + } |
| 370 | + } |
| 371 | + |
| 372 | + Random random = new Random(); |
| 373 | + nonEmptyBucketIndices.shuffle(random); |
| 374 | + |
| 375 | + int f = 0; |
| 376 | + int j = 0; |
| 377 | + int listIndex = 0; |
| 378 | + int optimalSubrangeLength = rangeLength / spawnDegree; |
| 379 | + int packed = 0; |
| 380 | + int sz = nonEmptyBucketIndices.size(); |
| 381 | + |
| 382 | + while (j != sz) { |
| 383 | + int bucketKey = nonEmptyBucketIndices.getBucketKey(j++); |
| 384 | + int tmp = globalBucketSizeMap[bucketKey]; |
| 385 | + packed += tmp; |
| 386 | + |
| 387 | + if (packed >= optimalSubrangeLength || j == sz) { |
| 388 | + packed = 0; |
| 389 | + |
| 390 | + for (int i = f; i != j; i++) { |
| 391 | + int bucketKey2 = nonEmptyBucketIndices.getBucketKey(i); |
| 392 | + |
| 393 | + BucketKeyList bucketKeyList = |
| 394 | + listOfBucketKeyLists.getBucketKeyList(listIndex); |
| 395 | + |
| 396 | + bucketKeyList.addBucketKey(bucketKey2); |
| 397 | + } |
| 398 | + |
| 399 | + listIndex++; |
| 400 | + f = j; |
| 401 | + } |
| 402 | + } |
| 403 | + |
| 404 | + ListOfBucketKeyLists listOfTaskArrays = |
| 405 | + new ListOfBucketKeyLists(spawnDegree); |
| 406 | + |
| 407 | + for (int i = 0; i != spawnDegree; i++) { |
| 408 | + BucketKeyList bucketKeyList = new BucketKeyList(BUCKETS); |
| 409 | + |
| 410 | + |
| 411 | + } |
346 | 412 | } |
347 | 413 |
|
348 | 414 | private static void rangeCheck( |
@@ -717,6 +783,15 @@ int getBucketKey(int index) { |
717 | 783 | int size() { |
718 | 784 | return size; |
719 | 785 | } |
| 786 | + |
| 787 | + void shuffle(Random random) { |
| 788 | + for (int i = 0; i != size - 1; i++) { |
| 789 | + int j = i + random.nextInt(size - i); |
| 790 | + int temp = bucketKeys[i]; |
| 791 | + bucketKeys[i] = bucketKeys[j]; |
| 792 | + bucketKeys[j] = temp; |
| 793 | + } |
| 794 | + } |
720 | 795 | } |
721 | 796 |
|
722 | 797 | private static final class ListOfBucketKeyLists { |
|
0 commit comments