Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Dec 8, 2024. It is now read-only.

Commit 0441f3f

Browse files
committed
Added examples and atomated Fortran-memory/transpose in fast kernels
1 parent bc6b4de commit 0441f3f

File tree

7 files changed

+94
-261
lines changed

7 files changed

+94
-261
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ qml/kernels/farad_kernels.so: qml/kernels/farad_kernels.f90
3636
mv farad_kernels*.so qml/kernels/farad_kernels.so
3737

3838
clean:
39-
rm -f qml/*.so
4039
rm -f qml/*.pyc
4140
rm -f qml/math/*.so
4241
rm -f qml/math/*.pyc

qml/compound.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ def __init__(self, xyz=None):
6060
if xyz is not None:
6161
self.read_xyz(xyz)
6262

63-
def generate_coulomb_matrix(self, size=23, sort="row-norm"):
63+
def generate_coulomb_matrix(self, size=23, sorting="row-norm"):
6464

65-
if (sort == "row-norm"):
65+
if (sorting == "row-norm"):
6666
self.coulomb_matrix = fgenerate_coulomb_matrix(self.nuclear_charges, \
6767
self.coordinates, self.natoms, size)
6868

69-
elif (sort == "unsorted"):
69+
elif (sorting == "unsorted"):
7070
self.coulomb_matrix = fgenerate_unsorted_coulomb_matrix(self.nuclear_charges, \
7171
self.coordinates, self.natoms, size)
7272

@@ -76,11 +76,11 @@ def generate_coulomb_matrix(self, size=23, sort="row-norm"):
7676

7777
def generate_atomic_coulomb_matrix(self,size=23, sorting ="row-norm"):
7878

79-
if (sort == "row-norm"):
79+
if (sorting == "row-norm"):
8080
self.local_coulomb_matrix = fgenerate_local_coulomb_matrix( \
8181
self.nuclear_charges, self.coordinates, self.natoms, size)
8282

83-
elif (sort == "distance"):
83+
elif (sorting == "distance"):
8484
self.atomic_coulomb_matrix = fgenerate_atomic_coulomb_matrix( \
8585
self.nuclear_charges, self.coordinates, self.natoms, size)
8686

qml/distance/distance.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ def manhattan_distance(A, B):
4444
if len(A.shape) != 2 or len(B.shape) != 2:
4545
raise ValueError('expected matrices of dimension=2')
4646

47-
if B.shape[0] != A.shape[0]:
47+
if B.shape[1] != A.shape[1]:
4848
raise ValueError('expected matrices containing vectors of same size')
4949

50-
na = A.shape[1]
51-
nb = B.shape[1]
50+
na = A.shape[0]
51+
nb = B.shape[0]
5252

5353
D = empty((na, nb), order='F')
5454

55-
fmanhattan_distance(A, B, D)
55+
fmanhattan_distance(A.T, B.T, D)
5656

5757
return D
5858

@@ -73,15 +73,15 @@ def l2_distance(A, B):
7373
if len(A.shape) != 2 or len(B.shape) != 2:
7474
raise ValueError('expected matrices of dimension=2')
7575

76-
if B.shape[0] != A.shape[0]:
76+
if B.shape[1] != A.shape[1]:
7777
raise ValueError('expected matrices containing vectors of same size')
7878

79-
na = A.shape[1]
80-
nb = B.shape[1]
79+
na = A.shape[0]
80+
nb = B.shape[0]
8181

8282
D = empty((na, nb), order='F')
8383

84-
fl2_distance(A, B, D)
84+
fl2_distance(A.T, B.T, D)
8585

8686
return D
8787

@@ -103,11 +103,11 @@ def p_distance(A, B, p=2):
103103
if len(A.shape) != 2 or len(B.shape) != 2:
104104
raise ValueError('expected matrices of dimension=2')
105105

106-
if B.shape[0] != A.shape[0]:
106+
if B.shape[1] != A.shape[1]:
107107
raise ValueError('expected matrices containing vectors of same size')
108108

109-
na = A.shape[1]
110-
nb = B.shape[1]
109+
na = A.shape[0]
110+
nb = B.shape[0]
111111

112112
D = empty((na, nb), order='F')
113113

@@ -116,10 +116,10 @@ def p_distance(A, B, p=2):
116116
if (p == 2):
117117
fl2_distance(A, B, D)
118118
else:
119-
fp_distance_integer(A, B, D, p)
119+
fp_distance_integer(A.T, B.T, D, p)
120120

121121
elif (type(p) == type(1.0)):
122-
fp_distance_double(A, B, D, p)
122+
fp_distance_double(A.T, B.T, D, p)
123123
else:
124124
raise ValueError('expected exponent of integer or float type')
125125

qml/distance/fdistance.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ subroutine fp_distance_double(A, B, D, p)
107107
!$OMP PARALLEL DO PRIVATE(temp)
108108
do i = 1, nb
109109
do j = 1, na
110-
temp(:) = A(:,j) - B(:,i)
110+
temp(:) = abs(A(:,j) - B(:,i))
111111
D(j,i) = (sum(temp**p))**inv_p
112112
enddo
113113
enddo
@@ -144,7 +144,7 @@ subroutine fp_distance_integer(A, B, D, p)
144144
!$OMP PARALLEL DO PRIVATE(temp)
145145
do i = 1, nb
146146
do j = 1, na
147-
temp(:) = A(:,j) - B(:,i)
147+
temp(:) = abs(A(:,j) - B(:,i))
148148
D(j,i) = (sum(temp**p))**inv_p
149149
enddo
150150
enddo

qml/kernels/kernels.py

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ def laplacian_kernel(A, B, sigma):
5151
K -- The Laplacian kernel matrix.
5252
"""
5353

54-
na = A.shape[1]
55-
nb = B.shape[1]
54+
na = A.shape[0]
55+
nb = B.shape[0]
5656

5757
K = empty((na, nb), order='F')
58-
flaplacian_kernel(A, na, B, nb, K, sigma)
58+
59+
# Note: Transposed for Fortran
60+
flaplacian_kernel(A.T, na, B.T, nb, K, sigma)
5961

6062
return K
6163

@@ -82,31 +84,51 @@ def gaussian_kernel(A, B, sigma):
8284
K -- The Gaussian kernel matrix.
8385
"""
8486

85-
na = A.shape[1]
86-
nb = B.shape[1]
87+
na = A.shape[0]
88+
nb = B.shape[0]
8789

8890
K = empty((na, nb), order='F')
8991

90-
fgaussian_kernel(A, na, B, nb, K, sigma)
92+
# Note: Transposed for Fortran
93+
fgaussian_kernel(A.T, na, B.T, nb, K, sigma)
9194

9295
return K
9396

9497

95-
def get_atomic_kernels_laplacian(x1, x2, N1, N2, sigmas):
98+
def get_atomic_kernels_laplacian(mols1, mols2, sigmas):
99+
100+
n1 = np.array([mol.natoms for mol in mols1], dtype=np.int32)
101+
n2 = np.array([mol.natoms for mol in mols2], dtype=np.int32)
102+
103+
amax1 = np.amax(n1)
104+
amax2 = np.amax(n2)
105+
106+
nm1 = len(mols1)
107+
nm2 = len(mols2)
108+
109+
cmat_size = mols1[0].local_coulomb_matrix.shape[1]
110+
111+
x1 = np.zeros((nm1,amax1,cmat_size), dtype=np.float64, order="F")
112+
x2 = np.zeros((nm2,amax2,cmat_size), dtype=np.float64, order="F")
113+
114+
for imol in range(nm1):
115+
x1[imol,:n1[imol],:cmat_size] = mols1[imol].local_coulomb_matrix
116+
117+
for imol in range(nm2):
118+
x2[imol,:n2[imol],:cmat_size] = mols2[imol].local_coulomb_matrix
119+
120+
# Reorder for Fortran speed
121+
x1 = np.swapaxes(x1,0,2)
122+
x2 = np.swapaxes(x2,0,2)
123+
124+
nsigmas = len(sigmas)
125+
126+
sigmas = np.array(sigmas, dtype=np.float64)
127+
128+
return fget_vector_kernels_laplacian(x1, x2, n1, n2, sigmas, \
129+
nm1, nm2, nsigmas)
96130

97-
nm1 = len(N1)
98-
nm2 = len(N2)
99-
100-
n1 = np.array(N1,dtype=np.int32)
101-
n2 = np.array(N2,dtype=np.int32)
102-
103-
nsigmas = len(sigmas)
104-
sigmas = np.array(sigmas)
105-
106-
return fget_vector_kernels_laplacian(x1, x2, n1, n2, sigmas, \
107-
nm1, nm2, nsigmas)
108131

109-
110132
def get_atomic_kernels_gaussian(mols1, mols2, sigmas):
111133

112134
n1 = np.array([mol.natoms for mol in mols1], dtype=np.int32)
@@ -117,7 +139,7 @@ def get_atomic_kernels_gaussian(mols1, mols2, sigmas):
117139

118140
nm1 = len(mols1)
119141
nm2 = len(mols2)
120-
142+
121143
cmat_size = mols1[0].local_coulomb_matrix.shape[1]
122144

123145
x1 = np.zeros((nm1,amax1,cmat_size), dtype=np.float64, order="F")
@@ -136,7 +158,7 @@ def get_atomic_kernels_gaussian(mols1, mols2, sigmas):
136158
nsigmas = len(sigmas)
137159

138160
sigmas = np.array(sigmas, dtype=np.float64)
139-
161+
140162
return fget_vector_kernels_gaussian(x1, x2, n1, n2, sigmas, \
141163
nm1, nm2, nsigmas)
142164

qml/representations/__init__.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@
2828
1. local_coulomb_matrix
2929
"""
3030

31-
from frepresentations import fgenerate_coulomb_matrix
32-
from frepresentations import fgenerate_unsorted_coulomb_matrix
33-
from frepresentations import fgenerate_local_coulomb_matrix
34-
from frepresentations import fgenerate_atomic_coulomb_matrix
35-
from arad import ARAD
31+
from .frepresentations import fgenerate_coulomb_matrix
32+
from .frepresentations import fgenerate_unsorted_coulomb_matrix
33+
from .frepresentations import fgenerate_local_coulomb_matrix
34+
from .frepresentations import fgenerate_atomic_coulomb_matrix
3635

37-
__all__ = ['fgenerate_coulomb_matrix']
36+
from .representations import generate_coulomb_matrix
37+
from .representations import generate_atomic_coulomb_matrix
38+
39+
from .arad import ARAD
40+
41+
__all__ = ['generate_coulomb_matrix', 'generate_atomic_coulomb_matrix']
3842

0 commit comments

Comments
 (0)