Skip to content

Commit 5ca4b51

Browse files
authored
Merge pull request #143 from sunqm/gpu4pyscf
Improve GPU4PySCF documentation
2 parents 8c95e19 + 1c26c5c commit 5ca4b51

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

source/user/gpu.rst

+49-5
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,20 @@ The binary package of GPU4PySCF is released based on the CUDA version.
3939

4040
Usage of GPU4PySCF
4141
==================
42-
GPU4PySCF APIs are designed to be compatible with PySCF. When supported, high-level functions and objects are named the same as PySCF. But, GPU4PySCF classes do not directly inherit from PySCF class.
43-
PySCF objects and GPU4PySCF objects can be converted into each other by :func:`to_gpu` and :func:`to_cpu`. In the conversion, the numpy arrays will be converted into cupy array. And the functions will be omitted if they are not supported with GPU acceleration.
42+
The GPU4PySCF APIs are designed to maintain compatibility with PySCF. The
43+
classes and methods in GPU4PySCF are named identically to those in PySCF,
44+
ensuring a familiar interface for users. However, GPU4PySCF classes do not
45+
directly inherit from PySCF classes.
4446

45-
One can use the two modes to accelerate the calculations: directly use GPU4PySCF object::
47+
PySCF objects and GPU4PySCF objects can be converted to each other using the `to_gpu` and `to_cpu` methods.
48+
The conversion process can automatically, recursively translate all attributes between GPU and CPU instances.
49+
For example, numpy arrays on the CPU are converted into CuPy arrays on the GPU, and vice versa.
50+
If certain attributes are exclusive to either the CPU or GPU, these attributes will be appropriately handled.
51+
They are omitted or specifically converted, depending on the target platform.
52+
53+
There are two approaches to execute the computation on GPU.
54+
55+
1. Directly import GPU4PySCF classes and methods::
4656

4757
import pyscf
4858
from gpu4pyscf.dft import rks
@@ -65,7 +75,7 @@ One can use the two modes to accelerate the calculations: directly use GPU4PySCF
6575
h = mf.Hessian()
6676
h_dft = h.kernel() # compute analytical Hessian
6777

68-
Alternatively, one can convert PySCF object to the corresponding GPU4PySCF object with :func:`to_gpu` since PySCF 2.5.0 ::
78+
2. Convert PySCF object to the corresponding GPU4PySCF object with :func:`to_gpu`::
6979

7080
import pyscf
7181
from pyscf.dft import rks
@@ -82,10 +92,44 @@ Alternatively, one can convert PySCF object to the corresponding GPU4PySCF objec
8292

8393

8494
When the GPU task is done, the GPU4PySCF object can be converted into the corresponding PySCF object via :func:`mf.to_cpu()`.
85-
Then, more sophisticated methods in PySCF can apply. One can also convert the individual CuPy array to numpy array with `Cupy APIs`_.
95+
96+
In GPU4PySCF, wavefunctions, density matrices, and other array data are stored in CuPy arrays.
97+
To transfer these data to NumPy arrays on the CPU, the `.get()` method of the CuPy array can be invoked.
98+
For more detailed information on handling CuPy array conversions, please refer to the `CuPy APIs` documentation.
8699

87100
.. Cupy APIs: https://docs.cupy.dev/en/stable/user_guide/index.html
88101
102+
GPU4PySCF and PySCF Hybrid Programming
103+
======================================
104+
GPU4PySCF allows for seamless integration with existing PySCF programs, enabling
105+
a hybrid approach that leverages both CPU and GPU resources in the program. This
106+
integration is facilitated through the use of `to_gpu()` and `to_cpu()`
107+
functions, which convert PySCF instances between CPU and GPU.
108+
109+
For instance, we can perform DFT calculations on GPU to obtain a set of DFT
110+
orbitals followed by orbital localization using the Boys method on the CPU::
111+
112+
import pyscf
113+
from pyscf import lo
114+
mol = pyscf.M(atom = '''
115+
O 0.0000000000 -0.0000000000 0.1174000000
116+
H -0.7570000000 -0.0000000000 -0.4696000000
117+
H 0.7570000000 0.0000000000 -0.4696000000
118+
''', basis='def2-tzvpp')
119+
120+
# Perform DFT computation on GPU
121+
mf = mol.RKS(xc='b3lyp').to_gpu().run()
122+
123+
# Transfer the computation back to CPU and continue the tasks on the CPU
124+
mf = mf.to_cpu()
125+
loc_orb = lo.Boys(mol, mf.mo_coeff[:,[2,3,4]]).kernel()
126+
127+
**GPU Implementation Availability**: The `to_gpu` method is implemented for
128+
almost all methods in PySCF. However, the actual availability of GPU4PySCF
129+
implementations for specific modules may vary. If a GPU4PySCF module is
130+
available, `to_gpu` will return a GPU4PySCF instance. Otherwise, it will raise a
131+
`NotImplementedError`.
132+
89133
Functionalities supported by GPU4PySCF
90134
======================================
91135
.. list-table::

0 commit comments

Comments
 (0)