Skip to content

Commit

Permalink
#3: Implement and bind block data model class
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrepebay committed Apr 28, 2023
1 parent 0d4e943 commit 5322a35
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 5 deletions.
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,44 @@ nanobind_build_library(nanobind SHARED)

# Compile an extension library
add_library(my_ext MODULE ${CMAKE_CURRENT_SOURCE_DIR}/circle.cc)
add_library(visualizer MODULE ${CMAKE_CURRENT_SOURCE_DIR}/visualizer.cc)
add_library(block MODULE ${CMAKE_CURRENT_SOURCE_DIR}/block.cc)

# .. and link it against the nanobind parts
message(STATUS "vtk libraries: ${VTK_LIBRARIES}")
target_link_libraries(my_ext PUBLIC ${VTK_LIBRARIES})
target_link_libraries(my_ext PRIVATE nanobind)
target_link_libraries(visualizer PUBLIC ${VTK_LIBRARIES})
target_link_libraries(visualizer PRIVATE nanobind)
target_link_libraries(block PUBLIC ${VTK_LIBRARIES})
target_link_libraries(block PRIVATE nanobind)

# .. enable size optimizations
nanobind_opt_size(my_ext)
nanobind_opt_size(visualizer)
nanobind_opt_size(block)

# .. enable link time optimization
nanobind_lto(my_ext)
nanobind_lto(visualizer)
nanobind_lto(block)

# .. disable the stack protector
nanobind_disable_stack_protector(my_ext)
nanobind_disable_stack_protector(visualizer)
nanobind_disable_stack_protector(block)

# .. set the Python extension suffix
nanobind_extension(my_ext)
nanobind_extension(visualizer)
nanobind_extension(block)

# .. set important compilation flags
nanobind_compile_options(my_ext)
nanobind_compile_options(visualizer)
nanobind_compile_options(block)

# .. set important linker flags
nanobind_link_options(my_ext)
nanobind_link_options(visualizer)
nanobind_link_options(block)
54 changes: 54 additions & 0 deletions block.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "block.h"

namespace vt { namespace tv {

Block::Block(uint64_t b_id,
uint64_t h_id,
double size,
std::unordered_set<uint64_t> o_ids)
: index(b_id)
, home_id(h_id)
, size(size)
, attached_object_ids(o_ids)
{}

uint64_t Block::detach_object_id(uint64_t o_id) {
try {
this->attached_object_ids.erase(o_id);
}
catch(const nb::type_error e) {
std::cerr << e.what() << '\n';
}
return this->attached_object_ids.size();
}

void Block::attach_object_id(uint64_t o_id) {
this->attached_object_ids.insert(o_id);
}


std::string Block::to_string() const {
std::string out;
out += "Block id: " + std::to_string(this->get_id())
+ ", home id: " + std::to_string(this->get_home_id())
+ ", size: " + std::to_string(this->get_size())
+ ", object ids: {";
for (const auto &elem : this->get_attached_object_ids()) {
out += std::to_string(elem) + " ";
}
out += "}";
return out;
}

NB_MODULE(block, m) {
nb::class_<Block>(m, "Block")
.def(nb::init<uint64_t, uint64_t, double, std::unordered_set<uint64_t>>())
.def("get_id", &Block::get_id)
.def("get_home_id", &Block::get_home_id)
.def("get_size", &Block::get_size)
.def("detach_object_id", &Block::detach_object_id)
.def("attach_object_id", &Block::attach_object_id)
.def("to_string", &Block::to_string);
}

}} /* end namesapce vt::tv */
62 changes: 62 additions & 0 deletions block.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#if !defined INCLUDED_VT_TV_API_BLOCK_H
#define INCLUDED_VT_TV_API_BLOCK_H

#include <nanobind/nanobind.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/list.h>
#include <nanobind/stl/map.h>
#include <nanobind/stl/unordered_set.h>
#include <nanobind/operators.h>

#include <string>
#include <iostream>
#include <list>
#include <map>
#include <format>
#include <unordered_set>

namespace nb = nanobind;

namespace vt { namespace tv {

/**
* A class representing a memory block with footprint and home.
*/
struct Block
{
private:
uint64_t index;
uint64_t home_id;
double size = 0;
std::unordered_set<uint64_t> attached_object_ids;
public:
uint64_t get_id() const { return this->index; };
uint64_t get_home_id() const { return this->home_id; };
double get_size() const { return this->size; };
std::unordered_set<uint64_t> get_attached_object_ids() const { return this->attached_object_ids; };

uint64_t detach_object_id(uint64_t);
void attach_object_id(uint64_t);

std::string to_string() const;

friend std::ostream & operator << (std::ostream &out, const Block &b);

Block(uint64_t, uint64_t, double, std::unordered_set<uint64_t>);
~Block() = default;
};

std::ostream & operator << (std::ostream &out, const Block &b)
{
out << "Block id: " << b.get_id() << ", home id: " << b.get_home_id()
<< ", size: " << b.get_size() << ", object ids: ";
for (const auto &elem : b.get_attached_object_ids()) {
out << elem << " ";
}
out << std::endl;
return out;
}

}} /* end namesapce vt::tv */

