Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyCUDA使用字符串,但不使用字符串数组_Python_C_Gpu_Pycuda - Fatal编程技术网

Python PyCUDA使用字符串,但不使用字符串数组

Python PyCUDA使用字符串,但不使用字符串数组,python,c,gpu,pycuda,Python,C,Gpu,Pycuda,如何让PyCuda拉入字符串数组而不是一个字符字符串?如果在C代码中取消注释该行,您将看到它遍历每个字符,而不是每个字符串 现在我只想计算每个字符串的长度,但最终会将其转换成一个字频率计数器。第一步:传入数组。。。 现在期望的输出应该是25,27,44 import pycuda.driver as drv import pycuda.tools import pycuda.autoinit import numpy from pycuda.compiler import SourceModul

如何让PyCuda拉入字符串数组而不是一个字符字符串?如果在C代码中取消注释该行,您将看到它遍历每个字符,而不是每个字符串

现在我只想计算每个字符串的长度,但最终会将其转换成一个字频率计数器。第一步:传入数组。。。 现在期望的输出应该是25,27,44

import pycuda.driver as drv
import pycuda.tools
import pycuda.autoinit
import numpy
from pycuda.compiler import SourceModule

# create an array of 1s
lines = numpy.array(['ok this is the first line','number two line is this one','alright last line is in the third place here'])
lines = numpy.array(lines)
blocks = len(lines)
block_size = 1
nbr_values = blocks * block_size

# create a destination array that will receive the result
a = numpy.zeros(nbr_values).astype(numpy.float32)
dest = numpy.zeros_like(a)

######################
# SourceModele SECTION
mod = SourceModule("""
__global__ void gpusin(float *dest, char *lines)
{
  const int i = blockDim.x*blockIdx.x + threadIdx.x;
  dest[i] = sizeof (lines[i]);
  //dest[i] = lines[i]; //uncomment this line to see that its iterating through individual chars not strings 
}
""")

#Run the sourc model
gpusin = mod.get_function("gpusin")
gpusin(drv.Out(dest), drv.In(lines), grid=(blocks,1), block=(block_size,1,1) )
print str(dest)
print lines

请陈述对你给出的答案的看法。
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule

import numpy

# create an array of 1s
data1 = ['Apple 12345 this is a long string again','Ding dong, it"s Pyton!!','digity! gigity!']
lines = numpy.array( data1, dtype=str)
linesGPU = cuda.mem_alloc(lines.size * lines.dtype.itemsize)
cuda.memcpy_htod(linesGPU, lines)
blocks = len(data1)
threadsPerBlock = lines.dtype.itemsize
nbr_values = lines.size * lines.dtype.itemsize # blocks * block_size
print("lines size: " + str(lines.size) + " itemsize : " + str(lines.dtype.itemsize))

# create a destination array that will receive the result
dest = numpy.zeros((nbr_values,), dtype=numpy.str)
destGPU =  cuda.mem_alloc(dest.size * dest.dtype.itemsize)

mod = SourceModule("""
__global__ void process(char **dest, char **line)
{
  int tID = threadIdx.x ;//+ blockIdx.x * blockDim.x;
  dest[tID] = line[tID];
}
""")

#Run the sourc model
gpusin = mod.get_function("process")
gpusin(destGPU, linesGPU, grid=(blocks,1), block=(threadsPerBlock,1,1))
cuda.memcpy_dtoh(dest, destGPU)
print str(len(dest)) 
print dest