2016-11-07 68 views
0

在JCublas2.cublasSdot的源代碼註釋中,它被評論爲'result'參數可以是'主機或設備指針'。JCuda的JCublas2.cublasSdot:未能使用設備指針作爲結果指針參數

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 
# ..... 

最小化節省了時間。如何使用設備指針作爲此方法的'result'參數,以及其他JCuda方法,結果指針用/ **主機或設備指針** /?註釋。

+0

非常感謝,馬可。我稍後再試。 – Tom

+0

這完全解決了這個問題。 – Tom

回答

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