-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace std::pow with integer powers to amrex::Math::powi (#1426)
With updated network to reflect changes in pynucastro/pynucastro#709. Added a script and CI to detect cases where we can potentially use powi instead of std::pow for integer powers.
- Loading branch information
Showing
47 changed files
with
979 additions
and
872 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import subprocess | ||
import sys | ||
import os | ||
import re | ||
|
||
|
||
def pow_to_powi(text): | ||
# Finds all possible std::pow(x, n) where n is a potential integer | ||
# to amrex::Math::powi<n>(x) | ||
|
||
# Check for all positive and negative integer, whole float numbers | ||
# with and without _rt | ||
std_pattern = r"std::pow\(([^,]+),\s*(-?(?:\d+\.0*_rt?|\d))\)" | ||
gcem_pattern = r"gcem::pow\(([^,]+),\s*(-?(?:\d+\.0*_rt?|\d))\)" | ||
|
||
def replacement(match): | ||
x = match.group(1) | ||
n = match.group(2) | ||
|
||
# Only extracts out the integer part before decimal point | ||
n = n.split('.')[0] if '.' in n else n | ||
return f"amrex::Math::powi<{n}>({x})" | ||
|
||
text = re.sub(std_pattern, replacement, text) | ||
text = re.sub(gcem_pattern, replacement, text) | ||
return text | ||
|
||
def process_content(dir_path): | ||
# This function processes all text in the given directory | ||
for root, dirs, filenames in os.walk(dir_path): | ||
for filename in filenames: | ||
if filename.endswith(".H") or filename.endswith(".cpp"): | ||
filepath = os.path.join(root, filename) | ||
|
||
with open(filepath, 'r') as file: | ||
content = file.read() | ||
|
||
updated_content = pow_to_powi(content) | ||
|
||
with open(filepath, 'w') as file: | ||
file.write(updated_content) | ||
|
||
def git_diff(): | ||
# Run git diff to see if there are any changes made | ||
|
||
git_diff_output = subprocess.run(['git', 'diff', '--color=always'], | ||
capture_output=True, text=True) | ||
|
||
# Print out suggested change and raise error after detecting modification | ||
if git_diff_output.stdout: | ||
print("Detected potential usage to replace std::pow" + | ||
"with integer powers via amrex::Math::powi\n") | ||
print("Below are the suggested change:\n") | ||
print(git_diff_output.stdout) | ||
|
||
raise RuntimeError("Changes detected after modification") | ||
|
||
if __name__ == '__main__': | ||
|
||
# Get directory paths | ||
directory_paths = sys.argv[1:] | ||
|
||
# Modify the std::pow -> amrex::Math::powi if needed | ||
for dir_path in directory_paths: | ||
process_content(dir_path) | ||
|
||
# Give suggested change if there are any modifications made | ||
git_diff() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: check powi | ||
|
||
on: | ||
push: | ||
branches: | ||
- development | ||
- main | ||
pull_request: | ||
branches: | ||
- development | ||
|
||
jobs: | ||
check-ifdefs: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Setup Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.10' | ||
|
||
- name: Cache pip | ||
uses: actions/cache@v3 | ||
with: | ||
# this path is specific to Ubuntu | ||
path: ~/.cache/pip | ||
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} | ||
restore-keys: | | ||
${{ runner.os }}-pip- | ||
- name: Run check_powi | ||
run: | | ||
python .github/workflows/check_powi.py . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.