並列アルゴリズムを実装しています。CyclicBarrier がなければ、シーケンシャル時間の半分で作業を完了できます。CyclicBarrier を使用すると、最大 100 倍の時間がかかります。スレッド呼び出しとスレッド関数を含めて、何が起こっているかを確認し、私を助けようとすることができます. CyclicBarrier は再利用され、毎回新しいスレッドが生成されます。何らかの理由で、TRY(barrier.await;) ビットが長時間回転しています。
//Threads use this ...
private class threadILoop implements Runnable {
protected int start, end, j, k;
public threadILoop(int start,int end,int j,int k){
this.start = start;
this.end = end;
this.j = j;
this.k = k;
}
public void run() {
for (int z = start; z < end; z++) {
int zxj = z ^ j;
if(zxj > z){
if((z&k) == 0 && (data[z] > data[zxj]))
swap(z, zxj);
if((z&k) != 0 && (data[z] < data[zxj]))
swap(z, zxj);
}
try{barrier.await();}
catch (InterruptedException ex) { return; }
catch (BrokenBarrierException ex) {return; }
}
}
}
//Main Driver here, where the CyclicBarrier gets allocated and the threads //are spawned from.
private void loopSort() throws InterruptedException {
//print(data);
barrier = new CyclicBarrier(N_THREADS);
int kMax = data.length;
for(int k = 2; k<=kMax; k*=2){
for (int j = k/2; j > 0; j/=2) {
int piece = data.length/N_THREADS;
if(j > N_THREADS) {
//DIVIDE UP DATA SPACE FOR THREADS -> do work faster
int start = 0;
for(int i = 0; i < N_THREADS; i++)
{
int end = i == N_THREADS - 1 ? data.length : start + piece;
threads[i] = new Thread(new threadILoop(start, end, j, k));
//threads[i].start();
start = end;
}
for(int i = 0; i < N_THREADS; i++)
{
threads[i].start();
}
// print(data);
for(int i = 0; i < N_THREADS; i++)
{
threads[i].join();
}
}