【问题标题】:JCuda's JCublas2.cublasSdot: failed to use a device Pointer for the result Pointer parameterJCuda 的 JCublas2.cublasSdot: failed to use a device Pointer for the result Pointer 参数
【发布时间】:2016-11-08 23:31:57
【问题描述】:

在JCublas2.cublasSdot源代码的cmets中,注释'result'参数可以是'host or device pointer'。

 public static int cublasSdot(
    cublasHandle handle, 
    int n, 
    Pointer x, 
    int incx, 
    Pointer y, 
    int incy, 
    Pointer result)/** host or device pointer */
{
    return checkResult(cublasSdotNative(handle, n, x, incx, y, incy, result));
}

但是,我只能使用像 Pointer.to(fs) 这样的主机指针和 float[] fs ={0}。如果我使用像 'CUdeviceptr devicePtr = new CUdeviceptr(); 这样的设备指针JCudaDriver.cuMemAlloc(devicePtr, 100 * Sizeof.FLOAT);',程序崩溃,控制台消息如下:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000007fed93af2a3, pid=9376, tid=0x0000000000003a7c
# .....

主机和设备之间的数据传输最小化可以节省时间。如何使用设备指针作为此方法的“结果”参数,以及其他 JCuda 方法的结果指针用 /** 主机或设备指针 **/ 注释?

【问题讨论】:

  • 非常感谢,马可。我稍后再试试。
  • 这正好解决了问题。

标签: jcuda


【解决方案1】:

CUBLAS 可以将某些计算的结果(如点积)写入主机设备内存。必须使用cublasSetPointerMode 显式设置目标内存类型。

JCublas2PointerModes 示例中显示了如何使用它的示例。

它曾经将点积计算的结果写入主机内存(这也是默认值,当没有显式设置指针模式时):

// Set the pointer mode to HOST
cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_HOST);

// Prepare the pointer for the result in HOST memory
float hostResult[] = { -1.0f };
Pointer hostResultPointer = Pointer.to(hostResult);

// Execute the 'dot' function
cublasSdot(handle, n, deviceData, 1, deviceData, 1, hostResultPointer);

然后改变指针模式,再次调用函数,这次将结果写入设备内存:

cublasSetPointerMode(handle, CUBLAS_POINTER_MODE_DEVICE);

// Prepare the pointer for the result in DEVICE memory
Pointer deviceResultPointer = new Pointer();
cudaMalloc(deviceResultPointer, Sizeof.FLOAT);

// Execute the 'dot' function
cublasSdot(handle, n, deviceData, 1, deviceData, 1, deviceResultPointer);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-27
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-27
    • 2021-10-01
    相关资源
    最近更新 更多