Java Code Examples for java.util.concurrent.atomic.DoubleAdder#sum()

The following examples show how to use java.util.concurrent.atomic.DoubleAdder#sum() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DoubleAdderTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * adds by multiple threads produce correct sum
 */
public void testAddAndSumMT() throws Throwable {
    final int incs = 1000000;
    final int nthreads = 4;
    final ExecutorService pool = Executors.newCachedThreadPool();
    DoubleAdder a = new DoubleAdder();
    CyclicBarrier barrier = new CyclicBarrier(nthreads + 1);
    for (int i = 0; i < nthreads; ++i)
        pool.execute(new AdderTask(a, barrier, incs));
    barrier.await();
    barrier.await();
    double total = (long)nthreads * incs;
    double sum = a.sum();
    assertEquals(sum, total);
    pool.shutdown();
}
 
Example 2
Source File: LinearBatch.java    From JSAT with GNU General Public License v3.0 6 votes vote down vote up
@Override
public double f(Vec w, boolean parallel)
{
    DoubleAdder sum = new DoubleAdder();
    DoubleAdder weightSum = new DoubleAdder();
    
    ParallelUtils.run(parallel, D.size(), (start, end)->
    {
        for(int i = start; i < end; i++)
        {
            DataPoint dp = D.getDataPoint(i);
            Vec x = dp.getNumericalValues();
            double y = getTargetY(D, i);
            sum.add(loss.getLoss(w.dot(x), y)*D.getWeight(i));
            weightSum.add(D.getWeight(i));
        }
    });
    
    if(lambda0 > 0)
        return sum.sum()/weightSum.sum() + lambda0*w.dot(w);
    else
        return sum.sum()/weightSum.sum();
}
 
Example 3
Source File: DoubleAdderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * adds by multiple threads produce correct sum
 */
public void testAddAndSumMT() throws Throwable {
    final int incs = 1000000;
    final int nthreads = 4;
    final ExecutorService pool = Executors.newCachedThreadPool();
    DoubleAdder a = new DoubleAdder();
    CyclicBarrier barrier = new CyclicBarrier(nthreads + 1);
    for (int i = 0; i < nthreads; ++i)
        pool.execute(new AdderTask(a, barrier, incs));
    barrier.await();
    barrier.await();
    double total = (long)nthreads * incs;
    double sum = a.sum();
    assertEquals(sum, total);
    pool.shutdown();
}
 
Example 4
Source File: DoubleAdderDemo.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    DoubleAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.add(1.0);
    result = a.sum();
    phaser.arrive();
}
 
Example 5
Source File: DoubleAdderTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void run() {
    try {
        barrier.await();
        DoubleAdder a = adder;
        for (int i = 0; i < incs; ++i)
            a.add(1.0);
        result = a.sum();
        barrier.await();
    } catch (Throwable t) { throw new Error(t); }
}
 
Example 6
Source File: DoubleAdderDemo.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    DoubleAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.add(1.0);
    result = a.sum();
    phaser.arrive();
}
 
Example 7
Source File: DoubleAdderDemo.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    DoubleAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.add(1.0);
    result = a.sum();
    phaser.arrive();
}
 
Example 8
Source File: LinearBatch.java    From JSAT with GNU General Public License v3.0 5 votes vote down vote up
@Override
public double f(Vec w, boolean parallel)
{
    DoubleAdder sum = new DoubleAdder();
    Vec pred = new DenseVector(D.getClassSize());//store the predictions in
    //bias terms are at the end, treat them seperate and special
    final int subWSize = (w.length() - (useBiasTerm ? bs.length : 0) )/D.getClassSize();
    DoubleAdder weightSum = new DoubleAdder();
    ParallelUtils.run(parallel, D.size(), (start, end)->
    {
        Vec pred_local = pred.clone();
        for (int i = start; i < end; i++)
        {
            DataPoint dp = D.getDataPoint(i);
            Vec x = dp.getNumericalValues();
            for(int k = 0; k < pred_local.length(); k++)
                pred_local.set(k, new SubVector(k*subWSize, subWSize, w).dot(x));
            if(useBiasTerm)
                pred_local.mutableAdd(new SubVector(w.length()-bs.length, bs.length, w));
            loss.process(pred_local, pred_local);
            int y = D.getDataPointCategory(i);
            sum.add(loss.getLoss(pred_local, y)*D.getWeight(i));
            weightSum.add(D.getWeight(i));
        }
    });
    if(lambda0 > 0 )
        return sum.sum()/weightSum.sum() + lambda0*w.dot(w);
    return sum.sum();
}
 
Example 9
Source File: DoubleAdderDemo.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    DoubleAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.add(1.0);
    result = a.sum();
    phaser.arrive();
}
 
Example 10
Source File: DoubleAdderDemo.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    DoubleAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.add(1.0);
    result = a.sum();
    phaser.arrive();
}
 
Example 11
Source File: DoubleAdderDemo.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    DoubleAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.add(1.0);
    result = a.sum();
    phaser.arrive();
}
 
Example 12
Source File: DoubleAdderDemo.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    DoubleAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.add(1.0);
    result = a.sum();
    phaser.arrive();
}
 
Example 13
Source File: DoubleAdderDemo.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    DoubleAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.add(1.0);
    result = a.sum();
    phaser.arrive();
}
 
Example 14
Source File: DoubleAdderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    try {
        barrier.await();
        DoubleAdder a = adder;
        for (int i = 0; i < incs; ++i)
            a.add(1.0);
        result = a.sum();
        barrier.await();
    } catch (Throwable t) { throw new Error(t); }
}
 
Example 15
Source File: DoubleAdderDemo.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    DoubleAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.add(1.0);
    result = a.sum();
    phaser.arrive();
}
 
Example 16
Source File: DoubleAdderDemo.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    DoubleAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.add(1.0);
    result = a.sum();
    phaser.arrive();
}
 
Example 17
Source File: DoubleAdderDemo.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    DoubleAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.add(1.0);
    result = a.sum();
    phaser.arrive();
}
 
Example 18
Source File: DoubleAdderDemo.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    DoubleAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.add(1.0);
    result = a.sum();
    phaser.arrive();
}
 
Example 19
Source File: DoubleAdderDemo.java    From native-obfuscator with GNU General Public License v3.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    DoubleAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.add(1.0);
    result = a.sum();
    phaser.arrive();
}
 
Example 20
Source File: DoubleAdderDemo.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    DoubleAdder a = adder;
    for (int i = 0; i < incs; ++i)
        a.add(1.0);
    result = a.sum();
    phaser.arrive();
}