Skip to content

Commit 0519fc0

Browse files
committed
[dev] replaced simple-embedding example with fibonacci
1 parent 6613799 commit 0519fc0

File tree

13 files changed

+159
-108
lines changed

13 files changed

+159
-108
lines changed

src/api/MoniLogger.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ namespace MoniLogger
245245
}
246246
}
247247

248+
MoniLoggerExecutionContext create_context(std::string name)
249+
{
250+
return MoniLoggerExecutionContext(name);
251+
}
252+
248253
void initialize_monilogger(std::vector<std::string> python_path,
249254
std::vector<std::string> python_scripts,
250255
std::string interface_module,

src/api/monilogger/include/MoniLogger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace MoniLogger
2626
virtual ~MoniLoggerExecutionContext() = default;
2727
};
2828

29-
MoniLoggerExecutionContext create_context(std::string name) { return MoniLoggerExecutionContext(name); }
29+
MoniLoggerExecutionContext create_context(std::string name);
3030

3131
void register_composite_event(std::string event_name, std::list<std::string> triggering_events);
3232

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Simple Embedding Example
1+
# Fibonacci Example
22

33
This example illustrates how to integrate monilogger in your C++ application.
44

55
In this directory:
66
- Create and activate a Python venv.
77
- Install pybind11: `pip install pybind11`
88
- Install monilogger: `pip install ../..`
9-
- Run the script: `./build-and-run.sh`
10-
9+
- Run the build script: `./build.sh`
10+
- Run the executable, _e.g._ `./build/fibonacci ./src example_moniloggers 10`

src/bindings/cpp/examples/simple-embedding/build-and-run.sh renamed to src/bindings/cpp/examples/fibonacci/build.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@
33
mkdir build
44
cmake -Bbuild -Ssrc
55
cmake --build build
6-
./build/simpleembedding
7-

src/bindings/cpp/examples/simple-embedding/src/CMakeLists.txt renamed to src/bindings/cpp/examples/fibonacci/src/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(CMAKE_CXX_COMPILER /usr/bin/g++)
66
set(CMAKE_CXX_STANDARD 17)
77

88
# PROJECT
9-
project(simpleembedding CXX)
9+
project(fibonacci CXX)
1010

1111
set(CMAKE_VERBOSE_MAKEFILE FALSE)
1212
set(PYBIND11_PYTHON_VERSION 3.8)
@@ -16,9 +16,9 @@ set(pybind11_DIR "${Python_SITELIB}/pybind11/share/cmake/pybind11")
1616
find_package(pybind11 REQUIRED)
1717
include_directories(${pybind11_INCLUDE_DIRS} ${Python_SITELIB}/monilogger/include)
1818

19-
# EXECUTABLE simpleembedding
20-
add_executable(simpleembedding SimpleEmbedding.cc)
21-
target_include_directories(simpleembedding PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
19+
# EXECUTABLE fibonacci
20+
add_executable(fibonacci fibonacci_main.cc fibonacci_embedding.cc fibonacci.cc)
21+
target_include_directories(fibonacci PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
2222
add_library(moniloggerlib SHARED IMPORTED)
2323
set_property(TARGET moniloggerlib PROPERTY IMPORTED_LOCATION ${Python_SITELIB}/monilogger/libmonilogger.so)
24-
target_link_libraries(simpleembedding PUBLIC pybind11::embed moniloggerlib)
24+
target_link_libraries(fibonacci PUBLIC pybind11::embed moniloggerlib)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from monilogger import *
2+
3+
numbers = []
4+
5+
@register("NewIteration")
6+
def log(ctx):
7+
global numbers
8+
numbers.append(ctx.current_number)
9+
print(numbers)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "fibonacci.h"
2+
#include "fibonacci_embedding.h"
3+
4+
int fibonacci(int n)
5+
{
6+
std::shared_ptr<FibonacciExecutionContext> ctx(new FibonacciExecutionContext("FibonacciExecutionContext"));
7+
8+
int u = 0;
9+
int v = 1;
10+
int i = 1;
11+
int t = 0;
12+
13+
ctx->previous_number = &u;
14+
ctx->current_number = &v;
15+
ctx->iteration = &i;
16+
17+
MoniLogger::trigger(0, ctx);
18+
19+
for(i = 2; i <= n; i++)
20+
{
21+
t = u + v;
22+
u = v;
23+
v = t;
24+
MoniLogger::trigger(0, ctx);
25+
}
26+
return v;
27+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef __FIBONACCI_H_
2+
#define __FIBONACCI_H_
3+
4+
int fibonacci(int n);
5+
6+
#endif
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "fibonacci_embedding.h"
2+
3+
PYBIND11_EMBEDDED_MODULE(fibonacci_interface, m) { }
4+
5+
void initialize_embedding(std::vector<std::string> python_path, std::vector<std::string> python_scripts)
6+
{
7+
// Name of the interface module (here, an embedded module declared above) exposing the execution context of the application.
8+
std::string interface_module = "fibonacci_interface";
9+
10+
// Initialization function for the interface module.
11+
std::function<void (pybind11::module_, pybind11::object)> interface_module_initializer =
12+
[](pybind11::module_ interface_module, pybind11::object context_class) {
13+
pybind11::class_<FibonacciExecutionContext, std::shared_ptr<FibonacciExecutionContext>>(interface_module, "FibonacciExecutionContext", context_class)
14+
// Defining the properties exposed by the FibonacciExecutionContext.
15+
.def_property_readonly("current_number", &FibonacciExecutionContext::get_current_number)
16+
.def_property_readonly("previous_number", &FibonacciExecutionContext::get_previous_number)
17+
.def_property_readonly("iteration", &FibonacciExecutionContext::get_iteration)
18+
// Defining legible __str__ and __repr__ functions for FibonacciExecutionContext.
19+
.def("__str__", [](FibonacciExecutionContext &self)
20+
{
21+
std::ostringstream oss;
22+
oss << "[" << self.name << "]\n"
23+
<< " previous number: " << self.previous_number << "\n"
24+
<< " current number: " << self.current_number << "\n"
25+
<< " iteration: " << self.iteration << "\n" ;
26+
return oss.str();
27+
})
28+
.def("__repr__", [](FibonacciExecutionContext &self)
29+
{
30+
std::ostringstream oss;
31+
oss << "[" << self.name << "]\n"
32+
<< " previous number: " << self.previous_number << "\n"
33+
<< " current number: " << self.current_number << "\n"
34+
<< " iteration: " << self.iteration << "\n" ;
35+
return oss.str();
36+
});
37+
};
38+
39+
// Define base execution events emitted by the application, to which moniloggers can register.
40+
MoniLogger::register_base_events({
41+
{"NewIteration", 0}
42+
});
43+
44+
// Bootstrapping monilogger, consisting mainly of starting the Python interpreter, initializing
45+
// the monilogger module, and evaluating the provided scripts.
46+
MoniLogger::initialize_monilogger(python_path, python_scripts, interface_module, interface_module_initializer);
47+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef __FIBONACCI_EMBEDDING_H_
2+
#define __FIBONACCI_EMBEDDING_H_
3+
4+
#include <MoniLogger.h>
5+
6+
struct FibonacciExecutionContext : MoniLogger::MoniLoggerExecutionContext
7+
{
8+
const pybind11::object get_previous_number() const { if (previous_number != nullptr) return pybind11::cast(*previous_number); else return pybind11::cast<pybind11::none>(Py_None); }
9+
const pybind11::object get_current_number() const { if (current_number != nullptr) return pybind11::cast(*current_number); else return pybind11::cast<pybind11::none>(Py_None); }
10+
const pybind11::object get_iteration() const { if (iteration != nullptr) return pybind11::cast(*iteration); else return pybind11::cast<pybind11::none>(Py_None); }
11+
12+
int *previous_number = nullptr;
13+
int *current_number = nullptr;
14+
int *iteration = nullptr;
15+
16+
using MoniLogger::MoniLoggerExecutionContext::MoniLoggerExecutionContext;
17+
};
18+
19+
void initialize_embedding(std::vector<std::string> python_path, std::vector<std::string> python_scripts);
20+
21+
#endif

0 commit comments

Comments
 (0)