void QuadTree::balance() { unsigned int flag; do { flag = 0; leaf_iterator leaf_it = begin_leaf(); for(; leaf_it != end_leaf(); ++leaf_it) { iterator_base leaf = leaf_it; int leaf_depth = depth(leaf); std::vector<iterator_base> neighbors; if(leaf_it->get_location().has_location(T)) neighbors.push_back(find_neighbor(leaf, B)); if(leaf_it->get_location().has_location(B)) neighbors.push_back(find_neighbor(leaf, T)); if(leaf_it->get_location().has_location(L)) neighbors.push_back(find_neighbor(leaf, R)); if(leaf_it->get_location().has_location(R)) neighbors.push_back(find_neighbor(leaf, L)); for(unsigned int n=0; n<neighbors.size(); ++n) { if(!neighbors[n].node) continue; int neighbor_depth = depth(neighbors[n]); if(leaf_depth-neighbor_depth>1) { neighbors[n]->divide_flag() = true; } } } for(leaf_it = begin_leaf(); leaf_it != end_leaf(); ) { iterator_base this_leaf = leaf_it++; if(this_leaf->divide_flag()) { subdivide(this_leaf); flag++; this_leaf->divide_flag() = false; } } } while(flag); }
int main(int argc, char *argv[]) { //printf("Creating thread.\n\n"); pthread_t p; int *arg; if(pthread_create(&p, NULL, listen_neighbor, arg)){ printf("Error in creating a new thread..\n"); exit(1); } //printf("Thread created..\n"); local_id = ID; int i,j; printf("Enter 1 to start election.\n"); //Waiting for nodes to be started. scanf("%d", &j); i = find_neighbor(); ELECTION=1; neighbor_index = i; printf("Current neighbor - %d:%d\n", i, neighbor[i]); char msg[1024]; printf("Sending election msg..\n\n"); sprintf(msg, "ELECTION %d", ID); send_to_neighbor(msg, neighbor_up, i); printf("Processing client %d, %d\n", inet_ntoa(clnt_addr.sin_addr.s_addr), (int)clnt_addr.sin_port); printf("Msg from client - %s\n", buffer); while(NO_LEADER){ sleep(1); } exit(1); }
int c_brem(w_coord wcoord, b_coord bcoord) { server_socket *dst; n_coord ncoord; void *p; lt_packet out_packet; client_func_checks(); ncoord = wcoord_to_ncoord(wcoord); dst = find_neighbor(ncoord); if (!dst) return 0; makepacket(&out_packet, T_BREM); p = &out_packet.payload; if (!put_wx(&p, wcoord.x, &PLength(&out_packet), &PArgc(&out_packet))) return 1; if (!put_wy(&p, wcoord.y, &PLength(&out_packet), &PArgc(&out_packet))) return 1; if (!put_wz(&p, wcoord.z, &PLength(&out_packet), &PArgc(&out_packet))) return 1; if (!put_bx(&p, bcoord.x, &PLength(&out_packet), &PArgc(&out_packet))) return 1; if (!put_by(&p, bcoord.y, &PLength(&out_packet), &PArgc(&out_packet))) return 1; if (!put_bz(&p, bcoord.z, &PLength(&out_packet), &PArgc(&out_packet))) return 1; return (sendpacket(dst, &out_packet)); }
/*---------------------------------------------------------------------------*/ void phase_update(const struct phase_list *list, const rimeaddr_t *neighbor, rtimer_clock_t time, int mac_status) { struct phase *e; uint8_t tmp; /* If we have an entry for this neighbor already, we renew it. */ e = find_neighbor(list, neighbor); if(e != NULL) { if(mac_status == MAC_TX_OK) { #if PHASE_DRIFT_CORRECT e->drift = time-e->time; #endif e->time = time; } /* If the neighbor didn't reply to us, it may have switched phase (rebooted). We try a number of transmissions to it before we drop it from the phase list. */ if(mac_status == MAC_TX_NOACK) { PRINTF("phase noacks %d to %d.%d\n", e->noacks, neighbor->u8[0], neighbor->u8[1]); tmp= e->noacks; e->noacks=tmp++; if(e->noacks == 1) { timer_set(&e->noacks_timer, MAX_NOACKS_TIME); } if(e->noacks >= MAX_NOACKS || timer_expired(&e->noacks_timer)) { PRINTF("drop %d\n", neighbor->u8[0]); list_remove(*list->list, e); memb_free(list->memb, e); return; } } else if(mac_status == MAC_TX_OK) { e->noacks = 0; } } else { /* No matching phase was found, so we allocate a new one. */ if(mac_status == MAC_TX_OK && e == NULL) { e = memb_alloc(list->memb); if(e == NULL) { PRINTF("phase alloc NULL\n"); /* We could not allocate memory for this phase, so we drop the last item on the list and reuse it for our phase. */ e = list_chop(*list->list); } rimeaddr_copy(&e->neighbor, neighbor); e->time = time; #if PHASE_DRIFT_CORRECT e->drift = 0; #endif e->noacks = 0; list_push(*list->list, e); } } }
/*---------------------------------------------------------------------------*/ void phase_remove(const struct phase_list *list, const rimeaddr_t *neighbor) { struct phase *e; e = find_neighbor(list, neighbor); if(e != NULL) { list_remove(*list->list, e); memb_free(list->memb, e); } }
bool QuadTree::has_neighbor_intersection_region(iterator_base & leaf_it) { iterator_base left_neighbor = find_neighbor(leaf_it, L); if(left_neighbor.node && left_neighbor->region_intersection_flag()==QuadTreeNodeData::INTERSECTION_REGION) return true; iterator_base right_neighbor = find_neighbor(leaf_it, R); if(right_neighbor.node && right_neighbor->region_intersection_flag()==QuadTreeNodeData::INTERSECTION_REGION) return true; iterator_base top_neighbor = find_neighbor(leaf_it, T); if(top_neighbor.node && top_neighbor->region_intersection_flag()==QuadTreeNodeData::INTERSECTION_REGION) return true; iterator_base bot_neighbor = find_neighbor(leaf_it, B); if(bot_neighbor.node && bot_neighbor->region_intersection_flag()==QuadTreeNodeData::INTERSECTION_REGION) return true; return false; }
void *process_msg(void *arg){ if(!ELECTION){ //If this node has not started election, it has not yet found its neighbor. ELECTION=1; neighbor_index = find_neighbor(); } char response[1024]; response[0] = '\0'; argument *msg = (argument *) arg; int newsockfd = msg->sock; //printf("Received - %s %s %s\n", msg->str1, msg->str2, msg->str3); if(strcmp(msg->str1,"ELECTION")==0){ strcpy(response, "ACK"); //printf("\nResponse sent-\n%s\n", response); int n = send(newsockfd, response, strlen(response), 0); if(n<0){ printf("Cannot write to socket. Possibly the node has disconnected..\n"); } printf("Processing election message..neighbor - %d:%d.\n\n", neighbor_index, neighbor[neighbor_index]); int rcvd_id = atoi(msg->str2); if(rcvd_id==ID){ sprintf(buffer, "LEADER %d", ID); send_to_neighbor(buffer, neighbor_up, neighbor_index); //printf("Leader message sent..\n"); } if(rcvd_id <= local_id){ local_id = rcvd_id; sprintf(buffer, "ELECTION %d", rcvd_id); send_to_neighbor(buffer, neighbor_up, neighbor_index); //printf("Election message sent..\n"); } else { //Forward your own id or better local_id. sprintf(buffer, "ELECTION %d", local_id); send_to_neighbor(buffer, neighbor_up, neighbor_index); } } else if(strcmp(msg->str1,"LEADER")==0){ strcpy(response, "ACK"); printf("\nResponse sent-\n%s\n", response); int n = send(newsockfd, response, strlen(response), 0); if(n<0){ printf("Cannot write to socket. Possibly the node has disconnected..\n"); } int rcvd_id = atoi(msg->str2); printf("Processing leader message..neighbor - %d:%d, rcvd - %d.\n\n", neighbor_index, neighbor[neighbor_index], rcvd_id); if(rcvd_id==ID){ //Drop the message. All have been notified. printf("\n\nAll have been notified that I am the leader.\n"); printf("\nMy ID - %d, Leader elected - %d.\n\n", ID, rcvd_id); NO_LEADER = 0; } if(rcvd_id!=local_id){ //Edit your own local_id(leader) and forward to neighbor. local_id = rcvd_id; sprintf(buffer, "LEADER %d", rcvd_id); send_to_neighbor(buffer, neighbor_up, neighbor_index); printf("\nMy ID - %d, Leader elected - %d.\n\n", ID, rcvd_id); NO_LEADER = 0; } else { //Just forward to neighbor. sprintf(buffer, "LEADER %d", rcvd_id); send_to_neighbor(buffer, neighbor_up, neighbor_index); printf("\nMy ID - %d, Leader elected - %d.\n\n", ID, rcvd_id); NO_LEADER = 0; } } else if(strcmp(msg->str1,"ACK")){ //printf("ack received..\n"); //ignore } else{ printf("Invalid request, cannot understand command.%s:%s.\n", msg->str1, msg->str2); } bzero(buffer,1024); //printf("msg process exited\n"); pthread_exit(NULL); }
/*---------------------------------------------------------------------------*/ phase_status_t phase_wait(struct phase_list *list, const rimeaddr_t *neighbor, rtimer_clock_t cycle_time, rtimer_clock_t guard_time, mac_callback_t mac_callback, void *mac_callback_ptr, struct rdc_buf_list *buf_list) { struct phase *e; size_t tmp; // const rimeaddr_t *neighbor = packetbuf_addr(PACKETBUF_ADDR_RECEIVER); /* We go through the list of phases to find if we have recorded a phase for this particular neighbor. If so, we can compute the time for the next expected phase and setup a ctimer to switch on the radio just before the phase. */ e = find_neighbor(list, neighbor); if(e != NULL) { rtimer_clock_t wait, now, expected, sync; clock_time_t ctimewait; /* We expect phases to happen every CYCLE_TIME time units. The next expected phase is at time e->time + CYCLE_TIME. To compute a relative offset, we subtract with clock_time(). Because we are only interested in turning on the radio within the CYCLE_TIME period, we compute the waiting time with modulo CYCLE_TIME. */ /* printf("neighbor phase 0x%02x (cycle 0x%02x)\n", e->time & (cycle_time - 1), cycle_time);*/ /* if(e->noacks > 0) { printf("additional wait %d\n", additional_wait); }*/ now = RTIMER_NOW(); sync = (e == NULL) ? now : e->time; #if PHASE_DRIFT_CORRECT { int32_t s; if(e->drift > cycle_time) { s = e->drift % cycle_time / (e->drift / cycle_time); /* drift per cycle */ s = s * (now - sync) / cycle_time; /* estimated drift to now */ sync += s; /* add it in */ } } #endif /* Check if cycle_time is a power of two */ if(!(cycle_time & (cycle_time - 1))) { /* Faster if cycle_time is a power of two */ wait = (rtimer_clock_t)((sync - now) & (cycle_time - 1)); } else { /* Works generally */ wait = cycle_time - (rtimer_clock_t)((now - sync) % cycle_time); } if(wait < guard_time) { wait += cycle_time; } ctimewait = (CLOCK_SECOND * (wait - guard_time)) / RTIMER_ARCH_SECOND; if(ctimewait > PHASE_DEFER_THRESHOLD) { struct phase_queueitem *p; p = memb_alloc(&queued_packets_memb); if(p != NULL) { if(buf_list == NULL) { p->q = queuebuf_new_from_packetbuf(); } p->mac_callback = mac_callback; //What the hell !! memcpy(&(p->mac_callback_ptr),&(mac_callback_ptr),sizeof(p->mac_callback_ptr)); memcpy(&(p->buf_list),&(buf_list),sizeof(p->buf_list)); ctimer_set(&p->timer, ctimewait, send_packet, p); return PHASE_DEFERRED; } else { memb_free(&queued_packets_memb, p); } } expected = now + wait - guard_time; if(!RTIMER_CLOCK_LT(expected, now)) { /* Wait until the receiver is expected to be awake */ // printf("%d ",expected%cycle_time); //for spreadsheet export while(RTIMER_CLOCK_LT(RTIMER_NOW(), expected)); } return PHASE_SEND_NOW; } return PHASE_UNKNOWN; }
int c_p(w_coord wcoord, b_coord bcoord) { server_socket *s; w_coord oldwcoord; int x; int y; int z; n_coord newcenter; void *p; lt_packet out_packet; lattice_message mess; lattice_bump submess; client_func_checks(); oldwcoord = lattice_player.wpos; newcenter = wcoord_to_ncoord(wcoord); if (!find_neighbor(newcenter)) { if (!lattice_player.last_p_oob) { mess.type = T_BUMP; ClrFlagFrom(&mess); mess.fromuid = 0; mess.length = sizeof submess; mess.args = &submess; submess.wcoord = lattice_player.wpos; submess.bcoord = lattice_player.bpos; submess.bad_wcoord = wcoord; submess.bad_bcoord = bcoord; (*gcallback)(&mess); } lattice_player.last_p_oob = 1; return 0; } lattice_player.last_p_oob = 0; lattice_player.wpos = wcoord; lattice_player.bpos = bcoord; if (user_is_within_outer_border(lattice_player.wpos, lattice_player.centeredon)) { // we are within the centered servers outer border for (x=0;x<3;x++) for (y=0;y<3;y++) for (z=0;z<3;z++) { if ((s=neighbor_table[x][y][z])) { if (user_is_within_outer_border(lattice_player.wpos, s->coord)) { // we need to relay the P makepacket(&out_packet, T_P); p = &out_packet.payload; if (!put_wx(&p, wcoord.x, &PLength(&out_packet), &PArgc(&out_packet))) return 1; if (!put_wy(&p, wcoord.y, &PLength(&out_packet), &PArgc(&out_packet))) return 1; if (!put_wz(&p, wcoord.z, &PLength(&out_packet), &PArgc(&out_packet))) return 1; if (!put_bx(&p, bcoord.x, &PLength(&out_packet), &PArgc(&out_packet))) return 1; if (!put_by(&p, bcoord.y, &PLength(&out_packet), &PArgc(&out_packet))) return 1; if (!put_bz(&p, bcoord.z, &PLength(&out_packet), &PArgc(&out_packet))) return 1; sendpacket(s, &out_packet); //sendto_one(s, "P %d %d %d %d %d %d\n", wcoord.x, wcoord.y, wcoord.z, bcoord.x, bcoord.y, bcoord.z); } else { // need to ENDP if we are walking away if (user_is_within_outer_border(oldwcoord, s->coord)) { makepacket(&out_packet, T_ENDP); sendpacket(s, &out_packet); //sendto_one(s, "ENDP\n"); } } } } } else { // we need to recenter to the neighboring server packet_recenter_neighbors(newcenter); } return 0; }
int main() { NETWORK *net; //MEMORY_SLPA *mem; PROCESSING_QUEUE *que; NEIGHBOR_VERTICES *nei; LABEL_PROBABILITIES *pro,*pro1; SEND_LIST *sendlist; RESULT_VERTEX *result1, *comp1; RESULT_COMMUNITY *result2, *comp2; COEFFICIENT1 *co1; COEFFICIENT2 *co2; DELETE_LIST *dellist; int i,j,iterator,c,k,*synlist; float nmi; //int MAX_T; FILE *fp,*fout; //if((fp=fopen("dolphins.gml","r"))==NULL) if((fp=fopen("network0.dat","r"))==NULL) { printf("can not open file\n"); system("pause"); exit(0); } if((fout=fopen("output.txt","w"))==NULL) { printf("can not open file\n"); system("pause"); exit(0); } net=(NETWORK *)malloc(sizeof(NETWORK)); //mem=(MEMORY_SLPA *)malloc(sizeof(MEMORY_SLPA)); que=(PROCESSING_QUEUE *)malloc(sizeof(PROCESSING_QUEUE)); nei=(NEIGHBOR_VERTICES *)malloc(sizeof(NEIGHBOR_VERTICES)); pro=(LABEL_PROBABILITIES *)malloc(sizeof(LABEL_PROBABILITIES)); pro1=(LABEL_PROBABILITIES *)malloc(sizeof(LABEL_PROBABILITIES)); //pro->index=NULL; //pro->probability=NULL; sendlist=(SEND_LIST *)malloc(sizeof(SEND_LIST)); result1=(RESULT_VERTEX *)malloc(sizeof(RESULT_VERTEX)); comp1=(RESULT_VERTEX *)malloc(sizeof(RESULT_VERTEX)); result2=(RESULT_COMMUNITY *)malloc(sizeof(RESULT_COMMUNITY)); comp2=(RESULT_COMMUNITY *)malloc(sizeof(RESULT_COMMUNITY)); co1=(COEFFICIENT1 *)malloc(sizeof(COEFFICIENT1)); co2=(COEFFICIENT2 *)malloc(sizeof(COEFFICIENT2)); dellist=(DELETE_LIST *)malloc(sizeof(DELETE_LIST)); MEMORY_VERTEX *mem1; mem1=(MEMORY_VERTEX *)malloc(sizeof(MEMORY_VERTEX)); printf("Reading network..."); read_network_bn(net,fp); //read_network(net,fp); //show_network(net,fout); printf("finish!\n"); fclose(fp); if((fp=fopen("community0.dat","r"))==NULL) { printf("can not open file\n"); system("pause"); exit(0); } printf("Reading communities of BN..."); read_communities(comp1,fp); community_statistics(comp1,comp2); //show_result_vertex(comp1,fout); //show_result_community(comp2,fout); printf("finish!\n"); //sort_network(net,1); printf("Calculating coefficients..."); calculate_coefficient(net,co1,co2); printf("finish!\n"); dellist->nvertices=net->nvertices; dellist->nlabels=(int *)calloc(dellist->nvertices,sizeof(int)); dellist->label=(int **)malloc(dellist->nvertices*sizeof(int *)); //for(i=0;i<co1->nvertices;i++){ // // fprintf(fout,"%d %f\n",i,co1->coefficients[i]); //} //fprintf(fout,"asdasdasdd\n"); //for(i=0;i<co2->nvertices;i++){ // for (j=0;j<co2->degrees[i];j++) // { // fprintf(fout,"%f ",co2->coefficients[i][j]); // } // fprintf(fout,"\n"); //} // //MAX_T=0; //while(MAX_T<100){ // MAX_T++; c=10; while(c--){ synlist=(int *)malloc(net->nvertices*sizeof(int)); printf("Initialing memory..."); //initial_memory(mem, net->nvertices); //show_memory(mem,fout); initial_memory1(mem1, net->nvertices); //show_memory1(mem1,fout); printf("finish!\n"); initial_queue(que, net->nvertices); iterator=MAX_T; printf("Calculating..."); while(iterator--){ //处理队列随机排序 random_sort(que); //show_queue(que); for(i=0;i<que->length;i++){ //查找邻节点 find_neighbor(net,nei,que->queue[i]); //printf("%d ",que->queue[i]); //show_neighbor(nei); sendlist->num=nei->degree; sendlist->list=(SL_ELEMENT *)malloc(nei->degree*sizeof(SL_ELEMENT)); for(j=0;j<nei->degree;j++){ //计算概率 //count_probabilities(pro,mem,nei->neighbor[j],MAX_T-iterator); count_probabilities1(pro1,mem1,nei->neighbor[j]); //printf("pro1: "); //for(k=0;k<pro->numlabel;k++){ // printf("%d %f ",pro->index[k],pro->probability[k]); //} //printf("\npro2: "); //for(k=0;k<pro1->numlabel;k++){ // printf("%d %f ",pro1->index[k],pro1->probability[k]); //} //printf("\n"); //if(!pro_compare(pro,pro1)){ // printf("%d %d %d %d",iterator,i,j,nei->neighbor[j]); // system("pause"); //} //根据概率发送标签至sendlist k=send_label(pro1,&sendlist->list[j].index); sendlist->list[j].weight=(1+co2->coefficients[que->queue[i]][j])*pro1->probability[k]; //sendlist->list[j].weight=1+co2->coefficients[que->queue[i]][j]; sendlist->list[j].weight2=co1->coefficients[nei->neighbor[j]]; //send_label(pro1,&sendlist->list[j].index); free(pro1->index); free(pro1->probability); } //show_sendlist(sendlist,fout); free(nei->neighbor); //记录dellist //count_delete_vertices(mem1,sendlist,dellist,que->queue[i]); //从sendlist中选择标签 //j=receive_label(sendlist); j=receive_label_old(sendlist); //异步写入memory //mem->memory[que->queue[i]][MAX_T-iterator]=j; //add_label(mem1,que->queue[i],j); //记录同步列表synlist synlist[que->queue[i]]=j; free(sendlist->list); } //同步接收标签 add_label_syn(mem1,synlist); //删除dellist中的标签 //delete_vertices(mem1,dellist); //重置dellist //reset_delete_list(dellist); } free(synlist); free(que->queue); show_memory1(mem1,fout); printf("finish!\n"); result1->nvertices=mem1->nvertices; result1->numbelong=(int *)malloc(result1->nvertices*sizeof(int)); result1->communities=(int **)malloc(result1->nvertices*sizeof(int*)); printf("Post processing..."); for(i=0;i<result1->nvertices;i++){ //count_probabilities(pro,mem,net->vertex[i].id,MAX_T+1); count_probabilities1(pro,mem1,net->vertex[i].id); post_processing(pro,result1,i,0.05); } printf("finish!\n"); free(pro->index); free(pro->probability); //for(i=0;i<mem->nvertices;i++){ // free(mem->memory[i]); //} //free(mem->memory); destroy_memory(mem1); //printf("Output results..."); //show_result_vertex(result1,fout); community_statistics(result1,result2); //show_result_community(result2,fout); nmi=1-(result_comparison(comp2,result2,net->nvertices)+result_comparison(result2,comp2,net->nvertices))/2; fprintf(fout,"%f\t",nmi); //删除被完全包含的社区 result_processing(result1,result2); //show_result_community(result2,fout); //printf("finish!\n"); printf("Calculating NMI..."); //log((float)exp((float)1)); nmi=1-(result_comparison(comp2,result2,net->nvertices)+result_comparison(result2,comp2,net->nvertices))/2; printf("finish!\n"); fprintf(fout,"%f\n",nmi); for(i=0;i<result1->nvertices;i++){ free(result1->communities[i]); } free(result1->communities); free(result1->numbelong); for(i=0;i<result2->ncommunities;i++){ free(result2->vertices[i]); } free(result2->vertices); free(result2->nvertices); } //} for(i=0;i<comp1->nvertices;i++){ free(comp1->communities[i]); } free(comp1->communities); free(comp1->numbelong); for(i=0;i<comp2->ncommunities;i++){ free(comp2->vertices[i]); } free(comp2->vertices); free(comp2->nvertices); free(dellist->label); free(dellist->nlabels); free(dellist); free(mem1); free(que); free(nei); free(sendlist); free_network(net); fclose(fout); fclose(fp); //printf("hello world!!"); system("pause"); return 0; }