简体   繁体   English

CyclicBarrier 浪费时间

[英]CyclicBarrier Wasting Time

I'm implementing a parallel algorithm.我正在实现一个并行算法。 Without CyclicBarrier I can chug through the work in half the Sequential Time.如果没有 CyclicBarrier,我可以用一半的 Sequential Time 完成工作。 Using CyclicBarrier makes it take up to 100 times longer.使用 CyclicBarrier 最多需要 100 倍的时间。 I'll include my thread calls and thread function so you can see what is going on and try to help me out.我将包括我的线程调用和线程函数,以便您可以看到发生了什么并尝试帮助我。 The CyclicBarrier is reused and new threads are spawned everytime. CyclicBarrier 被重用,并且每次都会产生新的线程。 For some reason the TRY(barrier.await;) bit is spinning for a LONG time.由于某种原因,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();
                        }
                }





You are having the barrier too far into the loop, right now each thread gets a range of elements to process and they all process one element, wait for all treads, the process the next, wait and so on.您在循环中遇到了障碍,现在每个线程都会处理一系列元素,并且它们都处理一个元素,等待所有踏板,然后处理下一个,等等。 In this case the overhead of waiting and communicating between the threads becomes much more work than the actual processing.在这种情况下,线程之间等待和通信的开销变得比实际处理要多得多。

Try to process more elements before aligning up with the other threads such as processin the whole range, then waiting.在与其他线程对齐之前尝试处理更多元素,例如整个范围内的进程,然后等待。

//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);
            }
            // Wait moved from here
        }
        // To here (outside the inner loop)
        try{barrier.await();}
        catch (InterruptedException ex) { return; }
        catch (BrokenBarrierException ex) {return; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM