Пример #1
0
ConvolutionalNeuralNetwork::ConvolutionalNeuralNetwork(const int* types, const int* widths, const int* heights, const int* pool_widths, const int* pool_heights, int size)
{
    cnn.resize(size);
    pool.resize(size);
    output_imgs.resize(size);
    output_pools.resize(size);
    conv_width.resize(size);
    conv_height.resize(size);

    std::vector<int> nets(2);
    for(int i=0; i<size; i++){
        conv_width[i] = widths[i];
        conv_height[i] = heights[i];

        nets[0] = heights[i]*widths[i];
        nets[1] = 1;
        cnn[i] = new NeuralNetwork(nets);

        if(i == size-1){
            nets[0] = 1;
            nets[1] = 1;
        }else{
            nets[0] = std::max(pool_widths[i+1], widths[i+1]);
            nets[1] = std::max(pool_heights[i+1], heights[i+1]);
        }

        pool[i] = create_pooling(types[i], nets[0], nets[1]);
    }

    calculate_minimum_size();
}
Пример #2
0
Node::ReasonForTermination Node::run()
	throw()
{
	_NodeImpl *impl = (_NodeImpl *)_impl;
	RuntimeEnvironment *_r = (RuntimeEnvironment *)&(impl->renv);

	impl->started = true;
	impl->running = true;

	try {
#ifdef ZT_LOG_STDOUT
		_r->log = new Logger((const char *)0,(const char *)0,0);
#else
		_r->log = new Logger((_r->homePath + ZT_PATH_SEPARATOR_S + "node.log").c_str(),(const char *)0,131072);
#endif

		LOG("starting version %s",versionString());

		// Create non-crypto PRNG right away in case other code in init wants to use it
		_r->prng = new CMWC4096();

		bool gotId = false;
		std::string identitySecretPath(_r->homePath + ZT_PATH_SEPARATOR_S + "identity.secret");
		std::string identityPublicPath(_r->homePath + ZT_PATH_SEPARATOR_S + "identity.public");
		std::string idser;
		if (Utils::readFile(identitySecretPath.c_str(),idser))
			gotId = _r->identity.fromString(idser);
		if (gotId) {
			// Make sure identity.public matches identity.secret
			idser = std::string();
			Utils::readFile(identityPublicPath.c_str(),idser);
			std::string pubid(_r->identity.toString(false));
			if (idser != pubid) {
				if (!Utils::writeFile(identityPublicPath.c_str(),pubid))
					return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"could not write identity.public (home path not writable?)");
			}
		} else {
			LOG("no identity found or identity invalid, generating one... this might take a few seconds...");
			_r->identity.generate();
			LOG("generated new identity: %s",_r->identity.address().toString().c_str());
			idser = _r->identity.toString(true);
			if (!Utils::writeFile(identitySecretPath.c_str(),idser))
				return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"could not write identity.secret (home path not writable?)");
			idser = _r->identity.toString(false);
			if (!Utils::writeFile(identityPublicPath.c_str(),idser))
				return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"could not write identity.public (home path not writable?)");
		}
		Utils::lockDownFile(identitySecretPath.c_str(),false);

		// Clean up some obsolete files if present -- this will be removed later
		Utils::rm((_r->homePath + ZT_PATH_SEPARATOR_S + "status"));
		Utils::rm((_r->homePath + ZT_PATH_SEPARATOR_S + "thisdeviceismine"));

		// Make sure networks.d exists
#ifdef __WINDOWS__
		CreateDirectoryA((_r->homePath + ZT_PATH_SEPARATOR_S + "networks.d").c_str(),NULL);
#else
		mkdir((_r->homePath + ZT_PATH_SEPARATOR_S + "networks.d").c_str(),0700);
#endif

		// Load or generate config authentication secret
		std::string configAuthTokenPath(_r->homePath + ZT_PATH_SEPARATOR_S + "authtoken.secret");
		std::string configAuthToken;
		if (!Utils::readFile(configAuthTokenPath.c_str(),configAuthToken)) {
			configAuthToken = "";
			unsigned int sr = 0;
			for(unsigned int i=0;i<24;++i) {
				Utils::getSecureRandom(&sr,sizeof(sr));
				configAuthToken.push_back("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"[sr % 62]);
			}
			if (!Utils::writeFile(configAuthTokenPath.c_str(),configAuthToken))
				return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"could not write authtoken.secret (home path not writable?)");
		}
		Utils::lockDownFile(configAuthTokenPath.c_str(),false);

		// Create the objects that make up runtime state.
		_r->mc = new Multicaster();
		_r->sw = new Switch(_r);
		_r->demarc = new Demarc(_r);
		_r->topology = new Topology(_r,(_r->homePath + ZT_PATH_SEPARATOR_S + "peer.db").c_str());
		_r->sysEnv = new SysEnv(_r);
		try {
			_r->nc = new NodeConfig(_r,configAuthToken.c_str(),impl->controlPort);
		} catch (std::exception &exc) {
			char foo[1024];
			Utils::snprintf(foo,sizeof(foo),"unable to bind to local control port %u: is another instance of ZeroTier One already running?",impl->controlPort);
			return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,foo);
		}
		_r->node = this;

		// Bind local port for core I/O
		if (!_r->demarc->bindLocalUdp(impl->port)) {
			char foo[1024];
			Utils::snprintf(foo,sizeof(foo),"unable to bind to global I/O port %u: is another instance of ZeroTier One already running?",impl->controlPort);
			return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,foo);
		}

		// Set initial supernode list
		_r->topology->setSupernodes(ZT_DEFAULTS.supernodes);
	} catch (std::bad_alloc &exc) {
		return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"memory allocation failure");
	} catch (std::runtime_error &exc) {
		return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,exc.what());
	} catch ( ... ) {
		return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"unknown exception during initialization");
	}

	// Start external service subprocesses, which is only used by special nodes
	// right now and isn't available on Windows.
