Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 21 additions & 5 deletions cpp/pypowsybl-java/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ set(CMAKE_CXX_STANDARD 17)
include(ExternalProject)

if(NOT DEFINED PYPOWSYBL_JAVA_SRC_DIR)
set(PYPOWSYBL_JAVA_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../java/pypowsybl)
set(PYPOWSYBL_JAVA_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../java)
endif()
set(PYPOWSYBL_DATA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../data)
set(PYPOWSYBL_JAVA_BIN_DIR ${CMAKE_CURRENT_BINARY_DIR}/../java)
set(PYPOWSYBL_JAVA_BIN_DIR ${PYPOWSYBL_JAVA_BIN_DIR}/../java PARENT_SCOPE)
set(PYPOWSYBL_JAVA_OLD_LIB pypowsybl-java${CMAKE_SHARED_LIBRARY_SUFFIX})
Expand Down Expand Up @@ -44,8 +45,8 @@ macro(extract_and_install_native_lib jar_file jar_entry library_file)
endif()
endmacro()

cmake_path(SET math-native-jar "${PYPOWSYBL_JAVA_SRC_DIR}/target/dependency/powsybl-math-native.jar")
cmake_path(SET ortools-jar "${PYPOWSYBL_JAVA_SRC_DIR}/target/dependency/ortools-${CMAKE_SYSTEM_NAME}-${ARCH_ID}.jar")
cmake_path(SET math-native-jar "${PYPOWSYBL_JAVA_SRC_DIR}/pypowsybl/target/dependency/powsybl-math-native.jar")
cmake_path(SET ortools-jar "${PYPOWSYBL_JAVA_SRC_DIR}/pypowsybl/target/dependency/ortools-${CMAKE_SYSTEM_NAME}-${ARCH_ID}.jar")

if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
extract_and_install_native_lib(${math-native-jar} natives/windows_${OS_BITS} math.dll)
Expand Down Expand Up @@ -87,13 +88,28 @@ else()
set(NATIVE_IMAGE_GC "serial")
endif()

if(DEFINED ENV{PYPOWSYBL_PGO})
set(NATIVE_IMAGE_EXTRA_DEPENDS "native-image-pgo")
# compile the pgo profiler binary from pypowsybl-pgo java project and then run to get a default.iprof file
# that will be updated in sources
ExternalProject_Add(native-image-pgo
DEPENDS mvn
SOURCE_DIR ${PYPOWSYBL_JAVA_BIN_DIR}
DOWNLOAD_COMMAND ""
PATCH_COMMAND $ENV{JAVA_HOME}/bin/native-image ${NATIVE_IMAGE_BUILD_OPTIONS} --class-path ${PYPOWSYBL_JAVA_SRC_DIR}/pypowsybl-pgo/target/pypowsybl-java-pgo.jar${EXTRA_JARS} --pgo-instrument -march=compatibility --no-fallback --gc=${NATIVE_IMAGE_GC} -H:Name=pypowsybl-java-pgo -H:CLibraryPath=${CMAKE_CURRENT_SOURCE_DIR} com.powsybl.python.pgo.Main
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ${PYPOWSYBL_JAVA_BIN_DIR}/pypowsybl-java-pgo ${PYPOWSYBL_DATA_DIR} COMMAND ${CMAKE_COMMAND} -E copy default.iprof ${CMAKE_CURRENT_SOURCE_DIR}/../..
)
endif()