#endif /*INCLUDED_VT_TV_API_BLOCK_H*/
5 changes: 1 addition & 4 deletions circle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ void Circle::render() {
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);

vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);

renderWindow->SetWindowName("Circle");
renderWindow->Render();

Expand All @@ -54,4 +51,4 @@ NB_MODULE(my_ext, m) {
.def("render", &Circle::render);
m.def("add", &add);
m.attr("the_answer") = 42;
}
}
3 changes: 2 additions & 1 deletion circle.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <nanobind/stl/string.h>

#include <string>
#include <cstdio>

namespace nb = nanobind;

Expand All @@ -41,4 +42,4 @@ struct Circle {
void render();
};

#endif /*INCLUDED_VT_TV_RENDER_CIRCLE_H*/
#endif /*INCLUDED_VT_TV_RENDER_CIRCLE_H*/
14 changes: 14 additions & 0 deletions lib_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import my_ext
import block

circle1 = my_ext.Circle("Red")
circle1.what_color()
circle1.render() # exports test.png

b = block.Block(2,3,5.4,set({1,2,3,4}))
print(b.to_string())
print(b.attach_object_id(42))
# trying to attach an object id that is not an int actually throws a python TypeError
print(b.to_string())
b.detach_object_id(42)
print(b.to_string())
42 changes: 42 additions & 0 deletions visualizer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "visualizer.h"

Visualizer::Visualizer(
std::list<std::string> qoi_request,
bool continuous_object_qoi,
std::list<Phase> phases,
std::list<std::string> grid_size,
double object_jitter,
std::string output_dir,
std::string output_file_stem,
std::map<std::string, std::string> distributions,
std::map<std::string, std::string> statistics,
double resolution
)
: qoi_request(qoi_request)
, continuous_object_qoi(continuous_object_qoi)
, phases(phases)
, grid_size(grid_size)
, object_jitter(object_jitter)
, output_dir(output_dir)
, output_file_stem(output_file_stem)
, distributions(distributions)
, statistics(statistics)
, resolution(resolution)
{}


NB_MODULE(visualizer, m) {
nb::class_<Phase>(m, "phase")
.def(nb::init<>());
nb::class_<Visualizer>(m, "Visualizer")
.def(nb::init<std::list<std::string>,
bool,
std::list<Phase>,
std::list<std::string>,
double,
std::string,
std::string,
std::map<std::string, std::string>,
std::map<std::string, std::string>,
double>());
}
76 changes: 76 additions & 0 deletions visualizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#if !defined INCLUDED_VT_TV_VISUALIZER_H
#define INCLUDED_VT_TV_VISUALIZER_H

// #include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkCamera.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkDoubleArray.h>
#include <vtkPointData.h>
#include <vtkGlyphSource2D.h>
#include <vtkGlyph2D.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkColorTransferFunction.h>
// #include <vtkScalarBarActor.h>
#include <vtkTextProperty.h>
#include <vtkArrayCalculator.h>
#include <vtkThresholdPoints.h>
#include <vtkWindowToImageFilter.h>
#include <vtkPNGWriter.h>
#include <vtkRegularPolygonSource.h>

#include <Python.h>

#include <nanobind/nanobind.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/list.h>
#include <nanobind/stl/map.h>

#include <string>
#include <iostream>
#include <list>
#include <map>

namespace nb = nanobind;

struct Phase {
uint64_t n_objects = 0;
};

struct Visualizer {
private:
std::list<std::string> qoi_request;
bool continuous_object_qoi;
std::list<Phase> phases;
std::list<std::string> grid_size;
double object_jitter = 0;
std::string output_dir = ".";
std::string output_file_stem = "LBAF_out";
std::map<std::string, std::string> distributions;
std::map<std::string, std::string> statistics;
double resolution = 1;

public:
Visualizer(
std::list<std::string>,
bool,
std::list<Phase>,
std::list<std::string>,
double,
std::string,
std::string,
std::map<std::string, std::string>,
std::map<std::string, std::string>,
double
);

};

#endif /*INCLUDED_VT_TV_VISUALIZER_H*/

0 comments on commit 5322a35

Please sign in to comment.