From a312312e7806942ea9535a5e96e5ec04e0d11f62 Mon Sep 17 00:00:00 2001 From: Sid Jain Date: Wed, 14 Oct 2020 19:00:25 +0530 Subject: [PATCH] windows build and managed clr --- .../MosesManagedDLL/ManagedMoses.cpp | 116 ++++++++++++ .../MosesManagedDLL/MosesManagedDLL.vcxproj | 170 +++++++++++++++++ .../MosesManagedDLL.vcxproj.filters | 22 +++ .../moses2wrapper/ManagedMoses.cpp | 36 ++++ .../moses2wrapper/moses2wrapper.vcxproj | 176 ++++++++++++++++++ moses2/Main.cpp | 6 +- moses2/Moses2Wrapper.cpp | 20 ++ moses2/Moses2Wrapper.h | 16 ++ moses2/TranslationTask.cpp | 11 ++ moses2/TranslationTask.h | 1 + moses2/server/Server.cpp | 4 +- 11 files changed, 573 insertions(+), 5 deletions(-) create mode 100644 contrib/other-builds/MosesManagedDLL/ManagedMoses.cpp create mode 100644 contrib/other-builds/MosesManagedDLL/MosesManagedDLL.vcxproj create mode 100644 contrib/other-builds/MosesManagedDLL/MosesManagedDLL.vcxproj.filters create mode 100644 contrib/other-builds/moses2wrapper/ManagedMoses.cpp create mode 100644 contrib/other-builds/moses2wrapper/moses2wrapper.vcxproj create mode 100644 moses2/Moses2Wrapper.cpp create mode 100644 moses2/Moses2Wrapper.h diff --git a/contrib/other-builds/MosesManagedDLL/ManagedMoses.cpp b/contrib/other-builds/MosesManagedDLL/ManagedMoses.cpp new file mode 100644 index 000000000..0ba728360 --- /dev/null +++ b/contrib/other-builds/MosesManagedDLL/ManagedMoses.cpp @@ -0,0 +1,116 @@ +#define NOMINMAX // Windows max macro collides with std:: +#include +#include +#include +#include +#include "Moses2Wrapper.h" + +using namespace System; +using namespace msclr::interop; + + +namespace Moses { + public ref class Moses2Wrapper + { + public: + Moses2Wrapper(String^ filePath) { + const std::string standardString = marshal_as(filePath); + m_pWrapper = new Moses2::Moses2Wrapper(standardString); + } + ~Moses2Wrapper() { this->!Moses2Wrapper(); } + String^ Translate(String^ input) { + const std::string standardString = marshal_as(input); + std::string output = m_pWrapper->Translate(standardString); + //Console::WriteLine(output); + String^ str = gcnew String(output.c_str()); + return str; + } + + protected: + !Moses2Wrapper() { delete m_pWrapper; m_pWrapper = nullptr; } + private: + Moses2::Moses2Wrapper *m_pWrapper; + }; +} +/* +public class ManagedMoses +{ + Moses2::Moses2Wrapper *m_Instance; +public: + ManagedMoses(String^ filepath) { + const std::string standardString = marshal_as(filepath); + m_Instance = new Moses2::Moses2Wrapper(standardString); + + } + String^ Translate(String^ input){ + const std::string standardString = marshal_as(input); + std::string output = m_Instance->Translate(standardString); + //Console::WriteLine(output); + String^ str = gcnew String(output.c_str()); + return str; + } +}; + + +/* +#include +#ifndef WIN32 +#define WIN32 +#endif +#include +#include "legacy/Parameter.h" +#include "System.h" + +using namespace System; +using namespace msclr::interop; + +// A wrapper around Faiss that lets you build indexes +// Right now just proof-of-concept code to makes sure it all works from C#, +// eventually may want to rework the interface, or possibly look at extending +// FaissSharp to support the windows dll + +namespace Moses { + + + + public ref class Parameter + { + public: + Parameter() { m_pWrapper = new Moses2::Parameter(); } + ~Parameter() { this->!Parameter(); } + bool LoadParams(String^ filePath) { + const std::string standardString = marshal_as(filePath); + auto flag = m_pWrapper->LoadParam(standardString); + return bool(flag); + } + Parameter* GetInstance() + { + return m_pWrapper; + } + protected: + !Parameter() { delete m_pWrapper; m_pWrapper = nullptr; } + private: + Moses2::Parameter* m_pWrapper; + }; + + + public ref class System { + public: + System(const Parameter^ paramsArg) { + new Moses2::System(paramsArg->GetInstance()); + } + ~System() { this->!System(); } + protected: + !System() { delete m_sWrapper; m_sWrapper = nullptr; } + private: + Moses2::System* m_sWrapper; + Moses2::Parameter* paramArgs; + }; + + + + + +} + +*/ \ No newline at end of file diff --git a/contrib/other-builds/MosesManagedDLL/MosesManagedDLL.vcxproj b/contrib/other-builds/MosesManagedDLL/MosesManagedDLL.vcxproj new file mode 100644 index 000000000..804bcc011 --- /dev/null +++ b/contrib/other-builds/MosesManagedDLL/MosesManagedDLL.vcxproj @@ -0,0 +1,170 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {5fb67144-27c9-4993-a084-e2097ffcc4fe} + MosesManagedDLL + 10.0 + + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + DynamicLibrary + true + v142 + Unicode + true + + + DynamicLibrary + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;MOSESMANAGEDDLL_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + true + true + WIN32;NDEBUG;MOSESMANAGEDDLL_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + true + true + false + + + + + Level3 + true + _DEBUG;MOSESMANAGEDDLL_EXPORTS;NO_COMPACT_TABLES;HAVE_CMPHXXX;MAX_NUM_FACTORS=4;KENLM_MAX_ORDER=6;_USE_MATH_DEFINES;NOMINMAX;WITH_THREADS;NO_PIPES;_CONSOLE;_LIB;%(PreprocessorDefinitions) + false + NotUsing + pch.h + F:\boost_1_72_0;D:\moses-mstranslator;D:\xmlrpc-c-1.51.06\include;D:\moses-mstranslator\moses2 + true + MultiThreadedDebugDLL + + + Windows + true + false + D:\zlib-1.2.8\contrib\vstudio\vc11\x64\ZlibDllRelease;D:\xmlrpc-c-1.51.06\bin\Debug-Static-x64;C:\Users\sija\Downloads\2.0.2\bonitao-cmph-e5f83da\Debug;D:\zlib_1_2_8_msvc2015_64\msvc2015_64\lib\zlib;D:\mman-win32-master\x64\Release;F:\boost_1_72_0\lib64-msvc-14.2;%(AdditionalLibraryDirectories) + libxmlrpc.lib;libxmlrpc_server_abyss.lib;libxmlrpc_server.lib;libxmlrpc_abyss.lib;libxmlrpc_util.lib;libxmlrpc_xmlparse.lib;libxmlrpc_xmltok.lib;libxmlrpc++.lib;zlibwapi.lib;mman.lib;cmph.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;MOSESMANAGEDDLL_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + true + true + false + + + + + + + + {b4304e97-d37f-4022-bd03-841a4faee398} + + + + + + \ No newline at end of file diff --git a/contrib/other-builds/MosesManagedDLL/MosesManagedDLL.vcxproj.filters b/contrib/other-builds/MosesManagedDLL/MosesManagedDLL.vcxproj.filters new file mode 100644 index 000000000..8bd68daf0 --- /dev/null +++ b/contrib/other-builds/MosesManagedDLL/MosesManagedDLL.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/contrib/other-builds/moses2wrapper/ManagedMoses.cpp b/contrib/other-builds/moses2wrapper/ManagedMoses.cpp new file mode 100644 index 000000000..adba8845f --- /dev/null +++ b/contrib/other-builds/moses2wrapper/ManagedMoses.cpp @@ -0,0 +1,36 @@ +#include +#include "legacy\Parameter.h" +#include "System.h" + +using namespace System; +using namespace msclr::interop; + +// A wrapper around Faiss that lets you build indexes +// Right now just proof-of-concept code to makes sure it all works from C#, +// eventually may want to rework the interface, or possibly look at extending +// FaissSharp to support the windows dll + +namespace Moses { + + public ref class System + { + + public: + + }; + + public ref class Parameter + { + public: + Parameter() { m_pWrapper = new Moses2::Parameter(); } + ~Parameter() { this->!Parameter(); } + + + + private: + // Review: I'm not using e.g. unique_ptr here because I don't know the lifetime + // semantics behind ref classes. + Moses2::Parameter* m_pWrapper; + }; + +} diff --git a/contrib/other-builds/moses2wrapper/moses2wrapper.vcxproj b/contrib/other-builds/moses2wrapper/moses2wrapper.vcxproj new file mode 100644 index 000000000..aa75c8dab --- /dev/null +++ b/contrib/other-builds/moses2wrapper/moses2wrapper.vcxproj @@ -0,0 +1,176 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {a190cd7d-ee0a-4eaa-8093-a751df1d4157} + moses2wrapper + 10.0 + + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;MOSES2WRAPPER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + true + true + WIN32;NDEBUG;MOSES2WRAPPER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + true + true + false + + + + + Level3 + true + _DEBUG;MOSES2WRAPPER_EXPORTS;_WINDOWS;_USRDLL;NO_COMPACT_TABLES;HAVE_CMPHXXX;MAX_NUM_FACTORS=4;KENLM_MAX_ORDER=6;_USE_MATH_DEFINES;NOMINMAX;WITH_THREADS;NO_PIPES;_WIN32;WIN32;_CONSOLE;_LIB;%(PreprocessorDefinitions) + true + NotUsing + pch.h + true + ProgramDatabase + Classic + Async + Default + true + true + true + /Zc:twoPhase- %(AdditionalOptions) + F:\boost_1_72_0;D:\mman-win32-master;D:\zlib-1.2.8;D:\cmph-2.0.2;D:\moses-mstranslator;D:\xmlrpc-c-1.51.06\include;D:\moses-mstranslator\moses2 + MultiThreadedDebugDLL + + + Windows + true + false + F:\boost_1_72_0\lib64-msvc-14.2;%(AdditionalLibraryDirectories) + + + + + Level3 + true + true + true + NDEBUG;MOSES2WRAPPER_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + true + true + false + + + + + {b4304e97-d37f-4022-bd03-841a4faee398} + + + + + + + + + \ No newline at end of file diff --git a/moses2/Main.cpp b/moses2/Main.cpp index cf833760a..7054ec4ce 100644 --- a/moses2/Main.cpp +++ b/moses2/Main.cpp @@ -6,7 +6,7 @@ #include "Phrase.h" #include "TranslationTask.h" #include "MemPoolAllocator.h" -#include "server/Server.h" +//#include "server/Server.h" #include "legacy/InputFileStream.h" #include "legacy/Parameter.h" #include "legacy/ThreadPool.h" @@ -59,8 +59,8 @@ int main(int argc, char** argv) //////////////////////////////////////////////////////////////////////////////////////////////// void run_as_server(Moses2::System &system) { - Moses2::Server server(system.options.server, system); - server.run(system); // actually: don't return. see Server::run() + //Moses2::Server server(system.options.server, system); + //server.run(system); // actually: don't return. see Server::run() } //////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/moses2/Moses2Wrapper.cpp b/moses2/Moses2Wrapper.cpp new file mode 100644 index 000000000..2d7155e03 --- /dev/null +++ b/moses2/Moses2Wrapper.cpp @@ -0,0 +1,20 @@ +#include "Moses2Wrapper.h" +#include "System.h" +#include "legacy/Parameter.h" +#include "TranslationTask.h" +using namespace std; +namespace Moses2 { + Moses2Wrapper::Moses2Wrapper(const std::string &filePath) { + m_param = new Parameter(); + m_param->LoadParam(filePath); + m_system = new System(*m_param); + } + std::string Moses2Wrapper::Translate(const std::string &input) { + //create id + long a = 11234567; + TranslationTask task(*m_system, input, a); + std::string translation = task.RunTranslation(); + //delete translation; + return translation; + } +} \ No newline at end of file diff --git a/moses2/Moses2Wrapper.h b/moses2/Moses2Wrapper.h new file mode 100644 index 000000000..01d021145 --- /dev/null +++ b/moses2/Moses2Wrapper.h @@ -0,0 +1,16 @@ +#pragma once +#include +namespace Moses2 { + class Parameter; + class System; + class Moses2Wrapper + { + Parameter *m_param; + System *m_system; + + public: + Moses2Wrapper(const std::string &filePath); + std::string Translate(const std::string &input); + }; + +} \ No newline at end of file diff --git a/moses2/TranslationTask.cpp b/moses2/TranslationTask.cpp index 219d9ffcb..0032ab0b0 100644 --- a/moses2/TranslationTask.cpp +++ b/moses2/TranslationTask.cpp @@ -23,7 +23,18 @@ TranslationTask::TranslationTask(System &system, TranslationTask::~TranslationTask() { } +std::string TranslationTask::RunTranslation() +{ + m_mgr->Decode(); + string out; + + out = m_mgr->OutputBest() + "\n"; + + + delete m_mgr; + return out; +} void TranslationTask::Run() { diff --git a/moses2/TranslationTask.h b/moses2/TranslationTask.h index bf2330357..e683e1174 100644 --- a/moses2/TranslationTask.h +++ b/moses2/TranslationTask.h @@ -16,6 +16,7 @@ public: TranslationTask(System &system, const std::string &line, long translationId); virtual ~TranslationTask(); virtual void Run(); + virtual std::string RunTranslation(); protected: ManagerBase *m_mgr; diff --git a/moses2/server/Server.cpp b/moses2/server/Server.cpp index 57218c374..2da72270a 100644 --- a/moses2/server/Server.cpp +++ b/moses2/server/Server.cpp @@ -19,12 +19,12 @@ Server::Server(ServerOptions &server_options, System &system) :m_server_options(server_options) ,m_translator(new Translator(*this, system)) { - m_registry.addMethod("translate", m_translator); + // m_registry.addMethod("translate", m_translator); } Server::~Server() { - unlink(m_pidfile.c_str()); + //unlink(m_pidfile.c_str()); } void Server::run(System &system)