// Like 'search', but do in parallel across 2 nodes with 2 threads each int psearch(uint8_t *str, int len, const unsigned char *hash) { if (len <= BLOCKLEN) { search_args a = { {0}, len, 0, len, hash }; strcpy((char*)a.str, (char*)str); search(&a); return found; } // Iterate over blocks, searching 4 blocks at a time in parallel int done = 0; do { int i; search_args a[nthreads]; for (i = 0; i < nthreads; i++) { strcpy((char*)a[i].str, (char*)str); //cprintf("forking child to check '%s'\n", str); a[i].len = len; a[i].lo = 0; a[i].hi = BLOCKLEN; a[i].hash = hash; bench_fork(i, search, &a[i]); done |= incstr(str, BLOCKLEN, len); } for (i = 0; i < nthreads; i++) bench_join(i); // collect results if (found) return 1; } while (!done); return 0; // no match at this string length }
void matmult(int nbi, int nbj, int dim) { assert(dim >= 1 && dim <= MAXDIM); assert(nbi >= 1 && nbi <= dim); assert(dim % nbi == 0); assert(nbj >= 1 && nbj <= dim); assert(dim % nbj == 0); int nth = nbi*nbj; assert(nth >= 1 && nth <= 256); int bi,bj; struct tharg arg[256]; // Fork off a thread to compute each cell in the result matrix for (bi = 0; bi < nbi; bi++) for (bj = 0; bj < nbj; bj++) { int child = bi*nbi + bj; arg[child].bi = bi; arg[child].bj = bj; arg[child].nbi = nbi; arg[child].nbj = nbj; arg[child].dim = dim; bench_fork(child, blkmult, &arg[child]); } // Now go back and merge in the results of all our children for (bi = 0; bi < nbi; bi++) for (bj = 0; bj < nbj; bj++) { int child = bi*nbi + bj; bench_join(child); } }
void writetest(struct args *a) { int th; for (th = 0; th < a->nthreads; th++) { a->thread = th; bench_fork(th, writefun, &a); } for (th = 0; th < a->nthreads; th++) { bench_join(th); } }
int main(int argc, char *argv[]) { bench_fork(); bench_thread(); bench_context_fork(); bench_context_thread(); return 0; }