Commit fc0240df authored by Artem Pulkin's avatar Artem Pulkin

cython: naive openmp

parent d27ff46b
Pipeline #44241 passed with stage
in 2 minutes and 26 seconds
......@@ -3,6 +3,7 @@ import numpy as np
cimport numpy as np
cimport cython
from libc.math cimport exp, cos, sin, pi, pow, sqrt
from cython.parallel import prange
# Template potential-2.pyx
# A two-point potential: depends on the distance between pairs of atoms
......@@ -31,7 +32,7 @@ def kernel_general_2(
# (no 'preamble' statements)
# ----------------
for row in range(nrows):
for row in range(nrows,):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -74,7 +75,7 @@ def kernel_g_general_2(
# (no 'preamble_grad' statements)
# ----------------
for row in range(nrows):
for row in range(nrows,):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -128,7 +129,7 @@ def kernel_general_3(
# (no 'preamble' statements)
# ----------------
for row in range(nrows):
for row in range(nrows,):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -152,8 +153,8 @@ def kernel_general_3(
r12_cos = 0
# (r1, r2)
for dim in range(3):
r12_cos += (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos /= r1 * r2
r12_cos = r12_cos + (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos = r12_cos / (r1 * r2)
# --- before ---
# (no 'before' statements)
if True:
......@@ -194,7 +195,7 @@ def kernel_g_general_3(
# (no 'preamble_grad' statements)
# ----------------
for row in range(nrows):
for row in range(nrows,):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -221,8 +222,8 @@ def kernel_g_general_3(
r12_cos = 0
# (r1, r2)
for dim in range(3):
r12_cos += (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos /= r1 * r2
r12_cos = r12_cos + (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos = r12_cos / (r1 * r2)
# --- before ---
# (no 'before' statements)
......@@ -238,9 +239,9 @@ def kernel_g_general_3(
dfunc_dr2 = df_dr2(r1, r2, r12_cos)
dfunc_dct = df_dt(r1, r2, r12_cos)
if r12_symmetry_allowed and ptr1 != ptr2:
dfunc_dr1 *= 2
dfunc_dr2 *= 2
dfunc_dct *= 2
dfunc_dr1 = dfunc_dr1 * 2
dfunc_dr2 = dfunc_dr2 * 2
dfunc_dct = dfunc_dct * 2
# Derivatives
......@@ -283,7 +284,7 @@ def kernel_harmonic_repulsion(
# (no 'preamble' statements)
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -326,7 +327,7 @@ def kernel_g_harmonic_repulsion(
# (no 'preamble_grad' statements)
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -378,7 +379,7 @@ def kernel_lj(
# (no 'preamble' statements)
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -421,7 +422,7 @@ def kernel_g_lj(
# (no 'preamble_grad' statements)
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -473,7 +474,7 @@ def kernel_sw_phi2(
# (no 'preamble' statements)
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -516,7 +517,7 @@ def kernel_g_sw_phi2(
# (no 'preamble_grad' statements)
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -570,7 +571,7 @@ def kernel_sw_phi3(
# (no 'preamble' statements)
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -594,8 +595,8 @@ def kernel_sw_phi3(
r12_cos = 0
# (r1, r2)
for dim in range(3):
r12_cos += (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos /= r1 * r2
r12_cos = r12_cos + (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos = r12_cos / (r1 * r2)
# --- before ---
# (no 'before' statements)
if True:
......@@ -636,7 +637,7 @@ def kernel_g_sw_phi3(
# (no 'preamble_grad' statements)
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -663,8 +664,8 @@ def kernel_g_sw_phi3(
r12_cos = 0
# (r1, r2)
for dim in range(3):
r12_cos += (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos /= r1 * r2
r12_cos = r12_cos + (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos = r12_cos / (r1 * r2)
# --- before ---
# (no 'before' statements)
......@@ -680,9 +681,9 @@ def kernel_g_sw_phi3(
dfunc_dr2 = - function_value * gamma / (r2 - a) / (r2 - a)
dfunc_dct = 2 * l * (r12_cos - cos_theta0) * exp(gamma * (1 / (r1 - a) + 1 / (r2 - a)))
if r12_symmetry_allowed and ptr1 != ptr2:
dfunc_dr1 *= 2
dfunc_dr2 *= 2
dfunc_dct *= 2
dfunc_dr1 = dfunc_dr1 * 2
dfunc_dr2 = dfunc_dr2 * 2
dfunc_dct = dfunc_dct * 2
# Derivatives
......@@ -725,7 +726,7 @@ def kernel_mlsf_g2(
cdef int pre_compute_r_fn_handle = pre_compute_r_handles[0]
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -768,7 +769,7 @@ def kernel_g_mlsf_g2(
cdef int pre_compute_r_fp_handle = pre_compute_r_handles[1]
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -824,7 +825,7 @@ def kernel_mlsf_g5(
cdef int pre_compute_r_cutoff_fn_handle = pre_compute_r_handles[0], pre_compute_r_exp_fn_handle = pre_compute_r_handles[2]
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -848,8 +849,8 @@ def kernel_mlsf_g5(
r12_cos = 0
# (r1, r2)
for dim in range(3):
r12_cos += (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos /= r1 * r2
r12_cos = r12_cos + (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos = r12_cos / (r1 * r2)
# --- before ---
_fn_cutoff2 = pre_compute_r[ptr2, pre_compute_r_cutoff_fn_handle]
_fn_exponent = _prefactor * pre_compute_r[ptr1, pre_compute_r_exp_fn_handle] * pre_compute_r[ptr2, pre_compute_r_exp_fn_handle]
......@@ -895,7 +896,7 @@ def kernel_g_mlsf_g5(
cdef int pre_compute_r_cutoff_fp_handle = pre_compute_r_handles[1]
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -922,8 +923,8 @@ def kernel_g_mlsf_g5(
r12_cos = 0
# (r1, r2)
for dim in range(3):
r12_cos += (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos /= r1 * r2
r12_cos = r12_cos + (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos = r12_cos / (r1 * r2)
# --- before ---
_fn_cutoff2 = pre_compute_r[ptr2, pre_compute_r_cutoff_fn_handle]
......@@ -941,9 +942,9 @@ def kernel_g_mlsf_g5(
dfunc_dr2 = - 2 * eta * r2 * function_value + _fn_pw * _fn_exponent * _fn_cutoff1 * _fp_cutoff2
dfunc_dct = zeta * l * pow(1 + l * r12_cos, zeta - 1) * _fn_exponent * _fn_cutoff1 * _fn_cutoff2
if r12_symmetry_allowed and ptr1 != ptr2:
dfunc_dr1 *= 2
dfunc_dr2 *= 2
dfunc_dct *= 2
dfunc_dr1 = dfunc_dr1 * 2
dfunc_dr2 = dfunc_dr2 * 2
dfunc_dct = dfunc_dct * 2
# Derivatives
......@@ -991,7 +992,7 @@ def kernel_mlsf_g4(
cdef double _r3, _r3_factor, g5_fun
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -1015,8 +1016,8 @@ def kernel_mlsf_g4(
r12_cos = 0
# (r1, r2)
for dim in range(3):
r12_cos += (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos /= r1 * r2
r12_cos = r12_cos + (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos = r12_cos / (r1 * r2)
# --- before ---
_r3 = sqrt(r1 * r1 + r2 * r2 - 2 * r1 * r2 * r12_cos)
if _r3 < a:
......@@ -1066,7 +1067,7 @@ def kernel_g_mlsf_g4(
cdef double _r3_factor_p, _r3_grad_r1, _r3_grad_r2, _r3_grad_cosine, g5_grad_r1, g5_grad_r2, g5_grad_cosine
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -1093,8 +1094,8 @@ def kernel_g_mlsf_g4(
r12_cos = 0
# (r1, r2)
for dim in range(3):
r12_cos += (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos /= r1 * r2
r12_cos = r12_cos + (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos = r12_cos / (r1 * r2)
# --- before ---
_r3 = sqrt(r1 * r1 + r2 * r2 - 2 * r1 * r2 * r12_cos)
......@@ -1121,9 +1122,9 @@ def kernel_g_mlsf_g4(
dfunc_dr2 = g5_grad_r2 * _r3_factor + g5_fun * _r3_factor_p * _r3_grad_r2
dfunc_dct = g5_grad_cosine * _r3_factor + g5_fun * _r3_factor_p * _r3_grad_cosine
if r12_symmetry_allowed and ptr1 != ptr2:
dfunc_dr1 *= 2
dfunc_dr2 *= 2
dfunc_dct *= 2
dfunc_dr1 = dfunc_dr1 * 2
dfunc_dr2 = dfunc_dr2 * 2
dfunc_dct = dfunc_dct * 2
# Derivatives
......@@ -1166,7 +1167,7 @@ def kernel_sigmoid(
# (no 'preamble' statements)
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -1209,7 +1210,7 @@ def kernel_g_sigmoid(
# (no 'preamble_grad' statements)
# ----------------
for row in range(nrows):
for row in prange(nrows,nogil=True):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......
......@@ -4,7 +4,8 @@
"type": "potential-2",
"parameters": "f, df_dr",
"kernel": "f(r)",
"grad": "df_dr(r)"
"grad": "df_dr(r)",
"openmp": false
},
{
"name": "general_3",
......@@ -13,7 +14,8 @@
"kernel": "f(r1, r2, r12_cos)",
"grad_r1": "df_dr1(r1, r2, r12_cos)",
"grad_r2": "df_dr2(r1, r2, r12_cos)",
"grad_cosine": "df_dt(r1, r2, r12_cos)"
"grad_cosine": "df_dt(r1, r2, r12_cos)",
"openmp": false
},
{
"name": "harmonic_repulsion",
......
......@@ -74,6 +74,7 @@ class BuildPotentials(Command):
parameters='',
degenerate=False,
final_filter=True,
openmp=True,
)
for k in "preamble", "preamble_grad", "before", "before_grad", "before1", "before1_grad", "before_inner", "before_inner_grad":
defaults[k] = f"# (no '{k}' statements)"
......@@ -81,6 +82,13 @@ class BuildPotentials(Command):
_p = defaults.copy()
_p.update(p)
p = _p
if p["openmp"]:
p["range"] = "prange"
p["range_args"] = "nogil=True"
else:
p["range"] = "range"
p["range_args"] = ""
del p["openmp"]
template_kind = p["type"]
if len(p["parameters"]) > 0:
p["parameters"] = ", ".join(i.strip() for i in p["parameters"].split(",")) + ","
......
......@@ -25,7 +25,7 @@ def kernel_g_$name(
$preamble_grad
# ----------------
for row in range(nrows):
for row in $range(nrows,$range_args):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......
......@@ -25,7 +25,7 @@ def kernel_$name(
$preamble
# ----------------
for row in range(nrows):
for row in $range(nrows,$range_args):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......
......@@ -29,7 +29,7 @@ def kernel_g_$name(
$preamble_grad
# ----------------
for row in range(nrows):
for row in $range(nrows,$range_args):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -56,8 +56,8 @@ def kernel_g_$name(
r12_cos = 0
# (r1, r2)
for dim in range(3):
r12_cos += (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos /= r1 * r2
r12_cos = r12_cos + (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos = r12_cos / (r1 * r2)
# --- before ---
$before
......@@ -73,9 +73,9 @@ def kernel_g_$name(
dfunc_dr2 = $grad_r2
dfunc_dct = $grad_cosine
if r12_symmetry_allowed and ptr1 != ptr2:
dfunc_dr1 *= 2
dfunc_dr2 *= 2
dfunc_dct *= 2
dfunc_dr1 = dfunc_dr1 * 2
dfunc_dr2 = dfunc_dr2 * 2
dfunc_dct = dfunc_dct * 2
# Derivatives
......
......@@ -27,7 +27,7 @@ def kernel_$name(
$preamble
# ----------------
for row in range(nrows):
for row in $range(nrows,$range_args):
if species_row[row] == row_mask:
ptr_fr = r_indptr[row]
ptr_to = r_indptr[row + 1]
......@@ -51,8 +51,8 @@ def kernel_$name(
r12_cos = 0
# (r1, r2)
for dim in range(3):
r12_cos += (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos /= r1 * r2
r12_cos = r12_cos + (cartesian_col[col1, dim] - cartesian_row[row, dim]) * (cartesian_col[col2, dim]- cartesian_row[row, dim])
r12_cos = r12_cos / (r1 * r2)
# --- before ---
$before
if $final_filter:
......
......@@ -3,5 +3,6 @@ import numpy as np
cimport numpy as np
cimport cython
from libc.math cimport exp, cos, sin, pi, pow, sqrt
from cython.parallel import prange
$kernels
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment