![](/img/trans.png)
[英]Can't understand why I am getting tuple error even though I typed everything correctly
[英]Why am I getting 'cuMemAlloc failed: not initialized' even though I am initializing correctly?
我的Django / Celery / PyCuda設置出現問題。 我正在使用PyCuda在Amazon EC2 G2實例上進行某些圖像處理。 這是我的支持Cuda的GRID K520卡上的信息:檢測到1個支持CUDA的設備
Device 0: "GRID K520"
CUDA Driver Version / Runtime Version 6.0 / 6.0
CUDA Capability Major/Minor version number: 3.0
Total amount of global memory: 4096 MBytes (4294770688 bytes)
( 8) Multiprocessors, (192) CUDA Cores/MP: 1536 CUDA Cores
GPU Clock rate: 797 MHz (0.80 GHz)
Memory Clock rate: 2500 Mhz
Memory Bus Width: 256-bit
L2 Cache Size: 524288 bytes
Maximum Texture Dimension Size (x,y,z) 1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
Maximum Layered 1D Texture Size, (num) layers 1D=(16384), 2048 layers
Maximum Layered 2D Texture Size, (num) layers 2D=(16384, 16384), 2048 layers
Total amount of constant memory: 65536 bytes
Total amount of shared memory per block: 49152 bytes
Total number of registers available per block: 65536
Warp size: 32
Maximum number of threads per multiprocessor: 2048
Maximum number of threads per block: 1024
Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535)
Maximum memory pitch: 2147483647 bytes
Texture alignment: 512 bytes
Concurrent copy and kernel execution: Yes with 2 copy engine(s)
Run time limit on kernels: No
Integrated GPU sharing Host Memory: No
Support host page-locked memory mapping: Yes
Alignment requirement for Surfaces: Yes
Device has ECC support: Disabled
Device supports Unified Addressing (UVA): Yes
Device PCI Bus ID / PCI location ID: 0 / 3
Compute Mode:
< Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 6.0, CUDA Runtime Version = 6.0, NumDevs = 1, Device0 = GRID K520
Result = PASS
我正在使用一個非常好的現成的celery配置。 我在utils / tasks.py中定義了一組任務,這些任務在嘗試使用PyCuda之前已經過測試並可以工作。 我通過pip安裝了PyCuda。
在遇到問題的文件頂部,執行標准導入:
from celery import task
# other imports
import os
try:
import Image
except Exception:
from PIL import Image
import time
#Cuda imports
import pycuda.autoinit
import pycuda.driver as cuda
from pycuda.compiler import SourceModule
import numpy
遠程服務器將啟動一個任務,該任務遵循以下基本工作流程:
@task()
def photo_function(photo_id,...):
print 'Got photo...'
... Do some stuff ...
result = do_photo_manipulation(photo_id)
return result
def do_photo_manipulation(photo_id):
im = Image.open(inPath)
px = numpy.array(im)
px = px.astype(numpy.float32)
d_px = cuda.mem_alloc(px.nbytes)
... (Do stuff with the pixel array) ...
return new_image
如果我在shell plus(即./manage.py shell_plus)中運行它,並且如果我將其作為獨立的,Django和芹菜之外的進程運行,則此方法有效。 僅在這種情況下它會失敗,並顯示以下錯誤:cuMemAlloc失敗:未初始化
我看了一段時間其他解決方案,並嘗試將import語句放入函數本身中進行初始化。 我還插入了一個wait()語句,以確保gpu准備工作不是問題。
這是一個提示錯誤的答案,它是由於我沒有導入pycuda.autoinit而導致的: http ://comments.gmane.org/gmane.comp.python.cuda/1975
這里的任何幫助將不勝感激!
如果我需要提供更多信息,請告訴我!
編輯:這是測試代碼:def CudaImageShift(imageIn,mode =“ luminsity”,log = 0):
if log == 1 :
print ("----------> CUDA CONVERSION")
# print "ENVIRON: "
# import os
# print os.environ
print 'AUTOINIT'
print pycuda.autoinit
print 'Making context...'
context = make_default_context()
print 'Context created.'
totalT0 = time.time()
print 'Doing test run...'
a = numpy.random.randn(4,4)
a = a.astype(numpy.float32)
print 'Test mem alloc'
a_gpu = cuda.mem_alloc(a.nbytes)
print 'MemAlloc complete, test mem copy'
cuda.memcpy_htod(a_gpu, a)
print 'memcopy complete'
[2014-07-15 14:52:20,469: WARNING/Worker-1] cuDeviceGetCount failed: not initialized
我相信您遇到的問題與CUDA上下文有關。 從CUDA 4.0開始,每個進程和每個設備都需要CUDA上下文。
在后台,芹菜將為任務工作者生成進程。 當進程/任務啟動時,它將沒有可用的上下文。 在pyCUDA中,上下文創建在autoinit模塊中進行。 這就是為什么如果您以獨立方式運行代碼(不創建任何額外的過程且上下文有效)或將import autoinit
放入CUDA任務中(現在的過程/任務將具有上下文),則代碼將起作用的原因已經嘗試過了)。
如果您想避免導入,雖然我對pyCUDA及其如何處理上下文管理不是很熟悉,但您也許可以使用make_default_context
的pycuda.tools
。
from pycuda.tools import make_default_context
@task()
def photo_function(photo_id,...):
ctx = make_default_context()
print 'Got photo...'
... Do some stuff ...
result = do_photo_manipulation(photo_id)
return result
注意上下文創建是一個昂貴的過程。 CUDA故意在上下文中進行大量工作,以避免以后出現意外延遲。 這就是為什么您有一堆可以在主機線程之間(而不是在進程之間)推送/彈出的上下文的原因。 如果您的內核代碼非常快,則可能會由於上下文創建/銷毀過程而導致延遲。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.