Hello World
Code example for PCJ 5: Print Hello World in parallel.
Calculations start from a special StartPoint class. That class contains main method (public void main()):
1
import java.io.IOException;
2
import org.pcj.*;
3
4
public class HelloWorld implements StartPoint {
5
6
public static void main(String[] args) throws IOException {
7
String nodesFile = "nodes.txt";
8
PCJ.executionBuilder (PcjExample.class)
9
.addNodes(new File("nodes.txt"))
10
.start();
11
}
12
}
13
14
@Override
15
public void main() throws Throwable {
16
System.out.println("Hello World from PCJ Thread " + PCJ.myId()
17
+ " out of " + PCJ.threadCount() );
18
}
19
}
Copied!
The compilation and execution requires PCJ-5.0.9.jar in the path:
javac -cp .:PCJ.jar HelloWorld.java java -cp .:PCJ-5.0.9.jar HelloWorld
The expected output is presented below:
wrz 23, 2016 2:00:22 AM org.pcj.internal.InternalPCJ start INFO: PCJ version 5.0.0.SNAPSHOT-a728f5f built on 2016-09-13 00:58:48.180 CEST. wrz 23, 2016 2:00:22 AM org.pcj.internal.InternalPCJ start INFO: Starting HelloWorld with 4 threads (on 1 node)... Hello World from PCJ Thread 2 out of 4 Hello World from PCJ Thread 0 out of 4 Hello World from PCJ Thread 1 out of 4 Hello World from PCJ Thread 3 out of 4 wrz 23, 2016 2:00:22 AM org.pcj.internal.InternalPCJ start INFO: Completed HelloWorld with 4 threads (on 1 node) after 0h 0m 0s. BUILD SUCCESSFUL (total time: 0 seconds)
Copy link