#ifndef __WINDOWS__
	try {
		std::string netconfServicePath(_r->homePath + ZT_PATH_SEPARATOR_S + "services.d" + ZT_PATH_SEPARATOR_S + "netconf.service");
		if (Utils::fileExists(netconfServicePath.c_str())) {
			LOG("netconf.d/netconfi.service appears to exist, starting...");
			_r->netconfService = new Service(_r,"netconf",netconfServicePath.c_str(),&_netconfServiceMessageHandler,_r);
		}
	} catch ( ... ) {
		LOG("unexpected exception attempting to start services");
	}
#endif

	// Core I/O loop
	try {
		uint64_t lastNetworkAutoconfCheck = 0;
		uint64_t lastPingCheck = 0;
		uint64_t lastClean = Utils::now(); // don't need to do this immediately
		uint64_t lastNetworkFingerprintCheck = 0;
		uint64_t networkConfigurationFingerprint = _r->sysEnv->getNetworkConfigurationFingerprint();
		uint64_t lastMulticastCheck = 0;
		long lastDelayDelta = 0;

		while (impl->reasonForTermination == NODE_RUNNING) {
			uint64_t now = Utils::now();
			bool resynchronize = false;

			// Detect sleep/wake by looking for delay loop pauses that are longer
			// than we intended to pause.
			if (lastDelayDelta >= ZT_SLEEP_WAKE_DETECTION_THRESHOLD) {
				resynchronize = true;
				LOG("probable suspend/resume detected, pausing a moment for things to settle...");
				Thread::sleep(ZT_SLEEP_WAKE_SETTLE_TIME);
			}

			// Periodically check our network environment, sending pings out to all
			// our direct links if things look like we got a different address.
			if ((resynchronize)||((now - lastNetworkFingerprintCheck) >= ZT_NETWORK_FINGERPRINT_CHECK_DELAY)) {
				lastNetworkFingerprintCheck = now;
				uint64_t fp = _r->sysEnv->getNetworkConfigurationFingerprint();
				if (fp != networkConfigurationFingerprint) {
					LOG("netconf fingerprint change: %.16llx != %.16llx, resyncing with network",networkConfigurationFingerprint,fp);
					networkConfigurationFingerprint = fp;
					resynchronize = true;
					_r->nc->whackAllTaps(); // call whack() on all tap devices -- hack, might go away
				}
			}

			// Request configuration for unconfigured nets, or nets with out of date
			// configuration information.
			if ((resynchronize)||((now - lastNetworkAutoconfCheck) >= ZT_NETWORK_AUTOCONF_CHECK_DELAY)) {
				lastNetworkAutoconfCheck = now;
				std::vector< SharedPtr<Network> > nets(_r->nc->networks());
				for(std::vector< SharedPtr<Network> >::iterator n(nets.begin());n!=nets.end();++n) {
					if ((now - (*n)->lastConfigUpdate()) >= ZT_NETWORK_AUTOCONF_DELAY)
						(*n)->requestConfiguration();
				}
			}

			// Periodically check for changes in our local multicast subscriptions and broadcast
			// those changes to peers.
			if ((resynchronize)||((now - lastMulticastCheck) >= ZT_MULTICAST_LOCAL_POLL_PERIOD)) {
				lastMulticastCheck = now;
				try {
					std::map< SharedPtr<Network>,std::set<MulticastGroup> > toAnnounce;
					std::vector< SharedPtr<Network> > networks(_r->nc->networks());
					for(std::vector< SharedPtr<Network> >::const_iterator nw(networks.begin());nw!=networks.end();++nw) {
						if ((*nw)->updateMulticastGroups())
							toAnnounce.insert(std::pair< SharedPtr<Network>,std::set<MulticastGroup> >(*nw,(*nw)->multicastGroups()));
					}
					if (toAnnounce.size())
						_r->sw->announceMulticastGroups(toAnnounce);
				} catch (std::exception &exc) {
					LOG("unexpected exception announcing multicast groups: %s",exc.what());
				} catch ( ... ) {
					LOG("unexpected exception announcing multicast groups: (unknown)");
				}
			}

			if ((resynchronize)||((now - lastPingCheck) >= ZT_PING_CHECK_DELAY)) {
				lastPingCheck = now;
				try {
					if (_r->topology->amSupernode()) {
						// Supernodes are so super they don't even have to ping out, since
						// all nodes ping them. They're also never firewalled so they
						// don't need firewall openers. They just ping each other.
						std::vector< SharedPtr<Peer> > sns(_r->topology->supernodePeers());
						for(std::vector< SharedPtr<Peer> >::const_iterator p(sns.begin());p!=sns.end();++p) {
							if ((now - (*p)->lastDirectSend()) > ZT_PEER_DIRECT_PING_DELAY)
								_r->sw->sendHELLO((*p)->address());
						}
					} else {
						if (resynchronize)
							_r->topology->eachPeer(Topology::PingAllActivePeers(_r,now));
						else _r->topology->eachPeer(Topology::PingPeersThatNeedPing(_r,now));
						_r->topology->eachPeer(Topology::OpenPeersThatNeedFirewallOpener(_r,now));
					}
				} catch (std::exception &exc) {
					LOG("unexpected exception running ping check cycle: %s",exc.what());
				} catch ( ... ) {
					LOG("unexpected exception running ping check cycle: (unkonwn)");
				}
			}

			if ((now - lastClean) >= ZT_DB_CLEAN_PERIOD) {
				lastClean = now;
				_r->mc->clean();
				_r->topology->clean();
				_r->nc->clean();
			}

			try {
				unsigned long delay = std::min((unsigned long)ZT_MIN_SERVICE_LOOP_INTERVAL,_r->sw->doTimerTasks());
				uint64_t start = Utils::now();
				_r->mainLoopWaitCondition.wait(delay);
				lastDelayDelta = (long)(Utils::now() - start) - (long)delay; // used to detect sleep/wake
			} catch (std::exception &exc) {
				LOG("unexpected exception running Switch doTimerTasks: %s",exc.what());
			} catch ( ... ) {
				LOG("unexpected exception running Switch doTimerTasks: (unknown)");
			}
		}
	} catch ( ... ) {
		return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"unexpected exception during outer main I/O loop");
	}

	return impl->terminate();
}
Пример #3
0
void netlist_state_t::reset()
{
	//FIXME: never used ???
	std::unordered_map<core_device_t *, bool> m;

	// Reset all nets once !
	log().verbose("Call reset on all nets:");
	for (auto & n : nets())
		n->reset();

	// Reset all devices once !
	log().verbose("Call reset on all devices:");
	for (auto & dev : m_devices)
		dev.second->reset();

	// Make sure everything depending on parameters is set
	// Currently analog input and logic input also
	// push their outputs to queue.

	log().verbose("Call update_param on all devices:");
	for (auto & dev : m_devices)
		dev.second->update_param();

	// Step all devices once !
	/*
	 * INFO: The order here affects power up of e.g. breakout. However, such
	 * variations are explicitly stated in the breakout manual.
	 */

	auto *netlist_params = get_single_device<devices::NETLIB_NAME(netlistparams)>("parameter");

	switch (netlist_params->m_startup_strategy())
	{
		case 0:
		{
			std::vector<core_device_t *> d;
			std::vector<nldelegate *> t;
			log().verbose("Using default startup strategy");
			for (auto &n : m_nets)
				for (auto & term : n->core_terms())
					if (term->m_delegate.has_object())
					{
						if (!plib::container::contains(t, &term->m_delegate))
						{
							t.push_back(&term->m_delegate);
							term->m_delegate();
						}
						auto *dev = reinterpret_cast<core_device_t *>(term->m_delegate.object());
						if (!plib::container::contains(d, dev))
							d.push_back(dev);
					}
			log().verbose("Devices not yet updated:");
			for (auto &dev : m_devices)
				if (!plib::container::contains(d, dev.second.get()))
				{
					log().verbose("\t ...{1}", dev.second->name());
					dev.second->update();
				}
		}
		break;
		case 1:     // brute force backward
		{
			log().verbose("Using brute force backward startup strategy");

			for (auto &n : m_nets)  // only used if USE_COPY_INSTEAD_OF_REFERENCE == 1
				n->update_inputs();

			std::size_t i = m_devices.size();
			while (i>0)
				m_devices[--i].second->update();

			for (auto &n : m_nets)  // only used if USE_COPY_INSTEAD_OF_REFERENCE == 1
				n->update_inputs();

		}
		break;
		case 2:     // brute force forward
		{
			log().verbose("Using brute force forward startup strategy");
			for (auto &d : m_devices)
				d.second->update();
		}
		break;
	}

#if 1
	/* the above may screw up m_active and the list */
	rebuild_lists();
#endif
}