PyCUDA内核为特定计算返回不一致的除法结果

问题描述 投票:0回答:1

我正在尝试实现一个计算百分比平均值的内核。

示例-取3D数组(在下面的代码中)[[2,4],[3,6],[4,8]]并计算(4+6+8)/((4+6+8)+(2+3+4))

这里是一个colab笔记本,可以快速运行以下代码:https://colab.research.google.com/drive/1k_XfOVOYWOTnNQFA9Vo_H93D9l-xWO8K?usp=sharing

# -*- coding: utf-8 -*-

import numpy as np
import pycuda.autoinit
import pycuda.driver as cuda
import pycuda.gpuarray as gpuarray
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule

# set dimentions
ROWS = 3
COLS = 2

h_perms = np.array([[
    [ 1,1],
    [ 1,1],
    [ 1,1]
],[
    [ 2,7],
    [ 3,11],
    [ 4,13]
],[
    [ 2,4],
    [ 3,6],
    [ 4,8]
],[
    [ 2,7],
    [ 3,11],
    [ 4,13]
],[
    [ 2,4],
    [ 3,6],
    [ 4,8]
],[
    [ 1,1],
    [ 1,1],
    [ 1,1]
]
], dtype=np.float32).flatten()

# send to device
d_perms = gpuarray.to_gpu(h_perms)

kernel = SourceModule("""
  __global__ 
  void calc(float *permutations, int *permutationShape, float *results)
  {
    __shared__ float c;
    __shared__ float b;
    int bIdx = blockIdx.y * gridDim.x + blockIdx.x;
    int tIdx = threadIdx.y * blockDim.x + threadIdx.x;
    int rowCount = permutationShape[0];
    int colCount = permutationShape[1];
    int i = (bIdx * rowCount * colCount) + (tIdx * colCount);
    c += permutations[i];
    b += permutations[i+1];
    __syncthreads();
    results[bIdx] = b / (b + c);
  }
  """)

calc = kernel.get_function('calc')

# prepare results array
d_results = gpuarray.zeros((6,1), np.float32)
d_results = gpuarray.to_gpu(d_results)

h_perms_shape = np.array([ROWS,COLS], np.int32);
d_perms_shape = gpuarray.to_gpu(h_perms_shape);

start = cuda.Event()
end = cuda.Event()
start.record()
calc(d_perms, d_perms_shape, d_results, block=(ROWS,1,1), grid=(ROWS*COLS,1,1))
end.record()
secs = start.time_till(end)*1e-3
print(secs)

print(d_results)

我希望得到这个-

array([[0.5      ],
       [0.775],
       [0.6666667],
       [0.775],
       [0.6666667],
       [0.5      ]], dtype=float32)

但是我明白了-

array([[0.5      ],
       [0.7777778],
       [0.6666667],
       [0.7777778],
       [0.6666667],
       [0.5      ]], dtype=float32)

我试图理解为什么(7+11+13)/((7+11+13)+(2+3+4))的特定计算会导致任何非0.775的结果>

我正在尝试实现一个计算百分比平均值的内核。示例-取3D数组(在下面的代码中)片段[[2,4],[3,6],[4,8]]并计算(4 + 6 + 8)/(((4 + 6 + 8) +(2 + 3 + 4))这是一个...

cuda pycuda
1个回答
0
投票

您发布的代码在此处包含内存争用:

© www.soinside.com 2019 - 2024. All rights reserved.