gpt4 book ai didi

java - cuMemcpyDtoH 产生 CUDA_ERROR_INVALID_VALUE

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:10 36 4
gpt4 key购买 nike

我有一个非常简单的 scala jcuda 程序,它添加了一个非常大的数组。一切都编译和运行得很好,直到我想从我的设备复制超过 4 个字节到主机。当我尝试复制超过 4 个字节时,我收到 CUDA_ERROR_INVALID_VALUE。

// This does pukes and gives CUDA_ERROR_INVALID_VALUE
var hostOutput = new Array[Int](numElements)
cuMemcpyDtoH(
Pointer.to(hostOutput),
deviceOutput,
8
)

// This runs just fine
var hostOutput = new Array[Int](numElements)
cuMemcpyDtoH(
Pointer.to(hostOutput),
deviceOutput,
4
)

为了更好地了解实际程序,下面是我的内核代码,它编译和运行得很好:

extern "C"
__global__ void add(int n, int *a, int *b, int *sum) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i<n)
{
sum[i] = a[i] + b[i];
}
}

然后我将一些 java 示例代码翻译成我的 scala 代码。无论如何,下面是运行的整个程序:

package dev

import jcuda.driver.JCudaDriver._

import jcuda._
import jcuda.driver._
import jcuda.runtime._

/**
* Created by dev on 6/7/15.
*/
object TestCuda {
def init = {
JCudaDriver.setExceptionsEnabled(true)

// Input vector

// Output vector

// Load module
// Load the ptx file.

val kernelPath = "/home/dev/IdeaProjects/jniopencl/src/main/resources/kernels/JCudaVectorAddKernel30.cubin"

cuInit(0)

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

// Create and load module
val module = new CUmodule()
cuModuleLoad(module, kernelPath)

// Obtain a function pointer to the kernel function.
var add = new CUfunction()
cuModuleGetFunction(add, module, "add")

val numElements = 100000

val hostInputA = 1 to numElements toArray
val hostInputB = 1 to numElements toArray
val SI: Int = Sizeof.INT.asInstanceOf[Int]

// Allocate the device input data, and copy
// the host input data to the device
var deviceInputA = new CUdeviceptr
cuMemAlloc(deviceInputA, numElements * SI)
cuMemcpyHtoD(
deviceInputA,
Pointer.to(hostInputA),
numElements * SI
)

var deviceInputB = new CUdeviceptr
cuMemAlloc(deviceInputB, numElements * SI)
cuMemcpyHtoD(
deviceInputB,
Pointer.to(hostInputB),
numElements * SI
)

// Allocate device output memory
val deviceOutput = new CUdeviceptr()
cuMemAlloc(deviceOutput, SI)

// Set up the kernel parameters: A pointer to an array
// of pointers which point to the actual values.
val kernelParameters = Pointer.to(
Pointer.to(Array[Int](numElements)),
Pointer.to(deviceInputA),
Pointer.to(deviceInputB),
Pointer.to(deviceOutput)
)

// Call the kernel function
val blockSizeX = 256
val gridSizeX = Math.ceil(numElements / blockSizeX).asInstanceOf[Int]
cuLaunchKernel(
add,
gridSizeX, 1, 1,
blockSizeX, 1, 1,
0, null,
kernelParameters, null
)

cuCtxSynchronize

// **** Code pukes here with that error
// If I comment this out the program runs fine
var hostOutput = new Array[Int](numElements)
cuMemcpyDtoH(
Pointer.to(hostOutput),
deviceOutput,
numElements
)

hostOutput.foreach(print(_))
}
}

无论如何,只是让你知道我电脑的规范。我在带有支持计算 3.0 的 GTX 770M 卡的 optimus 设置上运行 Ubuntu 14.04。我还在运行 NVCC 5.5 版。最后,我正在使用 Java 8 运行 scala 2.11.6 版。我是菜鸟,非常感谢任何帮助。

最佳答案

这里

val deviceOutput = new CUdeviceptr()
cuMemAlloc(deviceOutput, SI)

您正在分配 SI 字节 - 即 4 个字节,作为一个 int 的大小。向该设备指针写入超过 4 个字节会使事情变得困惑。应该是

cuMemAlloc(deviceOutput, SI * numElements)

同样,我认为有问题的电话应该是

cuMemcpyDtoH(
Pointer.to(hostOutput),
deviceOutput,
numElements * SI
)

(注意最后一个参数的 * SI)。

关于java - cuMemcpyDtoH 产生 CUDA_ERROR_INVALID_VALUE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30797257/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com