CuPy记录
因传统的Numpy运算速度较慢,使用其镜像GPU加速版本Cupy
官网
https://docs.cupy.dev/en/stable/index.html
Project Goal
The goal of the CuPy project is to provide Python users GPU acceleration capabilities, without the in-depth knowledge of underlying GPU technologies. The CuPy team focuses on providing:
A complete NumPy and SciPy API coverage to become a full drop-in replacement, as well as advanced CUDA features to maximize the performance.
Mature and quality library as a fundamental package for all projects needing acceleration, from a lab environment to a large-scale cluster.
安装
https://docs.cupy.dev/en/stable/install.html
需要安装CUDA Toolkit,我之前安装PyTorch环境时已经安装过,在NVIDIA控制面板或者运行下列代码查看CUDA版本:
import torch
print(torch.version.cuda)
选择对应CuPy版本进行安装,我这里是11.7(我在Anaconda Prompt中安装)
pip install cupy-cuda11x
之前安装PyTorch时候CUDA还是11.7,不知什么时候升级了到了12.0好像:

验证安装

可以开始使用了!
注意:Move array from a device to the host(使用gdal输出的时候):
https://docs.cupy.dev/en/stable/user_guide/basic.html
x_gpu = cp.array([1, 2, 3]) # create an array in the current device
x_cpu = cp.asnumpy(x_gpu) # move the array to the host.
x_cpu = x_gpu.get()
numpy 和 cumpy可以相互转换,读取的时候只能被其中一个读取(下边#1 读取了 #2就读取不到内容了):
f = open(dir, 'rb')
f_int = np.fromfile(f, dtype=np.int16) # 1
f_int2 = cp.fromfile(f, dtype=cp.int16) # 2
浙公网安备 33010602011771号