Skip to content

Tag: Parallel

parallel counting in Java using AtomicInteger

[java]
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.TimeUnit;

class ParallelCounterExample {
public static void main(String[] args) throws InterruptedException {
int threads = 2000;
ExecutorService executor = Executors.newFixedThreadPool(threads);
final AtomicInteger atomi = new AtomicInteger();
for(int i=0; i<threads; i++) {
executor.execute( new Runnable() {
@Override
public void run() {
atomi.getAndIncrement();
}
});
}
executor.awaitTermination(1, TimeUnit.SECONDS);
executor.shutdown();
System.out.println(atomi.get());
}
}
[/java]

Parallel Build Benchmark

You can optimizing your building times using a parallel build process.

The GNU Make supports it using the parameter –jobs=N (or -j=N), where N is the maximum number of jobs that can be realized at the same time, usually compilation jobs. By default, Make will perform just a job per time. Using -j without arguments imposes no limits on job number. There’s also the load approach using –load-average.

Here’s a benchmark I did showing four different complete builds of the Inkscape project, from one to four jobs at the same time. I used a Intel (Yonah FSB667 Mhz/2MbL2) Dual Core with 2 Gb of Ram with a common Ubuntu 8.10 and default build parameters and no additional optimizations.

chartinkscape_parallel_build.ods

Just compiling with make –jobs=2 instead of just make, almost doubles the speed of the build. As I’m using a dual core processor and the heavy compilations dominate the build process, the best result was with 2 jobs.

I had no trouble with those builds but it’s known that you can have problems such implicit dependencies among targets or memory exhaustion. There’s a good article, Optimizing Build Times Using Parallel “make”, on this subject. On the Make manual, there’s also a section on parallel excetution.

So, next time you try a make, try using make –jobs=2 and see the results. 🙂