Strategy::Strategy(Forwarder& forwarder) : afterAddFace(forwarder.getFaceTable().afterAdd) , beforeRemoveFace(forwarder.getFaceTable().beforeRemove) , m_forwarder(forwarder) , m_measurements(m_forwarder.getMeasurements(), m_forwarder.getStrategyChoice(), *this) { }
Strategy::Strategy(Forwarder& forwarder, const Name& name) : afterAddFace(forwarder.getFaceTable().onAdd) , beforeRemoveFace(forwarder.getFaceTable().onRemove) , m_name(name) , m_forwarder(forwarder) , m_measurements(m_forwarder.getMeasurements(), m_forwarder.getStrategyChoice(), *this) { }
int main(int argc, char **args) { Forwarder f; size_t alphabet_size = 3; f.read_seq_directory("sequences", alphabet_size); f.write_to_directory("example_out"); Matrix pi, A, B; read_HMM(pi, A, B, "example.hmm"); std::cout << "log-likelihood: " << f.forward(pi, A, B) << std::endl; // std::cout << "log-likelihood: " << f.pthread_forward(pi, A, B) << std::endl; // parallelized on the lengths of the sequences // std::cout << "log-likelihood: " << f.mr_pthread_forward(pi, A, B) << std::endl; // parallelized version on the number of sequences return 0; }
typename std::enable_if<std::is_base_of<fw::Strategy, S>::value, S&>::type choose(Forwarder& forwarder, const Name& prefix, Args&&... args) { S& strategy = install<S>(forwarder, std::forward<Args>(args)...); forwarder.getStrategyChoice().insert(prefix, strategy.getName()); return strategy; }
typename std::enable_if<std::is_base_of<fw::Strategy, S>::value, S&>::type install(Forwarder& forwarder, Args&&... args) { auto strategy = make_unique<S>(ref(forwarder), std::forward<Args>(args)...); fw::Strategy* installed = forwarder.getStrategyChoice().install(std::move(strategy)).second; return dynamic_cast<S&>(*installed); }
typename std::enable_if<std::is_base_of<fw::Strategy, S>::value, S&>::type choose(Forwarder& forwarder, const Name& prefix = "/", const Name& instanceName = S::getStrategyName()) { StrategyChoice& sc = forwarder.getStrategyChoice(); auto insertRes = sc.insert(prefix, instanceName); if (!insertRes) { BOOST_THROW_EXCEPTION(std::invalid_argument(boost::lexical_cast<std::string>(insertRes))); } return dynamic_cast<S&>(sc.findEffectiveStrategy(prefix)); }
void test_forwarder_multiple_seqs() { Forwarder forwarder; Matrix pi, A, B; size_t no_states, alphabet_size; std::cout << "Testing forwarder with multiple sequences: "; std::cout.flush(); read_HMM(pi, A, B, "../test_data/multiple_seqs.hmm"); no_states = pi.get_height(); alphabet_size = B.get_width(); forwarder.read_seq_directory("../test_data/seqs_directory", alphabet_size, no_states, 50); assertClose(forwarder.forward(pi, A, B), -52287.6, "Forwarder: Difference in loglikelihood:", 0.0001); assertClose(forwarder.pthread_forward(pi, A, B), -52287.6, "pthread_forward: Difference in loglikelihood:", 0.0001); assertClose(forwarder.mr_pthread_forward(pi, A, B), -52287.6, "mr_pthread_forward: Difference in loglikelihood:", 0.0001); std::cout << "ok." << std::endl; }
void test_forwarder_long_sequence(const std::string &hmmFilename, const std::string &seqFilename, double expected_ll) { Matrix pi, A, B; size_t no_states, alphabet_size; std::cout << "Testing " << hmmFilename << " " << seqFilename << ": "; std::cout.flush(); read_HMM(pi, A, B, hmmFilename); no_states = pi.get_height(); alphabet_size = B.get_width(); Forwarder forwarder; forwarder.read_seq(seqFilename, alphabet_size, 500); SimpleForwarder s_forwarder(seqFilename); assertClose(forwarder.forward(pi, A, B), expected_ll, "Forwarder: Difference in likelihood log likelihood:", 0.0001); assertClose(forwarder.pthread_forward(pi, A, B), expected_ll, "pthread_forward: Difference in likelihood log likelihood:", 0.0001); assertClose(s_forwarder.forward(pi, A, B), expected_ll, "s_forward: Difference in log likelihood:", 0.0001); std::cout << "ok." << std::endl; }
int main(int argc, char **args) { Forwarder f; size_t alphabet_size = 3; f.read_seq("example.seq", alphabet_size); f.write_to_directory("example_out"); Matrix pi, A, B; read_HMM(pi, A, B, "example.hmm"); std::cout << "log-likelihood: " << f.forward(pi, A, B) << std::endl; // std::cout << "log-likelihood: " << f.pthread_forward(pi, A, B) << std::endl; # parallelized version std::vector<unsigned> pd_path; Matrix pd_table; posterior_decoding("example.seq", pi, A, B, pd_path, pd_table); std::cout << "posterior path[0:10]: "; for(size_t i = 0; i < 10; ++i) std::cout << pd_path[i] << " "; std::cout << std::endl; std::cout << "posterior table column 0 - 9: " << std::endl; for(size_t r = 0; r < 2; ++r) { for(size_t c = 0; c < 10; ++c) { std::cout << pd_table(r, c) << "\t"; } std::cout << std::endl; } std::vector<unsigned> viterbi_path; double viterbi_ll = viterbi("example.seq", pi, A, B, viterbi_path); std::cout << "viterbi loglikelihood: " << viterbi_ll << std::endl; std::cout << "viterbi path[0:10]: "; for(size_t i = 0; i < 10; ++i) std::cout << viterbi_path[i] << " "; std::cout << std::endl; return 0; }
int main(int argc, char **argv) { if(argc < 4) { std::cerr << "Usage: forwarder <server_port> <host> <forw_port>"; return EXIT_FAILURE; } int port = std::stoi(argv[1]); int status; struct addrinfo hints; struct addrinfo *servinfo; memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; status = getaddrinfo(argv[2], NULL, &hints, &servinfo); if (0 != status) { std::cerr << "Can't resolve address"; freeaddrinfo(servinfo); return EXIT_FAILURE; } struct sockaddr_in *host_s = (struct sockaddr_in *) servinfo->ai_addr; int host_port = std::stoi(argv[3]); try { Forwarder *forwarder = new Forwarder((in_port_t) port, host_s->sin_addr, (in_port_t) host_port); forwarder->run(); } catch (std::runtime_error &e) { std::cerr << e.what(); return EXIT_FAILURE; } return EXIT_SUCCESS; }
int main(int argc, char** argv) { using namespace Sirikata; DynamicLibrary::Initialize(); InitOptions(); Trace::Trace::InitOptions(); SpaceTrace::InitOptions(); InitSpaceOptions(); ParseOptions(argc, argv, OPT_CONFIG_FILE, AllowUnregisteredOptions); PluginManager plugins; plugins.loadList( GetOptionValue<String>(OPT_PLUGINS) ); plugins.loadList( GetOptionValue<String>(OPT_EXTRA_PLUGINS) ); plugins.loadList( GetOptionValue<String>(OPT_SPACE_PLUGINS) ); plugins.loadList( GetOptionValue<String>(OPT_SPACE_EXTRA_PLUGINS) ); // Fill defaults after plugin loading to ensure plugin-added // options get their defaults. FillMissingOptionDefaults(); // Rerun original parse to make sure any newly added options are // properly parsed. ParseOptions(argc, argv, OPT_CONFIG_FILE); DaemonizeAndSetOutputs(); ReportVersion(); // After options so log goes to the right place std::string time_server=GetOptionValue<String>("time-server"); NTPTimeSync sync; if (time_server.size() > 0) sync.start(time_server); ServerID server_id = GetOptionValue<ServerID>("id"); String trace_file = GetPerServerFile(STATS_TRACE_FILE, server_id); Sirikata::Trace::Trace* gTrace = new Trace::Trace(trace_file); // Compute the starting date/time String start_time_str = GetOptionValue<String>("wait-until"); Time start_time = start_time_str.empty() ? Timer::now() : Timer::getSpecifiedDate( start_time_str ); start_time += GetOptionValue<Duration>("wait-additional"); Duration duration = GetOptionValue<Duration>("duration"); Network::IOService* ios = new Network::IOService("Space"); Network::IOStrand* mainStrand = ios->createStrand("Space Main"); ODPSST::ConnectionManager* sstConnMgr = new ODPSST::ConnectionManager(); OHDPSST::ConnectionManager* ohSstConnMgr = new OHDPSST::ConnectionManager(); SpaceContext* space_context = new SpaceContext("space", server_id, sstConnMgr, ohSstConnMgr, ios, mainStrand, start_time, gTrace, duration); String servermap_type = GetOptionValue<String>("servermap"); String servermap_options = GetOptionValue<String>("servermap-options"); ServerIDMap * server_id_map = ServerIDMapFactory::getSingleton().getConstructor(servermap_type)(space_context, servermap_options); space_context->add(space_context); String timeseries_type = GetOptionValue<String>(OPT_TRACE_TIMESERIES); String timeseries_options = GetOptionValue<String>(OPT_TRACE_TIMESERIES_OPTIONS); Trace::TimeSeries* time_series = Trace::TimeSeriesFactory::getSingleton().getConstructor(timeseries_type)(space_context, timeseries_options); String commander_type = GetOptionValue<String>(OPT_COMMAND_COMMANDER); String commander_options = GetOptionValue<String>(OPT_COMMAND_COMMANDER_OPTIONS); Command::Commander* commander = NULL; if (!commander_type.empty()) commander = Command::CommanderFactory::getSingleton().getConstructor(commander_type)(space_context, commander_options); Transfer::TransferMediator::getSingleton().registerContext(space_context); Sirikata::SpaceNetwork* gNetwork = NULL; String network_type = GetOptionValue<String>(NETWORK_TYPE); if (network_type == "tcp") gNetwork = new TCPSpaceNetwork(space_context); BoundingBox3f region = GetOptionValue<BoundingBox3f>("region"); Vector3ui32 layout = GetOptionValue<Vector3ui32>("layout"); srand( GetOptionValue<uint32>("rand-seed") ); ObjectHostSessionManager* oh_sess_mgr = new ObjectHostSessionManager(space_context); ObjectSessionManager* obj_sess_mgr = new ObjectSessionManager(space_context); String auth_type = GetOptionValue<String>(SPACE_OPT_AUTH); String auth_opts = GetOptionValue<String>(SPACE_OPT_AUTH_OPTIONS); Authenticator* auth = AuthenticatorFactory::getSingleton().getConstructor(auth_type)(space_context, auth_opts); gNetwork->setServerIDMap(server_id_map); Forwarder* forwarder = new Forwarder(space_context); String cseg_type = GetOptionValue<String>(CSEG); CoordinateSegmentation* cseg = NULL; if (cseg_type == "uniform") cseg = new UniformCoordinateSegmentation(space_context, region, layout); else if (cseg_type == "client") { cseg = new CoordinateSegmentationClient(space_context, region, layout, server_id_map); } else { assert(false); exit(-1); } String loc_update_type = GetOptionValue<String>(LOC_UPDATE); String loc_update_opts = GetOptionValue<String>(LOC_UPDATE_OPTIONS); LocationUpdatePolicy* loc_update_policy = LocationUpdatePolicyFactory::getSingleton().getConstructor(loc_update_type)(space_context, loc_update_opts); String loc_service_type = GetOptionValue<String>(LOC); String loc_service_opts = GetOptionValue<String>(LOC_OPTIONS); LocationService* loc_service = LocationServiceFactory::getSingleton().getConstructor(loc_service_type)(space_context, loc_update_policy, loc_service_opts); ServerMessageQueue* sq = NULL; String server_queue_type = GetOptionValue<String>(SERVER_QUEUE); if (server_queue_type == "fair") { sq = new FairServerMessageQueue( space_context, gNetwork, (ServerMessageQueue::Sender*)forwarder); } else { assert(false); exit(-1); } ServerMessageReceiver* server_message_receiver = NULL; String server_receiver_type = GetOptionValue<String>(SERVER_RECEIVER); if (server_queue_type == "fair") server_message_receiver = new FairServerMessageReceiver(space_context, gNetwork, (ServerMessageReceiver::Listener*)forwarder); else { assert(false); exit(-1); } LoadMonitor* loadMonitor = new LoadMonitor(space_context, cseg); // OSeg Cache OSegCache* oseg_cache = NULL; std::string cacheSelector = GetOptionValue<String>(CACHE_SELECTOR); uint32 cacheSize = GetOptionValue<uint32>(OSEG_CACHE_SIZE); if (cacheSelector == CACHE_TYPE_COMMUNICATION) { double cacheCommScaling = GetOptionValue<double>(CACHE_COMM_SCALING); oseg_cache = new CommunicationCache(space_context, cacheCommScaling, cseg, cacheSize); } else if (cacheSelector == CACHE_TYPE_ORIGINAL_LRU) { uint32 cacheCleanGroupSize = GetOptionValue<uint32>(OSEG_CACHE_CLEAN_GROUP_SIZE); Duration entryLifetime = GetOptionValue<Duration>(OSEG_CACHE_ENTRY_LIFETIME); oseg_cache = new CacheLRUOriginal(space_context, cacheSize, cacheCleanGroupSize, entryLifetime); } else { std::cout<<"\n\nUNKNOWN CACHE TYPE SELECTED. Please re-try.\n\n"; std::cout.flush(); assert(false); } //Create OSeg std::string oseg_type = GetOptionValue<String>(OSEG); std::string oseg_options = GetOptionValue<String>(OSEG_OPTIONS); Network::IOStrand* osegStrand = space_context->ioService->createStrand("OSeg"); ObjectSegmentation* oseg = OSegFactory::getSingleton().getConstructor(oseg_type)(space_context, osegStrand, cseg, oseg_cache, oseg_options); //end create oseg // We have all the info to initialize the forwarder now forwarder->initialize(oseg, sq, server_message_receiver, loc_service); String aggmgr_hostname = GetOptionValue<String>(OPT_AGGMGR_HOSTNAME); String aggmgr_service = GetOptionValue<String>(OPT_AGGMGR_SERVICE); String aggmgr_consumer_key = GetOptionValue<String>(OPT_AGGMGR_CONSUMER_KEY); String aggmgr_consumer_secret = GetOptionValue<String>(OPT_AGGMGR_CONSUMER_SECRET); String aggmgr_access_key = GetOptionValue<String>(OPT_AGGMGR_ACCESS_KEY); String aggmgr_access_secret = GetOptionValue<String>(OPT_AGGMGR_ACCESS_SECRET); String aggmgr_username = GetOptionValue<String>(OPT_AGGMGR_USERNAME); Transfer::OAuthParamsPtr aggmgr_oauth; // Currently you need to explicitly override hostname to enable upload if (!aggmgr_hostname.empty()&& !aggmgr_consumer_key.empty() && !aggmgr_consumer_secret.empty() && !aggmgr_access_key.empty() && !aggmgr_access_secret.empty()) { aggmgr_oauth = Transfer::OAuthParamsPtr( new Transfer::OAuthParams( aggmgr_hostname, aggmgr_service, aggmgr_consumer_key, aggmgr_consumer_secret, aggmgr_access_key, aggmgr_access_secret ) ); } std::string aggmgr_type = GetOptionValue<String>(OPT_AGGMGR); AggregateManager* aggmgr = AggregateManagerFactory::getSingleton().getConstructor(aggmgr_type)(loc_service, aggmgr_oauth, aggmgr_username); std::string prox_type = GetOptionValue<String>(OPT_PROX); std::string prox_options = GetOptionValue<String>(OPT_PROX_OPTIONS); Proximity* prox = ProximityFactory::getSingleton().getConstructor(prox_type)(space_context, loc_service, cseg, gNetwork, aggmgr, prox_options); // We need to do an async lookup, and to finish it the server needs to be // running. But we can't create the server until we have the address from // this lookup. We isolate as little as possible into this callback -- // creating the server, finishing prox initialization, and getting them both // registered. We pass storage for the Server to the callback so we can // handle cleaning it up ourselves. using std::tr1::placeholders::_1; using std::tr1::placeholders::_2; Server* server = NULL; ModuleList modules; ServerData sd; sd.space_context = space_context; sd.auth = auth; sd.forwarder = forwarder; sd.loc_service = loc_service; sd.cseg = cseg; sd.prox = prox; sd.oseg = oseg; sd.oh_sess_mgr = oh_sess_mgr; sd.obj_sess_mgr = obj_sess_mgr; server_id_map->lookupExternal( space_context->id(), space_context->mainStrand->wrap( std::tr1::bind( &createServer, &server, &modules, sd, _1, _2) ) ); // If we're one of the initial nodes, we'll have to wait until we hit the start time { Time now_time = Timer::now(); if (start_time > now_time) { Duration sleep_time = start_time - now_time; printf("Waiting %f seconds\n", sleep_time.toSeconds() ); fflush(stdout); Timer::sleep(sleep_time); } } ///////////Go go go!! start of simulation///////////////////// space_context->add(auth); space_context->add(gNetwork); space_context->add(cseg); space_context->add(loc_service); space_context->add(oseg); space_context->add(loadMonitor); space_context->add(sstConnMgr); space_context->add(ohSstConnMgr); space_context->add(prox); space_context->run(3); space_context->cleanup(); if (GetOptionValue<bool>(PROFILE)) { space_context->profiler->report(); } gTrace->prepareShutdown(); Mesh::FilterFactory::destroy(); ModelsSystemFactory::destroy(); LocationServiceFactory::destroy(); LocationUpdatePolicyFactory::destroy(); for(ModuleList::iterator it = modules.begin(); it != modules.end(); it++) delete *it; modules.clear(); delete server; delete sq; delete server_message_receiver; delete prox; delete aggmgr; delete server_id_map; delete loadMonitor; delete cseg; delete oseg; delete oseg_cache; delete loc_service; delete forwarder; delete obj_sess_mgr; delete oh_sess_mgr; delete gNetwork; gNetwork=NULL; gTrace->shutdown(); delete gTrace; gTrace = NULL; delete commander; delete space_context; space_context = NULL; delete time_series; delete mainStrand; delete osegStrand; delete ios; delete sstConnMgr; delete ohSstConnMgr; Transfer::TransferMediator::destroy(); sync.stop(); plugins.gc(); Sirikata::Logging::finishLog(); DaemonCleanup(); return 0; }
MadmStrategy::MadmStrategy(Forwarder& forwarder, const Name& name) : Strategy(forwarder, name), ownStrategyChoice(forwarder.getStrategyChoice()) { }