tvm_t* tvm_create(char* filename) { tvm_t* vm = (tvm_t*)malloc(sizeof(tvm_t)); if(!vm) return NULL; vm->pMemory = create_memory(MIN_MEMORY_SIZE); if(!vm->pMemory) { free(vm); return NULL; } vm->pProgram = create_program(); if(!vm->pProgram) { destroy_memory(vm->pMemory); free(vm); return NULL; } create_stack(vm->pMemory, MIN_STACK_SIZE); if(interpret_program(vm->pProgram, filename, vm->pMemory) != 0) { // avoid memory leak when the interpret_program failed! tvm_destroy(vm); return NULL; } //if(!vm || !vm->pMemory || !vm->pProgram) return NULL; return vm; }
void tvm_destroy(tvm_t* vm) { if(vm && vm->pMemory)destroy_memory(vm->pMemory); if(vm && vm->pProgram)destroy_program(vm->pProgram); if(vm) free(vm); }
int main(int argc, char** argv) { int i, eviction_algorithm, seed; //Parse input if(argc == 2){ eviction_algorithm = atoi(argv[1]); seed = time(NULL); }else if(argc == 3) { eviction_algorithm = atoi(argv[1]); seed = atoi(argv[2]); }else{ printf("1st argument: eviction algorithm (0-Random; 1-Clock; 2-Clock2ndChance)\n2nd argument: seed for random algorithm\n"); return -1; } init_memory(eviction_algorithm, seed); printf("STORE VALUES IN ORDER value = vAddr+1 (press enter to continue):\n"); memoryMaxer(PAGE_TABLE_SIZE+2);//beyond the limit (the last 2 page creations are ignored) print_memory_state(); getchar(); printf("GET_VALUE FROM ALL ADDRESSES (press enter to continue):\n"); for(i = 0; i < PAGE_TABLE_SIZE; i++){ if(get_value(i, NULL) != i+1){ printf("wrong answer for page %d\n", i); return -1;//not supposed to happen } } print_memory_state(); getchar(); printf("FREE RANDOM pages AND TRY TO GET_VALUE. AND CREATE SOME PAGES (press enter to continue):\n"); for (i = 0; i < 20; i++){ int addr = uniform_rand(0, PAGE_TABLE_SIZE); printf("free %d\n", addr); free_page(addr); int valid; get_value(addr, &valid); if(valid == 0) printf("Couldn't read from addr %d\n", addr); } for (i = 0; i < 10; i++) { vAddr addr = create_page(); uint32_t v = uniform_rand(0, PAGE_TABLE_SIZE); store_value(addr, &v); printf("created page %d with value %d\n", addr, v); } print_memory_state(); getchar(); printf("ALTERNATE STORE AND GET (press enter to continue)\n"); for (i = 0; i < 20; i++){ int addr = uniform_rand(0, PAGE_TABLE_SIZE); uint32_t v = uniform_rand(0, 100); store_value(addr, &v); printf("store %d in vAddr %d\n", v, addr); int valid; addr = uniform_rand(0, PAGE_TABLE_SIZE); v = get_value(addr, &valid); if(valid) printf("got %d from addr %d\n", v, addr); else printf("Couldn't read from addr %d\n", addr); } print_memory_state(); getchar(); destroy_memory(); 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; }