java - 线程 "main"java.lang.NoClassDefFoundError : jcuda/driver/JCudaDriver 中出现异常

标签 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)

最佳答案

看来你需要告诉JRE去哪里找到相应的操作系统库来运行cuda。如果您查看输出,就会发现找不到 libcuda.so.1 .

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

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

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

关于java - 线程 "main"java.lang.NoClassDefFoundError : jcuda/driver/JCudaDriver 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55343946/

相关文章:

java - jar:在 Bash 中找不到命令

cuda - 我如何告诉 PyCUDA 使用哪个 GPU?

c++ - 将复杂数据从主机传输到设备的简单 CUDA 代码问题

java - 如何在 FXControls 中垂直对齐 StatusBar 的子项?

Android Proguard 跳过外部 jar

java - Liferay renderURLParams

java - java如何在Mac OS上检查“jar launcher.app”正在使用的版本

c++ - 使用 CUDA 和 Maya API 时命名空间发生冲突

java - IKVM.NET 和 Excel VBA

java - 如何使用 ArchUnit 检查是否在正确的类中调用了构造函数?