Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
msupernaw committed Nov 18, 2023
1 parent 7eef3bc commit 0fa0c0a
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 40 deletions.
Empty file.
60 changes: 31 additions & 29 deletions inst/include/SharedRList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <map>

#include <boost/interprocess/containers/map.hpp>

#include "SharedRObject.hpp"
#include "SharedRVector.hpp"
class SharedList;
Expand Down Expand Up @@ -38,34 +37,16 @@ class SharedList : public SharedRObject {
};

std::vector<std::shared_ptr<SharedRObject> > stash;
//
// typedef std::string KeyType;
// typedef SEXP MappedType;
// typedef std::pair<const std::string, SEXP> ValueType;
//
// //allocator of for the map.
// typedef boost::interprocess::allocator<ValueType, bip::managed_shared_memory::segment_manager> ShmemAllocator2;
//
// //third parameter argument is the ordering function is used to compare the keys.
// typedef std::map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator2> MySHMMap;
//
// bip::managed_shared_memory segment;
// MySHMMap* list_m;

typedef bip::allocator<char, bip::managed_shared_memory::segment_manager> CharAllocator;
typedef std::basic_string<char, std::char_traits<char>, CharAllocator> ShmemString;

typedef std::string KeyType2;
typedef tuple MappedType2;
typedef std::pair<const std::string, tuple> ValueType2;

typedef boost::interprocess::allocator<ValueType2, bip::managed_shared_memory::segment_manager> ShmemAllocator3;

typedef std::string KeyType;
typedef tuple MappedType;
typedef std::pair<const std::string, tuple> ValueType;

typedef bip::map<KeyType2, MappedType2, std::less<KeyType2>, ShmemAllocator3> MyTupleMap;
typedef bip::allocator<ValueType, bip::managed_shared_memory::segment_manager> ShmemAllocator3;
typedef bip::map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator3> SMLIST;

bip::managed_shared_memory segment;
MyTupleMap* tlist_m;
SMLIST* tlist_m;

