如何在JCuda中创建本机指针结构

时间:2015-08-13 16:32:08

标签: cuda jcuda

我有一个带有结构列表的CUDA内核。

kernel<<<blockCount,blockSize>>>(MyStruct *structs);

每个结构包含3个指针。

typedef struct __align(16)__ {
    float* pointer1;
    float* pointer2;
    float* pointer3;
}

我有三个包含浮点数的设备数组,结构中的每个指针都指向三个设备数组中的一个浮点数。

结构列表表示树/图结构,它允许内核执行递归操作,具体取决于发送到内核的结构列表的顺序。 (这个位在C ++中工作,因此与我的问题无关)

我想做的是能够从JCuda发送我的指针结构。我知道除非像this post那样将其展平为填充数组,否则这本身不可能。

我理解在发送结构列表时可能发生的对齐和填充的所有问题,它本质上是一个重复的填充数组,我很好。

我不知道该怎么做,是用指针填充我的扁平结构缓冲区,例如,我想我可以做这样的事情:

Pointer A = ....(underlying device array1)
Pointer B = ....(underlying device array2)
Pointer C = ....(underlying device array3)

ByteBuffer structListBuffer = ByteBuffer.allocate(16*noSteps);
for(int x = 0; x<noSteps; x++) {
    // Get the underlying pointer values
    long pointer1 = A.withByteOffset(getStepOffsetA(x)).someGetUnderlyingPointerValueFunction();
    long pointer2 = B.withByteOffset(getStepOffsetB(x)).someGetUnderlyingPointerValueFunction();
    long pointer3 = C.withByteOffset(getStepOffsetC(x)).someGetUnderlyingPointerValueFunction();

    // Build the struct
    structListBuffer.asLongBuffer().append(pointer1);
    structListBuffer.asLongBuffer().append(pointer2);
    structListBuffer.asLongBuffer().append(pointer3);
    structListBuffer.asLongBuffer().append(0); //padding
}
然后

structListBuffer将包含内核预期方式的结构列表。

那么有没有办法从ByteBuffer中做someGetUnderlyingPointerValueFunction()

1 个答案:

答案 0 :(得分:2)

如果我理解正确,那么问题的关键在于是否存在像

这样的神奇功能
long address = pointer.someGetUnderlyingPointerValueFunction();

返回本机指针的地址。

答案简短:不,没有这样的功能。

