编辑:这是为了展示 GPU 和 CPU 之间的时序差异 - 它并不意味着用于任何实际应用程序。目标是展示在 CPU 和 GPU 上同步和异步使用的 2 种不同算法。每次运行结束时都会有一个长长的排序列表。该函数是从它在 CPU 上的工作方式复制而来的,但我之前从未将 cuda 与 python 或 numpa 库一起使用,因此修改它以在 GPU 上运行被证明是一场斗争。我只在 C# 中做过 cuda。我检查了文档并尝试了他们提供的所有关于类型错误的列表,但这些都没有用.
我正在努力解决函数中的输入错误。它不能确定函数参数的类型来成功编译和运行函数。我在 python 中使用 Numba 库
这里是问题子函数:
@cuda.jit
def quicksort_cuda(array):
# If the input array contains fewer than two elements,
# then return it as the result of the function
if len(array) < 2:
return array
low, same, high = [], [], []
# Select your `pivot` element randomly
pivot = array[randint(0, len(array) - 1)]
for item in array:
# Elements that are smaller than the `pivot` go to
# the `low` list. Elements that are larger than
# `pivot` go to the `high` list. Elements that are
# equal to `pivot` go to the `same` list.
if item < pivot:
low.append(item)
elif item == pivot:
same.append(item)
elif item > pivot:
high.append(item)
# The final result combines the sorted `low` list
# with the `same` list and the sorted `high` list
return quicksort(low) + same + quicksort(high)
这是它的名字:
import numpy as np
import time
import math
from numba import cuda
from typing import List
def cast_list(test_list, data_type):
return list(map(data_type, test_list))
def cast_matrix(test_matrix, data_type):
return list(map(lambda sub: list(map(data_type, sub)), test_matrix))
dataT = []
for n in range(1000):
#temporary holding for line of data
temp = []
num = 0
for i in range(1000):
num += 5393
num = num%1000
temp.append(num)
dataT.append(temp)
data = cast_matrix(dataT,int)
for i in range(1000):
quicksort_cuda[1,1](data[i])
final = data[0].copy()
for i in range(999):
#merge takes 2 sorted lists and returns a single sorted list
final = merge(final,data[i].copy())
这是它吐出的错误:
numba.core.errors.TypingError: Failed in cuda mode pipeline (step: nopython frontend)
No conversion from reflected list(int64)<iv=None> to none for '$16return_value.1', defined at None
File "experiment.py", line 189:
def quicksort_cuda(array):
<source elided>
if len(array) < 2:
return array
^
During: typing of assignment at C:/Users/Reagan/Documents/experiment.py (189)
File "experiment.py", line 189:
def quicksort_cuda(array):
<source elided>
if len(array) < 2:
return array
我如何解决它随数组抛出的“对于'$16return_value.1'没有从反射列表(int64)
我尝试通过以下方式对数组进行类型转换:
#typecast attempt 1
if len(array) < 2:
temp = [int(array[0])]
return temp
#attempt 2
if len(array) < 2:
return array.astype(int)
#attempt 3
if len(array) < 2:
temp = []
temp.append(array[0])
return temp
我尝试修改函数定义以将参数数组定义为整数数组:
def quicksort_cuda(array: List[int]):
我还尝试修改函数中创建的数组以声明为整数数组:
low = [np.intc(x) for x in range(0)]
same = [np.intc(x) for x in range(0)]
high = [np.intc(x) for x in range(0)]
这些都导致它没有认识到给定的数组是一个整数数组。是否有另一种方法来声明数组类型,numpa 将如何识别它,或者我不需要转换数组而是更改其他内容?