numpy - Pycuda弄乱了numpy矩阵转置

标签 numpy pycuda

为什么转置矩阵在转换为 pycuda.gpuarray 时看起来不同?

你能重现这个吗?什么可能导致这种情况?我使用了错误的方法吗?

示例代码

from pycuda import gpuarray
import pycuda.autoinit
import numpy

data = numpy.random.randn(2,4).astype(numpy.float32)
data_gpu = gpuarray.to_gpu(data.T)
print "data\n",data
print "data_gpu.get()\n",data_gpu.get()
print "data.T\n",data.T

输出
data
[[ 0.70442784  0.08845157 -0.84840715 -1.81618035]
 [ 0.55292499  0.54911566  0.54672164  0.05098847]]
data_gpu.get()
[[ 0.70442784  0.08845157]
 [-0.84840715 -1.81618035]
 [ 0.55292499  0.54911566]
 [ 0.54672164  0.05098847]]
data.T
[[ 0.70442784  0.55292499]
 [ 0.08845157  0.54911566]
 [-0.84840715  0.54672164]
 [-1.81618035  0.05098847]]

最佳答案

在 numpy 中,data.T对底层一维数组没有任何作用。它只是操纵步幅来获得转置。这使其成为恒定时间和恒定内存操作。

看起来pycuda.to_gpu()不尊重步幅,只是复制底层的一维数组。这将产生您正在观察的确切行为。

在我看来,您的代码没有任何问题。相反,我认为这是 pycuda 中的一个错误。 .

我用谷歌搜索,发现 a thread that discusses this issue in detail .

作为解决方法,您可以尝试通过 numpy.ascontiguousarray(data.T)gpuarray.to_gpu() .当然,这将在主机 RAM 中创建数据的第二个副本。

关于numpy - Pycuda弄乱了numpy矩阵转置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6901025/

相关文章:

python - Pycuda 代码无法工作 : the "block" line in the call of the function doesn't work

python - 从 NumPy 数组中获取值

python - 在 Spark Streaming 中查找中位数

cuda - 如何计算 gpu_array 的方差?

c - pyCUDA vs C 性能差异?

ubuntu - 在 CUDA 6.5 中使用 nvvp 分析 PyCUDA 代码

python - 用 X 相邻值的平均值替换 Numpy 数组中大于阈值的所有元素

python - 使用 Cython 将 np.ndarray 传递到 Fortran

python - 将 Numpy 数组转换为列表会打印出额外的零吗? Python

python - 如何将二维数组传递到 pycuda 中的内核?