Example #1
0
/*
 * Listen on IPv4 and/or IPv6 depending on how the local node is configured.
 */
static void
listen_to_peers(void)
{
	struct addrinfo hints = {
		.ai_family = AF_UNSPEC,
		.ai_socktype = SOCK_STREAM,
		.ai_flags = AI_PASSIVE | AI_ADDRCONFIG,
	};
	const int yes = 1;
	struct addrinfo *res, *addr;
	char *port_str;
	int g;

	if (asprintf(&port_str, "%u", fakedlm_port) == -1)
		fail(NULL);
	g = getaddrinfo(NULL, port_str, &hints, &res);
	if (g) {
		fprintf(stderr, "%s\n", gai_strerror(g));
		exit(1);
	}
	free(port_str);
	for (addr = res; addr; addr = addr->ai_next) {
		int fd;

		fd = socket(addr->ai_family, addr->ai_socktype | SOCK_NONBLOCK,
			    addr->ai_protocol);
		if (fd == -1)
			fail(NULL);
		if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes,
			       sizeof(yes)) == -1)
			fail(NULL);
		if (addr->ai_family == AF_INET6) {
			/* Prevent IPv6 sockets from conflicting with IPv4. */
			if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
				       (void *)&yes, sizeof(yes)) == -1)
				fail(NULL);
		}
		if (bind(fd, addr->ai_addr, addr->ai_addrlen) == -1)
			fail(NULL);
		if (listen(fd, MAX_NODES - 1) == -1)
			fail(NULL);
		add_poll_callback(&cbs, fd, POLLIN, incoming_connection,
				  LISTENING_SOCKET_MARKER);
	}
	freeaddrinfo(res);
}

/*
 * Tell DLM about a new node's ID, address, and whether the node is local.
 */
static void
configure_node(struct node *node)
{
	struct sockaddr_storage ss;

	mkdirf(0777, "%scomms/%d", CONFIG_DLM_CLUSTER, node->nodeid);
	printf_pathf("%d", "%scomms/%d/nodeid", node->nodeid, CONFIG_DLM_CLUSTER,
		     node->nodeid);
	if (node == local_node) {
		printf_pathf("1", "%scomms/%d/local", CONFIG_DLM_CLUSTER,
			     node->nodeid);
	}
	memset(&ss, 0, sizeof(ss));
	memcpy(&ss, node->addr->sa, node->addr->sa_len);
	write_pathf(&ss, sizeof(ss), "%scomms/%d/addr",
		    CONFIG_DLM_CLUSTER, node->nodeid);
}

/*
 * Load and configure the DLM kernel module.  This does not start any
 * lockspaces, yet.
 */
static void
configure_dlm(void)
{
	struct node *node;

	if (mkdir(CONFIG_DLM_CLUSTER, 0777) == -1 && errno != EEXIST) {
		modprobe("dlm");
		if (mkdir(CONFIG_DLM_CLUSTER, 0777) == -1 && errno != EEXIST)
			fail(CONFIG_DLM_CLUSTER);
	}
	if (cluster_name)
		printf_pathf("%s", "%s", cluster_name,
			     CONFIG_DLM_CLUSTER "cluster_name");
	if (dlm_port != DLM_PORT) {
		printf_pathf("%d", "%s", DLM_PORT,
			     CONFIG_DLM_CLUSTER "tcp_port");
	}
	if (dlm_protocol != PROTO_TCP) {
		printf_pathf("%d", "%s", dlm_protocol,
			     CONFIG_DLM_CLUSTER "protocol");
	}
	for (node = nodes; node; node = node->next) {
		configure_node(node);
	}
}

/*
 * Remove the DLM configuration so that the kernel module can be removed and/or
 * a different configuration can be created.
 */
static void
remove_dlm(void)
{
	struct node *node;

	for (node = nodes; node; node = node->next)
		rmdirf("%scomms/%d", CONFIG_DLM_CLUSTER, node->nodeid);
	rmdirf("%s", CONFIG_DLM_CLUSTER);

	if (control_fd != -1)
		close(control_fd);
	close(kernel_monitor_fd);
	rmmod("dlm");
}

/*
 * Print a uevent and its parameters (mostly for debugging purposes).
 */