(附注:很久以前就已经请求了类似的功能,但是我还没有添加它。主要是因为这样的函数对指向Java数组或(非直接)的指针没有意义此外,在32位和64位计算机上手动处理带有填充和对齐的结构,以及具有不同大小的指针,以及大端或小端的缓冲区是令人头疼的问题。但我明白了这一点,并且可能的应用案例,所以我很可能会添加类似getAddress()函数的东西。也许只有CUdeviceptr类,它绝对有意义 - 至少比{{1}更多人们使用这种方法来做奇怪的事情,他们做一些会导致VM崩溃的事情,但JCuda本身就是如此薄的抽象无论如何,在这方面没有安全网的层......)

也就是说,您可以通过以下方法解决当前的限制:

Pointer

当然,这很丑陋,显然与制作private static long getPointerAddress(CUdeviceptr p) { // WORKAROUND until a method like CUdeviceptr#getAddress exists class PointerWithAddress extends Pointer { PointerWithAddress(Pointer other) { super(other); } long getAddress() { return getNativePointer() + getByteOffset(); } } return new PointerWithAddress(p).getAddress(); } getNativePointer()方法getByteOffset()的意图相矛盾。但它可能最终会被一些官员&#34;方法:

protected

直到现在,这可能是最接近你在C方面做的解决方案。

以下是我为测试而编写的示例。内核只是一个虚拟内核,用#34;可识别的内容填充结构。值(看看它们是否最终在正确的位置),并且应该仅使用1个线程启动:

private static long getPointerAddress(CUdeviceptr p)
{
    return p.getAddress();
}

此内核在以下程序中启动(注意: PTX文件的编译在这里动态完成,其设置可能与您的应用程序案例不符。有疑问,您可以编译您的PTX文件手动)。

初始化每个结构的typedef struct __declspec(align(16)) { float* pointer1; float* pointer2; float* pointer3; } MyStruct; extern "C" __global__ void kernel(MyStruct *structs) { structs[0].pointer1[0] = 1.0f; structs[0].pointer1[1] = 1.1f; structs[0].pointer1[2] = 1.2f; structs[0].pointer2[0] = 2.0f; structs[0].pointer2[1] = 2.1f; structs[0].pointer2[2] = 2.2f; structs[0].pointer3[0] = 3.0f; structs[0].pointer3[1] = 3.1f; structs[0].pointer3[2] = 3.2f; structs[1].pointer1[0] = 11.0f; structs[1].pointer1[1] = 11.1f; structs[1].pointer1[2] = 11.2f; structs[1].pointer2[0] = 12.0f; structs[1].pointer2[1] = 12.1f; structs[1].pointer2[2] = 12.2f; structs[1].pointer3[0] = 13.0f; structs[1].pointer3[1] = 13.1f; structs[1].pointer3[2] = 13.2f; } pointer1pointer2指针,使它们指向设备缓冲区的pointer3AB,每个都有一个偏移量,允许识别内核写入的值。 (注意,我试图在32位或64位机器上处理这两种可能的情况,这意味着不同的指针大小 - 尽管,目前我只能测试32位版本)

C

结果如预期/期望:

import static jcuda.driver.JCudaDriver.*;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.util.Arrays;

import jcuda.Pointer;
import jcuda.Sizeof;
import jcuda.driver.CUcontext;
import jcuda.driver.CUdevice;
import jcuda.driver.CUdeviceptr;
import jcuda.driver.CUfunction;
import jcuda.driver.CUmodule;
import jcuda.driver.JCudaDriver;


public class JCudaPointersInStruct 
{
    public static void main(String args[]) throws IOException
    {
        JCudaDriver.setExceptionsEnabled(true);
        String ptxFileName = preparePtxFile("JCudaPointersInStructKernel.cu");
        cuInit(0);
        CUdevice device = new CUdevice();
        cuDeviceGet(device, 0);
        CUcontext context = new CUcontext();
        cuCtxCreate(context, 0, device);
        CUmodule module = new CUmodule();
        cuModuleLoad(module, ptxFileName);
        CUfunction function = new CUfunction();
        cuModuleGetFunction(function, module, "kernel");

        int numElements = 9;
        CUdeviceptr A = new CUdeviceptr();
        cuMemAlloc(A, numElements * Sizeof.FLOAT);
        cuMemsetD32(A, 0, numElements);
        CUdeviceptr B = new CUdeviceptr();
        cuMemAlloc(B, numElements * Sizeof.FLOAT);
        cuMemsetD32(B, 0, numElements);
        CUdeviceptr C = new CUdeviceptr();
        cuMemAlloc(C, numElements * Sizeof.FLOAT);
        cuMemsetD32(C, 0, numElements);

        int numSteps = 2;
        int sizeOfStruct = Sizeof.POINTER * 4;
        ByteBuffer hostStructsBuffer = 
            ByteBuffer.allocate(numSteps * sizeOfStruct);
        if (Sizeof.POINTER == 4)
        {
            IntBuffer b = hostStructsBuffer.order(
                ByteOrder.nativeOrder()).asIntBuffer();
            for(int x = 0; x<numSteps; x++) 
            {
                CUdeviceptr pointer1 = A.withByteOffset(getStepOffsetA(x));
                CUdeviceptr pointer2 = B.withByteOffset(getStepOffsetB(x));
                CUdeviceptr pointer3 = C.withByteOffset(getStepOffsetC(x));

                //System.out.println("Step "+x+" pointer1 is "+pointer1);
                //System.out.println("Step "+x+" pointer2 is "+pointer2);
                //System.out.println("Step "+x+" pointer3 is "+pointer3);

                b.put((int)getPointerAddress(pointer1));
                b.put((int)getPointerAddress(pointer2));
                b.put((int)getPointerAddress(pointer3));
                b.put(0);
            }
        }
        else
        {
            LongBuffer b = hostStructsBuffer.order(
                ByteOrder.nativeOrder()).asLongBuffer();
            for(int x = 0; x<numSteps; x++) 
            {
                CUdeviceptr pointer1 = A.withByteOffset(getStepOffsetA(x));
                CUdeviceptr pointer2 = B.withByteOffset(getStepOffsetB(x));
                CUdeviceptr pointer3 = C.withByteOffset(getStepOffsetC(x));

                //System.out.println("Step "+x+" pointer1 is "+pointer1);
                //System.out.println("Step "+x+" pointer2 is "+pointer2);
                //System.out.println("Step "+x+" pointer3 is "+pointer3);

                b.put(getPointerAddress(pointer1));
                b.put(getPointerAddress(pointer2));
                b.put(getPointerAddress(pointer3));
                b.put(0);
            }
        }

        CUdeviceptr structs = new CUdeviceptr();
        cuMemAlloc(structs, numSteps * sizeOfStruct);
        cuMemcpyHtoD(structs, Pointer.to(hostStructsBuffer), 
            numSteps * sizeOfStruct);

        Pointer kernelParameters = Pointer.to(
            Pointer.to(structs)
        );
        cuLaunchKernel(function, 
            1, 1, 1, 
            1, 1, 1, 
            0, null, kernelParameters, null);
        cuCtxSynchronize();


        float hostA[] = new float[numElements];
        cuMemcpyDtoH(Pointer.to(hostA), A, numElements * Sizeof.FLOAT);
        float hostB[] = new float[numElements];
        cuMemcpyDtoH(Pointer.to(hostB), B, numElements * Sizeof.FLOAT);
        float hostC[] = new float[numElements];
        cuMemcpyDtoH(Pointer.to(hostC), C, numElements * Sizeof.FLOAT);

        System.out.println("A "+Arrays.toString(hostA));
        System.out.println("B "+Arrays.toString(hostB));
        System.out.println("C "+Arrays.toString(hostC));
    }

    private static long getStepOffsetA(int x)
    {
        return x * Sizeof.FLOAT * 4 + 0 * Sizeof.FLOAT;
    }
    private static long getStepOffsetB(int x)
    {
        return x * Sizeof.FLOAT * 4 + 1 * Sizeof.FLOAT;
    }
    private static long getStepOffsetC(int x)
    {
        return x * Sizeof.FLOAT * 4 + 2 * Sizeof.FLOAT;
    }


    private static long getPointerAddress(CUdeviceptr p)
    {
        // WORKAROUND until a method like CUdeviceptr#getAddress exists
        class PointerWithAddress extends Pointer
        {
            PointerWithAddress(Pointer other)
            {
                super(other);
            }
            long getAddress()
            {
                return getNativePointer() + getByteOffset();
            }
        }
        return new PointerWithAddress(p).getAddress();
    }




    //-------------------------------------------------------------------------
    // Ignore this - in practice, you'll compile the PTX manually
    private static String preparePtxFile(String cuFileName) throws IOException
    {
        int endIndex = cuFileName.lastIndexOf('.');
        if (endIndex == -1)
        {
            endIndex = cuFileName.length()-1;
        }
        String ptxFileName = cuFileName.substring(0, endIndex+1)+"ptx";
        File cuFile = new File(cuFileName);
        if (!cuFile.exists())
        {
            throw new IOException("Input file not found: "+cuFileName);
        }
        String modelString = "-m"+System.getProperty("sun.arch.data.model");
        String command =
            "nvcc " + modelString + " -ptx -arch sm_11 -lineinfo "+
            cuFile.getPath()+" -o "+ptxFileName;
        System.out.println("Executing\n"+command);
        Process process = Runtime.getRuntime().exec(command);
        String errorMessage =
            new String(toByteArray(process.getErrorStream()));
        String outputMessage =
            new String(toByteArray(process.getInputStream()));
        int exitValue = 0;
        try
        {
            exitValue = process.waitFor();
        }
        catch (InterruptedException e)
        {
            Thread.currentThread().interrupt();
            throw new IOException(
                "Interrupted while waiting for nvcc output", e);
        }

        if (exitValue != 0)
        {
            System.out.println("nvcc process exitValue "+exitValue);
            System.out.println("errorMessage:\n"+errorMessage);
            System.out.println("outputMessage:\n"+outputMessage);
            throw new IOException(
                "Could not create .ptx file: "+errorMessage);
        }
        System.out.println("Finished creating PTX file");
        return ptxFileName;
    }
    private static byte[] toByteArray(InputStream inputStream)
        throws IOException
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte buffer[] = new byte[8192];
        while (true)
        {
            int read = inputStream.read(buffer);
            if (read == -1)
            {
                break;
            }
            baos.write(buffer, 0, read);
        }
        return baos.toByteArray();
    }

}