时间:2019-11-09 19:44:04

标签: python cupy

作为使用python和cupy的初学者,我正在尝试使用AORUS RTX 2070 GAMING BOX作为外部GPU来处理来自RF接收器的complex64 IQ数据。目的是要比从CPU上获得更快的解调和查看速度。

脚本的一部分的一般步骤是:
1-使用numpy从文件获取复杂数据数组
2-将该阵列分成较小的部分,以准备进行多次FFT。
3-执行FFT

我想对cupy进行大多数操作以加速该过程,但是cupy.split()似乎返回类型<class 'list'>而不是类型<class cupy.core.core.ndarray'>,它会“停留”在GPU中。

当我期望类型为<class 'list'>时,cupy.split()返回类型<class 'cupy.core.core.ndarray'>是否正常?

cupy.split()似乎使用cupy.array_split(),cupy.core.array_split()也使用

import numpy
import cupy   # Version cuda91-6.5.0


def numpy_split_version():
    filepath = 'C:\\Users\\Public\\Documents\\BladeRf2_20191108_145919_97900000_20000000_30.float32'
    grab_length = 500000000         #Quantity of complex sample to import
    with open(filepath, 'rb') as fr:
        data = numpy.fromfile( fr, dtype=(numpy.complex64), count=grab_length )    #import samples in numpy array
    len_count = len(data)       #Get the length of the numpy array

    #Validate end of file or format smaller file data. Test version
    if len_count != grab_length:
        if int(len_count/100) == 0:   
            pass
        else:        #Format data for equal split
            x = len_count%100
            if x != 0:
                data = data[:-x]

    #Calculate split value to get parts of 100 samples
    split_count = int(len(data)/100)

    #split the numpy array in arrays of size of 100 samples. Numpy version on cpu
    cpu_data = numpy.split(data, split_count)

    #Create an array inside the GPU from numpy array
    gpu_data = cupy.array(cpu_data)
    print( "gpu_data type after numpy split:", type(gpu_data) )       # type( gpu_data ) = <class 'cupy.core.core.ndarray'>

    #Perfom fft on each array of size 100 inside the GPU 
    gpu_fft = cupy.fft.fft(gpu_data, axis=1)
    gpu_fft = cupy.fft.fftshift(gpu_fft)    


def cupy_split_version():
    filepath = 'C:\\Users\\Public\\Documents\\BladeRf2_20191108_145919_97900000_20000000_30.float32'
    grab_length = 800000000         #Quantity of complex sample to import
    with open(filepath, 'rb') as fr:
        data = numpy.fromfile( fr, dtype=(numpy.complex64), count=grab_length )    #import samples in numpy array
    len_count = len(data)       #Get the length of the numpy array

    #Validate end of file or format smaller file data. Test version
    if len_count != grab_length:
        if int(len_count/100) == 0:   
            pass
        else:        #Format data for equal split
            x = len_count%100
            if x != 0:
                data = data[:-x]

    #Calculate split value to get parts of 100 samples
    split_count = int(len(data)/100)

    #Create an array inside the GPU from numpy array
    gpu_data = cupy.array(data)
    print( "gpu_data type after cupy array:", type(gpu_data) )       # type( gpu_data ) = <class 'cupy.core.core.ndarray'>

    #Split the cupy array in arrays of size of 100 samples. Cupy version on gpu
    gpu_data = cupy.split(gpu_data, split_count)
    print( "gpu_data type after cupy split:", type(gpu_data) )       # type( gpu_data ) = <class 'list'> ??? should be <class 'cupy.core.core.ndarray'>

    #Perfom fft on each array of size 100 inside the GPU. not possible because of <class 'list'>
    gpu_fft = cupy.fft.fft(gpu_data, axis=1)
    gpu_fft = cupy.fft.fftshift(gpu_fft)  


if __name__ == '__main__':
    numpy_split_version()
    cupy_split_version()

我期望从cupy.split()获得类型<class 'cupy.core.core.ndarray'>,但是得到类型<class 'list'>.

gpu_data type after numpy split: <class 'cupy.core.core.ndarray'>
gpu_data type after cupy array: <class 'cupy.core.core.ndarray'>
gpu_data type after cupy split: <class 'list'>
Traceback (most recent call last):
  File "C:/Users/Kaven/Desktop/cupy_split_web_help.py", line 70, in <module>
    cupy_split_version()
  File "C:/Users/Kaven/Desktop/cupy_split_web_help.py", line 64, in cupy_split_version
    gpu_fft = cupy.fft.fft(gpu_data, axis=1)
  File "C:\Users\Kaven\AppData\Local\Programs\Python\Python37\lib\site-packages\cupy\fft\fft.py", line 468, in fft
    return _fft(a, (n,), (axis,), norm, cupy.cuda.cufft.CUFFT_FORWARD)
  File "C:\Users\Kaven\AppData\Local\Programs\Python\Python37\lib\site-packages\cupy\fft\fft.py", line 145, in _fft
    a = _convert_dtype(a, value_type)
  File "C:\Users\Kaven\AppData\Local\Programs\Python\Python37\lib\site-packages\cupy\fft\fft.py", line 30, in _convert_dtype
    out_dtype = _output_dtype(a, value_type)
  File "C:\Users\Kaven\AppData\Local\Programs\Python\Python37\lib\site-packages\cupy\fft\fft.py", line 15, in _output_dtype
    if a.dtype in [np.float16, np.float32]:
AttributeError: 'list' object has no attribute 'dtype'

0 个答案:

没有答案