Created
October 30, 2018 15:37
-
-
Save MatteoRagni/579d7c204a0180137d8eeffd9d4ded20 to your computer and use it in GitHub Desktop.
Shared memory and Matlab using Boost
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MEX_OPT = ['-I', '/usr/local/Cellar/boost/1.67.0_1/include']; | |
MEX_SRC = { ... | |
'menage_share.cpp', ... | |
'read_share.cpp', ... | |
'write_share.cpp' ... | |
}; | |
for i = 1:length(MEX_SRC) | |
mex(MEX_OPT, MEX_SRC{i}); | |
end | |
!g++ share_server.cpp -o share_server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mex.hpp" | |
#include "mexAdapter.hpp" | |
#include "MatlabDataArray.hpp" | |
#include <string> | |
#include <algorithm> | |
#include <cstring> | |
#include <cstdlib> | |
#include <exception> | |
#include "boost/interprocess/shared_memory_object.hpp" | |
#include "boost/interprocess/mapped_region.hpp" | |
using namespace boost::interprocess; | |
using namespace matlab::data; | |
class MexFunction : public matlab::mex::Function { | |
private: | |
std::shared_ptr<matlab::engine::MATLABEngine> engine; | |
ArrayFactory factory; | |
void throwError(std::string errorMessage) { | |
engine->feval(matlab::engine::convertUTF8StringToUTF16String("error"), | |
0, std::vector<Array>({ factory.createScalar(errorMessage) })); | |
} | |
uint64_t write_shared_memory(const std::string & name, const std::string & value) { | |
try { | |
shared_memory_object sm(open_only, name.c_str(), read_write); | |
mapped_region sh_mem(sm, read_write); | |
uint64_t size = std::min(value.size(), sh_mem.get_size()); | |
std::memcpy(sh_mem.get_address(), value.c_str(), size); | |
return size; | |
} catch(std::exception & e) { | |
throwError(std::string("Reading error: ") + std::string(e.what())); | |
} | |
return 0; | |
} | |
void checkArguments(matlab::mex::ArgumentList inputs, matlab::mex::ArgumentList outputs) { | |
if (inputs.size() != 2) | |
throwError("Input must be of size 2"); | |
if (inputs[0].getType() != ArrayType::CHAR) | |
throwError("First element must be a matlab char array"); | |
if (inputs[1].getType() != ArrayType::CHAR) | |
throwError("Second element must be a matlab char array to save"); | |
if (outputs.size() > 1) | |
throwError("Too many outputs (required 1)"); | |
} | |
public: | |
MexFunction() { | |
engine = getEngine(); | |
} | |
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) { | |
checkArguments(inputs, outputs); | |
const CharArray name_array = std::move(inputs[0]); | |
std::string name = name_array.toAscii(); | |
const CharArray value_array = std::move(inputs[1]); | |
std::string value = value_array.toAscii(); | |
uint64_t written = write_shared_memory(name, value); | |
outputs[0] = factory.createScalar<uint64_t>(written); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment