forked from ebezzam/python-dev-tips
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcupy_fft.py
More file actions
95 lines (75 loc) · 1.84 KB
/
Copy pathcupy_fft.py
File metadata and controls
95 lines (75 loc) · 1.84 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
CuPy Example
Installing:
- check Cuda version, e.g. from Terminal run: `nvcc --version` or `nvidia-smi`
- install corresponding version of cupy, e.g. `pip install cupy-cuda11x`
Installation page: https://docs.cupy.dev/en/stable/install.html
"""
from importlib import util
import os
import numpy as np
import scipy
import time
try:
import cupy as cp
import cupyx
CUPY_AVAILABLE = True
except ImportError:
CUPY_AVAILABLE = False
def get_array_module(x):
"""
Returns correct numerical module based on input.
Parameters
----------
x : :obj:`numpy.ndarray` or :obj:`cupy.ndarray`
Array
Returns
-------
mod : :obj:`func`
Module to be used to process array (:mod:`numpy` or :mod:`cupy`)
"""
if CUPY_AVAILABLE:
return cp.get_array_module(x)
else:
return np
def fft2(x):
"""
Applies correct fft method based on input.
Parameters
----------
x : :obj:`numpy.ndarray` or :obj:`cupy.ndarray`
Array
Returns
-------
mod : :obj:`func`
Module to be used to process array (:mod:`numpy` or :mod:`cupy`)
"""
if get_array_module(x) == np:
func = scipy.fft.fft2
else:
func = cupyx.scipy.fft.fft2
return func(x)
# compare FFT computation
n = 1024
n_trials = 100
x = np.random.rand(n, n)
if CUPY_AVAILABLE:
x_gpu = cp.asarray(x)
print(x_gpu.device)
else:
x_gpu = x
print("Cupy not available. Using numpy instead.")
# numpy
start = time.perf_counter()
for _ in range(n_trials):
fft2(x)
time_cpu = time.perf_counter() - start
print(f"CPU processing took {time_cpu} seconds")
# cupy
start = time.perf_counter()
for _ in range(n_trials):
fft2(x_gpu)
time_gpu = time.perf_counter() - start
print(f"GPU processing took {time_gpu} seconds")
# speed-up
print(f"Speed-up: {time_cpu / time_gpu}")