Created
April 10, 2012 14:40
-
-
Save orbekk/2351825 to your computer and use it in GitHub Desktop.
Java Data Parallelism
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ExecutorService; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.ArrayList; | |
public class Par { | |
int parallelism = Runtime.getRuntime().availableProcessors() + 1; | |
ExecutorService executor = Executors.newCachedThreadPool(); | |
public Iterable<Integer> partition(final int total, final int chunkIndex, | |
final int numChunks) { | |
class Partition implements Iterator<Integer> { | |
int chunkSize = total / numChunks; | |
int start = chunkIndex * chunkSize; | |
boolean lastChunk = chunkIndex + 1 == numChunks; | |
int end = lastChunk ? total - 1 : (chunkIndex + 1) * chunkSize - 1; | |
int current = start; | |
@Override public boolean hasNext() { | |
return current <= end; | |
} | |
@Override public Integer next() { | |
return current++; | |
} | |
@Override public void remove() { | |
throw new UnsupportedOperationException(); | |
} | |
}; | |
return new Iterable<Integer>() { | |
@Override public Iterator<Integer> iterator() { | |
return new Partition(); | |
} | |
}; | |
} | |
public void run() { | |
final List<Integer> list = new ArrayList<Integer>(); | |
final int numElements = 10; | |
for (int i = 0; i < numElements; i++) { | |
list.add(i); | |
} | |
final CountDownLatch finished = new CountDownLatch(parallelism); | |
for (int i = 0; i < parallelism; i++) { | |
final int i_ = i; | |
executor.execute(new Runnable() { | |
@Override public void run() { | |
for (int j : partition(numElements, i_, parallelism)) { | |
int elem = list.get(j); | |
list.set(j, elem * 2); | |
} | |
finished.countDown(); | |
} | |
}); | |
} | |
executor.shutdown(); | |
try { | |
finished.await(); | |
System.out.println(list); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
new Par().run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment