Commit aa9d8ea9 authored by steffen.schotthoefer's avatar steffen.schotthoefer
Browse files

solver runs, with mem leak (prpbably)

parent 53c3f600
......@@ -8,11 +8,17 @@ class MLOptimizer : public OptimizerBase
public:
MLOptimizer( Config* settings );
inline ~MLOptimizer() {}
inline ~MLOptimizer();
void Solve( Vector& lambda, Vector& u, VectorVector& moments, unsigned idx_cell = 0 ) override;
private:
double* callNetwork(const unsigned input_size, double* nn_input);
void finalize_python();
void initialize_python();
void init_numpy();
// void initialize_Network();
};
#endif // MLOPTIMIZER_H
......@@ -2,52 +2,60 @@
import tensorflow as tf
import numpy as np
import tensorflow.keras.backend as K
# make the network a gobal variable here
import os
#model = tf.python.keras.engine.sequential.Sequential()
#tf.keras.Model()
def initialize_network():
# Load model
model = tf.keras.models.load_model('saved_model/my_model')
# Check its architecture
model.summary()
print("network initizalized")
return model
# make the network a gobal variable here
model = initialize_network()
def call_network(input):
#print(tf.__version__)
inputNP = np.asarray([input])
model = tf.keras.models.load_model('saved_model/my_model')
#print(os.getcwd())
#model = tf.keras.models.load_model('saved_model/my_model')
#model.summary()
print("input")
print(inputNP)
# print("input")
# print(inputNP)
predictions = model.predict(inputNP)
print("network called")
print(predictions)
print(predictions[0])
print("python out")
#print(predictions)
#print(predictions[0])
#print("python out")
print(predictions[0].shape)
print(np.zeros(4).shape)
#print(predictions[0].shape)
#print(np.zeros(4).shape)
output = np.zeros(4)
for i in range(0, 3):
for i in range(0, 4):
output[i] = predictions[0, i]
#print(output)
return output
def initialize_network():
# Load model
model = tf.keras.models.load_model('saved_model/my_model')
# Check its architecture
model.summary()
print("network initizalized")
return 0
def main():
input = [0,1,2,3]
initialize_network()
call_network(input)
#initialize_network()
print(call_network(input))
return 0
......
......@@ -20,6 +20,24 @@
#include <stdio.h>
using namespace std;
// ----
void init_numpy() { _import_array(); }
void callNetwork();
void initialize_python() {
// Initialize the Python Interpreter
std::string pyPath = RTSN_PYTHON_PATH;
if( !Py_IsInitialized() ) {
Py_InitializeEx( 0 );
if( !Py_IsInitialized() ) {
ErrorMessages::Error( "Python init failed!", CURRENT_FUNCTION );
}
PyRun_SimpleString( ( "import sys\nsys.path.append('" + pyPath + "')" ).c_str() );
}
}
void finalize_python() { Py_Finalize(); }
int main( int argc, char** argv ) {
......@@ -41,84 +59,61 @@ int main( int argc, char** argv ) {
solver->Save();
MPI_Finalize();
// // Initialize the Python Interpreter
// std::string pyPath = RTSN_PYTHON_PATH;
//
// if( !Py_IsInitialized() ) {
// Py_InitializeEx( 0 );
// if( !Py_IsInitialized() ) {
// ErrorMessages::Error( "Python init failed!", CURRENT_FUNCTION );
// }
// PyRun_SimpleString( ( "import sys\nsys.path.append('" + pyPath + "')" ).c_str() );
// }
//
// PyObject *pArgs, *pReturn, *pModule, *pFunc;
// PyArrayObject* np_ret;
//
// std::string moduleName = "callNN";
// pModule = PyImport_ImportModule( moduleName.c_str() );
// if( !pModule ) {
// PyErr_Print();
// ErrorMessages::Error( "'" + moduleName + "' can not be imported!", CURRENT_FUNCTION );
// }
//
// // pFunc = PyObject_GetAttrString( pModule, "initialize_network" );
// // if( !pFunc || !PyCallable_Check( pFunc ) ) {
// // PyErr_Print();
// // Py_DecRef( pModule );
// // Py_DecRef( pFunc );
// // ErrorMessages::Error( "'initialize_network' is null or not callable!", CURRENT_FUNCTION );
// // }
// //
// // // pArgs = PyTuple_New(); // No arguments ==> need empty tuple as arguments
// // PyObject_CallObject( pFunc, NULL ); // No return value
//
// pFunc = PyObject_GetAttrString( pModule, "call_network" );
// if( !pFunc || !PyCallable_Check( pFunc ) ) {
// PyErr_Print();
// Py_DecRef( pModule );
// Py_DecRef( pFunc );
// ErrorMessages::Error( "'call_network' is null or not callable!", CURRENT_FUNCTION );
// }
//
// // -----------------
// // std::string imageName = "testimageName";
// // std::string SU2Filename = "SU2Filenametest";
// //
// // auto image = PyUnicode_FromString( imageName.c_str() );
//
// double solutionOneCell[4] = { 0.0, 0.1, 0.2, 0.3 };
//
// const long int dims[1] = { 4 };
//
// import_array();
// PyObject* inputArray = PyArray_SimpleNewFromData( 1, dims, NPY_DOUBLE, (void*)solutionOneCell );
//
// pArgs = PyTuple_New( 1 );
// PyTuple_SetItem( pArgs, 0, reinterpret_cast<PyObject*>( inputArray ) );
//
// // -------------
// pReturn = PyObject_CallObject( pFunc, pArgs ); // PyObject
//
// np_ret = reinterpret_cast<PyArrayObject*>( pReturn ); // Cast from PyObject to PyArrayObject
//
// // size_t m{ static_cast<size_t>( PyArray_SHAPE( np_ret )[0] ) };
// // size_t n{ static_cast<size_t>( PyArray_SHAPE( np_ret )[1] ) };
// double* c_out = reinterpret_cast<double*>( PyArray_DATA( np_ret ) );
//
// for( unsigned i = 0; i < 4; i++ ) {
// std::cout << c_out[i] << std::endl;
// }
//
// // Finalizing
// Py_DecRef( pFunc );
// Py_DecRef( pModule );
// Py_DECREF( np_ret );
//
// // Finish the Python Interpreter
//
// Py_Finalize();
// initialize_python();
// callNetwork();
// finalize_python();
//
return 0;
}
void callNetwork() {
init_numpy();
PyObject *pArgs, *pReturn, *pModule, *pFunc;
PyArrayObject* np_ret;
std::string moduleName = "callNN";
pModule = PyImport_ImportModule( moduleName.c_str() );
if( !pModule ) {
PyErr_Print();
ErrorMessages::Error( "'" + moduleName + "' can not be imported!", CURRENT_FUNCTION );
}
pFunc = PyObject_GetAttrString( pModule, "call_network" );
if( !pFunc || !PyCallable_Check( pFunc ) ) {
PyErr_Print();
Py_DecRef( pModule );
Py_DecRef( pFunc );
ErrorMessages::Error( "'call_network' is null or not callable!", CURRENT_FUNCTION );
}
double solutionOneCell[4] = { 0.0, 0.1, 0.2, 0.3 };
const long int dims[1] = { 4 };
PyObject* inputArray = PyArray_SimpleNewFromData( 1, dims, NPY_DOUBLE, (void*)solutionOneCell );
pArgs = PyTuple_New( 1 );
PyTuple_SetItem( pArgs, 0, reinterpret_cast<PyObject*>( inputArray ) );
// -------------
printf( "check\n" );
pReturn = PyObject_CallObject( pFunc, pArgs ); // PyObject
printf( "check\n" );
np_ret = reinterpret_cast<PyArrayObject*>( pReturn ); // Cast from PyObject to PyArrayObject
printf( "check\n" );
double* c_out = reinterpret_cast<double*>( PyArray_DATA( np_ret ) );
printf( "check\n" );
for( unsigned i = 0; i < 4; i++ ) {
std::cout << c_out[i] << ", ";
}
cout << std::endl;
// Finalizing
Py_DecRef( pFunc );
Py_DecRef( pModule );
Py_DECREF( np_ret );
}
......@@ -10,12 +10,43 @@
MLOptimizer::MLOptimizer( Config* settings ) : OptimizerBase( settings ) {
// test
// Test
initialize_python();
// initialize_Network();
}
MLOptimizer::~MLOptimizer() { finalize_python(); }
void MLOptimizer::Solve( Vector& lambda, Vector& u, VectorVector& moments, unsigned idx_cell ) {
// Convert Vector to array
const unsigned input_size = u.size();
double nn_input[input_size];
double* nn_output = new double[input_size];
for( unsigned idx_sys = 0; idx_sys < u.size(); idx_sys++ ) {
nn_input[idx_sys] = u[idx_sys];
// std::cout << nn_input[idx_sys] << ", ";
}
// initialize_python();
nn_output = callNetwork( input_size, nn_input );
for( unsigned i = 0; i < input_size; i++ ) {
// std::cout << nn_output[i] << ", ";
lambda[i] = nn_output[i];
}
// std::cout << std::endl;
// delete[] nn_output; CHECK IF THIS PRODUCES A MEM LEAK!
}
void MLOptimizer::init_numpy() {
_import_array(); // Check, if this gives a mem Leak!
}
void MLOptimizer::initialize_python() {
// Initialize the Python Interpreter
std ::string pyPath = RTSN_PYTHON_PATH;
std::string pyPath = RTSN_PYTHON_PATH;
if( !Py_IsInitialized() ) {
Py_InitializeEx( 0 );
......@@ -24,15 +55,15 @@ void MLOptimizer::Solve( Vector& lambda, Vector& u, VectorVector& moments, unsig
}
PyRun_SimpleString( ( "import sys\nsys.path.append('" + pyPath + "')" ).c_str() );
}
init_numpy();
}
import_array(); // Dont know what it does, but otherwise we get a segfault DOUBLE CHECK
if( PyErr_Occurred() ) std::cout << "error!!!\n";
void MLOptimizer::finalize_python() { Py_Finalize(); }
// Declare Objects
PyObject *pArgs, *pReturn, *pModule, *pFunc;
PyArrayObject* np_ret;
/*
void MLOptimizer::initialize_Network() {
PyObject *pArgs, *pModule, *pFunc;
// Import module
std::string moduleName = "callNN";
pModule = PyImport_ImportModule( moduleName.c_str() );
if( !pModule ) {
......@@ -40,65 +71,55 @@ void MLOptimizer::Solve( Vector& lambda, Vector& u, VectorVector& moments, unsig
ErrorMessages::Error( "'" + moduleName + "' can not be imported!", CURRENT_FUNCTION );
}
// Call the Neural Network as a function
pFunc = PyObject_GetAttrString( pModule, "call_network" );
pFunc = PyObject_GetAttrString( pModule, "initialize_network" );
if( !pFunc || !PyCallable_Check( pFunc ) ) {
PyErr_Print();
Py_DecRef( pModule );
Py_DecRef( pFunc );
ErrorMessages::Error( "'call_network' is null or not callable!", CURRENT_FUNCTION );
ErrorMessages::Error( "'initialize_network' is null or not callable!", CURRENT_FUNCTION );
}
// REPLACE THAT BY A PROPER CAST
double cell_solution[4];
const long int dims[1] = { 4 };
pArgs = PyTuple_New( 0 ); // No arguments, so empty tuple
PyObject_CallObject( pFunc, pArgs ); // PyObject
} */
for( unsigned idx_sys = 0; idx_sys < u.size(); idx_sys++ ) {
cell_solution[idx_sys] = u[idx_sys];
std::cout << "," << u[idx_sys] << " ";
double* MLOptimizer::callNetwork( const unsigned input_size, double* nn_input ) {
PyObject *pArgs, *pReturn, *pModule, *pFunc;
PyArrayObject* np_ret;
std::string moduleName = "callNN";
pModule = PyImport_ImportModule( moduleName.c_str() );
if( !pModule ) {
PyErr_Print();
ErrorMessages::Error( "'" + moduleName + "' can not be imported!", CURRENT_FUNCTION );
}
std::cout << std::endl;
// double solutionOneCell[4] = { 0.0, 0.1, 0.2, 0.3 };
//
// const long int dims[1] = { 4 };
std::cout << "check\n";
pFunc = PyObject_GetAttrString( pModule, "call_network" );
if( !pFunc || !PyCallable_Check( pFunc ) ) {
PyErr_Print();
Py_DecRef( pModule );
Py_DecRef( pFunc );
ErrorMessages::Error( "'call_network' is null or not callable!", CURRENT_FUNCTION );
}
std::cout << "check1\n";
const long int dims[1] = { input_size };
// Cast solution array to a Python object
PyObject* inputArray = PyArray_SimpleNewFromData( 1, dims, NPY_DOUBLE, (void*)cell_solution );
std::cout << "check2\n";
PyObject* inputArray = PyArray_SimpleNewFromData( 1, dims, NPY_DOUBLE, (void*)nn_input );
// Pack arguments for function call
pArgs = PyTuple_New( 1 );
PyTuple_SetItem( pArgs, 0, reinterpret_cast<PyObject*>( inputArray ) );
// Call the Function
// Call Python function
pReturn = PyObject_CallObject( pFunc, pArgs ); // PyObject
// Prepare output ArrayObject
np_ret = reinterpret_cast<PyArrayObject*>( pReturn ); // Cast from PyObject to PyArrayObject
// Cast ArrayObject to c-array
double* tempArrayOutput;
std::cout << "check\n";
tempArrayOutput = reinterpret_cast<double*>( PyArray_DATA( np_ret ) );
std::cout << "check\n";
// Write Back to Vector ==> REPLACE BY PROPER CAST
// for( unsigned idx_sys = 0; idx_sys < u.size(); idx_sys++ ) {
// lambda[idx_sys] = tempArrayOutput[idx_sys];
//}
double* c_out = reinterpret_cast<double*>( PyArray_DATA( np_ret ) ); // Get Output
// Finalizing
Py_DecRef( pFunc );
Py_DecRef( pModule );
Py_DECREF( np_ret );
// Finish the Python Interpreter
Py_Finalize();
delete[] cell_solution;
delete[] tempArrayOutput;
return c_out;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment