File tree Expand file tree Collapse file tree 8 files changed +680
-15
lines changed Expand file tree Collapse file tree 8 files changed +680
-15
lines changed Original file line number Diff line number Diff line change @@ -6,9 +6,11 @@ find_package(Threads REQUIRED)
66
77set (CMAKE_CXX_STANDARD 17)
88
9+
910#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=thread")
1011#set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=thread")
1112
1213add_executable (example main.cpp)
1314target_include_directories (example PRIVATE ../../include $ENV{HOME} /libtensorflow2/include )
15+ target_link_libraries (example "${TENSORFLOW_LIB} " )
1416target_link_libraries (example "${TENSORFLOW_LIB} " Threads::Threads)
Original file line number Diff line number Diff line change 1+ cmake_minimum_required (VERSION 3.10)
2+ project (example)
3+
4+ find_library (TENSORFLOW_LIB tensorflow HINT $ENV{HOME} /libtensorflow2/lib)
5+ find_package (Threads REQUIRED)
6+
7+ #set(CMAKE_CXX_STANDARD 20)
8+ set (CMAKE_CXX_STANDARD 17)
9+
10+
11+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=address" )
12+ set (CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -lasan" )
13+
14+ add_executable (example main.cpp)
15+ target_include_directories (example PRIVATE ../../include $ENV{HOME} /libtensorflow2/include )
16+ target_link_libraries (example "${TENSORFLOW_LIB} " )
Original file line number Diff line number Diff line change 1+ #include < cmath>
2+ #include < vector>
3+ #include < cassert>
4+
5+ #include " cppflow/cppflow.h"
6+
7+ template <typename T>
8+ void test (const std::initializer_list<T>& input) {
9+ std::vector<T> values = input;
10+ cppflow::tensor t (input);
11+ auto span_result = t.get_data <T>();
12+
13+ assert (span_result.size () == values.size ());
14+ for (size_t i = 0 ; i < span_result.size (); i++) {
15+ assert (std::abs (values[i]/span_result[i]-1 .0f ) < 1e-6 );
16+ }
17+ }
18+
19+ int main () {
20+ test ({10 , 20 , 30 });
21+ test ({10.0 , 20.1 , 30.3 });
22+ return 0 ;
23+ }
Original file line number Diff line number Diff line change 66#define CPPFLOW2_CONTEXT_H
77
88#include < memory>
9+ #include < stdexcept>
910#include < utility>
1011
1112#include < tensorflow/c/c_api.h>
Original file line number Diff line number Diff line change 1+ #pragma once
2+ #include < functional>
3+
4+ namespace cppflow {
5+
6+ class defer {
7+ public:
8+ typedef std::function<void ()> Func;
9+
10+ explicit defer (const Func& func) : _func(func) {}
11+ ~defer () {
12+ // maybe throw exception
13+ _func ();
14+ }
15+
16+ defer (const defer&) = delete ;
17+ defer (defer&&) = delete ;
18+ defer& operator =(const defer&) = delete ;
19+ void * operator new (size_t ) = delete ;
20+ void operator delete (void *) = delete ;
21+
22+ private:
23+ Func _func;
24+ };
25+
26+ } // namespace cppflow
Original file line number Diff line number Diff line change 1313
1414#include " context.h"
1515#include " tensor.h"
16+ #include " defer.h"
1617
1718namespace cppflow {
1819
@@ -84,6 +85,12 @@ namespace cppflow {
8485
8586 std::vector<TF_Output> inp_ops (inputs.size ());
8687 std::vector<TF_Tensor*> inp_val (inputs.size ());
88+
89+ defer d ([&inp_val]{
90+ for (auto * tf_tensor : inp_val) {
91+ TF_DeleteTensor (tf_tensor);
92+ }
93+ });
8794 for (int i=0 ; i<inputs.size (); i++) {
8895
8996 // Operations
You can’t perform that action at this time.
0 commit comments