CuMemcpyDtoH gives CUDA_ERROR_INVALID_VALUE

I have a very simple scala jcuda program that adds a very large array. Everything compiles and works very well as long as I don't want to copy more than 4 bytes from my device to the host. I get CUDA_ERROR_INVALID_VALUE when I try to copy more than 4 bytes.

// 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
)

      

For better context of the actual program, here is the kernel code that compiles and runs very simply:

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];
    }
}

      

And then I translated some java example code into my scala code. Anyway, this is the whole program that runs:

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(_))
  }
}

      

Anyway, just so you know the specs of my computer. I am running Ubuntu 14.04 on an optimus setup with a GTX 770M card that is rated for 3.0. I am also running NVCC 5.5 version. Finally, I am running scala version 2.11.6 with Java 8. I am a nob and really appreciate any help.

+3


source to share


1 answer


Here

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

      

you allocate SI

bytes - 4 bytes as the size of one int. Writing more than 4 bytes to that device pointer will mess things up. It should be

cuMemAlloc(deviceOutput, SI * numElements)

      



And in a similar way, I think this call should be

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

      

(note * SI

for the last parameter).

+3


source







All Articles