线程“主”中的异常java.lang.NoClassDefFoundError:jcuda / driver / JCudaDriver

时间:2019-03-25 18:01:12

标签: java jar cuda jcuda

我正在尝试设置JCuda并执行示例附加内核。当我尝试在编译JCudaVectorAdd.java之后执行JCudaVectorAdd时,出现以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: jcuda/driver/JCudaDriver
        at JCudaVectorAdd.main(JCudaVectorAdd.java:38)
Caused by: java.lang.ClassNotFoundException: jcuda.driver.JCudaDriver
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more

我已经按照执行的步骤创建了一个bash文件,以实现自己所在的位置:

#!/bin/bash

# This system has multiple Cuda instances, so we need to load the correct one
module load cuda-9.2

# Try to remove any jcuda zip files that may have been created previously
rm $HOME/jcuda.zip

# Get jcuda zip file from online, storing into $HOME directory
wget http://www.jcuda.org/downloads/JCuda-All-0.9.2.zip -O $HOME/jcuda.zip

# Remove the 0.9.2 directory in case it exists to get ready for a clean install
rm -rf $HOME/jcuda/JCuda-All-0.9.2

# Unzip the file and store within jcuda directory. NOTE: The version number will be maintained as jcuda/JCuda-ALL-0.9.2.zip, so multiple versions of jcuda can be installed using this script
unzip $HOME/jcuda.zip -d $HOME/jcuda

# Remove the zipped file now that it is no longer needed
rm $HOME/jcuda.zip

# Move into the newly create jcuda directory
cd $HOME/jcuda/JCuda-All-0.9.2/

# Get the example Main program for Vector addition from Jcuda site
wget http://www.jcuda.org/samples/JCudaVectorAdd.java

# Create a sample kernel
echo 'extern "C"'                                                   >  JCudaVectorAddKernel.cu
echo '__global__ void add(int n, float *a, float *b, float *sum)'   >> JCudaVectorAddKernel.cu
echo '{'                                                            >> JCudaVectorAddKernel.cu
echo '    int i = blockIdx.x * blockDim.x + threadIdx.x;'           >> JCudaVectorAddKernel.cu
echo '    if (i<n)'                                                 >> JCudaVectorAddKernel.cu
echo '    {'                                                        >> JCudaVectorAddKernel.cu
echo '        sum[i] = a[i] + b[i];'                                >> JCudaVectorAddKernel.cu
echo '    }'                                                        >> JCudaVectorAddKernel.cu
echo '}'                                                            >> JCudaVectorAddKernel.cu

# Create a .ptx file from the cuda kernel to be consumed by the Main java program later
# The sample Main program also performs this action, but we have it here as well
nvcc -ptx JCudaVectorAddKernel.cu -o JCudaVectorAddKernel.ptx

# Try to generate a class file from the example Main .java file
javac -cp ".:jcuda-0.9.2.jar:jcuda-natives-0.9.2-linux-x86_64.jar" JCudaVectorAdd.java

# Run the compiled executable
java JCudaVectorAdd

尽管我对java或cuda / jcuda不够熟悉,无法识别出我所缺少的东西,但似乎似乎缺少了某个步骤。有人可以指出我要如何解决此问题,是否有其他步骤正在修改我执行的步骤?

编辑:看来我仍然需要在执行过程中引用其他.jar文件(就像在http://www.jcuda.org/tutorial/TutorialIndex.html基本测试中一样),因此我的上一个命令可能是错误的。将其更改为以下显示的结果略有不同:

java JCudaVectorAdd -> java -cp ".:jcuda-0.9.2.jar:jcuda-natives-0.9.2-linux-x86_64.jar" JCudaVectorAdd

Exception in thread "main" java.lang.UnsatisfiedLinkError: Error while loading native library "JCudaDriver-0.9.2-linux-x86_64"
Operating system name: Linux
Architecture         : amd64
Architecture bit size: 64
---(start of nested stack traces)---
Stack trace from the attempt to load the library as a file:
java.lang.UnsatisfiedLinkError: no JCudaDriver-0.9.2-linux-x86_64 in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
        at java.lang.Runtime.loadLibrary0(Runtime.java:870)
        at java.lang.System.loadLibrary(System.java:1122)
        at jcuda.LibUtils.loadLibrary(LibUtils.java:143)
        at jcuda.driver.JCudaDriver.<clinit>(JCudaDriver.java:296)
        at JCudaVectorAdd.main(JCudaVectorAdd.java:38)
Stack trace from the attempt to load the library as a resource:
java.lang.UnsatisfiedLinkError: /tmp/libJCudaDriver-0.9.2-linux-x86_64.so: libcuda.so.1: cannot open shared object file: No such file or directory
        at java.lang.ClassLoader$NativeLibrary.load(Native Method)
        at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1824)
        at java.lang.Runtime.load0(Runtime.java:809)
        at java.lang.System.load(System.java:1086)
        at jcuda.LibUtils.loadLibraryResource(LibUtils.java:260)
        at jcuda.LibUtils.loadLibrary(LibUtils.java:158)
        at jcuda.driver.JCudaDriver.<clinit>(JCudaDriver.java:296)
        at JCudaVectorAdd.main(JCudaVectorAdd.java:38)
---(end of nested stack traces)---

        at jcuda.LibUtils.loadLibrary(LibUtils.java:193)
        at jcuda.driver.JCudaDriver.<clinit>(JCudaDriver.java:296)
        at JCudaVectorAdd.main(JCudaVectorAdd.java:38)

1 个答案:

答案 0 :(得分:2)

似乎您需要告诉JRE在哪里找到相应的OS库来运行cuda。如果您查看输出,则说明找不到libcuda.so.1

找到这些库在文件系统中的位置,然后将java.library.path属性设置为指向该库。 java -Djava.library.path=/path/to/cudalibdir/ <rest of commandline>

如果您的系统没有该库,则可能需要安装该库或在本地对其进行编译。

免责声明:我没有专门使用cuda的经验,但是与其他需要OS级库才能工作的jar一起工作。