Last active
January 1, 2022 08:56
-
-
Save mgmarino/6581152 to your computer and use it in GitHub Desktop.
Build BOOST shared memory using vectors on hopper.
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 "TROOT.h" | |
//#include "TFile.h" | |
//#include "TParameter.h" | |
#include <iostream> | |
#include <boost/interprocess/managed_shared_memory.hpp> | |
#include <boost/interprocess/containers/vector.hpp> | |
#include <boost/interprocess/allocators/allocator.hpp> | |
#include <boost/interprocess/sync/scoped_lock.hpp> | |
#include <boost/interprocess/sync/named_mutex.hpp> | |
#include <boost/interprocess/sync/named_condition.hpp> | |
#include <boost/interprocess/detail/os_thread_functions.hpp> | |
#include <string> | |
#include <cstdlib> //std::system | |
#include <vector> | |
using boost::interprocess::shared_memory_object; | |
using boost::interprocess::managed_shared_memory; | |
using boost::interprocess::open_or_create; | |
//Define an STL compatible allocator of ints that allocates from the managed_shared_memory. | |
//This allocator will allow placing containers in the segment | |
typedef boost::interprocess::allocator<int, boost::interprocess::managed_shared_memory::segment_manager> ShmemAllocator; | |
//Alias a vector that uses the previous STL-like allocator so that allocates | |
//its values from the segment | |
typedef boost::interprocess::vector<int, ShmemAllocator> MyVector; | |
typedef std::vector<int, ShmemAllocator> CountVector; | |
//Main function. For parent process argc == 1, for child process argc == 2 | |
int main(int argc, char *argv[]) | |
{ | |
bool iNeedToDeleteEverything = false; | |
const int pid = boost::interprocess::detail::get_current_process_id(); | |
boost::interprocess::named_mutex nm(open_or_create, "EXOAnalysisMutex1"); | |
std::cout << "Waiting for lock" << std::endl; | |
nm.lock(); | |
std::cout << "Acquired lock" << std::endl; | |
// If we get here, we need to check to make sure that we have everything. | |
managed_shared_memory segment(open_or_create, "EXOAnalysisSharedMemory", 65536); | |
//Initialize shared memory STL-compatible allocator | |
const ShmemAllocator alloc_inst (segment.get_segment_manager()); | |
//Find the count vector using the c-string name | |
CountVector *myvector = segment.find_or_construct<CountVector>("CountVector")(alloc_inst); | |
MyVector* datavector = NULL; | |
// Save the | |
myvector->push_back(pid); | |
if (myvector->size() == 1) { | |
// Ok, we need to fill everything. | |
// HERE, we would open up the file and read it in. | |
std::cout << "Master: " << pid << std::endl; | |
datavector = segment.construct<MyVector>("Data")(alloc_inst); | |
for(int i = 0; i < 100; ++i) datavector->push_back(i); | |
iNeedToDeleteEverything = true; | |
} else { | |
// Everything else just gets the pointer to the data | |
datavector = segment.find<MyVector>("Data").first; | |
} | |
nm.unlock(); | |
// Do stuff... now come to the end | |
std::cout << pid << " "; | |
for (size_t i=0;i<datavector->size();i++) { | |
std::cout << (*datavector)[i] << " "; | |
} | |
std::cout << std::endl; | |
// Simulate work | |
sleep(5); | |
if (iNeedToDeleteEverything) { | |
boost::interprocess::scoped_lock<boost::interprocess::named_mutex> lock(nm); | |
boost::interprocess::named_condition cond(open_or_create, "EXOA_Cond"); | |
while ( myvector->size() > 1 ) cond.wait(lock); | |
// Cleanup | |
shared_memory_object::remove("EXOAnalysisSharedMemory"); | |
boost::interprocess::named_condition::remove("EXOA_Cond"); | |
nm.unlock(); | |
boost::interprocess::named_mutex::remove("EXOAnalysisMutex1"); | |
std::cout << "Parent " << pid << std::endl; | |
} else { | |
boost::interprocess::named_condition cond(open_or_create, "EXOA_Cond"); | |
nm.lock(); | |
for (size_t i=0;i<myvector->size();i++) { | |
if (pid == (*myvector)[i]) { | |
myvector->erase(myvector->begin()+i); | |
break; | |
} | |
} | |
nm.unlock(); | |
cond.notify_all(); | |
std::cout << "Child " << pid << std::endl; | |
} | |
return 0; | |
}; |
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
# Makefile for linking against ROOT | |
# M. Marino 22 May 2007 | |
TARGETS = main | |
SOURCES = $(wildcard *.cc) #uncomment these to add all cc files in directory to your compile list | |
SOURCES += $(wildcard *.C) #uncomment these to add all cc files in directory to your compile list | |
OBJS = $(SOURCES:.cc=.o) | |
TARGETOBJ = $(patsubst %, %.o, $(TARGETS)) | |
CXX = CC | |
CXXFLAGS = -Wall -g -O2 | |
LIBS = | |
CXXFLAGS += -I/usr/common/usg/boost/1.47.0/include | |
LIBS += -lrt #$(shell root-config --libs) | |
#$(shell exo-config -l) -L$(G4LIB)/$(G4SYSTEM) -lG4UIbasic -lG4UIcommon -lG4intercoms -lG4globman -lCint -lRIO | |
all: $(TARGETS) | |
$(TARGETS): $(TARGETOBJ) $(OBJS) | |
$(CXX) $(CXXFLAGS) -o $@ $(OBJS) $(LIBS) | |
.cc.o: | |
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< | |
clean: | |
rm -f $(TARGETS) | |
rm -f *.o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment