Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
88cecaa
Improve the way to find Enzyme. (#48)
nkoukpaizan Feb 10, 2025
f33d600
Rename namespace ModelLib -> GridKit. (#56)
pelesh Feb 11, 2025
bc3f969
Aligning the code with contributing guidelines (#42)
shakedregev Feb 17, 2025
1c09d11
Phasor dynamics components modeling framework (#55)
pelesh Feb 17, 2025
77f61bd
Add missing forward declaration (#58)
alexander-novo Feb 19, 2025
7bd2825
Reorganize model evaluator implementations (#61)
pelesh Feb 21, 2025
ee4c1f4
Add dependency-tracking variable data type. (#63)
pelesh Feb 27, 2025
c3d86e6
Additional Enzyme examples and use cases (e.g DistributedGenerator) (…
nkoukpaizan Feb 27, 2025
ddd483e
First pass on clang format file
alexander-novo Dec 2, 2024
5ed73f9
Fixed a few issues
alexander-novo Dec 2, 2024
68e0706
Put templates on their own lines
alexander-novo Dec 2, 2024
52e86f1
Organize .clang-format file into sections
alexander-novo Dec 2, 2024
bc77c0c
Indent namespace contents
alexander-novo Dec 2, 2024
746cbc5
Properly regroup
alexander-novo Dec 2, 2024
ad6b9ec
Indent only inner namespace contents
alexander-novo Dec 2, 2024
ffef34f
More small formatting to align with current code
alexander-novo Dec 2, 2024
8850e95
Alignment
alexander-novo Dec 2, 2024
bda9f76
Add clang-format as developer dependency
alexander-novo Dec 3, 2024
4163462
Fix readme heading
alexander-novo Dec 3, 2024
2bc96e9
Better comment handling
alexander-novo Feb 9, 2025
523961d
Move binary operators in broken lines to the next line
alexander-novo Feb 14, 2025
dd29741
Remove extra comment columns
alexander-novo Feb 14, 2025
8f01021
Remove empty lines
alexander-novo Feb 14, 2025
fbede92
Add auto-formatting section to CONTRIBUTING.md
alexander-novo Feb 17, 2025
86cad04
Added usage details for auto-formatting
alexander-novo Feb 17, 2025
dfc0b6b
Pre-commit setup from ExaGO. To test.
nkoukpaizan Feb 28, 2025
395a5b4
Apply pre-commmit fixes
nkoukpaizan Feb 28, 2025
58392db
Revert "Apply pre-commmit fixes"
nkoukpaizan Feb 28, 2025
cc89871
Remove line limit from .clang-format.
nkoukpaizan Feb 28, 2025
323b399
Merge branch 'add-clang-format' into nicholson/clang-format-pre-commi…
nkoukpaizan Feb 28, 2025
be80729
Apply pre-commmit fixes
nkoukpaizan Feb 28, 2025
ac0bca7
Revert "Apply pre-commmit fixes"
nkoukpaizan Feb 28, 2025
7c03313
Remove some pre-commit steps (e.g., cmake-format) for now.
nkoukpaizan Feb 28, 2025
e2a7d73
Apply pre-commmit fixes
nkoukpaizan Feb 28, 2025
845269b
Apply pre-commmit fixes
nkoukpaizan Feb 28, 2025
9944745
Revert "Apply pre-commmit fixes"
nkoukpaizan Feb 28, 2025
729dd12
Revert "Apply pre-commmit fixes"
nkoukpaizan Feb 28, 2025
5a15140
Disable auto commit from pre-commit bot and fix action files.
nkoukpaizan Feb 28, 2025
27765d1
Revert "Remove line limit from .clang-format."
nkoukpaizan Feb 28, 2025
4ca9137
Re-instate auto commit from pre-commit to test.
nkoukpaizan Feb 28, 2025
cf97464
Apply pre-commmit fixes
nkoukpaizan Feb 28, 2025
d9f2599
Bus --> BusBase in Branch and Load forward declarations.
nkoukpaizan Feb 28, 2025
1ab9b7b
Apply pre-commmit fixes
nkoukpaizan Feb 28, 2025
329b904
Change pre-commit commiter to default. Should prevent disabling of ch…
nkoukpaizan Feb 28, 2025
fec7666
Purposeful modification to non-compliant code to trigger pre-commit.
nkoukpaizan Feb 28, 2025
1ae8914
Apply pre-commmit fixes
nkoukpaizan Feb 28, 2025
7a873e5
Revert "Change pre-commit commiter to default. Should prevent disabli…
nkoukpaizan Feb 28, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/pre_commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: gridkit-bot pre-commit

# Won't run on develop/main directly
on: [pull_request]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
- uses: actions/setup-python@v5.4.0
- uses: pre-commit/action@v3.0.1
- uses: EndBug/add-and-commit@v9.1.4
# Only need to try and commit if the action failed
if: failure()
with:
fetch: false
committer_name: GitHub Actions
committer_email: actions@github.com
message: Apply pre-commmit fixes

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
.vscode/
*.DS_Store
build/
*.DS_Store
*.DS_Store
12 changes: 12 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v19.1.7
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pick a version that we expect people to install with Enzyme, like v15 or so.

hooks:
- id: clang-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-toml
- id: forbid-new-submodules
- id: end-of-file-fixer
- id: check-yaml
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious - will these check- hooks actually do anything? For instance, if I make a new submodule and then submit a PR will it fail CI or?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, the check will work as you describe. You can see what it's trying yo do in CI (e.g. https://github.com/ORNL/GridKit/actions/runs/13594004211/job/38006645346#step:4:154). These are mostly legacy checks. We can remove anything we don't need.

1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ add_subdirectory(src)
# Create examples and tests
enable_testing()
add_subdirectory(examples)
add_subdirectory(tests)

export(EXPORT gridkit-targets FILE ${CMAKE_CURRENT_BINARY_DIR}/GridKitTargets.cmake)

Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,4 @@ To help enforce the above code style guidelines, we use autoformatting provided

VS Code users can use `clang-format` to automatically format their code by installing the [C/C++ Extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) and using the `Format Document` command in the command palette with the file to be formatted open. As well, you can enabled the `editor.formatOnSave` option to auto-format any file when you save it.

Other users can use the [`git clang-format`](https://clang.llvm.org/docs/ClangFormat.html#git-integration) subcommand installed along with `clang-format` to auto-format all staged files.
Other users can use the [`git clang-format`](https://clang.llvm.org/docs/ClangFormat.html#git-integration) subcommand installed along with `clang-format` to auto-format all staged files.
110 changes: 47 additions & 63 deletions cmake/FindEnzyme.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -64,78 +64,44 @@ User may set:

Author(s):
- Asher Mancinelli <ashermancinelli@gmail.com>
- Nicholson Koukpaizan <koukpaizannk@ornl.gov>

]]

if(NOT ${CMAKE_C_COMPILER_ID} STREQUAL "Clang" OR NOT ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
message(FATAL_ERROR "Enzyme may only be enabled if building with Clang")
endif()
find_package(Enzyme REQUIRED CONFIG
PATHS
${ENZYME_DIR}
${ENZYME_DIR}/lib/cmake/Enzyme)
message(STATUS "Enzyme configuration found: ${Enzyme_CONFIG}")

find_library(ENZYME_LLVM_PLUGIN_LIBRARY
NAMES
LLVMEnzyme-19.so
LLVMEnzyme-18.so
LLVMEnzyme-17.so
LLVMEnzyme-16.so
LLVMEnzyme-15.so
LLVMEnzyme-14.so
LLVMEnzyme-13.so
LLVMEnzyme-12.so
LLVMEnzyme-11.so
LLVMEnzyme-10.so
LLVMEnzyme-9.so
LLVMEnzyme-${Enzyme_LLVM_VERSION_MAJOR}.so
LLVMEnzyme-${Enzyme_LLVM_VERSION_MAJOR}.dylib
LLVMEnzyme-${Enzyme_LLVM_VERSION_MAJOR}.dll
PATHS
${ENZYME_DIR} $ENV{ENZYME_DIR}
ENV LD_LIBRARY_PATH ENV DYLD_LIBRARY_PATH
${ENZYME_DIR}
ENV LD_LIBRARY_PATH
ENV DYLD_LIBRARY_PATH
PATH_SUFFIXES
lib64 lib)
lib64 lib
REQUIRED)
message(STATUS "Enzyme LLVM plugin library: ${ENZYME_LLVM_PLUGIN_LIBRARY}")

find_library(ENZYME_CLANG_PLUGIN_LIBRARY
NAMES
ClangEnzyme-19.so
ClangEnzyme-18.so
ClangEnzyme-17.so
ClangEnzyme-16.so
ClangEnzyme-15.so
ClangEnzyme-14.so
ClangEnzyme-13.so
ClangEnzyme-12.so
ClangEnzyme-11.so
ClangEnzyme-10.so
ClangEnzyme-9.so
PATHS
${ENZYME_DIR} $ENV{ENZYME_DIR}
ENV LD_LIBRARY_PATH ENV DYLD_LIBRARY_PATH
PATH_SUFFIXES
lib64 lib)

find_program(GRIDKIT_LLVM_LINK llvm-link)
find_program(GRIDKIT_OPT opt)

if("${GRIDKIT_LLVM_LINK}" STREQUAL "GRIDKIT_LLVM_LINK-NOTFOUND")
message(FATAL_ERROR "Could not find llvm-link! Will not be able to build Enzyme targets.")
endif()

if("${GRIDKIT_OPT}" STREQUAL "GRIDKIT_OPT-NOTFOUND")
message(FATAL_ERROR "Could not find opt! Will not be able to build Enzyme targets.")
endif()

message(STATUS "${ENZYME_CLANG_PLUGIN_LIBRARY};${ENZYME_LLVM_PLUGIN_LIBRARY}")
if((NOT ${ENZYME_LLVM_PLUGIN_LIBRARY}) OR (NOT ${ENZYME_CLANG_PLUGIN_LIBRARY}))
set(ENZYME_CLANG_PLUGIN_LIBRARY CACHE FILEPATH "Path to Enzyme Clang plugin library")
set(ENZYME_LLVM_PLUGIN_LIBRARY CACHE FILEPATH "Path to Enzyme LLVM plugin library")
message(STATUS "Found Enzyme clang plugin: ${ENZYME_CLANG_PLUGIN_LIBRARY}")
message(STATUS "Found Enzyme LLVM plugin: ${ENZYME_LLVM_PLUGIN_LIBRARY}")
get_filename_component(ENZYME_LIBRARY_DIR ${ENZYME_CLANG_PLUGIN_LIBRARY} DIRECTORY CACHE "Enzyme library directory")
else()
message(FATAL_ERROR "Enzyme library not found!"
" Set ENZYME_DIR to Enzyme's installation prefix.")
endif()
find_program(GRIDKIT_LLVM_LINK llvm-link
PATHS ${Enzyme_LLVM_BINARY_DIR}
REQUIRED)
message(STATUS "llvm-link: ${GRIDKIT_LLVM_LINK}")

find_program(GRIDKIT_OPT opt
PATHS ${Enzyme_LLVM_BINARY_DIR}
REQUIRED)
message(STATUS "opt: ${GRIDKIT_OPT}")

macro(enzyme_add_executable)
set(options)
set(oneValueArgs NAME)
set(multiValueArgs SOURCES LINK_LIBRARIES)
set(multiValueArgs SOURCES LINK_LIBRARIES INCLUDE_DIRECTORIES)
cmake_parse_arguments(enzyme_add_executable "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN})

Expand All @@ -145,14 +111,32 @@ macro(enzyme_add_executable)
set(PHASE5 "${CMAKE_CURRENT_BINARY_DIR}/${enzyme_add_executable_NAME}")

set(OBJS "")
set(includes "${enzyme_add_executable_INCLUDE_DIRECTORIES}")

foreach(lib ${enzyme_add_executable_LINK_LIBRARIES})
get_target_property(include ${lib} INCLUDE_DIRECTORIES)
set(includes "${includes}" ${include})

get_target_property(libsource ${lib} SOURCES)
string(FIND "${libsource}" "TARGET" found)
if(NOT(${found} EQUAL -1))
list(APPEND LINKER_FLAGS "-Wl,${libsource}")
endif()
endforeach()

foreach(dir ${includes})
if(EXISTS ${dir})
list(APPEND INCLUDE_COMPILER_LIST "-I${dir}")
endif()
endforeach()

foreach(SRC ${enzyme_add_executable_SOURCES})
set(PHASE0 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC}")
set(PHASE1 "${CMAKE_CURRENT_BINARY_DIR}/${enzyme_add_executable_NAME}_${SRC}_compile.o")
add_custom_command(
DEPENDS ${PHASE0}
DEPENDS ${PHASE0}
OUTPUT ${PHASE1}
COMMAND ${CMAKE_CXX_COMPILER} -flto -c ${PHASE0} -O2 -fno-vectorize -ffast-math -fno-unroll-loops -o ${PHASE1}
COMMAND ${CMAKE_CXX_COMPILER} -flto -c ${PHASE0} ${INCLUDE_COMPILER_LIST} -O2 -fno-vectorize -ffast-math -fno-unroll-loops -o ${PHASE1}
COMMENT "Compiling ${SRC} to object file for target ${enzyme_add_executable_NAME}"
)
set(OBJS "${OBJS} ${PHASE1}")
Expand Down Expand Up @@ -182,9 +166,9 @@ macro(enzyme_add_executable)
)

add_custom_command(
DEPENDS ${PHASE4}
DEPENDS ${PHASE4} ${enzyme_add_executable_LINK_LIBRARIES}
OUTPUT ${PHASE5}
COMMAND ${CMAKE_CXX_COMPILER} ${PHASE4} -o ${PHASE5}
COMMAND ${CMAKE_CXX_COMPILER} ${LINKER_FLAGS} ${PHASE4} -o ${PHASE5}
)

add_custom_target(
Expand Down
55 changes: 26 additions & 29 deletions examples/AdjointSensitivity/AdjointSensitivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@
*
*/


#include <iostream>
#include <iomanip>
#include <iostream>

#include <Model/PowerFlow/Bus/BusSlack.hpp>
#include <Model/PowerFlow/Generator4/Generator4.hpp>
#include <SystemModel.hpp>
#include <Model/PowerFlow/SystemModel.hpp>
#include <Solver/Dynamic/Ida.hpp>
#include <Utilities/Testing.hpp>

Expand All @@ -79,7 +78,7 @@
*/
int main()
{
using namespace ModelLib;
using namespace GridKit;
using namespace AnalysisManager::Sundials;
using namespace AnalysisManager;
using namespace GridKit::Testing;
Expand Down Expand Up @@ -112,7 +111,6 @@ int main()
idas->configureQuadrature();
idas->initializeQuadrature();


idas->runSimulation(0.1, 2);
idas->saveInitialCondition();

Expand All @@ -138,30 +136,29 @@ int main()
std::cout << "\n\nCost of computing objective function:\n\n";
idas->printFinalStats();

const double g1 = Q[0];
const double g1 = Q[0];
const double eps = 2e-3;

// Compute gradient of the objective function numerically
std::vector<double> dGdp(model->size_opt());
std::vector<double> dGdp(model->sizeParams());

for (unsigned i=0; i<model->size_opt(); ++i)
for (unsigned i = 0; i < model->sizeParams(); ++i)
{
model->param()[i] += eps;
idas->getSavedInitialCondition();
idas->initializeSimulation(t_init);
idas->initializeQuadrature();
idas->runSimulationQuadrature(t_final,100);

std::cout << "\n\nCost of computing derivative with respect to parameter "
<< i << ":\n\n";
idas->printFinalStats();
double g2 = Q[0];

// restore parameter to original value
model->param()[i] -= eps;

// Evaluate dG/dp numerically
dGdp[i] = (g2 - g1)/eps;
model->param()[i] += eps;
idas->getSavedInitialCondition();
idas->initializeSimulation(t_init);
idas->initializeQuadrature();
idas->runSimulationQuadrature(t_final, 100);

std::cout << "\n\nCost of computing derivative with respect to parameter " << i << ":\n\n";
idas->printFinalStats();
double g2 = Q[0];

// restore parameter to original value
model->param()[i] -= eps;

// Evaluate dG/dp numerically
dGdp[i] = (g2 - g1) / eps;
}

// Compute gradient of the objective function using adjoint method
Expand All @@ -185,15 +182,15 @@ int main()
int retval = 0;
std::cout << "\n\nComparison of numerical and adjoint results:\n\n";
double* neg_dGdp = idas->getAdjointIntegral();
for (unsigned i=0; i<model->size_opt(); ++i)
for (unsigned i = 0; i < model->sizeParams(); ++i)
{
std::cout << "dG/dp" << i << " (numerical) = " << dGdp[i] << "\n";
std::cout << "dG/dp" << i << " (numerical) = " << dGdp[i] << "\n";
std::cout << "dG/dp" << i << " (adjoint) = " << -neg_dGdp[i] << "\n\n";
if(!isEqual(dGdp[i], -neg_dGdp[i], 10*eps))
--retval;
if (!isEqual(dGdp[i], -neg_dGdp[i], 10 * eps))
--retval;
}

if(retval < 0)
if (retval < 0)
{
std::cout << "The two results differ beyond solver tolerance!\n";
}
Expand Down
2 changes: 0 additions & 2 deletions examples/AdjointSensitivity/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,3 @@ target_link_libraries(adjoint GRIDKIT::bus GRIDKIT::generator4 GRIDKIT::solvers_
install(TARGETS adjoint DESTINATION bin)

add_test(NAME AdjointSens COMMAND $<TARGET_FILE:adjoint> )


2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
#

add_subdirectory(MatPowerTesting)
add_subdirectory(SparseTest)
add_subdirectory(LinearAlgebra)
add_subdirectory(DistributedGeneratorTest)

if(TARGET SUNDIALS::kinsol)
Expand Down
6 changes: 3 additions & 3 deletions examples/DistributedGeneratorTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


add_executable(dgtest DGTest.cpp)
target_link_libraries(dgtest GRIDKIT::powerelec_disgen
GRIDKIT::powerelec_mircoline
GRIDKIT::powerelec_microload
target_link_libraries(dgtest GRIDKIT::power_elec_disgen
GRIDKIT::power_elec_microline
GRIDKIT::power_elec_microload
GRIDKIT::solvers_dyn)

add_test(NAME DistributedGeneratorTest COMMAND $<TARGET_FILE:dgtest>)
Expand Down
Loading
Loading