OpenMP works on windows. Add per thread global variables in ReducedModelOptimizer
This commit is contained in:
parent
4071750771
commit
67c2c0c0ac
16
src/main.cpp
16
src/main.cpp
|
|
@ -47,7 +47,11 @@ int main(int argc, char *argv[]) {
|
||||||
ReducedModelOptimizer::xRange beamDimensionsRatio{"bOverh", 0.7, 1.3};
|
ReducedModelOptimizer::xRange beamDimensionsRatio{"bOverh", 0.7, 1.3};
|
||||||
ReducedModelOptimizer::xRange beamE{"E", 0.1, 1.9};
|
ReducedModelOptimizer::xRange beamE{"E", 0.1, 1.9};
|
||||||
// Test set of full patterns
|
// Test set of full patterns
|
||||||
std::string fullPatternsTestSetDirectory = "../TestSet";
|
std::string fullPatternsTestSetDirectory = "TestSet";
|
||||||
|
if (!std::filesystem::exists(std::filesystem::path(fullPatternsTestSetDirectory))) {
|
||||||
|
std::cerr << "Full pattern directory does not exist:" << fullPatternsTestSetDirectory << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
// "/home/iason/Documents/PhD/Research/Approximating shapes with flat "
|
// "/home/iason/Documents/PhD/Research/Approximating shapes with flat "
|
||||||
// "patterns/Pattern_enumerator/Results/1v_0v_2e_1e_1c_6fan/3/Valid";
|
// "patterns/Pattern_enumerator/Results/1v_0v_2e_1e_1c_6fan/3/Valid";
|
||||||
std::vector<std::pair<FlatPattern *, FlatPattern *>> patternPairs;
|
std::vector<std::pair<FlatPattern *, FlatPattern *>> patternPairs;
|
||||||
|
|
@ -74,7 +78,7 @@ int main(int argc, char *argv[]) {
|
||||||
// for (double rangeOffset = 0.15; rangeOffset <= 0.95; rangeOffset += 0.05)
|
// for (double rangeOffset = 0.15; rangeOffset <= 0.95; rangeOffset += 0.05)
|
||||||
// {
|
// {
|
||||||
ReducedModelOptimizer::Settings settings;
|
ReducedModelOptimizer::Settings settings;
|
||||||
for (settings.maxSimulations = 100; settings.maxSimulations < 3500;
|
for (settings.maxSimulations = 100; settings.maxSimulations < 5000;
|
||||||
settings.maxSimulations += 100) {
|
settings.maxSimulations += 100) {
|
||||||
std::string xRangesString = beamWidth.toString() + " " +
|
std::string xRangesString = beamWidth.toString() + " " +
|
||||||
beamDimensionsRatio.toString() + " " +
|
beamDimensionsRatio.toString() + " " +
|
||||||
|
|
@ -96,7 +100,7 @@ int main(int argc, char *argv[]) {
|
||||||
resultsPerPattern(patternPairs.size());
|
resultsPerPattern(patternPairs.size());
|
||||||
auto start = std::chrono::high_resolution_clock::now();
|
auto start = std::chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
//#pragma omp parallel for
|
#pragma omp parallel for
|
||||||
for (int patternPairIndex = 0; patternPairIndex < patternPairs.size();
|
for (int patternPairIndex = 0; patternPairIndex < patternPairs.size();
|
||||||
patternPairIndex++) {
|
patternPairIndex++) {
|
||||||
// const auto filepathString = filepath.string();
|
// const auto filepathString = filepath.string();
|
||||||
|
|
@ -153,7 +157,11 @@ int main(int argc, char *argv[]) {
|
||||||
// resultsPerPattern[patternPairIndex].second.save(saveToPath);
|
// resultsPerPattern[patternPairIndex].second.save(saveToPath);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
csvfile statistics(std::filesystem::path("../OptimizationResults")
|
if (!std::filesystem::exists(std::filesystem::path("OptimizationResults/"))) {
|
||||||
|
std::filesystem::create_directory(std::filesystem::path("OptimizationResults"));
|
||||||
|
}
|
||||||
|
|
||||||
|
csvfile statistics(std::filesystem::path("OptimizationResults")
|
||||||
.append("statistics.csv")
|
.append("statistics.csv")
|
||||||
.string(),
|
.string(),
|
||||||
false);
|
false);
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,22 @@ struct GlobalOptimizationVariables {
|
||||||
std::vector<std::vector<double>> failedSimulationsXRatio;
|
std::vector<std::vector<double>> failedSimulationsXRatio;
|
||||||
int numOfSimulationCrashes{false};
|
int numOfSimulationCrashes{false};
|
||||||
int numberOfFunctionCalls{0};
|
int numberOfFunctionCalls{0};
|
||||||
} global;
|
} ;
|
||||||
|
|
||||||
|
//static GlobalOptimizationVariables global;
|
||||||
|
|
||||||
|
const static int MAX_THREAD = 64;
|
||||||
|
|
||||||
|
struct MY_TLS_ITEM
|
||||||
|
{
|
||||||
|
std::map<int, int> theMap;
|
||||||
|
char padding[64 - sizeof(theMap)];
|
||||||
|
};
|
||||||
|
|
||||||
|
__declspec(align(64)) GlobalOptimizationVariables tls[MAX_THREAD];
|
||||||
|
|
||||||
//#pragma omp threadprivate(global)
|
//#pragma omp threadprivate(global)
|
||||||
|
|
||||||
// struct OptimizationCallback {
|
// struct OptimizationCallback {
|
||||||
// double operator()(const size_t &iterations, const Eigen::VectorXd &x,
|
// double operator()(const size_t &iterations, const Eigen::VectorXd &x,
|
||||||
// const double &fval, Eigen::VectorXd &gradient) const {
|
// const double &fval, Eigen::VectorXd &gradient) const {
|
||||||
|
|
@ -115,6 +129,7 @@ double ReducedModelOptimizer::computeError(
|
||||||
const SimulationResults &reducedPatternResults,
|
const SimulationResults &reducedPatternResults,
|
||||||
const Eigen::MatrixX3d &optimalReducedPatternDisplacements) {
|
const Eigen::MatrixX3d &optimalReducedPatternDisplacements) {
|
||||||
double error = 0;
|
double error = 0;
|
||||||
|
auto& global = tls[omp_get_thread_num()];
|
||||||
for (const auto reducedFullViPair : global.g_reducedToFullViMap) {
|
for (const auto reducedFullViPair : global.g_reducedToFullViMap) {
|
||||||
VertexIndex reducedModelVi = reducedFullViPair.first;
|
VertexIndex reducedModelVi = reducedFullViPair.first;
|
||||||
// const auto pos =
|
// const auto pos =
|
||||||
|
|
@ -141,6 +156,7 @@ double ReducedModelOptimizer::computeError(
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateMesh(long n, const double *x) {
|
void updateMesh(long n, const double *x) {
|
||||||
|
auto& global = tls[omp_get_thread_num()];
|
||||||
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh =
|
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh =
|
||||||
global
|
global
|
||||||
.g_reducedPatternSimulationJob[global.g_simulationScenarioIndices[0]]
|
.g_reducedPatternSimulationJob[global.g_simulationScenarioIndices[0]]
|
||||||
|
|
@ -214,6 +230,7 @@ double ReducedModelOptimizer::objective(double x0, double x1, double x2,
|
||||||
}
|
}
|
||||||
|
|
||||||
double ReducedModelOptimizer::objective(long n, const double *x) {
|
double ReducedModelOptimizer::objective(long n, const double *x) {
|
||||||
|
auto& global = tls[omp_get_thread_num()];
|
||||||
// std::cout.precision(17);
|
// std::cout.precision(17);
|
||||||
|
|
||||||
// for (size_t parameterIndex = 0; parameterIndex < n; parameterIndex++) {
|
// for (size_t parameterIndex = 0; parameterIndex < n; parameterIndex++) {
|
||||||
|
|
@ -356,6 +373,7 @@ void ReducedModelOptimizer::computeMaps(
|
||||||
// std::endl;
|
// std::endl;
|
||||||
|
|
||||||
// Save excluded edges
|
// Save excluded edges
|
||||||
|
auto& global = tls[omp_get_thread_num()];
|
||||||
global.g_reducedPatternExludedEdges.clear();
|
global.g_reducedPatternExludedEdges.clear();
|
||||||
const size_t fanSize = 6;
|
const size_t fanSize = 6;
|
||||||
const size_t reducedBaseTriangleNumberOfEdges = reducedPattern.EN();
|
const size_t reducedBaseTriangleNumberOfEdges = reducedPattern.EN();
|
||||||
|
|
@ -491,6 +509,7 @@ void ReducedModelOptimizer::initializePatterns(
|
||||||
FlatPattern copyReducedPattern;
|
FlatPattern copyReducedPattern;
|
||||||
copyFullPattern.copy(fullPattern);
|
copyFullPattern.copy(fullPattern);
|
||||||
copyReducedPattern.copy(reducedPattern);
|
copyReducedPattern.copy(reducedPattern);
|
||||||
|
auto& global = tls[omp_get_thread_num()];
|
||||||
global.g_optimizeInnerHexagonSize = copyReducedPattern.EN() == 2;
|
global.g_optimizeInnerHexagonSize = copyReducedPattern.EN() == 2;
|
||||||
if (global.g_optimizeInnerHexagonSize) {
|
if (global.g_optimizeInnerHexagonSize) {
|
||||||
const double h = copyReducedPattern.getBaseTriangleHeight();
|
const double h = copyReducedPattern.getBaseTriangleHeight();
|
||||||
|
|
@ -521,6 +540,7 @@ void ReducedModelOptimizer::initializePatterns(
|
||||||
|
|
||||||
void ReducedModelOptimizer::initializeOptimizationParameters(
|
void ReducedModelOptimizer::initializeOptimizationParameters(
|
||||||
const std::shared_ptr<SimulationMesh> &mesh) {
|
const std::shared_ptr<SimulationMesh> &mesh) {
|
||||||
|
auto& global = tls[omp_get_thread_num()];
|
||||||
const int numberOfOptimizationParameters = 3;
|
const int numberOfOptimizationParameters = 3;
|
||||||
global.g_initialParameters.resize(global.g_optimizeInnerHexagonSize
|
global.g_initialParameters.resize(global.g_optimizeInnerHexagonSize
|
||||||
? numberOfOptimizationParameters + 1
|
? numberOfOptimizationParameters + 1
|
||||||
|
|
@ -613,6 +633,7 @@ void ReducedModelOptimizer::computeDesiredReducedModelDisplacements(
|
||||||
ReducedModelOptimizer::Results ReducedModelOptimizer::runOptimization(
|
ReducedModelOptimizer::Results ReducedModelOptimizer::runOptimization(
|
||||||
const Settings &settings,
|
const Settings &settings,
|
||||||
double (*pObjectiveFunction)(long, const double *)) {
|
double (*pObjectiveFunction)(long, const double *)) {
|
||||||
|
auto& global = tls[omp_get_thread_num()];
|
||||||
|
|
||||||
global.gObjectiveValueHistory.clear();
|
global.gObjectiveValueHistory.clear();
|
||||||
|
|
||||||
|
|
@ -981,6 +1002,7 @@ void ReducedModelOptimizer::visualizeResults(
|
||||||
simulator.executeSimulation(pFullPatternSimulationJob);
|
simulator.executeSimulation(pFullPatternSimulationJob);
|
||||||
fullModelResults.registerForDrawing();
|
fullModelResults.registerForDrawing();
|
||||||
fullModelResults.saveDeformedModel();
|
fullModelResults.saveDeformedModel();
|
||||||
|
auto& global = tls[omp_get_thread_num()];
|
||||||
const std::shared_ptr<SimulationJob> &pReducedPatternSimulationJob =
|
const std::shared_ptr<SimulationJob> &pReducedPatternSimulationJob =
|
||||||
global.g_reducedPatternSimulationJob[simulationScenarioIndex];
|
global.g_reducedPatternSimulationJob[simulationScenarioIndex];
|
||||||
SimulationResults reducedModelResults =
|
SimulationResults reducedModelResults =
|
||||||
|
|
@ -1011,6 +1033,7 @@ void ReducedModelOptimizer::visualizeResults(
|
||||||
ReducedModelOptimizer::Results ReducedModelOptimizer::optimize(
|
ReducedModelOptimizer::Results ReducedModelOptimizer::optimize(
|
||||||
const Settings &xRanges,
|
const Settings &xRanges,
|
||||||
const std::vector<SimulationScenario> &simulationScenarios) {
|
const std::vector<SimulationScenario> &simulationScenarios) {
|
||||||
|
auto& global = tls[omp_get_thread_num()];
|
||||||
|
|
||||||
global.g_simulationScenarioIndices = simulationScenarios;
|
global.g_simulationScenarioIndices = simulationScenarios;
|
||||||
if (global.g_simulationScenarioIndices.empty()) {
|
if (global.g_simulationScenarioIndices.empty()) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue