Exception in thread "main" java.lang.NoClassDefFoundError: jcuda/driver/JCudaDriver

Exception in thread "main" java.lang.NoClassDefFoundError: jcuda/driver/JCudaDriver

我正在尝试设置 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)

看来你需要告诉JRE 到哪里去找OS cuda 对应的OS 库。如果您查看输出,它表明找不到 libcuda.so.1.

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

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

免责声明:我没有专门使用 cuda 的经验,但使用过其他需要 OS 级库才能运行的 jar。