static void subbCheck ( sTimer *tp ) { sub_sBuffer *bp = (sub_sBuffer *)tp->data; if (gdbroot->db->log.b.tmon) errh_Info("subbCheck: %u", tp->clock); #if 0 if (EXCL_ON) { /* Exclusive mode is on, requeue the buffer for later transmission */ addTime(nowTime(&tp->time), msToTime(NULL, bp->dt)); insertTimer(tp); } #endif if (subsm_SendBuffer(bp)) { setTimer(tp, msToClock(NULL, bp->dt)); insertTimer(tp); } else { freeTimer(tp); } }
int main() { int i,k,n; char *text; unsigned char *trans; FILE *f; initTimer(); // Open file f = fopen("/tmp/dbtss", "r"); if(f == 0) { printf("File not found.\n"); return 0; } // Obtain file size. fseek (f, 0 , SEEK_END); n = ftell (f); rewind (f); printf("File size: %d\n", n); text = malloc(sizeof(char)*(n)); k = fread(text, 1, n, f); printf("Read size: %d\n", k); fclose(f); addTimer("End reading"); // Translate trans = malloc(sizeof(char)*(n+50)); translate(trans, text, n, "ACGT|"); addTimer("End translating"); free(text); // Initialize tail for(i=n; i<n+50; i++) trans[i]=5; addTimer("End initialize tail"); // Sort int *result = malloc(sizeof(int)*n); count_radix(trans, result, n, 5); addTimer("End sorting"); printTimer(); freeTimer(); // for(i=0; i<n; i++){ // for(k=0; k<3; k++) // printf("%i", trans[result[i]+k]); // printf("\n"); // } return 0; }
// now main program, as usual, with some options parsing, etc. int main(int argc, char** argv){ int i; pTimer T = newTimer(); startTimer(T); // let's measure execution time of the whole program. // options parsing, configuration and reporting variables parseOptions(argc,argv); printf("[i] vector size is: %li\n",SIZE); printf("[i] threads requested: %i\n", PTHR); printf("[i] total mem for data: %1.2f MB\n",SIZE*sizeof(Data)/1024.0/1024.0); if(PTHR<=0){printf("Too few threads, exiting...\n");exit(0);} printf("\n"); // preparing data for calculations pVector V = newVector(SIZE); setVector(V,1.0); // preparing barrier for all threads involved in calculations if(pthread_barrier_init( &barrier, NULL, PTHR)){ fprintf(stderr,"[E] could not create barrier!\n"); exit(-1); } // preparing threads for calculations Info threads[PTHR]; for(i=0;i<PTHR;i++){ threads[i].nr = i; threads[i].V = V; threads[i].sum = 0.0; threads[i].start = (i )*SIZE/PTHR; threads[i].end = (i+1)*SIZE/PTHR; int rc = pthread_create(&threads[i].id,NULL,reduceVector,(void*) &threads[i]); if(rc){ fprintf(stderr,"[E] thread creation error, code returned: %i\n",rc); exit(-1); } else{ if(LOUD) printf("[i] thread %i: start=%i, end=%i\n",i,threads[i].start,threads[i].end); } } for(i=0;i<PTHR;i++){ if(pthread_join(threads[i].id,NULL)){ fprintf(stderr,"[e] Can't join thread id=%li, nr=%i\n",threads[i].id,threads[i].nr); return -1; } } // at the end we have to collect all data from threads double sum = 0.0; double time = 0.0; for(i=0;i<PTHR;i++){ sum += threads[i].sum; time+= threads[i].time; } stopTimer(T); printf("Main sum is %1.2f, avg thread time: %-1.12f s, TOTAL time: %1.12f\n",sum,time/PTHR,getTime(T)); freeTimer(T); }
// this function is our reduction function, it will be executed in parallel by threading // as a multiple threads sharing the same vector, and adding diffrent regions from it // data here is a pointer to struct Info with all necessary fields, like vector itself, // start and end of region to calculate and sum. There are also some other less important things. void* reduceVector(void* data){ int i; pTimer T = newTimer(); // we will measure time in each thread now pInfo info = (pInfo) data; // copy data structure info->time = 0.0; // time is zero at start if(info->V){ // if we have anything startTimer(T); // start timer! for(i=info->start; i<info->end; i++){ (info->sum) += (info->V->data[i]); // add all values to the sum } stopTimer(T); // store time info->time = getTime(T); // get time as a double precision value if(LOUD) printf("[i] thread %i, sum=%f, time=%1.4e\n",info->nr,info->sum,info->time); } freeTimer(T); // wait for all other threads to finish. int rc = pthread_barrier_wait(&barrier); if(rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD){ fprintf(stderr,"[E] Could not wait on barrier for some reason, code=%i\n",rc); exit(-1); } pthread_exit(NULL); // exit nicely }
ESA build_ESA(char *pStr, int size, char *pAlphabet, char *pIgnore, int free_pStr) { // Check if the string includes a zero termination if(pStr[size] != '\0') { setError("The string MUST include a zero termination within the size\n"); if(free_pStr) free(pStr); return NULL; } initTimer(); int overshoot; ESA esa = malloc(sizeof(*esa)); if(!esa) { setError("Couldn't allocate memory for ESA.\n"); if(free_pStr) { free(pStr); freeTimer(); } return NULL; } unsigned char *text; int n = size + 1; // Include the zeroterninatin in the string // Calculate the overshoot overshoot=init_ds_ssort(500,2000); text = malloc((n + overshoot)*sizeof *text); if(!text) { setError("Couldn't allocate memory for translated text.\n"); free(esa); if(free_pStr) { free(pStr); freeTimer(); } return NULL; } // Translate the text and stop if it fails if(! translate(text, pStr, n-1, pAlphabet, pIgnore) ) { free(text); free(esa); if(free_pStr) free(pStr); freeTimer(); return NULL; } // Free pStr if possible if(free_pStr) free(pStr); // Save the text, alphabet and size in the esa structure setStr(esa, text); setSize(esa, n); setAlphabetSize(esa, strlen(pAlphabet)); setIgnoreAlphabetSize(esa, strlen(pIgnore)); setAlphabet(esa, pAlphabet); setIgnoreAlphabet(esa, pIgnore); addTimer("Initializing"); // Do the sorting, calc. lcp and calc. skip esa->suf = malloc(sizeof(int) * n); if(!esa->suf) { free(text); free(esa); freeTimer(); setError("Couldn't allocate memory for suffix column in suffix array.\n"); return NULL; } ds_ssort(esa->pStr, esa->suf, n, MAXPSSMSIZE); addTimer("DS-Sort"); esa->lcp = malloc(sizeof(unsigned char) * n); if(!esa->lcp) { setError("Couldn't allocate memory for LCP column in suffix array.\n"); free(esa->suf); free(text); free(esa); freeTimer(); return NULL; } calcLcpNaiv(esa); addTimer("Calc Lcp"); // The line below can be commented in to verify that there are "errors" in the suffix array // it will scan the array for errors and report the minimum depth at which an error was found // the last parameter specifies the max depth to search to). // As a side effect it calculates lcp (when used for this purpose the depth parameter should equa // that used when calling ds_ssort). // verifyNaively(esa, n, MAX_DEPTH); esa->skip = malloc(sizeof(int) * n); if(!esa->skip) { setError("Couldn't allocate memory for SKIP column in suffix array.\n"); free(esa->lcp); free(esa->suf); free(text); free(esa); freeTimer(); return NULL; } if(calcSkip(esa) == 0) { free(esa->skip); free(esa->lcp); free(esa->suf); free(text); free(esa); freeTimer(); return NULL; } addTimer("Calc Skip"); printTimer(); freeTimer(); return esa; }