Skip to content

Commit 06ee856

Browse files
committed
Getting closer to having the algorithm filled.
1 parent cfcd052 commit 06ee856

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

src/main/java/com/github/coderodde/util/ParallelRadixSort.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package com.github.coderodde.util;
2+
3+
import java.util.Random;
4+
25
/**
36
*
47
* @author Rodion "rodde" Efremov
@@ -343,6 +346,69 @@ private static void parallelRadixSortImpl(
343346

344347
listOfBucketKeyLists.addBucketKeyList(bucketKeyList);
345348
}
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+
}
346412
}
347413

348414
private static void rangeCheck(
@@ -717,6 +783,15 @@ int getBucketKey(int index) {
717783
int size() {
718784
return size;
719785
}
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+
}
720795
}
721796

722797
private static final class ListOfBucketKeyLists {

0 commit comments

Comments
 (0)