-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpufft.py
48 lines (38 loc) · 1.27 KB
/
gpufft.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import numpy as np
from reikna.cluda import cuda_api, dtypes
from reikna.fft import FFT
class _FFTLocalState(object):
def __init__(self):
self._pid = None
self._cluda_api = None
self._thr = None
self._fftc = None
def _initialize(self):
if self._pid != os.getpid():
print 'Initializing CUDA thread for pid', os.getpid()
self._cluda_api = cuda_api()
self._thr = self._cluda_api.Thread.create()
self._fftc = {}
self._pid = os.getpid()
def get_thread(self):
self._initialize()
return self._thr
def get_fftc(self, arr):
self._initialize()
shape = arr.shape
if shape in self._fftc:
return self._fftc[shape]
fft = FFT(self._thr.array(shape, np.complex64))
fftc = fft.compile(self._thr)
self._fftc[shape] = fftc
return fftc
_local_state = _FFTLocalState()
def fft(arr, inverse=False):
if arr.dtype != np.complex64:
arr = arr.astype(np.complex64)
fftc = _local_state.get_fftc(arr)
arr_dev = _local_state.get_thread().to_device(arr)
res_dev = _local_state.get_thread().array(arr.shape, np.complex64)
fftc(res_dev, arr_dev, int(inverse))
return res_dev.get()