From 47583c6ff358874698c1144c2bf985349cb9a357 Mon Sep 17 00:00:00 2001 From: Jun Tian Date: Thu, 21 Feb 2019 11:05:50 +0800 Subject: [PATCH] [WIP] Add support for Julia (#529) * sync * sync * sync * sync * only support Float32 * add more data types * clang format * clang format * typo fix in vdljl.cc * Delete vdl.jl --- CMakeLists.txt | 5 ++ visualdl/julia/CMakeLists.txt | 5 ++ visualdl/julia/vdljl.cc | 134 ++++++++++++++++++++++++++++++++++ visualdl/logic/sdk.h | 2 + 4 files changed, 146 insertions(+) create mode 100644 visualdl/julia/CMakeLists.txt create mode 100644 visualdl/julia/vdljl.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a0a6c6b..89ebdf88 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ set(THIRD_PARTY_PATH "${CMAKE_BINARY_DIR}/third_party" CACHE STRING option(WITH_DOC "Compile VisualDL with documentation" OFF) option(WITH_TESTING "Compile VisualDL with unit testing" ON) option(WITH_PYTHON3 "Compile VisualDL with Python 3" OFF) +option(WITH_JULIA "Compile VisualDL with Julia" OFF) option(ON_RELEASE "RELEASE mode" ON) @@ -65,6 +66,10 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/visualdl/storage) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/visualdl/logic) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/visualdl/python) +if(WITH_JULIA) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/visualdl/julia) +endif() + # compile test only in release mode if (NOT ON_RELEASE) add_executable(vl_test diff --git a/visualdl/julia/CMakeLists.txt b/visualdl/julia/CMakeLists.txt new file mode 100644 index 00000000..41f0edba --- /dev/null +++ b/visualdl/julia/CMakeLists.txt @@ -0,0 +1,5 @@ +find_package(JlCxx REQUIRED) + +add_library(vdljl SHARED vdljl.cc) +add_dependencies(vdljl im entry tablet sdk storage protobuf eigen3) +target_link_libraries(vdljl PRIVATE JlCxx::cxxwrap_julia entry binary_record im tablet storage sdk protobuf ${OPTIONAL_LINK_FLAGS}) diff --git a/visualdl/julia/vdljl.cc b/visualdl/julia/vdljl.cc new file mode 100644 index 00000000..f2aaccc3 --- /dev/null +++ b/visualdl/julia/vdljl.cc @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include "visualdl/logic/sdk.h" +#include "visualdl/storage/storage.h" +#include "visualdl/storage/tablet.h" + +namespace vs = visualdl; +namespace cp = visualdl::components; + +struct WrapScalar { + template + void operator()(TypeWrapperT&& wrapped) { + typedef typename TypeWrapperT::type WrappedT; + wrapped.template constructor(); + wrapped.method("set_caption", &WrappedT::SetCaption); + wrapped.method("add_record", &WrappedT::AddRecord); + } +}; + +struct WrapHistogram { + template + void operator()(TypeWrapperT&& wrapped) { + typedef typename TypeWrapperT::type WrappedT; + typedef typename WrappedT::data_type T; + wrapped.template constructor(); + wrapped.method("add_record", + [](WrappedT h, int step, jlcxx::ArrayRef data) { + std::vector d(data.begin(), data.end()); + return h.AddRecord(step, d); + }); + } +}; + +JLCXX_MODULE define_julia_module(jlcxx::Module& vdl) { + vdl.add_type("Tablet"); + vdl.add_type("TabletReader"); + vdl.add_type("Storage"); + vdl.add_type("StorageReader"); + + vdl.add_type("LogWriter") + .constructor() + .constructor() + .method("set_mode", &vs::LogWriter::SetMode) + .method("save", &vs::LogWriter::Save) + .method("as_mode", &vs::LogWriter::AsMode) + .method("add_tablet", &vs::LogWriter::AddTablet) + .method("storage", &vs::LogWriter::storage); + + vdl.add_type("LogReader") + .constructor() + .method("set_mode", &vs::LogReader::SetMode) + .method("as_mode", &vs::LogReader::AsMode) + .method("mode", &vs::LogReader::mode) + .method("tablet", &vs::LogReader::tablet) + .method("storage", &vs::LogReader::storage) + .method("alltags", + [](vs::LogReader& lr) { + auto alltags = lr.all_tags(); + return jlcxx::ArrayRef(alltags.data(), + alltags.size()); + }) + .method("tags", [](vs::LogReader& lr, const std::string& x) { + auto tags = lr.tags(x); + return jlcxx::ArrayRef(tags.data(), tags.size()); + }); + + // components + vdl.add_type>>("Scalar") + .apply, + cp::Scalar, + cp::Scalar, + cp::Scalar>(WrapScalar()); + + vdl.add_type>>("Histogram") + .apply, + cp::Histogram, + cp::Histogram, + cp::Histogram>(WrapHistogram()); + + vdl.add_type("Image") + .constructor() + .method("set_caption", &cp::Image::SetCaption) + .method("start_sampling", &cp::Image::StartSampling) + .method("finish_sampling", &cp::Image::FinishSampling) + .method("add_sample", + [](cp::Image& img, + jlcxx::ArrayRef shape, + jlcxx::ArrayRef data) { + std::vector s(shape.begin(), shape.end()); + std::vector d(data.begin(), data.end()); + return img.AddSample(s, d); + }) + .method("index_of_sample_taken", &cp::Image::IndexOfSampleTaken) + .method("set_sample", + [](cp::Image& img, + int index, + jlcxx::ArrayRef shape, + jlcxx::ArrayRef data) { + std::vector s(shape.begin(), shape.end()); + std::vector d(data.begin(), data.end()); + return img.SetSample(index, s, d); + }); + + vdl.add_type("Text") + .constructor() + .method("set_caption", &cp::Text::SetCaption) + .method("add_record", &cp::Text::AddRecord); + + vdl.add_type("Audio") + .constructor() + .method("set_caption", &cp::Audio::SetCaption) + .method("start_sampling", &cp::Audio::StartSampling) + .method("finish_sampling", &cp::Audio::FinishSampling) + .method("add_sample", + [](cp::Audio& img, + jlcxx::ArrayRef shape, + jlcxx::ArrayRef data) { + std::vector s(shape.begin(), shape.end()); + std::vector d(data.begin(), data.end()); + return img.AddSample(s, d); + }) + .method("index_of_sample_taken", &cp::Audio::IndexOfSampleTaken) + .method("set_sample", + [](cp::Audio& img, + int index, + jlcxx::ArrayRef shape, + jlcxx::ArrayRef data) { + std::vector s(shape.begin(), shape.end()); + std::vector d(data.begin(), data.end()); + return img.SetSample(index, s, d); + }); +} diff --git a/visualdl/logic/sdk.h b/visualdl/logic/sdk.h index e4bdf36d..65e6a47d 100644 --- a/visualdl/logic/sdk.h +++ b/visualdl/logic/sdk.h @@ -258,6 +258,8 @@ private: */ template struct Histogram { + typedef T data_type; + Histogram(Tablet tablet, int num_buckets) : writer_(tablet), num_buckets_(num_buckets) { writer_.SetType(Tablet::Type::kHistogram); -- GitLab