void init(const std::string name) {
this->name = name;
Expand All @@ -78,7 +59,7 @@ class SharedList : public SharedRObject {
const ShmemAllocator3 alloc_inst(segment.get_segment_manager());

//Construct a vector named "MyVector" in shared memory with argument alloc_inst
this->tlist_m = segment.construct<MyTupleMap>("MyTupleMap")(std::less<KeyType2>(), alloc_inst);
this->tlist_m = segment.construct<SMLIST>("SMLIST")(std::less<KeyType>(), alloc_inst);
std::map<std::string, std::shared_ptr<SharedRObject> > local_map;
}

Expand All @@ -98,6 +79,22 @@ class SharedList : public SharedRObject {

public:

typedef typename SMLIST::key_type key_type;
typedef typename SMLIST::key_type mapped_type;
typedef typename SMLIST::value_type value_type;
typedef typename SMLIST::size_type size_type;
typedef typename SMLIST::difference_type difference_type;
typedef typename SMLIST::key_compare key_compare;
typedef typename SMLIST::allocator_type allocator_type;
typedef typename SMLIST::reference reference;
typedef typename SMLIST::const_reference const_reference;
typedef typename SMLIST::pointer pointer;
typedef typename SMLIST::const_pointer const_pointer;
typedef typename SMLIST::iterator iterator;
typedef typename SMLIST::const_iterator const_iterator;
typedef typename SMLIST::reverse_iterator reverse_iterator;
typedef typename SMLIST::node_type node_type;

SharedList() {
}

Expand All @@ -117,9 +114,9 @@ class SharedList : public SharedRObject {
this->segment = bip::managed_shared_memory(bip::open_only, name.c_str());

//Find the vector using the c-string name
this->tlist_m = segment.find<MyTupleMap>("MyTupleMap").first;
this->tlist_m = segment.find<SMLIST>("SMLIST").first;

typename MyTupleMap::iterator it;
typename SMLIST::iterator it;

for (it = tlist_m->begin(); it != tlist_m->end(); ++it) {
SMTYPE sm_type = (*it).second.type;
Expand All @@ -136,7 +133,7 @@ class SharedList : public SharedRObject {
}

void destroy(const std::string & name) {
this->segment.destroy<MyTupleMap>(name.c_str());
this->segment.destroy<SMLIST>(name.c_str());
}

void set(const std::string& key, SEXP object) {
Expand Down Expand Up @@ -246,8 +243,13 @@ namespace Rcpp {


}

RCPP_EXPOSED_CLASS(SharedList)


SharedList sm_list() {
return SharedList();
}


#endif
13 changes: 13 additions & 0 deletions inst/include/SharedRMutex.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef SHAREDRMUTEX_HPP
#define SHAREDRMUTEX_HPP


#include "SharedRObject.hpp"


class SharedMutex{

};


#endif
24 changes: 18 additions & 6 deletions inst/include/SharedRObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@ class SharedRObject {
SharedRObject() {
}


// virtual void create(const std::string& name) = 0;
// virtual void open(const std::string& name) = 0;
// virtual void destroy(const std::string& name) = 0;

bool is_SharedRObject(SEXP s) {
try {
if(this->is_Numeric(s)){

if (this->is_Numeric(s)) {
return true;
}

if (TYPEOF(s) != S4SXP) {
::Rf_error("supplied object not from SharedR Library.");
return false;
Expand Down Expand Up @@ -89,13 +94,13 @@ class SharedRObject {

if (this->is_Numeric(s)) {
return SMTYPE::SMNUMERIC;
}
}


try {


if (TYPEOF(s) != S4SXP ) {
if (TYPEOF(s) != S4SXP) {
Rcpp::Rcout << __LINE__ << std::endl;
std::stringstream ss;
ss << TYPEOF(s) << " supplied object not from SharedR Library.";
Expand Down Expand Up @@ -166,7 +171,14 @@ void SMInitialize(std::string system_name, bool open) {
// processR_sm_declarations_g = segment_g.construct<SharedTupleVector>("SharedTupleVector")(alloc_inst);
}


/**
* Destroy shared memory.
*
* @param name
*/
void destroy(const std::string& name) {
bip::shared_memory_object::remove(name.c_str());
}


#endif
10 changes: 7 additions & 3 deletions inst/include/SharedRVector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ class SharedVector : public SharedRObject {
}
}

void create(const std::string& name) {
virtual void create(const std::string& name) {
this->init(name);
}

void open(const std::string& name) {
virtual void open(const std::string& name) {
//Open the managed segment
this->name = name;
this->segment = managed_shared_memory(open_only, name.c_str());
Expand All @@ -137,7 +137,7 @@ class SharedVector : public SharedRObject {
this->vec_m = segment.find<REALSMVector>("REALSMVector").first;
}

void destroy(const std::string& name) {
virtual void destroy(const std::string& name) {
this->segment.destroy<REALSMVector>(name.c_str());
}

Expand Down Expand Up @@ -479,6 +479,10 @@ bool operator==(const SharedVector& lhs,
//
// }

SharedVector sm_vector(){
return SharedVector();
}

SharedVector as_shared_vector(SEXP s){
return Rcpp::as<SharedVector>(s);
}
Expand Down
Empty file added inst/include/SharedSet.hpp
Empty file.
4 changes: 3 additions & 1 deletion src/ProcessR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,9 @@ RCPP_MODULE(processR) {
.method("resize", &SharedVector::resize, "resize SharedVector.")
.method("size", &SharedVector::size, "get the size of SharedVector.")
.method("destroy", &SharedVector::destroy, "destroy SharedVector.");
Rcpp::function("as_shared_vector", &as_shared_vector);
Rcpp::function("sm_vector", &sm_vector);
Rcpp::function("sm_list", &sm_list);
Rcpp::function("destroy", &destroy);
}

// // [[Rcpp::export]]
Expand Down
3 changes: 2 additions & 1 deletion test/test_sm.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ if (parent == TRUE) {#parent process
cat(SMV$get(i))
cat(" ")
}
cat("\n\n")
cat(SML$get("PI"))
cat("\n")

#destroy shared memory
SMV$destroy(sm_name)

Expand Down

0 comments on commit 0fa0c0a

Please sign in to comment.