int main(int argc, char ** argv){ db_t i; db_t first; if(argc>1) { i = atoi(argv[1]); dbase = malloc_file(sizeof(struct ITEM)*DBSIZE,FMODE_RO,"dbase24"); printf("%08X %X %X\n",dbase[i].db,dbase[i].dw,dbase[i].dd); return 0; } dbase = malloc_file(sizeof(struct ITEM)*DBSIZE,FMODE_CR,"dbase24"); lastp = dbsize; first = db_alloc(); dbase[first].db = 0xfff00fff; dbase[first].dw = 0x00000fff; dbase[first].dd = 0; rootp[dbase[first].dw] = first; time(&started); for(level=0;;level++){ tprintf("level=%d tail=%d dbsize=%u\n",level,dbsize-lastp,dbsize); db_t newp = dbsize; for(;lastp!=newp;lastp++){ uint32_t w,b,d; Unpack(dbase[lastp].db,dbase[lastp].dw,dbase[lastp].dd,&w,&b,&d); MoveWhite(w,b,d); } if(dbsize==newp) break; } return 0; }
int main(int argc, char ** argv){ int i; if( (argc>=2) && (atoi(argv[1])!=RANK)) error("rank %d mandatory",RANK); printf("CUDA RANK=%d\n",RANK); kernel.print(); // build busylist busylist = (uint32_t*)malloc_file(CNK*sizeof(uint32_t),FMODE_RO,BLIST_FORMAT,RANK); // put busylist SafeCall(cuMemHostRegister(busylist,CNK*sizeof*busylist,CU_MEMHOSTREGISTER_DEVICEMAP)); SafeCall(cuMemHostGetDevicePointer(&host_busylist,busylist,0)); SafeCall(cuModuleGetGlobal(&dev_busylist,&bytes,kernel.module[0].module,"busylist")); if(bytes!=sizeof(host_busylist)) error("busylist!"); SafeCall(cuMemcpyHtoD(dev_busylist,&host_busylist,bytes)); // put array #ifdef IN_mk_data mkdir(DATADIR,0755); errno=0; array = (unsigned char *)malloc_file(abytes(RANK,CNK),1,DATADIR"%d",RANK); #else array = (unsigned char *)malloc_file(abytes(RANK,CNK),0,DATADIR"%d",RANK); #endif SafeCall(cuMemHostRegister(array,abytes(RANK,CNK),CU_MEMHOSTREGISTER_DEVICEMAP)); SafeCall(cuMemHostGetDevicePointer(&host_array,array,0)); SafeCall(cuModuleGetGlobal(&dev_array,&bytes,kernel.module[0].module,"array")); if(bytes!=sizeof(host_array)) error("array!"); SafeCall(cuMemcpyHtoD(dev_array,&host_array,bytes)); #define THREADS 512 #define MAXG 65535 uint64_t nado = (cnk[RANK] +(THREADS-1))/THREADS; uint32_t gridx = nado>MAXG?MAXG:nado; uint32_t gridy = (nado+(MAXG-1))/MAXG; printf("gridy=%d gridx=%d THREAD=%d\n",gridy, gridx, THREADS); kernel.launch(params,THREADS,gridx,gridy); kernel.wait(); SafeCall(cuMemHostUnregister(busylist)); SafeCall(cuMemHostUnregister(array)); SafeCall(cuModuleGetGlobal(&dev_changed,&bytes,kernel.module[0].module,"changed")); if(bytes!=sizeof(changed)) error("changed!"); SafeCall(cuMemcpyDtoH(changed,dev_changed,bytes)); for(i=0;i<CACHESIZE;i++) total += changed[i]; printf("changed=%ju\n",total); return 0; }
b_torrent* b_torrent_recover(const char* filename) { // b_torrent* bt = malloc(sizeof(b_torrent)); int i; uint64_t file_size ; char *url, *name; FILE* fp = fopen(filename, "rb"); // fseek(fp, 0, SEEK_SET); if (fp == NULL) { fprintf(stderr, "%s:%d can not open file[%s]\n", __FILE__, __LINE__, filename); return NULL; } size_t len; FREAD_KV_STRING(bt->name, len, fp); FREAD_KV_STRING(bt->comment, len, fp); FREAD_KV_STRING(bt->created_by, len, fp); FREAD_KV_STRING(bt->encoding, len, fp); FREAD_KV_STRING(bt->pieces, len, fp); FREAD_KV_SIZE_OF(bt->create_date, len, fp); FREAD_KV_SIZE_OF(bt->piece_size, len, fp); FREAD_KV_SIZE_OF(bt->total_size, len, fp); FREAD_KV_SIZE_OF(bt->info_hash, len, fp); FREAD_KV_SIZE_OF(bt->peer_id, len, fp); FREAD_KV_SIZE_OF(bt->uploaded, len, fp); FREAD_KV_SIZE_OF(bt->downloaded, len, fp); FREAD_KV_SIZE_OF(bt->left, len, fp); // tracker FREAD_KV_SIZE_OF(bt->tracker_len, len, fp); b_torrent_tracker thead; b_torrent_tracker* tracker = &thead; for (i = 0; i < bt->tracker_len; i++) { FREAD_KV_STRING(url, len, fp); tracker = tracker->next = malloc_tracker(url, len); free(url); } bt->tracker = thead.next; // file FREAD_KV_SIZE_OF(bt->file_len, len, fp); b_torrent_file fhead; b_torrent_file* file = &fhead; for (i = 0; i < bt->file_len; i++) { FREAD_KV_SIZE_OF(file_size, len, fp); FREAD_KV_STRING(name, len, fp); file = file->next = malloc_file(name, len, file_size); free(name); } bt->file = fhead.next; fclose(fp); return bt; }
static void init_dbase(int maxrank){ int rank; char fname[PATH_MAX]; for(rank=1;rank<maxrank;rank++) { struct stat buf; snprintf(fname,sizeof(fname),DB_FORMAT,rank); if(stat(fname,&buf)) continue; db[rank] = malloc_file(buf.st_size,FMODE_RO,"%s",fname); //printf("%s in use\n",fname); } }
int main(int argc, char ** argv){ uint32_t x,b,w,d; unsigned char * array; int rank; int idx = 0; if(argc!=4) panic(); b = getarg(1); w = getarg(2); d = getarg(3); rank = _popc(b); array = malloc_file(ARRAY_SIZE_S(rank),FMODE_RO,DATA_FORMAT,rank); // search index for(x=ALLONE(rank);x!=b;x=_permut(x)) idx++; if ( blist_get(b) != idx ) error("What's f***a with blist\n"); // int res = twobit_get(array+(uint64_t)((w<<rank)|d)*cnk(32,rank)/4,idx); printf("%08X %X %X = %d\n",b,w,d,res); return 0; }
static void _b_torrent_init (b_torrent* tt, b_encode* bp) { b_dict* bd = bp->data.dpv; char buf[1024]; // printf("type=%d\n", bp->type); while(NULL != bd) { // printf("key=%s\n", bd->key); unsigned int key_len = strlen(bd->key); if(strncmp("announce-list", bd->key, max(key_len, 13)) == 0) { b_list* bl = bd->value->data.lpv; b_torrent_tracker head; b_torrent_tracker* tp = &head; tt->tracker_len = 0; while(NULL != bl) { b_encode* bll = bl->item->data.lpv->item; tp = tp->next = malloc_tracker(bll->data.cpv, bll->len); // printf("%s\n", tp->url); bl = bl->next; tt->tracker_len++; } tt->tracker = head.next; } if(strncmp("announce", bd->key, max(key_len, 8)) == 0) { tt->tracker_len = 0; if (NULL == tt->tracker) { b_encode* bp = bd->value; tt->tracker = malloc_tracker(bp->data.cpv, bp->len); tt->tracker_len = 1; } } if(strncmp("comment", bd->key, max(key_len, 7)) == 0) { tt->comment = malloc_string(bd->value->data.cpv, bd->value->len); } if(strncmp("encoding", bd->key, max(key_len, 8)) == 0) { tt->encoding = malloc_string(bd->value->data.cpv, bd->value->len); } if(strncmp("created by", bd->key, max(key_len, 10)) == 0) { tt->created_by = malloc_string(bd->value->data.cpv, bd->value->len); } if(strncmp("creation date", bd->key, max(key_len, 13)) == 0) { tt->create_date = bd->value->data.iv; } if (strncmp("length", bd->key, max(key_len, 6)) == 0) { tt->total_size = bd->value->data.iv; } if (strncmp("name", bd->key, max(key_len, 4)) == 0) { tt->name = malloc_string(bd->value->data.cpv, bd->value->len); } else if (strncmp("pieces", bd->key, max(key_len, 6)) == 0) { tt->pieces = malloc_string(bd->value->data.cpv, bd->value->len); } else if (strncmp("piece length", bd->key, max(key_len, 12)) == 0) { tt->piece_size = bd->value->data.iv; } else if (strncmp("files", bd->key, max(key_len, 5)) == 0) { b_list* list = bd->value->data.lpv; b_torrent_file head; b_torrent_file* tf = &head; tt->file_len = 0; while (list) { b_dict* ldict = list->item->data.dpv; int64_t size; unsigned int len = 0; for (; ldict != NULL; ldict = ldict->next) { key_len = strlen(ldict->key); if (strncmp("length", ldict->key, max(key_len, 6)) == 0) { size = ldict->value->data.iv; } if (strncmp("path", ldict->key, max(key_len, 4)) == 0) { b_list* paths = ldict->value->data.lpv; while (NULL != paths) { memcpy(buf + len, paths->item->data.cpv, paths->item->len); len += paths->item->len; paths = paths->next; } buf[len] = '\0'; } } // printf("file buffer: %s:%d\n", buf, size); tf = tf->next = malloc_file(buf, len, size); list = list->next; // set total size tt->total_size += size; tt->file_len++; // printf("%llu\n", (unsigned long long)tt->total_size); } tt->file = head.next; } else if (strncmp("info", bd->key, max(key_len, 4)) == 0) { _b_torrent_init(tt, bd->value); SHA1_CTX context; SHA1Init(&context); SHA1Update(&context, (unsigned char*)&bd->value->begin[0], bd->value->len); SHA1Final(tt->info_hash, &context); // tt->info_hash[20] = '\0'; } bd = bd->next; } // set peer_id srand_curr_time; sprintf((char*)tt->peer_id, "-XOY1000-%d", rand()); }
static void init_blist(void){ int rank; for(rank=1;rank<MAXRANK;rank++) blist[rank] = malloc_file(sizeof(uint32_t)*cnk[rank],FMODE_RO,BLIST_FORMAT,rank); }