jCUDA中cuMemcpyHtoD函数出错

时间:2014-08-11 07:16:36

标签: cuda jcuda

我是java编程的新手,并尝试在jCUDA中编写矩阵乘法程序。

在将数据从主机传输到设备时,反之亦然,我使用:

cuMemcpyHtoD(devMatrixA, Pointer.to(hostMatrixA), numRows * numCols * Sizeof.FLOAT);
cuMemcpyHtoD(devMatrixB, Pointer.to(hostMatrixA), numRows * numCols * Sizeof.FLOAT);
cuMemcpyDtoH(Pointer.to(hostMatrixC), devMatrixC, numRows * numCols * Sizeof.FLOAT);

这里,devMatrixA,devMatrixB和devMatrixC是存储在设备存储器中的矩阵。 hostMatrixA,hostMatrixB和hostMatrixC是存储在主机内存中的矩阵。

当我调用上述函数进行数据传输时,它会给出以下错误'指针类型中的(byte [])方法不适用于参数(float [] [])'和'to'in' Pointer.to('红色下划线。我正在使用eclipse。我已经提供了完整的代码如下。

请原谅我的java知识,如果我走向错误的方向,请提出建议。

Package JCudaMatrixAddition;
import static jcuda.driver.JCudaDriver.*;

import java.io.*;

import jcuda.*;
import jcuda.driver.*;
import jcuda.Pointer;
import jcuda.Sizeof;


public class JCudaMatrixAddition {
    public static void main(String[] args) throws IOException 
    {
        // Enable exceptions and omit all subsequent error checks
        JCudaDriver.setExceptionsEnabled(true);

        // Create the PTX file by calling the NVCC
        String ptxFilename = preparePtxFile("JCudaMatrixAdditionKernel.cu");

        //Initialize the driver and create a context for the first device.
        cuInit(0);
        CUdevice device = new CUdevice();
        cuDeviceGet (device, 0);
        CUcontext context = new CUcontext();
        cuCtxCreate(context, 0, device);

        //Load PTX file
        CUmodule module = new CUmodule();
        cuModuleLoad(module,ptxFilename);

        //Obtain a function pointer to the Add function
        CUfunction function = new CUfunction();
        cuModuleGetFunction(function, module, "add");

        int numRows = 32;
        int numCols = 32;

        //Allocate and fill Host input Matrices:
        float hostMatrixA[][] = new float[numRows][numCols];
        float hostMatrixB[][] = new float[numRows][numCols];
        float hostMatrixC[][] = new float[numRows][numCols];


        for(int i = 0; i<numRows; i++)

        {
            for(int j = 0; j<numCols; j++)
            {
                hostMatrixA[i][j] = (float) 1.0;
                hostMatrixB[i][j] = (float) 1.0;
            }
        }
        // Allocate the device input data, and copy the
        // host input data to the device
        CUdeviceptr devMatrixA = new CUdeviceptr();
        cuMemAlloc(devMatrixA, numRows * numCols * Sizeof.FLOAT);

        //This is the part where it gives me the error
        cuMemcpyHtoD(devMatrixA, Pointer.to(hostMatrixA), numRows * numCols * Sizeof.FLOAT);

        CUdeviceptr devMatrixB = new CUdeviceptr();
        cuMemAlloc(devMatrixB, numRows * numCols * Sizeof.FLOAT);

        //This is the part where it gives me the error
        cuMemcpyHtoD(devMatrixB, Pointer.to(hostMatrixA), numRows * numCols * Sizeof.FLOAT);

        //Allocate device matrix C to store output
        CUdeviceptr devMatrixC = new CUdeviceptr();
        cuMemAlloc(devMatrixC, numRows * numCols * Sizeof.FLOAT);

        // Set up the kernel parameters: A pointer to an array
        // of pointers which point to the actual values.

        Pointer kernelParameters = Pointer.to(Pointer.to(new int[]{numRows}),
                                   Pointer.to(new int[]{numRows}), 
                                   Pointer.to(devMatrixA),
                                   Pointer.to(devMatrixB),
                                   Pointer.to(devMatrixC));

        //Kernel thread configuration
        int blockSize = 32;
        int gridSize = 1;

        cuLaunchKernel(function, 
                       gridSize, 1, 1,
                       blockSize, 32, 1,
                       0, null, kernelParameters, null);

        cuCtxSynchronize();
        // Allocate host output memory and copy the device output
        // to the host.

        //This is the part where it gives me the error
        cuMemcpyDtoH(Pointer.to(hostMatrixC), devMatrixC, numRows * numCols * Sizeof.FLOAT);

        //verify the result
        for (int i =0; i<numRows; i++)
        {
            for (int j =0; j<numRows; j++)
            {
                System.out.print("   "+ hostMatrixB[i][j]);
            }
            System.out.println("");
        }
        cuMemFree(devMatrixA);
        cuMemFree(devMatrixB);
        cuMemFree(devMatrixC);

    }

1 个答案:

答案 0 :(得分:2)

您无法直接将float[][]阵列从主机复制到设备。

创建float[][]数组时,这不是一大堆float值。相反,它是数组。想象一下,您甚至可以创建一个像

这样的数组
float array[][] = new float[3];
array[0] = new float[42];
array[1] = null;
array[2] = new float[1234];

这不是一个连续的内存块,因此,这样的数组无法复制到设备上。

当处理CUDA中的矩阵时(不仅在JCuda中,而且在CUDA中),它们通常表示为一维数组。所以在这种情况下,您可以将矩阵声明为

float hostMatrixA[] = new float[numRows*numCols];

要访问矩阵元素,您必须计算适当的索引:

int row = ...;
int col = ...;
hostMatrix[col+row*numCols] = 123.0f; // Column-major

// Or
hostMatrix[row+col*numRows] = 123.0f; // Row-major

最后两行之间的区别在于,一个假定为列主要顺序,另一个假定为行主顺序。有关详细信息,请参阅Wikipedia site about row-major order

一些旁注:

像CUBLAS这样的CUDA矩阵库使用列主要排序,因此遵循相同的约定可能是个好主意。特别是当您以后想要使用CUBLAS / JCublas函数时。例如,cublasSgeam函数已经提供了执行矩阵加法的功能。

想要添加矩阵时,使用CUDA / JCuda时不会看到加速。我在this answer中写了一篇关于此的摘要。

而BTW:从技术上讲,可以使用“2D阵列”。 JCudaDriverSample显示了如何做到这一点。但它非常不方便,推荐用于矩阵运算。

相关问题