# as GraalVm does not follow same library naming convention (lib prefix is missing on Linux and MacOS) we need to rename
# it in the install command step
ExternalProject_Add(native-image
DEPENDS mvn
DEPENDS mvn ${NATIVE_IMAGE_EXTRA_DEPENDS}
SOURCE_DIR ${PYPOWSYBL_JAVA_BIN_DIR}
DOWNLOAD_COMMAND ""
PATCH_COMMAND $ENV{JAVA_HOME}/bin/native-image ${NATIVE_IMAGE_BUILD_OPTIONS} --class-path ${PYPOWSYBL_JAVA_SRC_DIR}/target/pypowsybl-java.jar${EXTRA_JARS} -march=compatibility --no-fallback --shared --gc=${NATIVE_IMAGE_GC} -H:Name=pypowsybl-java -H:CLibraryPath=${CMAKE_CURRENT_SOURCE_DIR}
PATCH_COMMAND $ENV{JAVA_HOME}/bin/native-image ${NATIVE_IMAGE_BUILD_OPTIONS} --class-path ${PYPOWSYBL_JAVA_SRC_DIR}/pypowsybl/target/pypowsybl-java.jar${EXTRA_JARS} --pgo=${CMAKE_CURRENT_SOURCE_DIR}/../../default.iprof -march=compatibility --no-fallback --shared --gc=${NATIVE_IMAGE_GC} -H:Name=pypowsybl-java -H:CLibraryPath=${CMAKE_CURRENT_SOURCE_DIR}
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ${PYPOWSYBL_JAVA_BIN_DIR}/${PYPOWSYBL_JAVA_OLD_LIB} ${PYPOWSYBL_JAVA_BIN_DIR}/${PYPOWSYBL_JAVA_LIB} ${NATIVE_IMAGE_INSTALL_EXTRA_COMMAND}
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions default.iprof

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@

<modules>
<module>pypowsybl</module>
<module>pypowsybl-pgo</module>
</modules>
</project>
72 changes: 72 additions & 0 deletions java/pypowsybl-pgo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2024, RTE (http://www.rte-france.com)
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.powsybl</groupId>
<artifactId>pypowsybl-parent</artifactId>
<version>1.10.0-SNAPSHOT</version>
</parent>

<artifactId>pypowsybl-pgo</artifactId>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<id>shade</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<finalName>pypowsybl-java-pgo</finalName>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>standalone</shadedClassifierName>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>pypowsybl</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-math-native</artifactId>
</dependency>
</dependencies>
</project>
58 changes: 58 additions & 0 deletions java/pypowsybl-pgo/src/main/java/com/powsybl/python/pgo/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright (c) 2025, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.python.pgo;

import com.powsybl.commons.io.WorkingDirectory;
import com.powsybl.iidm.network.Network;
import com.powsybl.loadflow.LoadFlow;
import com.powsybl.loadflow.LoadFlowParameters;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public final class Main {

private Main() {
}

private static void runConverters(Path dataDir) throws IOException {
System.out.println("Run converters");
for (int i = 0; i < 10; i++) {
System.out.println(" iteration " + i);
Network network = Network.read(dataDir.resolve("CGMES_v2_4_15_RealGridTestConfiguration_v2.zip"));
try (var tempDir = new WorkingDirectory(Files.createTempDirectory(null), "pp-tmp", false)) {
network.write("XIIDM", null, tempDir.toPath().resolve("CGMES_v2_4_15_RealGridTestConfiguration_v2.xiidm"));
network.write("JIIDM", null, tempDir.toPath().resolve("CGMES_v2_4_15_RealGridTestConfiguration_v2.jiidm"));
network.write("BIIDM", null, tempDir.toPath().resolve("CGMES_v2_4_15_RealGridTestConfiguration_v2.biidm"));
}
}
}

private static void runLoadFlow(Path dataDir) {
System.out.println("Run load flow");
Network network = Network.read(dataDir.resolve("CGMES_v2_4_15_RealGridTestConfiguration_v2.zip"));
LoadFlow.Runner runner = LoadFlow.find("OpenLoadFlow");
for (int i = 0; i < 50; i++) {
System.out.println(" iteration " + i);
LoadFlowParameters parameters = new LoadFlowParameters()
.setVoltageInitMode(LoadFlowParameters.VoltageInitMode.DC_VALUES)
.setUseReactiveLimits(false);
runner.run(network, parameters);
}
}

public static void main(String[] args) throws IOException {
Path dataDir = Path.of(args[0]);
runConverters(dataDir);
runLoadFlow(dataDir);
}
}
Loading