美文网首页
Linux Java OpenMPI 安装

Linux Java OpenMPI 安装

作者: SmileySure | 来源:发表于2018-02-26 10:16 被阅读0次
    1. 下载jdk8:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

    2. 解压jdk:

      tar -zxvf /Dowloads/jdk-8u161-linux-x64.tar.gz
      
    3. 下载openMPI:https://www.open-mpi.org/software/ompi/v3.0/

    4. 解压OpenMPI:

      tar -zxvf /Dowloads/openmpi-3.0.0.tar.gz
      
    5. 配置包含Java的Makefile,编译:

      ./configure --prefix=/opt/openmpi --enable-mpi-java ##不加prefix可能找不到mpi.jar
      
      make
      
      sudo make install
      
    6. 配置环境变量(如 .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
      
    7. 检查是否正常运行

      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();
          }
      }
      
    8. 在当前路径下建立hosts文件(单机)

      localhost    slots=4 ##表示使用本机的4个进程
      
    9. 编译并运行代码

      mpijavac ComputePi.java
      mpiexec --hostfile hosts -np 4 --allow-run-as-root java ComputePi
      
    10. 显示结果:

      PI: 3.141600986923125
      

    相关文章

      网友评论

          本文标题:Linux Java OpenMPI 安装

          本文链接:https://www.haomeiwen.com/subject/wmdexftx.html