-
下载jdk8:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
-
解压jdk:
tar -zxvf /Dowloads/jdk-8u161-linux-x64.tar.gz
-
解压OpenMPI:
tar -zxvf /Dowloads/openmpi-3.0.0.tar.gz
-
配置包含Java的Makefile,编译:
./configure --prefix=/opt/openmpi --enable-mpi-java ##不加prefix可能找不到mpi.jar make sudo make install
-
配置环境变量(如 .bashrc)
export JAVA_HOME=/root/Downloads/jdk1.8.0_161 export JRE_HOME=${JAVA_HOME}/jre export MPI_HOME=/opt/openmpi export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:${MPI_HOME}/lib export PATH=${JAVA_HOME}/bin:${MPI_HOME}/bin:$PATH
-
检查是否正常运行
import mpi.*; class ComputePi { public static void main(String args[]) throws MPIException { MPI.Init(args); int rank = MPI.COMM_WORLD.getRank(), size = MPI.COMM_WORLD.getSize(), nint = 100; // Intervals. double h = 1.0/(double)nint, sum = 0.0; for(int i=rank+1; i<=nint; i+=size) { double x = h * ((double)i - 0.5); sum += (4.0 / (1.0 + x * x)); } double sBuf[] = { h * sum }, rBuf[] = new double[1]; MPI.COMM_WORLD.reduce(sBuf, rBuf, 1, MPI.DOUBLE, MPI.SUM, 0); if(rank == 0) System.out.println("PI: " + rBuf[0]); MPI.Finalize(); } }
-
在当前路径下建立hosts文件(单机)
localhost slots=4 ##表示使用本机的4个进程
-
编译并运行代码
mpijavac ComputePi.java mpiexec --hostfile hosts -np 4 --allow-run-as-root java ComputePi
-
显示结果:
PI: 3.141600986923125
网友评论