void ZFastFetch::distribute_hash(uint8_t* start, uint32_t num_bytes, hash_t* result) {
	hash_assign assignments[NUM_THREADS];
	hash_t results[NUM_THREADS];

	uint32_t num_bytes_per_thread = (int)(num_bytes / (float) NUM_THREADS);

	for (int i = 0; i < NUM_THREADS; i++) {
		assignments[i].start = start + i * num_bytes_per_thread;
		assignments[i].result = &results[i];
		assignments[i].event_done = this->syncf->new_event(true);

		if (i < NUM_THREADS - 1) { 
			assignments[i].num_bytes = num_bytes_per_thread;
		} else { // Assign any leftovers to the final thread
			assignments[i].num_bytes = num_bytes - (i * num_bytes_per_thread);
		}
		//ZLC_TERSE(ze, "Dispatching a thread\n");
		this->tf->create_thread(parallel_hash, (void*)&assignments[i], STACK_SIZE);
	}

	// Wait for the results (serially, since we don't have a semaphor)
	for (int i = 0; i < NUM_THREADS; i++) {
		assignments[i].event_done->wait();
		delete assignments[i].event_done;	// Clean up the memory
	}

	// Combine all of the results
	zhash((uint8_t*)results, sizeof(hash_t)*NUM_THREADS, result);
}
void parallel_hash(void* arg) {
	hash_assign* assign = (hash_assign*) arg;

	zhash(assign->start, assign->num_bytes, assign->result);

	assign->event_done->signal();
}
ZBinaryRecord::ZBinaryRecord(const uint8_t* binary, uint32_t binaryLen) 
	: ZRecord(NULL, ZOOG_RECORD_TYPE_BINARY ) {
	this->flags = 0;
	this->digestType = ZOOG_DIGEST_DEFAULT;

	this->digest = malloc_array(HASH_SIZE);
	if(!this->digest ) { Throw(CryptoException::OUT_OF_MEMORY, "Failed to allocate digest"); }

	zhash(binary, binaryLen, (hash_t*)this->digest);

	this->addDataLength(sizeof(flags) + sizeof(digestType) + HASH_SIZE);  
}
Beispiel #4
0
void imcsplay(int argc, char **argv) {
    setlinebuf(stdout);
    if (argc < 5 || argc > 7)
	usage();
    char mecolor = '?';
    int megame = 0;
    switch(argv[2][0]) {
    case 'O':
	switch(argv[2][1]) {
	case 'W':
	case 'B':
	case '?':
	    mecolor = argv[2][1];
	    break;
	default:
	    usage();
	}
	break;
    case 'A': {
	char ch = argv[2][1];
	if (isdigit(ch)) {
	    megame = atoi(&argv[2][1]);
	} else if (ch == 'W' || ch == 'B') {
	    mecolor = ch;
	    megame = atoi(&argv[2][2]);
	} else {
	    usage();
	}
	if (megame <= 0)
	    usage();
	break;
    }
    default:
	usage();
    }
    char *meuser = argv[3];
    char *mepassword = argv[4];
    char *host = "imcs.svcs.cs.pdx.edu";
    if (argc > 5)
	host = argv[5];
    int port = 3589;
    if (argc > 6) {
	port = atoi(argv[6]);
	if (port <= 0)
	    usage();
    }
    FILE *nf = netopen(host, port);
    setlinebuf(nf);
    startlog();
    char *greeting = expectcmd(nf, 1, 100, 0);
    (void) strtok(greeting, " ");
    char *pgm = strtok(0, " ");
    assert(!strcmp(pgm, "imcs"));
    char *version = strtok(0, " \r\n");
    if(strcmp(version, "2.5")) {
	fprintf(stderr, "got unexpected imcs version %s\n", version);
	exit(1);
    }
    sendcmd(nf, "me %s %s", meuser, mepassword);
    (void) expectcmd(nf, 1, 201, 0);
    if (megame != 0) {
	if (mecolor == '?')
	    sendcmd(nf, "accept %d", megame);
	else
	    sendcmd(nf, "accept %d %c", megame, mecolor);
	(void) expectcmd(nf, 1, 105, 106, 0);
    } else {
	if (mecolor == '?')
	    sendcmd(nf, "offer");
	else
	    sendcmd(nf, "offer %c", mecolor);
	(void) expectcmd(nf, 1, 103, 0);
	logmsg("waiting for opponent");
	(void) expectcmd(nf, 1, 105, 106, 0);
	logmsg("opponent found");
    }
    struct state s = s0;
    s.cureval = eval(&s);
    if (nttable > 0)
	s.curzhash = zhash(&s);
    while (1) {
	int ch = fgetc(nf);
	int r = ungetc(ch, nf);
	assert(r != EOF);
	if (isdigit(ch)) {
	    s = readstate(nf, 1);
	    s.cureval = eval(&s);
	    if (nttable > 0)
		s.curzhash = zhash(&s);
	    continue;
	}
	switch (ch) {
	case '?': {
	    char *r = getnet(nf, "?");
	    char *q = strtok(r, " ");
	    assert(!strcmp(q, "?"));
	    char *tl = strtok(0, " ");
	    char *tr = strtok(0, " ");
	    assert(tl && tr);
	    int t = readtimems(tl);
	    t = 95 * t / (100 * ((81 - s.ply) / 2));
	    struct move m = idnegamax(&s, t, 0);
	    logmsg("value %d at time %d depth %d for %s\n\n",
		   v0, t, d0, movestr(&m));
	    move(&s, &m, 0);
	    sendcmd(nf, "%s", movestr(&m));
	    printstate(&s, 1);
	    if (ponder)
		(void) idnegamax(&s, 0, nf);
	    continue;
	}
	case '!':
	    assert(fgetc(nf) == '!');
	    int ch;
	    do
		ch = fgetc(nf);
	    while (isspace(ch));
	    ungetc(ch, nf);
	    struct move m = getmove(nf, &s);
	    move(&s, &m, 0);
	    continue;
	case '=':
	    (void) getnet(nf, "=");
	    break;
	case 'X':
	    (void) getnet(nf, "X");
	    break;
	default:
	    (void) getnet(nf, "...");
	    continue;
	}
	break;
    }
    fclose(nf);
}