我正在尝试从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:
使用命令行:
在命令行中,我尝试执行与eclipse相同的操作,eclipse指定共享库的路径,mpi lib也指定mpi.jar并尝试运行HelloJNI,但我得到相同的错误。我不知道为什么我会收到这些错误,我想知道为什么我会得到它们。我将非常感谢对此提出的任何建议或想法。
答案 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
如果没有,
为openmpi库创建符号链接
带有openmpi .so文件的ldconfig -n 文件夹
将该路径添加到LD_LIBRARY_PATH
ldd libhello.so
一旦它开始由ldd加载,它将在java中运行。