当前位置: 首页 > 知识库问答 >
问题:

用Java运行CUDA代码最简单的方法是什么?

毕黎昕
2023-03-14

最好的方法是什么?JCUDA是一个完美的映射到C CUDA还是完全不同?或者从Java调用C代码并共享结果是否有意义(链接列表是否可以访问)?

共有1个答案

微生季
2023-03-14

IMO?JavaCPP.例如,下面是一个到Java的端口,该端口显示在Strust的Web站点的主页上:

import com.googlecode.javacpp.*;
import com.googlecode.javacpp.annotation.*;

@Platform(include={"<thrust/host_vector.h>", "<thrust/device_vector.h>", "<thrust/generate.h>", "<thrust/sort.h>",
                   "<thrust/copy.h>", "<thrust/reduce.h>", "<thrust/functional.h>", "<algorithm>", "<cstdlib>"})
@Namespace("thrust")
public class ThrustTest {
    static { Loader.load(); }

    public static class IntGenerator extends FunctionPointer {
        static { Loader.load(); }
        protected IntGenerator() { allocate(); }
        private native void allocate();
        public native int call();
    }

    @Name("plus<int>")
    public static class IntPlus extends Pointer {
        static { Loader.load(); }
        public IntPlus() { allocate(); }
        private native void allocate();
        public native @Name("operator()") int call(int x, int y);
    }

    @Name("host_vector<int>")
    public static class IntHostVector extends Pointer {
        static { Loader.load(); }
        public IntHostVector() { allocate(0); }
        public IntHostVector(long n) { allocate(n); }
        public IntHostVector(IntDeviceVector v) { allocate(v); }
        private native void allocate(long n);
        private native void allocate(@ByRef IntDeviceVector v);

        public IntPointer begin() { return data(); }
        public IntPointer end() { return data().position((int)size()); }

        public native IntPointer data();
        public native long size();
        public native void resize(long n);
    }

    @Name("device_ptr<int>")
    public static class IntDevicePointer extends Pointer {
        static { Loader.load(); }
        public IntDevicePointer() { allocate(null); }
        public IntDevicePointer(IntPointer ptr) { allocate(ptr); }
        private native void allocate(IntPointer ptr);

        public native IntPointer get();
    }

    @Name("device_vector<int>")
    public static class IntDeviceVector extends Pointer {
        static { Loader.load(); }
        public IntDeviceVector() { allocate(0); }
        public IntDeviceVector(long n) { allocate(n); }
        public IntDeviceVector(IntHostVector v) { allocate(v); }
        private native void allocate(long n);
        private native void allocate(@ByRef IntHostVector v);

        public IntDevicePointer begin() { return data(); }
        public IntDevicePointer end() { return new IntDevicePointer(data().get().position((int)size())); }

        public native @ByVal IntDevicePointer data();
        public native long size();
        public native void resize(long n);
    }

    public static native @MemberGetter @Namespace IntGenerator rand();
    public static native void copy(@ByVal IntDevicePointer first, @ByVal IntDevicePointer last, IntPointer result);
    public static native void generate(IntPointer first, IntPointer last, IntGenerator gen);
    public static native void sort(@ByVal IntDevicePointer first, @ByVal IntDevicePointer last);
    public static native int reduce(@ByVal IntDevicePointer first, @ByVal IntDevicePointer last, int init, @ByVal IntPlus binary_op);

    public static void main(String[] args) {
        // generate 32M random numbers serially
        IntHostVector h_vec = new IntHostVector(32 << 20);
        generate(h_vec.begin(), h_vec.end(), rand());

        // transfer data to the device
        IntDeviceVector d_vec = new IntDeviceVector(h_vec);

        // sort data on the device (846M keys per second on GeForce GTX 480)
        sort(d_vec.begin(), d_vec.end());

        // transfer data back to host
        copy(d_vec.begin(), d_vec.end(), h_vec.begin());

        // compute sum on device
        int x = reduce(d_vec.begin(), d_vec.end(), 0, new IntPlus());
    }
}

不过,C语言的代码应该更容易映射。

我们可以使用以下命令在Linux x86_64上编译并运行该命令,或者通过适当修改-properties选项在其他受支持的平台上运行该命令:

$ javac -cp javacpp.jar ThrustTest.java
$ java -jar javacpp.jar ThrustTest -properties linux-x86_64-cuda
$ java  -cp javacpp.jar ThrustTest
 类似资料:
  • 我有一些使用下面某个模块的网页抓取Python代码 硒 bs4 MySQLdb 调度器 在网上或云上运行代码的最简单方法或平台是什么?

  • 问题内容: 我找到了要在Java应用程序中使用的开源库。该库是用C编写的,并且是在Unix / Linux下开发的,我的应用程序将在Windows上运行。它是一个主要包含数学函数的库,据我所知,它不使用任何依赖于平台的内容,它只是非常基本的C代码。而且,它也不大,少于5,000行。 在我的应用程序中使用库的最简单方法是什么?我知道这里有JNI,但这涉及到找到一个编译器来在Windows下编译该库,

  • 问题内容: 如何简单地从本地Python(3.0)脚本SSH到远程服务器,提供登录名/密码,执行命令并将输出打印到Python控制台? 我宁愿不使用任何大型外部库或在远程服务器上安装任何东西。 问题答案: 我没有尝试过,但是这个pysftp模块可能会有所帮助,而后者又使用了paramiko。我相信一切都在客户端。 有趣的命令可能是在远程计算机上执行任意命令。(该模块的功能和方法也更多地暗示了它的F

  • 问题内容: 在Java中,数组不会覆盖toString(),因此,如果您尝试直接打印一个数组,则得到数组的十六进制,如下所示: 但是通常情况下,我们实际上会想要更多类似的东西。最简单的方法是什么?以下是一些示例输入和输出: 问题答案: 从Java 5开始,您可以;然后将或用于数组中的数组。请注意,版本调用数组中的每个对象。输出甚至以您要求的确切方式修饰。 例子: 简单数组: 输出: 嵌套数组: 输

  • 问题内容: 说我有一个类似的任务: 并行化每个compute()的最简单方法是什么(假设它们已经可以并行化了)? 我不需要严格匹配上面代码的答案,而只是一个常规答案。但是,如果您需要更多信息:我的任务是IO绑定的,这是针对Spring Web应用程序的,这些任务将在HTTP请求中执行。 问题答案: 我建议看一下ExecutorService。 特别是这样的事情: 请注意,如果列表很大,使用可能会很