从java运行本机MPI代码

时间:2015-07-09 12:06:55

标签: java java-native-interface mpi

我正在尝试从java运行一个简单的本机mpi代码,稍后我需要为我的项目。我正在使用Ubuntu。 我有:

HelloJNI.java

public class HelloJNI {
 static {
   System.loadLibrary("hello");
 }

 private native void sayHello();

 public static void main(String[] args) {
   new HelloJNI().sayHello();  // invoke the native method
 }
}

标题文件:

 JNIEXPORT void JNICALL Java_HelloJNI_sayHello (JNIEnv *, jobject);

C源文件:

JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject     thisObj) {
printf("Hello World- MPI from C!\n");
MPI_Init(NULL, NULL);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
printf("Hello world from processor %s, rank %d"
      " out of %d processors\n",
      processor_name, world_rank, world_size);
MPI_Finalize();
}

我已经按照本教程: https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

我可以通过make文件创建共享库:

INCLUDE1 = /usr/lib/java/jdk1.8.0_45/include
INCLUDE2 = /usr/lib/java/jdk1.8.0_45/include/linux
MPI_INCLUDE = /home/openmpi-1.8.5/ompi/include
JAVAHPATH = /usr/lib/java/jdk1.8.0_45/bin
MPICC = /home/openmpi-1.8.5/bin/mpicc
# Define a variable for classpath
CLASS_PATH = ../bin

# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)

all : libhello.so

# $@ matches the target, $< matches the first dependancy
libhello.so : HelloJNI.o
${MPICC} $< -L/home/openmpi-1.8.5/lib -lmpi -shared -fpic -o $@ -Wl,-   rpath=/home/openmpi-1.8.5/lib 

# $@ matches the target, $< matches the first dependancy
HelloJNI.o : HelloJNI.c HelloJNI.h
${MPICC} -fpic -I$(INCLUDE1) -I$(INCLUDE2) -I$(MPI_INCLUDE) -c $< -o $@

# $* matches the target filename without the extension
HelloJNI.h : HelloJNI.class     
    $(JAVAHPATH)/javah -classpath $(CLASS_PATH) $*


clean :
      rm HelloJNI.h HelloJNI.o libhello.so

问题是,当我尝试运行它时,我遇到了这些MPI错误:

"mca: base: component_find: unable to open /home/openmpi-1.8.5/lib/openmpi/mca_shmem_sysv: /home/openmpi-1.8.5/lib/openmpi/mca_shmem_sysv.so: undefined symbol: opal_show_help (ignored)
mca: base: component_find: unable to open /home/openmpi-1.8.5/lib/openmpi/mca_shmem_mmap: /home/openmpi-1.8.5/lib/openmpi/mca_shmem_mmap.so: undefined symbol: opal_show_help (ignored)
mca: base: component_find: unable to open /home/openmpi-1.8.5/lib/openmpi/mca_shmem_posix: /home/openmpi-1.8.5/lib/openmpi/mca_shmem_posix.so: undefined symbol: opal_shmem_base_framework (ignored)
It looks like opal_init failed .....orte_init failed ..... mpi_init  failed"

到目前为止,我已经从命令行和使用eclipse尝试了各种各样的东西: 使用Eclipse:

  • 在运行配置中通过VM Argument中的-Djava.library.path添加了共享库路径
    • 同样通过LD_LIBRARY_PATH,如环境和bashrc
    • 在makefile中,我尝试使用mpicc
    • 编译共享库
    • 我已将mpi.jar添加到项目中

使用命令行:

  • javac -cp /home/s3050004/Downloads/openmpi-1.8.5/lib/mpi.jar HelloJNI.java
  • java -Djava.library.path = / home / workspace / HelloJNI / jni:/home/openmpi-1.8.5/lib -cp /home/openmpi-1.8.5/lib/mpi.jar:/home/ workspace / HelloJNI / src HelloJNI

在命令行中,我尝试执行与eclipse相同的操作,eclipse指定共享库的路径,mpi lib也指定mpi.jar并尝试运行HelloJNI,但我得到相同的错误。我不知道为什么我会收到这些错误,我想知道为什么我会得到它们。我将非常感谢对此提出的任何建议或想法。

2 个答案:

答案 0 :(得分:2)

已修复,这是OpenMPI的问题,我重新安装并重新配置它并且它有效:

private String createDeleteRecord(int rowNum, String[] record) {
StringBuffer sb = new StringBuffer(rowNum + "~D~N~");

for (int i = 0; i < (intNoOfStaticCols - 1); i++) {
sb.append(record[i].trim() + "~");
}

return sb.substring(0, sb.length() - 1);
}

答案 1 :(得分:0)

看起来java无法加载libhello.so的所有依赖项

请检查ldd:

是否可以加载依赖项
ldd libhello.so

如果没有,

  1. 为openmpi库创建符号链接

    带有openmpi .so文件的ldconfig -n 文件夹

  2. 将该路径添加到LD_LIBRARY_PATH

  3. 并再次尝试ldd libhello.so
  4. 一旦它开始由ldd加载,它将在java中运行。