3

非常に大きな配列を追加する非常に単純な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 カードを使用したオプティマス セットアップで Ubuntu 14.04 を実行しています。NVCC バージョン 5.5 も実行しています。最後に、Java 8 でバージョン 2.11.6 の scala を実行しています。

4

1 に答える 1

3

ここ

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

SI1 つの int のサイズとして 4 バイトのバイトを割り当てています。このデバイス ポインタに 4 バイト以上を書き込むと、問題が発生します。そのはず

cuMemAlloc(deviceOutput, SI * numElements)

同様に、問題の呼び出しは

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

* SI(最後のパラメーターの に注意してください)。

于 2015-06-12T08:09:19.593 に答える