/** * @brief Fill a sockaddr_x with this DAG * * Fill the provided sockaddr_x with this DAG * * @param s The sockaddr_x struct to be filled in (allocated by caller) */ void Graph::fill_sockaddr(sockaddr_x *s) const { s->sx_family = AF_XIA; s->sx_addr.s_count = num_nodes(); for (int i = 0; i < num_nodes(); i++) { node_t* node = (node_t*)&(s->sx_addr.s_addr[i]); // check this // Set the node's XID and type node->s_xid.s_type = get_node(i).type(); memcpy(&(node->s_xid.s_id), get_node(i).id(), Node::ID_LEN); // Get the node's out edge list std::vector<std::size_t> out_edges; if (i == num_nodes()-1) { out_edges = get_out_edges(-1); // put the source node's edges here } else { out_edges = get_out_edges(i); } // Set the out edges in the header for (uint8_t j = 0; j < EDGES_MAX; j++) { if (j < out_edges.size()) node->s_edge[j] = out_edges[j]; else node->s_edge[j] = EDGE_UNUSED; } } }
int main(int argc, char *argv[]) { int c; int x = 0; int y = 0; struct node *tree = NULL; printf("at beginning, num nodes = %d\n", num_nodes(tree)); add_node(&tree, x, y); printf("after initialization, num nodes = %d\n", num_nodes(tree)); while ((c = getchar()) != EOF) { if (c == '>') { x++; } else if (c == '<') { x--; } else if (c == '^') { y++; } else if (c == 'v') { y--; } add_node(&tree, x, y); } printf("at end, num nodes = %d\n", num_nodes(tree)); printf("height = %d\n", height(tree)); free_tree(tree); return 0; }
int num_nodes(struct node *tree) { int n = 0; if (tree == NULL) return 0; else return 1 + num_nodes(tree->left) + num_nodes(tree->right); }
LocalIdBulkData(stk::mesh::BulkData& bulkData) : m_bulkData(bulkData), bulkDataLocalIdMapper() { bulkDataLocalIdMapper.set_size(bulkData); m_localIdToElement.resize(num_elements()); fillIds(stk::topology::ELEM_RANK, m_localIdToElement); m_localIdToNode.resize(num_nodes()); fillIds(stk::topology::NODE_RANK, m_localIdToNode); m_localIdToNode.resize(num_nodes()); m_coords = m_bulkData.mesh_meta_data().get_field<CoordFieldType>(stk::topology::NODE_RANK, "coordinates"); }
memptr func_true(memptr func_expr) { if (num_nodes(func_expr) != 2) { #ifdef DEBUG printf("15\n"); #endif ERROR(ERROR_TRUE, ERROR_FAILURE, ERROR_INVALID, ERROR_COUNT); return NOT_FOUND; } #ifdef EXTENDED_SECURITY memptr local_security = safe_allocate_cons(); cons_pool[local_security].carKind = NIL; cons_pool[local_security].cdrKind = NIL; #endif memptr param = resolve_expr(cons_pool[cons_pool[func_expr].cdr].car); #ifdef EXTENDED_SECURITY cons_pool[local_security].car = param; cons_pool[local_security].carKind = CONS; #endif if (type(param) == TYPE_OBJECT) { memptr lu = object_lookup(param, cons_pool[func_expr].car); if (lu == NOT_FOUND) { param = object_lookup(param, core_symbol); } else { return resolve_func_expr(func_expr, param, lu, true); } } return ((param == t) ? t : nil); }
UInt64 virtual_size() const { return sizeof(Header) + (sizeof(Entry) * num_keys()) + (sizeof(Block) * num_blocks()) + (sizeof(Node) * num_nodes()) + total_key_length(); }
Graph::EdgeId Graph::add_edge(NodeId tail, NodeId head) { if ((tail >= num_nodes()) or (head >= num_nodes())) { throw std::runtime_error("Edge cannot be added due to undefined endpoint."); return invalid_edge; } else { _edges.push_back(Edge(tail, head)); EdgeId edge_id = _edges.size() - 1; _nodes[tail].add_outgoing_edge(edge_id); _nodes[head].add_incoming_edge(edge_id); return edge_id; } }
void pageRank(Graph g, float* solution, float damping, float convergence) { int numNodes = num_nodes(g); State<float> s(g, damping, convergence); VertexSet* frontier = newVertexSet(SPARSE, numNodes, numNodes); for (int i = 0; i < numNodes; i++) { addVertex(frontier, i); } float error = INFINITY; while (error > convergence) { Local<float> local(g, s.pcurr, s.pnext, s.diff, damping); VertexSet* frontier2 = edgeMap<State<float> >(g, frontier, s); VertexSet* frontier3 = vertexMap<Local<float> >(frontier2, local); freeVertexSet(frontier); freeVertexSet(frontier2); frontier = frontier3; error = s.getError(); std::swap(s.pcurr, s.pnext); } freeVertexSet(frontier); #pragma omp parallel for schedule(static) for (int i = 0; i < numNodes; i++) { solution[i] = s.pcurr[i]; } }
memptr func_list(memptr func_expr) { if (num_nodes(func_expr) == 1) { return nil; } memptr current_node, current_param, current_dup_node = safe_allocate_cons(), base = current_dup_node; cons_pool[current_dup_node].carKind = NIL; cons_pool[current_dup_node].cdrKind = NIL; current_node = cons_pool[func_expr].cdr; current_param = resolve_expr(cons_pool[current_node].car); if (current_param == NOT_FOUND) { #ifdef DEBUG printf("29\n"); #endif ERROR(ERROR_LIST, ERROR_FAILURE, ERROR_INVALID, ERROR_EMPTY); return NOT_FOUND; } else if (type(current_param) == TYPE_OBJECT) { memptr lu = object_lookup(current_param, cons_pool[func_expr].car); if (lu != NOT_FOUND) { #ifdef EXTENDED_SECURITY memptr local_security = safe_allocate_cons(); cons_pool[local_security].car = current_param; cons_pool[local_security].carKind = CONS; cons_pool[local_security].cdrKind = NIL; #endif return resolve_func_expr(func_expr, current_param, lu, true); } } cons_pool[current_dup_node].car = current_param; cons_pool[current_dup_node].carKind = CONS; while (cons_pool[current_node].cdrKind == CONS) { current_node = cons_pool[current_node].cdr; cons_pool[current_dup_node].cdr = allocate_cons(); cons_pool[current_dup_node].cdrKind = CONS; current_dup_node = cons_pool[current_dup_node].cdr; cons_pool[current_dup_node].carKind = NIL; cons_pool[current_dup_node].cdrKind = NIL; current_param = resolve_expr(cons_pool[current_node].car); if (current_param == NOT_FOUND) { #ifdef DEBUG printf("30\n"); #endif ERROR(ERROR_LIST, ERROR_FAILURE, ERROR_INVALID, ERROR_EMPTY); return NOT_FOUND; } cons_pool[current_dup_node].car = current_param; cons_pool[current_dup_node].carKind = CONS; } cons_pool[current_dup_node].cdrKind = CONS; cons_pool[current_dup_node].cdr = nil; return base; }
Decomosition(Graph g, int* solution) : cluster(solution) { for (int i = 0; i < num_nodes(g); i++) { cluster[i] = NA; } }
bool operator()(Vertex i) { pnext[i] = (damping * pnext[i]) + (1 - damping) / num_nodes(graph); diff[i] = fabs(pnext[i] - pcurr[i]); pcurr[i] = 0.f; return true; }
std::size_t Graph::index_from_dag_string_index(std::size_t dag_string_index, std::size_t source_index, std::size_t sink_index) const { if (dag_string_index == -1) return source_index; if (dag_string_index == num_nodes()-1) return sink_index; std::size_t real_index = dag_string_index; if (source_index < sink_index) { // add 1 if after real sink index (sink moves back from -1) if (real_index >= source_index) real_index++; // add 1 if after sink index (sink moves back from end) if (real_index >= sink_index) real_index++; } else { // add 1 if after sink index (sink moves back from end) if (real_index >= sink_index) real_index++; // add 1 if after real sink index (sink moves back from -1) if (real_index >= source_index) real_index++; } return real_index; }
int __send_to(unsigned short node, long ticket, unsigned short type, int slot, long value, unsigned short flags) { int n = node % (num_nodes()); log_graph(my_id(), node, type, 0); if (n == my_id()) { return __send_local(ticket, type, slot, value, flags); } return send_intheory(node, create_message(my_id(), node, ticket, type, slot, value, flags)); }
Bfs(Graph g, int* solution) : currentDistance(1), distances_(solution) { for (int i = 0; i < num_nodes(g); i++) { distances_[i] = NA; } distances_[0] = 0; }
/** Remove all nodes and edges from this graph. * @post num_nodes() == 0 && num_edges() == 0 * * Invalidates all outstanding Node and Edge objects. */ void clear() { std::cout << "Graph.clean()" << std::endl; internal_nodes_.clear(); internal_edges_.clear(); i2u_nodes_.clear(); i2u_edges_.clear(); num_edges_ = 0; assert(num_nodes() == 0); assert(num_edges() == 0); }
T getError() { int numNodes = num_nodes(graph); T error = 0.0; #pragma omp parallel for reduction(+:error) schedule(static) for (int i = 0; i < numNodes; i++) error += diff[i]; return error; }
void draw_base_graph(char *line, int line_size) { memset(line, ' ', line_size); line[line_size - 1] = 0; int i; for (i = 0; i < num_nodes(); i++) { line[graph_index(i, PROPOSER)] = '|'; line[graph_index(i, ACCEPTOR)] = '|'; line[graph_index(i, LEARNER)] = '|'; line[graph_index(i, CLIENT)] = '|'; } }
void UniformCostSearch::print_frame_data( int frame_number, float elapsed, Action curr_action, std::ostream& output ) { output << "frame=" << frame_number; output << ",expanded=" << expanded_nodes(); output << ",generated=" << generated_nodes(); output << ",pruned=" << pruned(); output << ",depth_tree=" << max_depth(); output << ",tree_size=" << num_nodes(); output << ",best_action=" << action_to_string( curr_action ); output << ",branch_reward=" << get_root_value(); output << ",elapsed=" << elapsed << std::endl; }
void log_state(state s, enum role_t r) { if (log_level < TRACE) return; int line_size = (num_nodes()) * 9 + 6; char line[line_size]; draw_base_graph(line, line_size); int meidx = graph_index(my_id(), r); line[meidx] = getStateName(s.state)[2]; printf("GRAPH: %s STATE %s %s %ld %ld %ld dead %ld\n", line, getRole(r), getStateName(s.state), s.ticket, s.slot, s.value, s.deadline); }
State(Graph g, T damping_, T convergence_) : graph(g), damping(damping_), convergence(convergence_) { int numNodes = num_nodes(g); pcurr = (T*)(malloc(sizeof(T) * numNodes)); pnext = (T*)(malloc(sizeof(T) * numNodes)); diff = (T*)(malloc(sizeof(T) * numNodes)); for (int i = 0; i < numNodes; i++) { pcurr[i] = 1.0 / numNodes; pnext[i] = 0.0; } }
memptr func_cdr(memptr func_expr) { if (num_nodes(func_expr) != 2) { #ifdef DEBUG printf("12\n"); #endif ERROR(ERROR_CDR, ERROR_FAILURE, ERROR_INVALID, ERROR_COUNT); return NOT_FOUND; } memptr local_security = safe_allocate_cons(); cons_pool[local_security].carKind = NIL; cons_pool[local_security].cdrKind = NIL; memptr param = resolve_expr(cons_pool[cons_pool[func_expr].cdr].car); cons_pool[local_security].car = param; cons_pool[local_security].carKind = CONS; while (true) { Type param_type = type(param); if (param_type == TYPE_CONS) { return cons_pool[param].cdr; } else if (param_type == TYPE_STRING) { memptr result; if ((cons_pool[param].cdr < 0) || (string_length(param) == 1)) { result = nil; } else { result = allocate_cons(); cons_pool[result].car = cons_pool[param].car; cons_pool[result].cdr = cons_pool[param].cdr + 1; cons_pool[result].carKind = STRING; cons_pool[result].cdrKind = INTEGER; } return result; } else if (param_type == TYPE_OBJECT) { memptr lu = object_lookup(param, cons_pool[func_expr].car); if (lu == NOT_FOUND) { param = object_lookup(param, core_symbol); continue; } else { return resolve_func_expr(func_expr, param, lu, true); } } break; } #ifdef DEBUG printf("13\n"); #endif ERROR(ERROR_CDR, ERROR_FAILURE, ERROR_INVALID, ERROR_EMPTY); return NOT_FOUND; }
// Finds the BFS distance to each node starting from node 0. void bfs(graph *g, int *solution) { Bfs f(g, solution); // Initialize frontier. VertexSet* frontier = newVertexSet(SPARSE, 1, num_nodes(g)); addVertex(frontier, 0); VertexSet *newFrontier; while (frontier->size != 0) { newFrontier = edgeMap<Bfs>(g, frontier, f); freeVertexSet(frontier); frontier = newFrontier; f.currentDistance++; } freeVertexSet(frontier); }
int main (int argc, char **argv) { // If bad input, bail if (argc != 4) return 1; int num_verts = num_nodes (argv[1]); int **weights = malloc (num_verts * sizeof (int *)); int i; // Don't need to zero, as construct_f_f will write over every entry for (i = 0; i < num_verts; i++) weights[i] = malloc (num_verts * sizeof (int)); struct node **graph = init_cache (num_verts); construct_from_file (weights, graph, argv[1]); struct path *shortest = find_shortest (weights, graph, num_verts, atoi(argv[2]), atoi(argv[3])); print_path (shortest); return 0; }
void graph<T>::bellmanford(node<T> *start) { cout << "BELLMAN-FORD:\n"; const int LARGENUMBER = 10000000; for (auto elem : nodelist) { node2parent[elem] = NULL; node2shortest[elem] = LARGENUMBER; } node2shortest[start] = 0; for (int i = 0; i < num_nodes()-1; ++i) { for (auto src : nodelist) { auto t_adjlist = src->get_adjlist(); for (auto elem : t_adjlist) { node<T> *dst = elem.first; int w = elem.second; if (node2shortest[src] + w < node2shortest[dst]) { node2parent[dst] = src; node2shortest[dst] = node2shortest[src] + w; } } } } cout << "Shortest path lengths from " << start->get_data() << " are:\n"; for (auto elem : nodelist) { cout << elem->get_data() << ": " << node2shortest[elem] << "\n"; } cout << "Shortest path parents from " << start->get_data() << " are:\n"; for (auto elem : nodelist) { if (node2parent[elem]) cout << elem->get_data() << ": " << node2parent[elem]->get_data() << "\n"; } cout << "***********************\n"; }
memptr func_value(memptr func_expr) { if (num_nodes(func_expr) < 2) { #ifdef DEBUG printf("31\n"); #endif ERROR(ERROR_VALUE, ERROR_FAILURE, ERROR_INVALID, ERROR_COUNT); return NOT_FOUND; } memptr next_node = cons_pool[func_expr].cdr, current_node, current_value; bool first_param = true; do { #ifdef EXTENDED_SECURITY memptr local_security = safe_allocate_cons(); cons_pool[local_security].carKind = NIL; cons_pool[local_security].cdrKind = NIL; #endif current_node = next_node; current_value = resolve_expr(cons_pool[current_node].car); #ifdef EXTENDED_SECURITY cons_pool[local_security].car = current_value; cons_pool[local_security].carKind = CONS; #endif if (current_value == NOT_FOUND) { #ifdef DEBUG printf("32\n"); #endif ERROR(ERROR_VALUE, ERROR_FAILURE, ERROR_INVALID, ERROR_EMPTY); return NOT_FOUND; } else if (first_param && (type(current_value) == TYPE_OBJECT)) { memptr lu = object_lookup(current_value, cons_pool[func_expr].car); if (lu != NOT_FOUND) { return resolve_func_expr(func_expr, current_value, lu, true); } } next_node = cons_pool[current_node].cdr; first_param = false; } while (cons_pool[current_node].cdrKind == CONS); return current_value; }
memptr func_cons(memptr func_expr) { if (num_nodes(func_expr) != 3) { #ifdef DEBUG printf("9\n"); #endif ERROR(ERROR_CONS, ERROR_FAILURE, ERROR_INVALID, ERROR_COUNT); return NOT_FOUND; } memptr res = safe_allocate_cons(); cons_pool[res].carKind = NIL; cons_pool[res].cdrKind = NIL; memptr current_node, current_data; // first parameter current_node = cons_pool[func_expr].cdr; current_data = cons_pool[current_node].car; cons_pool[res].car = resolve_expr(current_data); cons_pool[res].carKind = CONS; if (type(cons_pool[res].car) == TYPE_OBJECT) { memptr lu = object_lookup(cons_pool[res].car, cons_pool[func_expr].car); if (lu == NOT_FOUND && OBJECT_OPERATE_CORE) { cons_pool[res].car = object_lookup(cons_pool[res].car, core_symbol); } else if (lu != NOT_FOUND) { return resolve_func_expr(func_expr, cons_pool[res].car, lu, true); } } // second parameter current_node = cons_pool[current_node].cdr; current_data = cons_pool[current_node].car; cons_pool[res].cdr = resolve_expr(current_data); cons_pool[res].cdrKind = CONS; if (type(cons_pool[res].cdr) == TYPE_OBJECT && OBJECT_OPERATE_CORE) { cons_pool[res].cdr = object_lookup(cons_pool[res].cdr, core_symbol); } return res; }
void Scheduler::run_tasks_loop_with_task_id(TaskId root_id) { const auto num_additional_threads = num_nodes() - 1; std::vector<std::thread> threads; threads.reserve(num_additional_threads); for (NodeId i_node = 0; i_node < num_nodes_; ++i_node) { node(i_node).attach(*this, i_node); } for (NodeId i_thread = 0; i_thread < num_additional_threads; ++i_thread) { threads.emplace_back([this, i_thread]{ node(i_thread + 1).run_tasks_loop(); }); } auto x = SDL_GetPerformanceCounter(); node(0).generic_tasks_.unique_push(root_id); node(0).run_tasks_loop(); LOG(INFO) << ((SDL_GetPerformanceCounter() - x) / double(SDL_GetPerformanceFrequency())); for (auto& thread : threads) thread.join(); }
memptr func_supps(memptr func_expr) { if (num_nodes(func_expr) != 3) { #ifdef DEBUG printf("36\n"); #endif ERROR(ERROR_SUPPS, ERROR_FAILURE, ERROR_INVALID, ERROR_COUNT); return NOT_FOUND; } memptr symbol = cons_pool[cons_pool[func_expr].cdr].car; memptr value = resolve_expr( cons_pool[cons_pool[cons_pool[func_expr].cdr].cdr].car); if (value == NOT_FOUND) { #ifdef DEBUG printf("37\n"); #endif ERROR(ERROR_SUPPS, ERROR_FAILURE, ERROR_INVALID, ERROR_EMPTY); return NOT_FOUND; } return (supps(value, symbol) ? t : nil); }
void log_graph(int from_node, int to_node, int message, int recv) { if (log_level < GRAPH) return; enum role_t to_role = message_to_role(message); enum role_t from_role = message_from_role(message); int line_size = (num_nodes()) * 9 + 6; char line[line_size]; draw_base_graph(line, line_size); // printf("GRAPH(0): %s %s\n", line, getMessageName(message)); int to_idx = graph_index(to_node, to_role); int from_idx = graph_index(from_node, from_role); line[from_idx] = '*'; line[to_idx] = 'M'; if (to_idx < from_idx) { int tmp = to_idx; to_idx = from_idx; from_idx = tmp; line[to_idx - 1] = '-'; line[from_idx + 1] = '<'; } else { line[from_idx + 1] = '-'; line[to_idx - 1] = '>'; } int i; for (i = (from_idx + 2); i < (to_idx - 1); i++) { line[i] = '-'; } printf("GRAPH: %s %s %s\n", line, recv ? "RECV " : "SENT ", getMessageName(message)); }
void Clustering::optimize() { vector<clus_t> node_clus_initial = node_clus; vector<clus_t> best_node_clus; double best_loss = 1e100; for (int rep = 0 ; rep < params.num_repeats ; ++rep) { if (params.verbosity >= 1) params.debug_out << "Repetition " << rep << endl; // reset clustering node_clus = node_clus_initial; recalc_internal_data(); trace("initial"); // lots of optimization if (params.optimize_num_clusters_with_outer_loop && (params.min_num_clusters > 1 || params.max_num_clusters < (int)num_nodes())) { reduce_num_clusters_with_extra_loss(); } else { for (int it = 0 ; it < params.num_partitions ; ++it) { optimize_all_levels(); optimize_partition(); } optimize_all_levels(); // enforce number of clusters? if (params.min_num_clusters > (int)num_clusters() || params.max_num_clusters < (int)num_clusters()) { reduce_num_clusters(); } } // is it an improvement? if (rep == 0 || loss < best_loss) { best_loss = loss; best_node_clus = node_clus; } trace("done"); } // store this->loss = best_loss; this->node_clus = best_node_clus; }