JCuda中的JIT,加载多个ptx模块

时间:2015-09-12 06:17:53

标签: cuda jit jcuda

我在this问题中说我在JCuda中加载ptx模块时遇到了一些问题,在@ talonmies的想法之后,我实现了他的解决方案的JCuda版本来加载多个ptx文件并将它们作为单个模块加载。以下是代码的相关部分:

import static jcuda.driver.JCudaDriver.cuLinkAddFile;
import static jcuda.driver.JCudaDriver.cuLinkComplete;
import static jcuda.driver.JCudaDriver.cuLinkCreate;
import static jcuda.driver.JCudaDriver.cuLinkDestroy;
import static jcuda.driver.JCudaDriver.cuModuleGetFunction;
import static jcuda.driver.JCudaDriver.cuModuleLoadData;

import jcuda.driver.CUjitInputType;
import jcuda.driver.JITOptions;
import jcuda.driver.CUlinkState;
import jcuda.driver.CUfunction;

public class JCudaTestJIT{

    private CUmodule module;
    private CUfunction functionKernel;

    public void prepareModule(){
        String ptxFileName4 = "file4.ptx";
        String ptxFileName3 = "file3.ptx";
        String ptxFileName2 = "file2.ptx";
        String ptxFileName1 = "file1.ptx";

        CUlinkState linkState = new CUlinkState();
        JITOptions jitOptions = new JITOptions();
        cuLinkCreate(jitOptions, linkState);

        cuLinkAddFile(linkState, CUjitInputType.CU_JIT_INPUT_PTX, ptxFileName4, jitOptions);
        cuLinkAddFile(linkState, CUjitInputType.CU_JIT_INPUT_PTX, ptxFileName3, jitOptions);
        cuLinkAddFile(linkState, CUjitInputType.CU_JIT_INPUT_PTX, ptxFileName2, jitOptions);
        cuLinkAddFile(linkState, CUjitInputType.CU_JIT_INPUT_PTX, ptxFileName1, jitOptions);

        long sizeOut = 32768;
        byte[] image = new byte[32768];

        Pointer cubinOut = Pointer.to(image);

        cuLinkComplete(linkState, cubinOut, (new long[]{sizeOut}));

        module = new CUmodule();

        // Load the module from the image buffer
        cuModuleLoadData(module, cubinOut.getByteBuffer(0, 32768).array());

        cuLinkDestroy(linkState);

        functionKernel = new CUfunction();
        cuModuleGetFunction(functionKernel, module, "kernel");
    }

    // Other methods 
}

但是我在调​​用CUDA_ERROR_INVALID_IMAGE方法时遇到cuModuleLoadData错误。在调试时,我看到在调用cuLinkComplete方法并将 image 数组作为输出后,数组仍然保持不变且清晰。我正确传递输出参数吗?这是如何在JCuda中通过引用传递变量的吗?

1 个答案:

答案 0 :(得分:4)

我从来没有写过一行Java代码,直到30分钟前,更不用说之前使用过JCUDA了,但是我给你here的本机C ++代码的几乎字面的逐行翻译似乎工作正常完美:

import static jcuda.driver.JCudaDriver.*;
import java.io.*;
import jcuda.*;
import jcuda.driver.*;

public class JCudaRuntimeTest
{
    public static void main(String args[])
    {
        JCudaDriver.setExceptionsEnabled(true);

        cuInit(0);
        CUdevice device = new CUdevice();
        cuDeviceGet(device, 0);
        CUcontext context = new CUcontext();
        cuCtxCreate(context, 0, device);

        CUlinkState linkState = new CUlinkState();
        JITOptions jitOptions = new JITOptions();
        cuLinkCreate(jitOptions, linkState);

        String ptxFileName2 = "test_function.ptx";
        String ptxFileName1 = "test_kernel.ptx";

        cuLinkAddFile(linkState, CUjitInputType.CU_JIT_INPUT_PTX, ptxFileName2, jitOptions);
        cuLinkAddFile(linkState, CUjitInputType.CU_JIT_INPUT_PTX, ptxFileName1, jitOptions);

        long sz[] = new long[1];
        Pointer image = new Pointer();
        cuLinkComplete(linkState, image, sz);
        System.out.println("Pointer: " + image);
        System.out.println("CUBIN size: " + sz[0]);

        CUmodule module = new CUmodule();
        cuModuleLoadDataEx(module, image, 0, new int[0], Pointer.to(new int[0]));   
        cuLinkDestroy(linkState);

        CUfunction functionKernel = new CUfunction();
        String kernelname = "_Z6kernelPfS_S_S_";
        cuModuleGetFunction(functionKernel, module, kernelname);
        System.out.println("Function: " + functionKernel);
    }
}

的工作原理如下:

> nvcc -ptx -arch=sm_21 test_function.cu
test_function.cu

> nvcc -ptx -arch=sm_21 test_kernel.cu
test_kernel.cu

> javac -cp ".;jcuda-0.7.0a.jar" JCudaRuntimeTest.java
> java -cp ".;jcuda-0.7.0a.jar" JCudaRuntimeTest
Pointer: Pointer[nativePointer=0xa5a13a8,byteOffset=0]
CUBIN size: 5924
Function: CUfunction[nativePointer=0xa588160]

这里的关键似乎是使用cuModuleLoadDataEx,注意cuLinkComplete的返回值是指向链接的CUBIN的系统指针,以及作为long[]返回的图像的大小。根据C ++代码,指针只是直接传递给模块数据加载。

作为最后的评论,如果你发布了一个可以被直接攻击的适当的repro案例,而不是让我在创建一个有用的repro之前学习JCUDA和Java的雏形,那将会更加简单和容易。案例并让它工作。 JCUDA的文档是基本的,但是完整的,并且针对已经提供的工作C ++示例,只需要几分钟的阅读就可以看到如何执行此操作。