static void
print_uevent(const char *buf, int len)
{
	printf("Uevent '%s'", buf);
	if (verbose) {
		const char *end = buf + len;
		bool first = true;

		for (buf = strchr(buf, 0) + 1;
		     buf < end;
		     buf = strchr(buf, 0) + 1) {
			if (first) {
				printf(" (%s", buf);
				first = false;
			} else {
				printf(", %s", buf);
			}
		}
		if (!first)
			 printf(")");
	}
	printf("\n");
	fflush(stdout);
}
void ff_rgrsn_tree::configure_node(const vector< vector<nl_vector > > & multi_scale_features,
                                   const vector< double >& labels,
                                   const vector< unsigned int >& indices,
                                   const unsigned int depth,
                                   ff_rgrsn_tree_node * node,
                                   const rf_rgrsn_tree_parameter & para,
                                   const ff_tree_cost_function & costFunction) const
{
    assert(node);
    
    if (depth > para.max_tree_depth_ || indices.size() < para.min_feature_num_) {
        double min_cost = INT_MAX;
        int opt_scale_index = -1;
        nl_vector optimal_wt;
        
        // loop each scale
        for (int i = 0; i<multi_scale_features[0].size(); i++) {
            nl_vector wt;
            double cost = costFunction.cost(multi_scale_features, labels, indices, i, wt);
            if (cost < min_cost) {
                min_cost = cost;
                opt_scale_index = i;
                optimal_wt = wt;
            }
        }
        assert(opt_scale_index != -1);
        
        if (verbose_leaf_) {
            printf("depth, scale, leaf node size: %d %d %d\n", (int)depth, opt_scale_index, (int)indices.size());
        }
        
        // set node parameters
        node->isLeaf_ = true;
        node->split_scale_index_ = opt_scale_index;
        node->wt_ = optimal_wt;
        return ;
    }
    
    vector<unsigned int> leftIndices;
    vector<unsigned int> rightIndices;
    unsigned int minNodeSize = para.min_feature_num_;
    unsigned int splitScaleIndex = 0;
    unsigned int splitDim = 0;
    double splitThreshold = 0;
    
    // split in different scale and different dimension
    bool canSplit = bestSplittingScale(multi_scale_features, labels,
                                       indices, minNodeSize, para, costFunction,
                                       splitScaleIndex, splitDim, splitThreshold,
                                       leftIndices, rightIndices);
    
    
    if (canSplit) {
        assert(leftIndices.size() + rightIndices.size() == indices.size());
        
        node->split_scale_index_ = splitScaleIndex;
        node->split_dim_ = splitDim;
        node->split_threshold_ = splitThreshold;
        node->isLeaf_ = false;
        
        assert(splitDim < multi_scale_features[0][splitScaleIndex].size());
        if (verbose_) {
            int scale = (int)multi_scale_features[0][splitScaleIndex].size();
            printf("split scale, dim ,left, right node size: %d %d %d %d\n", scale, splitDim, (int)leftIndices.size(), (int)rightIndices.size());
        }
        
        if (leftIndices.size() != 0) {
            ff_rgrsn_tree_node *leftNode = costFunction.new_tree_node();
            leftNode->depth_ = depth + 1;
            configure_node(multi_scale_features, labels, leftIndices, depth + 1, leftNode, para, costFunction);
            node->left_child_ = leftNode;
        }
        if (rightIndices.size() != 0) {
            ff_rgrsn_tree_node *rightNode = costFunction.new_tree_node();
            rightNode->depth_ = depth + 1;
            configure_node(multi_scale_features, labels, rightIndices, depth + 1, rightNode, para, costFunction);
            node->right_child_ = rightNode;
        }
        
        return;
        
    }
    else
    {
        double min_cost = INT_MAX;
        int opt_scale_index = -1;
        nl_vector optimal_wt;
        
        // loop each scale
        for (int i = 0; i<multi_scale_features[0].size(); i++) {
            nl_vector wt;
            double cost = costFunction.cost(multi_scale_features, labels, indices, i, wt);
            if (cost < min_cost) {
                min_cost = cost;
                opt_scale_index = i;
                optimal_wt = wt;
            }
        }
        assert(opt_scale_index != -1);
        
        if (verbose_leaf_) {
            printf("depth, scale, leaf node size: %d %d %d\n", (int)depth, opt_scale_index, (int)indices.size());
        }
        
        // set node parameters
        node->isLeaf_ = true;
        node->split_scale_index_ = opt_scale_index;
        node->wt_ = optimal_wt;
        return ;
    }   
    
}