-
Notifications
You must be signed in to change notification settings - Fork 5
Enzyme Jacobian for PhasorDynamics::Load within GridKit #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
76cb30a
Separated out Enzyme macros from FindEnzyme and added a EnzymeAddLibr…
nkoukpaizan 853f3c0
Enzyme wrapper within GridKit.
nkoukpaizan d2c3f8a
Fixes to rebase.
nkoukpaizan b3810bd
More interesting vector residual for Enzyme examples.
nkoukpaizan af9cef7
Fix some warnings.
nkoukpaizan 7878bc6
Address one more warning.
nkoukpaizan 58d7ffa
Apply pre-commmit fixes
nkoukpaizan f342e8c
Empty commit to trigger CI after pre-commit fixes.
nkoukpaizan 75aa4d5
Successful use of target_compiile_options to pass enzyme-auto-sparsit…
nkoukpaizan b21eccc
More flags needed to get the correct answer to machine precision in E…
nkoukpaizan 4ea98c3
EnzymeAddLibrary not currently needed.
nkoukpaizan 1679497
Apply pre-commmit fixes
nkoukpaizan 97afa84
Empty commit to trigger CI after pre-commit fixes.
nkoukpaizan 6c724b3
Updated documentation in EnzymeAddLibrary.
nkoukpaizan c32524f
Fix warnings previously hidden by manual builds through enzyme_add_ex…
nkoukpaizan 0c479c3
Use GridKit::Testing::isEqual in Enzyme examples.
nkoukpaizan 88b4153
Fixes to rebase.
nkoukpaizan 03b165d
Apply pre-commmit fixes
nkoukpaizan c5c5203
Empty commit to trigger CI after pre-commit fixes.
nkoukpaizan 9261d2c
residual_wrapper can now be placed within GridKit::Enzyme namespace.
nkoukpaizan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,123 @@ | ||
# | ||
#[[ | ||
|
||
Macro to manually compile with Enzyme | ||
|
||
Author(s): | ||
- Asher Mancinelli <ashermancinelli@gmail.com> | ||
- Nicholson Koukpaizan <koukpaizannk@ornl.gov> | ||
|
||
]] | ||
|
||
macro(enzyme_build_object) | ||
set(options) | ||
set(oneValueArgs NAME) | ||
set(multiValueArgs SOURCES LINK_LIBRARIES INCLUDE_DIRECTORIES) | ||
cmake_parse_arguments(enzyme_build_object "${options}" "${oneValueArgs}" | ||
"${multiValueArgs}" ${ARGN}) | ||
|
||
set(PHASE2 "${CMAKE_CURRENT_BINARY_DIR}/${enzyme_build_object_NAME}.bc") | ||
set(PHASE3 "${CMAKE_CURRENT_BINARY_DIR}/${enzyme_build_object_NAME}_enzyme.ll") | ||
set(PHASE4 "${CMAKE_CURRENT_BINARY_DIR}/${enzyme_build_object_NAME}_opt.ll") | ||
set(PHASE5 "${CMAKE_CURRENT_BINARY_DIR}/${enzyme_build_object_NAME}") | ||
|
||
set(OBJS "") | ||
set(includes "${enzyme_build_object_INCLUDE_DIRECTORIES}") | ||
|
||
foreach(lib ${enzyme_build_object_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_build_object_SOURCES}) | ||
set(PHASE0 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC}") | ||
set(PHASE1 "${CMAKE_CURRENT_BINARY_DIR}/${enzyme_build_object_NAME}_${SRC}_compile.o") | ||
add_custom_command( | ||
DEPENDS ${PHASE0} | ||
OUTPUT ${PHASE1} | ||
COMMAND ${CMAKE_CXX_COMPILER} -flto -c ${PHASE0} ${INCLUDE_COMPILER_LIST} -O2 -fno-vectorize -ffast-math -fno-unroll-loops -fpass-plugin=${ENZYME_CLANG_PLUGIN_LIBRARY} -Xclang -load -Xclang ${ENZYME_CLANG_PLUGIN_LIBRARY} -mllvm -enable-load-pre=0 -mllvm -enzyme-auto-sparsity=1 -o ${PHASE1} | ||
COMMENT "Compiling ${SRC} to object file for target ${enzyme_build_object_NAME}" | ||
) | ||
set(OBJS "${OBJS} ${PHASE1}") | ||
endforeach() | ||
|
||
cmake_language(EVAL CODE " | ||
add_custom_command( | ||
DEPENDS ${OBJS} | ||
OUTPUT ${PHASE2} | ||
COMMAND ${GRIDKIT_LLVM_LINK} ${OBJS} -o ${PHASE2} | ||
COMMENT \"Linking object files to LLVM bytecode for target ${enzyme_build_object_NAME}\" | ||
) | ||
") | ||
|
||
add_custom_command( | ||
DEPENDS ${PHASE2} | ||
OUTPUT ${PHASE3} | ||
COMMAND ${GRIDKIT_OPT} ${PHASE2} -load-pass-plugin=${ENZYME_LLVM_PLUGIN_LIBRARY} -passes=enzyme -o ${PHASE3} -S | ||
COMMENT "Running Enzyme opt pass on target ${enzyme_build_object_NAME}" | ||
) | ||
|
||
add_custom_command( | ||
DEPENDS ${PHASE3} | ||
OUTPUT ${PHASE4} | ||
COMMAND ${GRIDKIT_OPT} ${PHASE3} -O2 -o ${PHASE4} -S | ||
COMMENT "Running remaining opt passes on target ${enzyme_build_object_NAME}" | ||
) | ||
|
||
add_custom_command( | ||
DEPENDS ${PHASE4} | ||
OUTPUT ${PHASE5} | ||
COMMAND ${CMAKE_CXX_COMPILER} -c ${PHASE4} -o ${PHASE5} | ||
COMMENT "Generating optimized object file for target ${enzyme_build_object_NAME}" | ||
) | ||
endmacro() | ||
|
||
macro(enzyme_add_executable) | ||
set(options) | ||
set(oneValueArgs NAME) | ||
set(multiValueArgs SOURCES LINK_LIBRARIES INCLUDE_DIRECTORIES) | ||
cmake_parse_arguments(enzyme_add_executable "${options}" "${oneValueArgs}" | ||
"${multiValueArgs}" ${ARGN}) | ||
|
||
enzyme_build_object( | ||
NAME "${enzyme_add_executable_NAME}.o" | ||
SOURCES ${enzyme_add_executable_SOURCES} | ||
LINK_LIBRARIES ${enzyme_add_executable_LINK_LIBRARIES} | ||
INCLUDE_DIRECTORIES ${enzyme_add_executable_INCLUDE_DIRECTORIES} | ||
) | ||
|
||
add_executable("${enzyme_add_executable_NAME}" "${enzyme_add_executable_NAME}.o") | ||
set_target_properties("${enzyme_add_executable_NAME}" PROPERTIES LINKER_LANGUAGE CXX) | ||
target_link_libraries("${enzyme_add_executable_NAME}" ${enzyme_add_executable_LINK_LIBRARIES}) | ||
endmacro() | ||
|
||
macro(enzyme_add_library) | ||
set(options) | ||
set(oneValueArgs NAME) | ||
set(multiValueArgs SOURCES LINK_LIBRARIES INCLUDE_DIRECTORIES) | ||
cmake_parse_arguments(enzyme_add_library "${options}" "${oneValueArgs}" | ||
"${multiValueArgs}" ${ARGN}) | ||
|
||
enzyme_build_object( | ||
NAME "${enzyme_add_library_NAME}.o" | ||
SOURCES ${enzyme_add_library_SOURCES} | ||
LINK_LIBRARIES ${enzyme_add_library_LINK_LIBRARIES} | ||
INCLUDE_DIRECTORIES ${enzyme_add_library_INCLUDE_DIRECTORIES} | ||
) | ||
|
||
add_library("${enzyme_add_library_NAME}" "${enzyme_add_library_NAME}.o") | ||
set_target_properties("${enzyme_add_library_NAME}" PROPERTIES LINKER_LANGUAGE CXX) | ||
target_link_libraries("${enzyme_add_library_NAME}" ${enzyme_add_library_LINK_LIBRARIES}) | ||
endmacro() |
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,6 +1,4 @@ | ||
enzyme_add_executable( | ||
NAME EnzymeLibScalarCheck | ||
SOURCES EnzymeScalar.cpp ScalarModel.cpp | ||
) | ||
add_executable(EnzymeLibScalarCheck EnzymeScalar.cpp ScalarModel.cpp) | ||
target_link_libraries(EnzymeLibScalarCheck ClangEnzymeFlags GRIDKIT::Utilities) | ||
|
||
add_test(NAME "EnzymeLibScalarCheck" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/EnzymeLibScalarCheck) |
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,7 +1,4 @@ | ||
enzyme_add_executable( | ||
NAME EnzymeLibVectorCheck | ||
SOURCES EnzymeVector.cpp VectorModel.cpp | ||
LINK_LIBRARIES GRIDKIT::DenseMatrix | ||
) | ||
add_executable(EnzymeLibVectorCheck EnzymeVector.cpp VectorModel.cpp) | ||
target_link_libraries(EnzymeLibVectorCheck ClangEnzymeFlags GRIDKIT::DenseMatrix GRIDKIT::Utilities) | ||
|
||
add_test(NAME "EnzymeLibVectorCheck" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/EnzymeLibVectorCheck) |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
|
||
#include "EnzymeWrapper.hpp" | ||
|
||
VectorModel::VectorModel(int n) | ||
VectorModel::VectorModel(size_t n) | ||
: x_(n), | ||
f_(n), | ||
df_dx_(n, n) | ||
|
@@ -18,15 +18,19 @@ inline double VectorModel::square_scalar(double x) | |
|
||
void VectorModel::square(std::vector<double>& x, std::vector<double>& y) | ||
{ | ||
for (int idx = 0; idx < x.size(); ++idx) | ||
for (size_t idx = 0; idx < x.size(); ++idx) | ||
{ | ||
y[idx] = this->square_scalar(x[idx]); | ||
y[idx] = 0.0; | ||
for (size_t idy = 0; idy <= idx; idy++) | ||
{ | ||
y[idx] += this->square_scalar(x[idy]); | ||
} | ||
} | ||
} | ||
|
||
void VectorModel::setVariable(std::vector<double> x) | ||
{ | ||
for (int idx = 0; idx < x.size(); ++idx) | ||
for (size_t idx = 0; idx < x.size(); ++idx) | ||
{ | ||
x_[idx] = x[idx]; | ||
} | ||
|
@@ -39,13 +43,13 @@ void VectorModel::evalResidual() | |
|
||
void VectorModel::evalJacobian() | ||
{ | ||
const int n = x_.size(); | ||
const size_t n = x_.size(); | ||
std::vector<double> v(n); | ||
VectorModel d_vector_model(n); | ||
for (int idy = 0; idy < n; ++idy) | ||
for (size_t idy = 0; idy < n; ++idy) | ||
{ | ||
// Elementary vector for Jacobian-vector product | ||
for (int idx = 0; idx < n; ++idx) | ||
for (size_t idx = 0; idx < n; ++idx) | ||
{ | ||
v[idx] = 0.0; | ||
} | ||
Comment on lines
+52
to
55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are we not able to use |
||
|
@@ -60,7 +64,7 @@ void VectorModel::evalJacobian() | |
&d_vector_model); | ||
|
||
// Store result | ||
for (int idx = 0; idx < n; ++idx) | ||
for (size_t idx = 0; idx < n; ++idx) | ||
{ | ||
df_dx_.setValue(idx, idy, d_res[idx]); | ||
} | ||
|
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
enzyme_add_executable( | ||
NAME EnzymePowerElectronicsCheck | ||
SOURCES main.cpp | ||
LINK_LIBRARIES GRIDKIT::DenseMatrix GRIDKIT::power_elec_disgen | ||
) | ||
add_executable(EnzymePowerElectronicsCheck main.cpp) | ||
target_compile_options(EnzymePowerElectronicsCheck PUBLIC -fno-vectorize -ffast-math -fno-unroll-loops) | ||
target_link_libraries(EnzymePowerElectronicsCheck ClangEnzymeFlags GRIDKIT::DenseMatrix GRIDKIT::power_elec_disgen GRIDKIT::Utilities) | ||
|
||
add_test(NAME "EnzymePowerElectronicsCheck" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/EnzymePowerElectronicsCheck) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure what is the purpose of this definition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using it to guard the Jacobian test. See
GridKit/tests/UnitTests/PhasorDynamics/LoadTests.hpp
Line 103 in aa9e7d6