Exemple #1
0
/* seed our RNGs with high quality data from /dev/random */
void init_rngs(void)
{
#ifdef HAVE_SRAND48_R
    unsigned long v;
    int fd;
    ssize_t bytes;

    fd = open("/dev/random", O_RDONLY);
    if (fd < 0) {
        syslogerr("/dev/random");
        goto srand_time;
    }

    bytes = read(fd, &v, sizeof(v));
    if (bytes < 0)
        syslogerr("/dev/random read");

    close(fd);

    if (bytes < sizeof(v))
        goto srand_time;

    srand48_r(v, &rng);
    srand(v);
    return;

srand_time:
    srand48_r(getpid() ^ time(NULL), &rng);
#else
    srand48(getpid() ^ time(NULL));
#endif
    srand(getpid() ^ time(NULL));
}
Exemple #2
0
int main(int argc, char *argv[])
{
        SWRESET(generation);
        SWRESET(normalisation);
        SWRESET(split);
        SWRESET(construction);
        SWRESET(final);
        SWRESET(sampling);
        SWRESET(total);

        if (argc > 1) {
          omp_set_num_threads(atoi(argv[1]));
        }

        int j;
        SWTICK(total);
#pragma omp parallel
        {
#pragma omp master
                printf("Running with %d threads\n",
                                omp_get_num_threads());
                srand48_r(omp_get_thread_num(), buffer);
        }
        for (j = 0; j < 100; j++) {
                float *weights = malloc(NUM_SIDES * sizeof(float));
                float *dartboard = malloc(NUM_SIDES * sizeof(float));
                int *aliases = malloc(NUM_SIDES * sizeof(int));
                int i;
                SWTICK(generation);
#pragma omp parallel for shared(weights)
                for (i = 0; i < NUM_SIDES; i++) {
                        double w;
                        drand48_r(buffer, &w);
                        weights[i] = (float) w;
                }       
                SWTOCK(generation);
                SWTICK(normalisation);
                normalise(weights, NUM_SIDES);
                SWTOCK(normalisation);
                make_table(weights, dartboard, aliases, NUM_SIDES);
                SWTICK(sampling);
#pragma omp parallel for shared(dartboard, aliases) schedule(static) 
                for (i = 0; i < NUM_ROLLS; i++) {
                        roll(dartboard, aliases, NUM_SIDES, buffer);
                }
                SWTOCK(sampling);
                free(aliases);
                free(dartboard);
                free(weights);
        }
        SWTOCK(total);
        print_interval("Weight generation", SWGET(generation));
        print_interval("Normalisation", SWGET(normalisation));
        print_interval("Table split", SWGET(split));
        print_interval("Table construction", SWGET(construction));
        print_interval("Table final", SWGET(final));
        print_interval("Sampling", SWGET(sampling));
        print_interval("Total", SWGET(total));
        return 0;
}
Exemple #3
0
// START FUNC DECL
int
permute_I4(
    int *X,
    long long n
    )
// STOP FUNC DECL
{
  int status = 0;
  struct drand48_data buffer;
  srand48_r(852187451, &buffer);
  if ( X == NULL ) { go_BYE(-1); }
  if ( n <= 1 ) { go_BYE(-1); }
  for (long long k = n-1; k > 0; k--) {
    long long pos; 
    double dtemp;
    int swap; 
    drand48_r(&buffer, &dtemp);
    pos = dtemp * n; if ( pos == n ) { pos--; }
    swap   = X[pos];
    X[pos] = X[k];
    X[k]   = swap;
  }
BYE:
  return(status);
}
int main(int argc, char **argv){
	double x, y, dist;
	struct drand48_data drand_buf;
	long long n, i, fav;
	int threads = atoi(argv[2]);
	fav=0;
	n=atof(argv[1]);
	double start = omp_get_wtime();
	#pragma omp parallel num_threads(threads) private(x, y, dist, drand_buf)
	{
		srand48_r ((unsigned) time(NULL), &drand_buf);
		#pragma omp for reduction(+: fav) 
		for(i=0.0; i<n; i++){
			drand48_r(&drand_buf, &x);//(double)rand() / (double)RAND_MAX - 0.5;
			drand48_r(&drand_buf, &y);//y = (double)rand() / (double)RAND_MAX - 0.5;
			x -= 0.5;
			y -= 0.5;

			dist = (x*x + y*y);
			if(dist < 0.25)
				fav++;
		}
	}
	//printf("Approx. Val. of PI is: %f\n", 4*((double)fav/(double)n));
	//printf("Total Time taken: %f\n", omp_get_wtime()-start);
	printf("%lld %f\n", n, omp_get_wtime()-start);
	return 0;
}
Exemple #5
0
long int randoom(void)
{
    long int rval = 0;
    struct timeval tv;

#if defined (__linux__)
    struct drand48_data buff;
    long int res;
#endif

#if defined (__linux__)
    gettimeofday(&tv, NULL);
    srand48_r(tv.tv_usec, &buff);
    lrand48_r(&buff, &res);
    rval = (res % (tv.tv_usec + 1)) / 100;
#elif (defined (__FreeBSD__) || defined (__OpenBSD__))
    gettimeofday(&tv, NULL);
    rval = (arc4random() % (tv.tv_usec + 1)) / 100;
#else
    gettimeofday(&tv, NULL);
    srand((tv.tv_sec + tv.tv_usec) >> (sizeof(long) / 2));
    rval = (random() % (tv.tv_usec + 1)) / 100;
#endif

    return (rval);
}
Exemple #6
0
void chaos_init(int enable_chaos)
{
    chaos_enabled_p = enable_chaos;
    long seed = (long)time(0);
    if (chaos_enabled_p) {
        log_INFO("chaos seed: %ld", seed);
    }
    srand48_r(seed, &dr_buf); /* FIXME use TLS instead of dr_buf. */
}
Exemple #7
0
void url_InitRandom(RandState state)
{
  unsigned int seed;

  apr_generate_random_bytes((unsigned char*)&seed, sizeof(int));

#if HAVE_LRAND48_R
  srand48_r(seed, state);
#else
  srand(seed);
#endif
}
void mexFunction( int nlhs, mxArray *plhs[],
		int nrhs, const mxArray *prhs[]
		) {
	
	/* Declare */
	unsigned int seed_init;
	unsigned int s0, s1, s2, *ret;
	
	if(nrhs!=1)
		mexErrMsgTxt("Single input required.");
	else if(nlhs > 1)
		mexErrMsgTxt("Too many output arguments.");
	
	/* Allocate input */
	seed_init = (unsigned int) mxGetScalar(prhs[0]);
	
	/* Algo */
#ifdef USER_MODE
struct drand48_data buffer;
unsigned int result = 0;
#endif
	
if (seed_init == 0) {
	s0 = (unsigned int)time(NULL);
	s1 = (unsigned int)time(NULL);
	s2 = (unsigned int)time(NULL);
}
else {
#ifndef USER_MODE
s0 = (unsigned int)0x1e23d852;
s1 = (unsigned int)0x81f38a1c;
s2 = (unsigned int)0xfe1a133e;
#else
/* Use reentrant version of rand48 to ensure that no conflicts with other generators occur */
srand48_r((long int)seed_init, &buffer);
mrand48_r(&buffer, (long int *)&result);
s0 = result;
mrand48_r(&buffer, (long int *)&result);
s1 = result;
mrand48_r(&buffer, (long int *)&result);
s2 = result;
#endif
}
	
	/* Allocate Output */	
	plhs[0] = mxCreateNumericMatrix(3, 1, mxUINT32_CLASS, mxREAL);
	ret = (unsigned int*) mxGetPr(plhs[0]);
	
	ret[0] = s0;
	ret[1] = s1;
	ret[2] = s2;
	
}
Exemple #9
0
void init_rand_ctx(struct drand48_data* ctx) {
  static long state = 0;
  static int done = 0;

  if (!done) {
    struct timeval tv;

    assert(!gettimeofday(&tv, NULL));
    done = 1;
    state = tv.tv_usec + (tv.tv_sec * 1000000);
  }
  assert(!srand48_r(state++, ctx));
}
Exemple #10
0
/*
 * Name             open_read_close
 * Description      This metric is only applicable for small files.
 *                  -   Measures the time taken to open a small file, read all of the file and
 *                      close the file.
 *                  -   Files to be opened/read/closed are chosen in random ( lessen prefetching
 *                      effects.)
 *                  -   Overhead : one if-loop + overhead of measuring time.
 * Input            struct share_it
 * Input            filepath - to pick files from.
 * Output           Boolean to indicate if all reads succeed.
 */
bool open_read_close(struct share_it* my_state, char *filepath) {
    timestamp start     = 0;
    timestamp end       = 0;
    int bytes           = 0;

    struct drand48_data     randBuffer;
    srand48_r(time(NULL), &randBuffer);

    int i           = 0;
    long int random = 0;
    int idx         = 0;

    for (i = 0; i < my_state->count; i++) {
        lrand48_r(&randBuffer, &random);
        idx = random % MAX_FILES + 1;
        char num[5];
        sprintf(num, "%d", idx);

        char my_file[100] = {'\0'};
        strcat(my_file, filepath);
        strcat(my_file, "/file");
        strcat(my_file, num);

        RDTSCP(start);
        int fd = open(my_file, FLAGS);
        if (fd == -1) {
            int err = errno;
            printf("Could not open file descriptor for file %s. Error = %d\n",
                   my_file, err);
            return false;
        }
        bytes = read(fd, my_state->buf, my_state->block_size);
        close(fd);
        RDTSCP(end);


        if (bytes <= 0 || bytes != my_state->block_size)
            return false;
        dummy_call(my_state->buf);
        *(my_state->total_bytes) += bytes;
        my_state->duration  += (end - start);
    }

    return true;

}
Exemple #11
0
void *perform_work(void *argument) {
  int passed_in_value, i=0x11, j=33;
  int *seeds, nSeeds;
  passed_in_value = *((int *) argument);
  seeds = malloc( 1*sizeof *seeds);
  nSeeds = 1;

  seeds[0] = seed_vertices[0];

  /*
   * Record whether a vertex is infected
   */
  char *infected ;
  int s =  (G->V + 5) * sizeof *infected;
  infected= malloc(s);
  memset(infected, 0,s);
  unsigned long seed_int = (unsigned long) (
      (1 + passed_in_value + time(NULL)) % 888);
#ifdef __CYGWIN__
  reent seed = (reent) seed_int;
#else
  struct drand48_data seed;
  srand48_r(seed_int, &seed);
#endif
  for (i = 1, j = 1; i <= G->V; ++i) {
    if (i != seeds[0] &&
        getRandTo_r(MAX_EDGE_WEIGHT, &seed) <= G->adj_list[i]->prob) {
      if(j >= nSeeds){
        seeds= realloc(seeds, (1+j)*2*sizeof *seeds);
        nSeeds = 2+j*2;
      }
      seeds[j] = i;
      j++;
    }
  }
  n_seed = j ;
  Ninfected_ptr[passed_in_value] =
      infect(G,n_seed , seeds, infected, &seed);

  fprintf(out2, "infected[%d] : %d\n", passed_in_value,
          Ninfected_ptr[passed_in_value]);

  return NULL;
}
RTPRandom::RTPRandom()
{
#if defined(RTP_SUPPORT_GNUDRAND) || defined(RTP_SUPPORT_RANDR)
	u_int32_t x;

	x = (u_int32_t)getpid();
	x += (u_int32_t)time(0);
	x -= (u_int32_t)clock();
	x ^= (u_int32_t)(this);

#ifdef RTP_SUPPORT_GNUDRAND
	srand48_r(x,&drandbuffer);
#else
	state = (unsigned int)x;
#endif
	
#else // use simple rand and srand functions
	if (init)
		return;

	u_int32_t x;

#ifndef _WIN32_WCE
	x = (u_int32_t)getpid();
	x += (u_int32_t)time(0);
	x -= (u_int32_t)clock();
#else
	x = (u_int32_t)GetCurrentProcessId();

	FILETIME ft;
	SYSTEMTIME st;
	
	GetSystemTime(&st);
	SystemTimeToFileTime(&st,&ft);
	
	x += ft.dwLowDateTime;
#endif // _WIN32_WCE
	x ^= (u_int32_t)(this);
	srand((unsigned int)x);

	init = true;
#endif
}
Exemple #13
0
/*--------------------------------------------------------------------*/
int main(int argc, char* argv[]) {
   	int thread_count = strtol(argv[1], NULL, 10); 
   	double* A = (double*) malloc(9*sizeof(double));
	#pragma omp parallel num_threads(thread_count) 
	{
		struct drand48_data randBuffer;
		srand48_r((time(NULL) - omp_get_thread_num()), &randBuffer);
	   	int i;
	   	#pragma omp for
		   	for (i = 0; i < 9; ++i){
		   		double x;
		   		drand48_r(&randBuffer, &x);
				double tmp = -1 + 2*x;
					A[i] = tmp;
					printf("%f\n", tmp);
			}
	    
	}
	return 0;
}  /* main */
Exemple #14
0
void * tpcc_wl::threadInitWarehouse(void * This) {
	tpcc_wl * wl = (tpcc_wl *) This;
	int tid = ATOM_FETCH_ADD(wl->next_tid, 1);
	uint32_t wid = tid + 1;
	tpcc_buffer[tid] = (drand48_data *) _mm_malloc(sizeof(drand48_data), 64);
	assert((uint64_t)tid < g_num_wh);
	srand48_r(wid, tpcc_buffer[tid]);
	
	if (tid == 0)
		wl->init_tab_item();
	wl->init_tab_wh( wid );
	wl->init_tab_dist( wid );
	wl->init_tab_stock( wid );
	for (uint64_t did = 1; did <= DIST_PER_WARE; did++) {
		wl->init_tab_cust(did, wid);
		wl->init_tab_order(did, wid);
		for (uint64_t cid = 1; cid <= g_cust_per_dist; cid++) 
			wl->init_tab_hist(cid, did, wid);
	}
	return NULL;
}
u_long _create_xid (void)
{
  long res;

  __UCLIBC_MUTEX_LOCK(mylock);

  if (!is_initialized)
    {
      struct timeval now;

      gettimeofday (&now, (struct timezone *) 0);
      srand48_r (now.tv_sec ^ now.tv_usec, &__rpc_lrand48_data);
      is_initialized = 1;
    }

  lrand48_r (&__rpc_lrand48_data, &res);

  __UCLIBC_MUTEX_UNLOCK(mylock);

  return res;
}
Exemple #16
0
void 
Query_thd::init(workload * h_wl, int thread_id) {
	uint64_t request_cnt;
	q_idx = 0;
	request_cnt = WARMUP / g_thread_cnt + MAX_TXN_PER_PART + 4;
#if WORKLOAD == YCSB	
	queries = (ycsb_query *) 
		mem_allocator.alloc(sizeof(ycsb_query) * request_cnt, thread_id);
	srand48_r(thread_id + 1, &buffer);
#elif WORKLOAD == TPCC
	queries = (tpcc_query *) _mm_malloc(sizeof(tpcc_query) * request_cnt, 64);
#endif
	for (UInt32 qid = 0; qid < request_cnt; qid ++) {
#if WORKLOAD == YCSB	
		new(&queries[qid]) ycsb_query();
		queries[qid].init(thread_id, h_wl, this);
#elif WORKLOAD == TPCC
		new(&queries[qid]) tpcc_query();
		queries[qid].init(thread_id, h_wl);
#endif
	}
}
Exemple #17
0
void *customer(void* data) {
    struct drand48_data randData;
    double ranNum;
    int i;

    cData *cD = (cData *) data;
    
    srand48_r((long)(time(NULL)), &randData);   // seed random generator

    // put coin and select coffee
    for (i = 0; i < ITERATIONS; i++) {
        drand48_r(&randData, &ranNum);          // get random number (0.0 - 1.0)
        pthread_mutex_lock(&(cD->mutex));
        cD->coinCount      += 1;
        if (ranNum < 0.5)
            cD->selCount1 += 1;
        else
            cD->selCount2 += 1;
        pthread_mutex_unlock(&(cD->mutex));
    }
    
    pthread_exit(0);  
}
Exemple #18
0
/*
 * Randomly generates a graph of the specified size,
 * branching factor, and maximum edge weight.
 *
 * @param size  the number of vertices in the graph; positive.
 *
 * @param b  the branching factor; probability that any given
 * source destination pair will have an edge.
 *
 * @param max_weight  the upper bound edge weight; each edge
 * has a pseudorandomly chosen non-negative weight at most this.
 * Must be a power of 2 less than RAND_MAX for a fair weight
 * distribution.
 */
void pgenerate_graph(unsigned int size,
                    float b,
                    unsigned int max_weight,
                    int *edges)
{
    int bint = b * (double) BIG_POWER_OF_TWO;

#pragma omp parallel
    {
        struct drand48_data buffer;
        srand48_r(current_seed + omp_get_thread_num(), &buffer);

#pragma omp for
        for (int i = 0; i < (size*size); i += 1) {
            long rng;
            lrand48_r(&buffer, &rng);
            const bool should_add_edge = ((int) rng % BIG_POWER_OF_TWO) < bint;
            edges[i] = (should_add_edge) ? (int) rng % (max_weight+1) : -1;
        }
    }
    srand(current_seed);
    current_seed = rand();
}
Exemple #19
0
static uint64_t fill_buffer(void *buf, size_t size, uint64_t offset)
{
	uint8_t *p, *ptr_next_sector, *ptr_end;
	struct drand48_data state;

	assert(size > 0);
	assert(size % SECTOR_SIZE == 0);

	p = buf;
	ptr_end = p + size;
	while (p < ptr_end) {
		memmove(p, &offset, sizeof(offset));
		srand48_r(offset, &state);
		ptr_next_sector = p + SECTOR_SIZE;
		p += sizeof(offset);
		for (; p < ptr_next_sector; p += sizeof(long int))
			lrand48_r(&state, (long int *)p);
		assert(p == ptr_next_sector);
		offset += SECTOR_SIZE;
	}

	return offset;
}
Exemple #20
0
int main (int argc, char *argv[])
{
    int i, count, samples, nthreads, seed;
    struct drand48_data drand_buf;
    double x, y;
    double t0, t1;

    if(argc<3){
        printf("Introduction: This excercise aims to use openmp to speed up solving Pi-Calculation.\n");
        printf("Usage: ./exercise n_samples n_threads\n");
        return -1;
    }
    samples  = atoi(argv[1]);
    nthreads = atoi(argv[2]);
    omp_set_num_threads (nthreads);

    t0 = omp_get_wtime();
    count = 0;

#pragma omp parallel private(i, x, y, seed, drand_buf) shared(samples)
    {
        seed = 1202107158 + omp_get_thread_num() * 1999;
        srand48_r (seed, &drand_buf);

#pragma omp for reduction(+:count)
        for (i=0; i<samples; i++) {
            drand48_r (&drand_buf, &x);
            drand48_r (&drand_buf, &y);
            if (x*x + y*y <= 1.0) count++;
        }
    }

    t1 = omp_get_wtime();
    printf("Estimate of pi: %7.5f\n", 4.0*count/samples);
    printf("Time: %7.2f\n", t1-t0);
}
Exemple #21
0
static void *submitter_fn(void *data)
{
	struct submitter *s = data;
	struct io_sq_ring *ring = &s->sq_ring;
	int ret, prepped;

	printf("submitter=%d\n", gettid());

	srand48_r(pthread_self(), &s->rand);

	prepped = 0;
	do {
		int to_wait, to_submit, this_reap, to_prep;

		if (!prepped && s->inflight < DEPTH) {
			to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
			prepped = prep_more_ios(s, to_prep);
		}
		s->inflight += prepped;
submit_more:
		to_submit = prepped;
submit:
		if (to_submit && (s->inflight + to_submit <= DEPTH))
			to_wait = 0;
		else
			to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);

		/*
		 * Only need to call io_uring_enter if we're not using SQ thread
		 * poll, or if IORING_SQ_NEED_WAKEUP is set.
		 */
		if (!sq_thread_poll || (*ring->flags & IORING_SQ_NEED_WAKEUP)) {
			unsigned flags = 0;

			if (to_wait)
				flags = IORING_ENTER_GETEVENTS;
			if ((*ring->flags & IORING_SQ_NEED_WAKEUP))
				flags |= IORING_ENTER_SQ_WAKEUP;
			ret = io_uring_enter(s, to_submit, to_wait, flags);
			s->calls++;
		}

		/*
		 * For non SQ thread poll, we already got the events we needed
		 * through the io_uring_enter() above. For SQ thread poll, we
		 * need to loop here until we find enough events.
		 */
		this_reap = 0;
		do {
			int r;
			r = reap_events(s);
			if (r == -1) {
				s->finish = 1;
				break;
			} else if (r > 0)
				this_reap += r;
		} while (sq_thread_poll && this_reap < to_wait);
		s->reaps += this_reap;

		if (ret >= 0) {
			if (!ret) {
				to_submit = 0;
				if (s->inflight)
					goto submit;
				continue;
			} else if (ret < to_submit) {
				int diff = to_submit - ret;

				s->done += ret;
				prepped -= diff;
				goto submit_more;
			}
			s->done += ret;
			prepped = 0;
			continue;
		} else if (ret < 0) {
			if (errno == EAGAIN) {
				if (s->finish)
					break;
				if (this_reap)
					goto submit;
				to_submit = 0;
				goto submit;
			}
			printf("io_submit: %s\n", strerror(errno));
			break;
		}
	} while (!s->finish);

	finish = 1;
	return NULL;
}
Exemple #22
0
void adajoint_chr(char **R_file_prefix, int *R_method,
int *R_nperm, int *R_seed, 
int *R_nthread, int *R_nsnp, int *R_ngene, 
double *R_vU, double *R_score0, double *R_vV, 
int *R_vgene_idx, int *R_gene_start, int *R_gene_end, 
int *R_vgene_cutpoint, 
int *R_gene_cutpoint_start, int *R_gene_cutpoint_end, 
double *R_gene_pval, int *R_arr_rank, 
int *R_sel_id, int *R_marg_id){
  
  int len_file_prefix = strlen(*R_file_prefix);
  char *file_prefix = new char[len_file_prefix + 1];
  file_prefix[0] = '\0';
  strcat(file_prefix, *R_file_prefix);
  
  int method = *R_method;
  assert(method == 1 || method == 2);
  
  int nperm = *R_nperm;
  int seed = *R_seed;
  int nthread = *R_nthread;
  int nsnp = *R_nsnp;
  int ngene = *R_ngene;
  
  fvec score0;
  fmat V;
  fmat U;
  load_score0(R_score0, score0, nsnp);
  load_cov(R_vV, V, nsnp);
  load_U(R_vU, U, nsnp);
  
  imat gene_idx; // index of SNPs in a gene
  
  load_gene_idx(R_vgene_idx, R_gene_start, R_gene_end, 
  gene_idx, ngene);
  
  imat cutpoint;
  load_gene_cutpoint(R_vgene_cutpoint, R_gene_cutpoint_start, R_gene_cutpoint_end, 
  cutpoint, ngene);
  
  string fprefix (file_prefix);
  svec gene_out (ngene, fprefix);
  for(int g = 0; g < ngene; ++g){
    ostringstream gid;
    gid << g;
    gene_out[g] = gene_out[g] + string("GID.") + gid.str() + string(".bin");
  }
  
  // write obs statistics for all genes
  imat sel_id(ngene);
  ivec marg_id(ngene);
  for(int g = 0; g < ngene; ++g){
    fstream gout(gene_out[g].c_str(), ios::out | ios::binary);
  	if(!gout){
  		error("Fail to write observed statistics to file");
  	}
  	
  	fvec S;
  	fmat Sigma;
  	extract_score(S, score0, gene_idx[g]);
  	extract_cov(Sigma, V, gene_idx[g]);
  	fvec s;
    int ncp = cutpoint[g].size();
    int mc = cutpoint[g][ncp - 1];
    
    if(method == 1){
      search1(s, sel_id[g], marg_id[g], S, Sigma, mc);
    }else{//assert(method == 2)
      search2(s, sel_id[g], marg_id[g], S, Sigma, mc);
    }
    
    for(int k = 0; k < ncp; ++k){
      float u = s[cutpoint[g][k]];
      gout.write((char*)(&u), sizeof(u));
    }
  	gout.close();
  }
  
  int i_sel_id = -1;
  for(int g = 0; g < ngene; ++g){
    R_marg_id[g] = gene_idx[g][marg_id[g]] + 1;
    for(int k = 0; k < sel_id[g].size(); ++k){
      ++i_sel_id;
      R_sel_id[i_sel_id] = gene_idx[g][sel_id[g][k]] + 1;
    }
    int nn = gene_idx[g].size() - sel_id[g].size();
    while(nn){
      ++i_sel_id;
      R_sel_id[i_sel_id] = -1;
      --nn;
    }
  }
  
  int ngap = min(10000, nperm);
  int nblock = nperm / ngap;
  
  for(int b = 0; b < nblock; ++b){
  	fmat null(ngap, fvec (nsnp, .0f));
  	drand48_data buf;
  	// compute null score
  	#pragma omp parallel num_threads(nthread) private(buf)
  	{
  		srand48_r(seed + b * nthread + omp_get_thread_num(), &buf);
  		#pragma omp for
	  	for(int i = 0; i < ngap; ++i){
	  		fvec rn;
	  		rnorm(buf, nsnp, rn);
	  		for(int j = 0; j < nsnp; ++j){
	  			null[i][j] = .0f;
	  			for(int k = 0; k < nsnp; ++k){
	  				null[i][j] += rn[k] * U[k][j];
	  			}
	  		}
	  	}
	  }
	  
	  // write null statistics to local files (per gene)
	  #pragma omp parallel num_threads(min(nthread, ngene))
	  {
	  	#pragma omp for
	  	for(int g = 0; g < ngene; ++g){
	  		ofstream gout;
	  		gout.open(gene_out[g].c_str(), ios::out | ios::binary | ios::app);
	  		if(!gout){
	  			error("Fail to write null statistics to file");
	  		}
	  		
	  		fmat Sigma;
	  		extract_cov(Sigma, V, gene_idx[g]);
	  		
        int ns = gene_idx[g].size();
        int ncp = cutpoint[g].size();
        int mc = cutpoint[g][ncp - 1];
	  		for(int i = 0; i < ngap; ++i){
	  			fvec S;
	  			extract_score(S, null[i], gene_idx[g]);
	  			
	  			fvec s;
          ivec sel_id;
          int marg_id;
          if(method == 1){
            search1(s, sel_id, marg_id, S, Sigma, mc);
          }else{
            search2(s, sel_id, marg_id, S, Sigma, mc);
          }
          
          for(int k = 0; k < ncp; ++k){
            float u = s[cutpoint[g][k]];
            gout.write((char*)(&u), sizeof(u));
          }
	  		}
	  		gout.close();
	  	}
	  }
	  //fmat().swap(null);
  }
  
  // read null statistics (per gene)
  int irk = -1;
  for(int g = 0; g < ngene; ++g){
  	int ncp = cutpoint[g].size();
  	vector<VecStat> stat(ncp, VecStat (nperm + 1, STAT0));
    fstream gin(gene_out[g].c_str(), ios::in | ios::binary);
    
  	for(int i = 0; i < nperm + 1; ++i){
  		for(int j = 0; j < ncp; ++j){
  			float s = .0f;
  			gin.read((char*)(&s), sizeof(s));
  			stat[j][i].stat = s;
  			stat[j][i].id = i;
  		}
  	}
  	gin.close();
    
    if(remove(gene_out[g].c_str())){
      error("Cannot delete gene output file");
    }
  	
  	imat arr_rank(ncp, ivec (nperm + 1, 0));
  	#pragma omp parallel num_threads(min(ncp, nthread))
  	{
      #pragma omp for
  		for(int j = 0; j < ncp; ++j){
  			sort(stat[j].begin(), stat[j].end(), descending);
  			for(int i = 0; i < nperm + 1; ++i){
  				int id = stat[j][i].id;
  				arr_rank[j][id] = i;
  			}
  		}
  	}
  	
  	vector<VecStat>().swap(stat);
    
    ivec gene_min_p (nperm + 1, -1);
    ivec subsum(nthread, 0);
    ivec subtie(nthread, 0);
    int m = nperm + 1;
    for(int j = 0; j < ncp; ++j){
      ++irk;
      R_arr_rank[irk] = arr_rank[j][0];
      if(arr_rank[j][0] < m){
        m = arr_rank[j][0];
      }
    }
    gene_min_p[0] = m;
    
    #pragma omp parallel num_threads(nthread)
    {
      #pragma omp for
      for(int i = 1; i < nperm + 1; ++i){
        int tid = omp_get_thread_num();
        int m = nperm + 1;
        for(int j = 0; j < ncp; ++j){
          if(arr_rank[j][i] < m){
            m = arr_rank[j][i];
          }
        }
        gene_min_p[i] = m;
        if(gene_min_p[i] < gene_min_p[0]){
          subsum[tid] += 1;
        }else if(gene_min_p[i] == gene_min_p[0]){
          subtie[tid] += 1;
        }else{
          ;
        }
      }
    }
    
    R_gene_pval[g] = 1.0;
    int rep = 0;
    for(int t = 0; t < nthread; ++t){
      R_gene_pval[g] += subsum[t];
      rep += subtie[t];
    }
    R_gene_pval[g] += rep / 2.0;
    R_gene_pval[g] /= nperm + 1;
    
    fstream gout(gene_out[g].c_str(), ios::out | ios::binary);
    if(!gout){
      error("Fail to write gene statistics to file");
    }
    for(int i = 0; i < nperm + 1; ++i){
      gout.write((char*)(&(gene_min_p[i])), sizeof(gene_min_p[i]));
    }
    gout.close();
    
  }
  
  
  delete[] file_prefix;
  
}
Exemple #23
0
void
do_sync_io(th_arg_t *th_arg, int *fd_list)
{
    struct timeval       start_tv;
    struct timeval       timer;
    meter_t             *meter;
    struct drand48_data  rand;
    int64_t              addr;
    void                *buf;
    int                  i;

    int file_idx;
    int64_t *ofst_list;
    int64_t *ofst_min_list;
    int64_t *ofst_max_list;

    meter = th_arg->meter;
    srand48_r(th_arg->common_seed ^ th_arg->tid, &rand);

    register double iowait_time = 0;
    register int64_t io_count = 0;

    buf = memalign(option.blk_sz, option.blk_sz);

    // offset handling
    ofst_list = malloc(sizeof(int64_t) * option.nr_files);
    ofst_min_list = malloc(sizeof(int64_t) * option.nr_files);
    ofst_max_list = malloc(sizeof(int64_t) * option.nr_files);

    for (i = 0; i < option.nr_files; i++) {
        if (option.ofst_start >= 0) {
            ofst_min_list[i] = option.ofst_start;
        } else {
            ofst_min_list[i] = 0;
        }

        if (option.ofst_end >= 0) {
            ofst_max_list[i] = option.ofst_end;
        } else {
            ofst_max_list[i] = option.file_size_list[i] / option.blk_sz;
        }

        if (option.seq) {
            ofst_list[i] = ofst_min_list[i]
                + (ofst_max_list[i] - ofst_min_list[i]) * th_arg->id / option.multi;
        } else {
            ofst_list[i] = ofst_min_list[i];
        }
    }

    file_idx = 0;

    GETTIMEOFDAY(&start_tv);
    if (option.rand){
        while (mb_elapsed_time_from(&start_tv) < option.timeout) {
            for(i = 0;i < 100; i++){
                // select file
                long ret;
                lrand48_r(&rand, &ret);
                file_idx = ret % option.nr_files;

                ofst_list[file_idx] = (int64_t) mb_rand_range_long(&rand,
                                                                   ofst_min_list[file_idx],
                                                                   ofst_max_list[file_idx]);

                addr = ofst_list[file_idx] * option.blk_sz + option.misalign;

                GETTIMEOFDAY(&timer);
                if (mb_read_or_write() == MB_DO_READ) {
                    mb_preadall(fd_list[file_idx], buf, option.blk_sz, addr, option.continue_on_error);
                } else {
                    mb_pwriteall(fd_list[file_idx], buf, option.blk_sz, addr, option.continue_on_error);
                }
                iowait_time += mb_elapsed_time_from(&timer);
                io_count ++;

                long idx;
                volatile double dummy = 0.0;
                for(idx = 0; idx < option.bogus_comp; idx++){
                    dummy += idx;
                }
            }
        }
    } else if (option.seq) {
        while (mb_elapsed_time_from(&start_tv) < option.timeout) {
            for(i = 0;i < 100; i++){
                // select file
                file_idx++;
                file_idx %= option.nr_files;

                // incr offset
                ofst_list[file_idx]++;
                if (ofst_list[file_idx] >= ofst_max_list[file_idx]) {
                    ofst_list[file_idx] = ofst_min_list[file_idx];
                }
                addr = ofst_list[file_idx] * option.blk_sz + option.misalign;
                if (lseek64(fd_list[file_idx], addr, SEEK_SET) == -1){
                    perror("do_sync_io:lseek64");
                    exit(EXIT_FAILURE);
                }

                GETTIMEOFDAY(&timer);
                if (option.read) {
                    mb_readall(fd_list[file_idx], buf, option.blk_sz, option.continue_on_error);
                } else if (option.write) {
                    mb_writeall(fd_list[file_idx], buf, option.blk_sz, option.continue_on_error);
                } else {
                    fprintf(stderr, "Only read or write can be specified in seq.");
                    exit(EXIT_FAILURE);
                }
                iowait_time += mb_elapsed_time_from(&timer);
                io_count ++;

                long idx;
                volatile double dummy = 0.0;
                for(idx = 0; idx < option.bogus_comp; idx++){
                    dummy += idx;
                }
            }
        }
    }

    meter->iowait_time = iowait_time;
    meter->count = io_count;

    free(buf);
}
Exemple #24
0
void
do_async_io(th_arg_t *arg, int *fd_list)
{
    meter_t *meter;
    struct timeval start_tv;
    int64_t addr;
    int n;
    int i;
    void *buf;
    mb_aiom_t *aiom;
    mb_res_pool_t *buffer_pool;
    struct drand48_data  rand;

    int file_idx;
    int64_t *ofst_list;
    int64_t *ofst_min_list;
    int64_t *ofst_max_list;

    srand48_r(arg->common_seed ^ arg->tid, &rand);

    meter = arg->meter;
    aiom = mb_aiom_make(option.aio_nr_events);
    if (aiom == NULL) {
        perror("do_async_io:mb_aiom_make failed");
        exit(EXIT_FAILURE);
    }
    buf = malloc(option.blk_sz);
    bzero(buf, option.blk_sz);
    buffer_pool = mb_res_pool_make(option.aio_nr_events);
    for(i = 0; i < option.aio_nr_events; i++){
        buf = memalign(512, option.blk_sz);
        mb_res_pool_push(buffer_pool, buf);
    }

    // offset handling
    ofst_list = malloc(sizeof(int64_t) * option.nr_files);
    ofst_min_list = malloc(sizeof(int64_t) * option.nr_files);
    ofst_max_list = malloc(sizeof(int64_t) * option.nr_files);

    for (i = 0; i < option.nr_files; i++) {
        if (option.ofst_start >= 0) {
            ofst_min_list[i] = option.ofst_start;
        } else {
            ofst_min_list[i] = 0;
        }

        if (option.ofst_end >= 0) {
            ofst_max_list[i] = option.ofst_end;
        } else {
            ofst_max_list[i] = option.file_size_list[i] / option.blk_sz;
        }

        if (option.seq) {
            ofst_list[i] = ofst_min_list[i] + (ofst_max_list[i] - ofst_min_list[i]) * arg->id / option.multi;
        } else {
            ofst_list[i] = ofst_min_list[i];
        }
    }

    if (option.read == false && option.write == false) {
        fprintf(stderr, "Only read or write can be specified in sync. mode\n");
        exit(EXIT_FAILURE);
    }

    file_idx = 0;

    GETTIMEOFDAY(&start_tv);
    while(mb_elapsed_time_from(&start_tv) < option.timeout) {
        while(mb_aiom_nr_submittable(aiom) > 0) {
            // select file
            if (option.rand) {
                long ret;
                lrand48_r(&rand, &ret);
                file_idx = ret % option.nr_files;
            } else {
                file_idx++;
                file_idx %= option.nr_files;
            }

            if (option.rand) {
                ofst_list[file_idx] = (int64_t) mb_rand_range_long(&rand,
                                                                   ofst_min_list[file_idx],
                                                                   ofst_max_list[file_idx]);
            } else {
                ofst_list[file_idx]++;
                if (ofst_list[file_idx] >= ofst_max_list[file_idx]) {
                    ofst_list[file_idx] = ofst_min_list[file_idx];
                }
            }
            addr = ofst_list[file_idx] * option.blk_sz + option.misalign;

            buf = mb_res_pool_pop(buffer_pool);
            if (mb_read_or_write() == MB_DO_READ) {
                mb_aiom_prep_pread(aiom, fd_list[file_idx], buf, option.blk_sz, addr);
            } else {
                mb_aiom_prep_pwrite(aiom, fd_list[file_idx], buf, option.blk_sz, addr);
            }
        }
        if (0 >= (n = mb_aiom_submit(aiom))) {
            perror("do_async_io:mb_aiom_submit failed");
            switch(-n){
            case EAGAIN : fprintf(stderr, "EAGAIN\n"); break;
            case EBADF  : fprintf(stderr, "EBADF\n"); break;
            case EFAULT : fprintf(stderr, "EFAULT\n"); break;
            case EINVAL : fprintf(stderr, "EINVAL\n"); break;
            case ENOSYS : fprintf(stderr, "ENOSYS\n"); break;
            }
            exit(EXIT_FAILURE);
        }

        if (0 >= (n = mb_aiom_wait(aiom, NULL))) {
            perror("do_async_io:mb_aiom_wait failed");
            exit(EXIT_FAILURE);
        }

        for(i = 0; i < n; i++) {
            struct io_event *event;
            event = &aiom->events[i];

            mb_res_pool_push(buffer_pool, event->obj->u.c.buf);
        }
    }

    mb_aiom_waitall(aiom);

    meter->count = aiom->iocount;
    meter->iowait_time = aiom->iowait;

    mb_aiom_destroy(aiom);

    free(ofst_list);
    free(ofst_min_list);
    free(ofst_max_list);
}
Exemple #25
0
void srand48 (long seedval)
{
    //fprintf(stderr, "srandom 2\n");
    srand48_r (seedval, &__libc_drand48_data);
}
Exemple #26
0
/* Different Seed for each thread */
void seed_rand(struct drand48_data *buffer)
{
    unsigned long seed;
    seed = (unsigned long)pthread_self();
    srand48_r(seed, buffer);
}
Exemple #27
0
int
main (void)
{
  time_t t = time (NULL);
  int i, ret = 0;
  double d;
  long int l;
  struct drand48_data data;
  unsigned short int buf[3];

  srand48 ((long int) t);
  for (i = 0; i < 50; i++)
    if ((d = drand48 ()) < 0.0 || d >= 1.0)
      {
        printf ("drand48 %d %g\n", i, d);
        ret = 1;
      }

  srand48_r ((long int) t, &data);
  for (i = 0; i < 50; i++)
    if (drand48_r (&data, &d) != 0 || d < 0.0 || d >= 1.0)
      {
        printf ("drand48_r %d %g\n", i, d);
        ret = 1;
      }

  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
  for (i = 0; i < 50; i++)
    if ((d = erand48 (buf)) < 0.0 || d >= 1.0)
      {
        printf ("erand48 %d %g\n", i, d);
        ret = 1;
      }

  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
  for (i = 0; i < 50; i++)
    if (erand48_r (buf, &data, &d) != 0 || d < 0.0 || d >= 1.0)
      {
        printf ("erand48_r %d %g\n", i, d);
        ret = 1;
      }

  srand48 ((long int) t);
  for (i = 0; i < 50; i++)
    if ((l = lrand48 ()) < 0 || l > INT32_MAX)
      {
        printf ("lrand48 %d %ld\n", i, l);
        ret = 1;
      }

  srand48_r ((long int) t, &data);
  for (i = 0; i < 50; i++)
    if (lrand48_r (&data, &l) != 0 || l < 0 || l > INT32_MAX)
      {
        printf ("lrand48_r %d %ld\n", i, l);
        ret = 1;
      }

  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
  for (i = 0; i < 50; i++)
    if ((l = nrand48 (buf)) < 0 || l > INT32_MAX)
      {
        printf ("nrand48 %d %ld\n", i, l);
        ret = 1;
      }

  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
  for (i = 0; i < 50; i++)
    if (nrand48_r (buf, &data, &l) != 0 || l < 0 || l > INT32_MAX)
      {
        printf ("nrand48_r %d %ld\n", i, l);
        ret = 1;
      }

  srand48 ((long int) t);
  for (i = 0; i < 50; i++)
    if ((l = mrand48 ()) < INT32_MIN || l > INT32_MAX)
      {
        printf ("mrand48 %d %ld\n", i, l);
        ret = 1;
      }

  srand48_r ((long int) t, &data);
  for (i = 0; i < 50; i++)
    if (mrand48_r (&data, &l) != 0 || l < INT32_MIN || l > INT32_MAX)
      {
        printf ("mrand48_r %d %ld\n", i, l);
        ret = 1;
      }

  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
  for (i = 0; i < 50; i++)
    if ((l = jrand48 (buf)) < INT32_MIN || l > INT32_MAX)
      {
        printf ("jrand48 %d %ld\n", i, l);
        ret = 1;
      }

  buf[2] = (t & 0xffff0000) >> 16; buf[1] = (t & 0xffff); buf[0] = 0x330e;
  for (i = 0; i < 50; i++)
    if (jrand48_r (buf, &data, &l) != 0 || l < INT32_MIN || l > INT32_MAX)
      {
        printf ("jrand48_r %d %ld\n", i, l);
        ret = 1;
      }

  return ret;
}
void TraceAnalyzer::colorizeTasks()
{
	unsigned int cpu;
	double nf;
	int n;
	int ncolor;
	double s;
	int step;
	int red;
	int green;
	int blue;
	struct drand48_data rdata;
	int i, j;
	QList<TColor> colorList;
	const TColor black(0, 0, 0);
	const TColor white(255, 255, 255);
	TColor gray;
	TColor tmp;
	long int rnd = 0;

	srand48_r(290876, &rdata);

	for (cpu = 0; cpu <= getMaxCPU(); cpu++) {
		DEFINE_CPUTASKMAP_ITERATOR(iter) = cpuTaskMaps[cpu].begin();
		while (iter != cpuTaskMaps[cpu].end()) {
			CPUTask &task = iter.value();
			iter++;
			if (colorMap.contains(task.pid))
				continue;
			TColor color;
			colorMap.insert(task.pid, color);
		}
	}

	n = colorMap.size();
	nf = (double) n;
	s = 0.95 * cbrt( (1 / nf) * (255 * 255 * 255 ));
	s = TSMIN(s, 128.0);
	s = TSMAX(s, 1.0);
retry:
	step = (int) s;
	for (red = 0; red < 256; red += step) {
		for (green = 0; green < 256; green += step)  {
			for (blue = 0; blue < 256; blue += step) {
				TColor color(red, green, blue);
				if (color.SqDistance(black) < 10000)
					continue;
				if (color.SqDistance(white) < 12000)
					continue;
				gray = TColor(red, red, red);
				if (color.SqDistance(gray) < 2500)
					continue;
				colorList.append(color);
			}
		}
	}

	ncolor = colorList.size();
	if (ncolor < n) {
		s = s * 0.95;
		if (s >= 1) {
			colorList.clear();
			vtl::warnx("Retrying colors in %s:%d\n", __FILE__,
				   __LINE__);
			goto retry;
		}
	}

	/*
	 * Randomize the order by swapping every element with a random
	 * element
	 */
	for (i = 0; i < ncolor; i++) {
		lrand48_r(&rdata, &rnd);
		j = rnd % ncolor;
		tmp = colorList[j];
		colorList[j] = colorList[i];
		colorList[i] = tmp;
	}

	i = 0;

	DEFINE_COLORMAP_ITERATOR(iter) = colorMap.begin();
	while (iter != colorMap.end()) {
		TColor &color = iter.value();
		iter++;
		color = colorList.at(i % ncolor);
		i++;
	}
}
Exemple #29
0
void prefetch_irritator(void *arg)
{
    int i, rc, no_of_pages, tid , thread_no, tc, oper , number_of_operations;
    unsigned long long  saved_seed, random_no , starting_address , memory_fetch_size;
	pthread_t ptid;
	unsigned char *start_addr;
    struct thread_context *th = (struct thread_context *)arg;
 	struct ruleinfo *current_rule 	= th->current_rule;
	int cache_type = current_rule->tgt_cache;
	int cache_line_size = system_information.cinfo[cache_type].line_size;
	unsigned int loop_count 	;
	long int	offset;
	unsigned long long temp_storage = 0x1, temp_pattern = 0x1;

    /*
     * char *contig_mem[NUM_SEGS*SEG_SIZE/(16*M)]; Physically contiguous
     * memory pointer. memory_set_size variable  gives total memory
     * allocated both are variables of global structure.
     */

    thread_no = th->thread_no ;
	int pcpu = pcpus_thread_wise[thread_no];
    tid       = th->bind_to_cpu;    /* Bind to the processor */
	ptid	  = th->tid;			/* PThread Id for this thread	*/
	prefetch_streams 	= th->prefetch_streams;		/* Number of prefetch streams for this thread. 	*/

    if (current_rule->testcase_type != PREFETCH_ONLY) {
        /* Set Thread Cancel Type as ASYNCHRONOUS */
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
    }

    #ifdef __HTX_LINUX__
       /*printf(" Prefetch:calling htx_bind with pcpu=%d for thread_no= %d\n",pcpu,thread_no);*/
    if(pcpu == -1){
        pcpu = htx_bind_thread(tid, -1);
        rc = pcpu;
        pcpus_thread_wise[thread_no]=pcpu;
        if(pcpu < 0){
            pcpus_thread_wise[thread_no]= -1;
        }

    }
    else {
        rc = htx_bind_thread(tid,pcpu);
    }
    #else
        rc = bindprocessor(BINDTHREAD, thread_self(), tid);
    #endif
		DEBUG_LOG("[%d] thread %d, binding to cpu %d \n",__LINE__,thread_no,tid);
	    
		if(rc < 0) {
	#ifdef __HTX_LINUX__
            if( rc == -2) {
				tot_thread_count --;
                sprintf(msg,"lcpu:%d(pcpu=%d) prefetch has been hot removed, thread will be terminating now tot_thread_count=%d\n",tid,pcpu,tot_thread_count);
                hxfmsg(&h_d, errno, HTX_HE_INFO, msg);
                pthread_exit(NULL);
            }
            else {
                sprintf(msg, "%d: Bindprocessor for prefetch irritator on lcpu:%d and corresponding pcpu:%d failed with rc=%d\n", __LINE__, tid,pcpu,rc);
                hxfmsg(&h_d, errno, HTX_HE_HARD_ERROR, msg);
            }

		#else
        sprintf(msg, "Binding to cpu:%d  failed with errno: %d \n",tid,errno);
        hxfmsg(&h_d, errno, HTX_HE_SOFT_ERROR, msg);

		#endif

        } /* End of if */
        else {
			/*sprintf(msg,"::physical cpu:%d for log cpu:%d\n",pcpu,tid);
            hxfmsg(&h_d, rc , HTX_HE_INFO, msg);*/
            #ifdef DEBUG
            sprintf(msg,"[%d] Bindprocessor success [prefetch thread_bo %d]! cpu_no : %d , pthread id : 0x%x \n",__LINE__,thread_no,tid,ptid);
            hxfmsg(&h_d, errno, HTX_HE_INFO, msg);
            #endif
        }

        th->seedval = time(NULL);
        srand48_r(th->seedval,&th->buffer);

       number_of_operations = current_rule->num_oper;

		starting_address = (unsigned long long)(th_array[thread_no].start_of_contiguous_memory);
		memory_fetch_size = current_rule->prefetch_memory_size - BYTES_EXC ;
		loop_count	= memory_fetch_size / cache_line_size;
       for (oper = 0; oper < number_of_operations ; oper++) {

        /* if SIGTERM was received, exit */
        if(exit_flag != 0) {
            break;
        }

        random_no = get_random_number_perf(thread_no);
        random_no = (unsigned long long)(random_no<<32) | (random_no);
		/*random_no = 0xaabbccdd;
		random_no = th_array[thread_no].random_pattern;*/
        th_array[thread_no].prev_seed = random_no;

		/* Now write DSCR if needed */
		if ( system_information.pvr >= POWER8_MURANO ) {
			prefetch_randomise_dscr(random_no, th->current_rule->pf_dscr , thread_no);
		}

        if (th_array[thread_no].prefetch_algorithm == RR_ALL_ENABLED_PREFETCH_ALGORITHMS) {
            /* Run all the enabled prefetch variants in round robin method */

            /* If prefetch nstride is set in the current prefetch configuration */
            if ( (PREFETCH_NSTRIDE & current_rule->pf_conf) == PREFETCH_NSTRIDE ) {
                n_stride(starting_address,memory_fetch_size,random_no,&th_array[thread_no].prefetch_scratch_mem[0]);
            }

        	/* if SIGTERM was received, exit */
            if (exit_flag != 0) {
                break;
            }

            /* If prefetch partial is set in the current prefetch configuration */
            if ( (PREFETCH_PARTIAL & current_rule->pf_conf) == PREFETCH_PARTIAL ) {
                partial_dcbt(starting_address,memory_fetch_size,random_no,&th_array[thread_no].prefetch_scratch_mem[0]);
            }

        	/* if SIGTERM was received, exit */
            if (exit_flag != 0) {
                break;
            }

            if ( (PREFETCH_IRRITATOR & current_rule->pf_conf) == PREFETCH_IRRITATOR ) {
				rc = do_prefetch( starting_address , memory_fetch_size , random_no, thread_no, loop_count, th_array[thread_no].pattern);
				if ( rc != 0 ) {
					sprintf(msg,"[%d] Miscompare in Prefetch!! Expected data = 0x%x Actual data = 0x%x thread_index : 0x%x Start of memory = %p, memory size = 0x%x\n"
								,__LINE__,th_array[thread_no].pattern, *(unsigned long long *)((unsigned char *)starting_address + 128*(loop_count-rc)), thread_no, starting_address, memory_fetch_size);
					hxfmsg(&h_d, 0, HTX_HE_MISCOMPARE, msg);
					dump_miscompare_data(thread_no, (unsigned char *)starting_address);
					return;
				}
                /*prefetch(starting_address,memory_fetch_size,random_no,&th_array[thread_no].prefetch_scratch_mem[0]);*/
            }

        	/* if SIGTERM was received, exit */
            if (exit_flag != 0) {
                break;
            }

            if( (PREFETCH_TRANSIENT & current_rule->pf_conf) == PREFETCH_TRANSIENT ) {
				/*lrand48_r(&th->buffer, &offset);*/
				offset		= random_no % (long)16;

				start_addr = (unsigned char *)starting_address + offset;
				rc = transient_dcbt((unsigned long long)start_addr, loop_count, th_array[thread_no].pattern );
				if ( rc != 0 ) {
					sprintf(msg,"[%d] Miscompare in Prefetch!! Expected data = 0x%x Actual data = 0x%x thread_index : 0x%x Start of memory = %p, memory size = 0x%x\n"
								,__LINE__,th_array[thread_no].pattern, *(unsigned long long *)((unsigned char *)starting_address + 128*(loop_count-rc)), thread_no, starting_address, memory_fetch_size);
					hxfmsg(&h_d, 0, HTX_HE_MISCOMPARE, msg);
					dump_miscompare_data(thread_no, (unsigned char *)starting_address);
					return;
				}
            }

        	/* if SIGTERM was received, exit */
            if (exit_flag != 0) {
                break;
            }

			if ( (PREFETCH_NA & current_rule->pf_conf) == PREFETCH_NA ) {

				/*lrand48_r(&th->buffer, &offset);*/
				offset		= random_no % (long)16;
				start_addr = (unsigned char *)starting_address + offset;
				rc = prefetch_dcbtna((unsigned long long)start_addr, loop_count, th_array[thread_no].pattern,&temp_storage,&temp_pattern);
				if ( rc != 0 ) {
					sprintf(msg,"[%d] Miscompare in Prefetch!! Expected data = 0x%x Actual data = 0x%x copied data = %x0x, copied pattern = %x0x, thread_index : 0x%x Start of memory = %p, memory size = 0x%x\n"
								,__LINE__,th_array[thread_no].pattern, *(unsigned long long *)((unsigned char *)starting_address + 128*(loop_count-rc)), temp_storage, temp_pattern, thread_no, starting_address, memory_fetch_size);
					hxfmsg(&h_d, 0, HTX_HE_MISCOMPARE, msg);
					dump_miscompare_data(thread_no, (unsigned char *)starting_address);
					return;
				}
			}
            if (exit_flag != 0) {
                break;
            }

        }
        else { /* Else Run only the specified algorithm */
            /*starting_address = (unsigned long long)(th_array[thread_no].start_of_contiguous_memory);
            memory_fetch_size = current_rule->prefetch_memory_size - BYTES_EXC ;*/

            if(th_array[thread_no].prefetch_algorithm == PREFETCH_NSTRIDE) {
				/*lrand48_r(&th->buffer, &random_no);*/
                n_stride(starting_address, memory_fetch_size, random_no, &th_array[thread_no].prefetch_scratch_mem[0]);
            }
            else if(th_array[thread_no].prefetch_algorithm == PREFETCH_PARTIAL) {
                partial_dcbt(starting_address, memory_fetch_size, random_no, &th_array[thread_no].prefetch_scratch_mem[0]);
            }
            else if(th_array[thread_no].prefetch_algorithm == PREFETCH_TRANSIENT) {
				/*lrand48_r(&th->buffer, &offset);*/
				offset		= random_no % (long)16;
				start_addr = (unsigned char *)starting_address + offset;

				rc = transient_dcbt((unsigned long long)start_addr, loop_count, th_array[thread_no].pattern );
				if ( rc != 0 ) {
					sprintf(msg,"[%d] Miscompare in Prefetch!! Expected data = 0x%x Actual data = 0x%x thread_index : 0x%x Start of memory = %p, memory size = 0x%x\n"
								,__LINE__,th_array[thread_no].pattern, *(unsigned long long *)((unsigned char *)starting_address + 128*(loop_count-rc)), thread_no, starting_address, memory_fetch_size);
					hxfmsg(&h_d, 0, HTX_HE_MISCOMPARE, msg);
					dump_miscompare_data(thread_no, (unsigned char *)starting_address);
					return;
				}
            }
            else if(th_array[thread_no].prefetch_algorithm == PREFETCH_IRRITATOR) {
				rc = do_prefetch( starting_address , memory_fetch_size , random_no, thread_no, loop_count, th_array[thread_no].pattern);
				if ( rc != 0 ) {
					sprintf(msg,"[%d] Miscompare in Prefetch!! Expected data = 0x%x Actual data = 0x%x thread_index : 0x%x Start of memory = %p, memory size = 0x%x\n"
								,__LINE__,th_array[thread_no].pattern, *(unsigned long long *)((unsigned char *)starting_address + 128*(loop_count-rc)), thread_no, starting_address, memory_fetch_size);
					hxfmsg(&h_d, 0, HTX_HE_MISCOMPARE, msg);
					dump_miscompare_data(thread_no, (unsigned char *)starting_address);
					return;
				}
            }
			else if ( th_array[thread_no].prefetch_algorithm == PREFETCH_NA ) {
				/*lrand48_r(&th->buffer, &offset);*/
				offset		= random_no % (long)16;
				start_addr = (unsigned char *)starting_address + offset;
				rc = prefetch_dcbtna((unsigned long long)start_addr, loop_count, th_array[thread_no].pattern,&temp_storage, &temp_pattern);
				if ( rc != 0 ) {
					sprintf(msg,"[%d] Miscompare in Prefetch ( returned %d)!! Expected data = 0x%x Actual data = 0x%x copied data = 0x%x, copied pattern = 0x%x, thread_index : 0x%x Start of memory = %p, offset = %d\n"
								,__LINE__, rc, th_array[thread_no].pattern, *(unsigned long long *)((unsigned char *)start_addr + 128*(loop_count-rc)), temp_storage, temp_pattern, thread_no, starting_address, offset);
					hxfmsg(&h_d, 0, HTX_HE_MISCOMPARE, msg);
					dump_miscompare_data(thread_no, (unsigned char *)starting_address);
					return;
				}
			}

        	/* if SIGTERM was received, exit */
            if(exit_flag != 0) {
                break;
            }
        }

    } /* End of for loop */
	#ifdef __HTX_LINUX__
        /* Restore original/default CPU affinity so that it binds to ANY available processor */

        rc = htx_unbind_thread();
	#else
        rc = bindprocessor(BINDTHREAD, thread_self(), PROCESSOR_CLASS_ANY);
	#endif
        if(rc == -1) {
                sprintf(msg, "%d: Unbinding from cpu:%d failed with errno %d \n",__LINE__, tid, errno);
                hxfmsg(&h_d, errno, HTX_HE_SOFT_ERROR, msg);
        }
	
#if defined(__HTX_MAMBO__) || defined(AWAN)
	printf("[%d] Thread no: %d, completed passes : %d\n",__LINE__, thread_no, oper);
#endif
}
Exemple #30
0
void* consumerThread(void *args) {
	int id = *((int *)args);
	double random;
	struct drand48_data randBuffer;
	int sock;
	QElement *elmnt;
	ssize_t rcvBytes;
	int type;
	int ret;
	HttpGet httpGet;
	ssize_t totalSize;	
	char *savedBuffer;
	
	int lenBuffer = sizeof(char) * BUFSIZE;
	char *buffer;
	
	srand48_r(time(NULL), &randBuffer);
	
	while (1) {
		totalSize = 0;
		
		pthread_mutex_lock(&queue_mutex);
		while (hasPending == 0) {
			LOG_INFO("Consumer[%d] is waiting.\n", id);
			pthread_cond_wait(&queue_cond, &queue_mutex);
		}
		
		if (queue_clients.size > 0 && queue_webservers.size > 0) {
			drand48_r(&randBuffer, &random);
			if (random < 0.5) {
				elmnt = pop_queue(&queue_clients);
			} else {
				elmnt = pop_queue(&queue_webservers);
			}
		} else if (queue_clients.size > 0) {
			elmnt = pop_queue(&queue_clients);
		} else {
			elmnt = pop_queue(&queue_webservers);
		}
		
		hasPending--;
		if (hasPending > 0) {
			pthread_cond_signal(&queue_cond);
		}
		
		pthread_mutex_unlock(&queue_mutex);
		
		buffer = (char *) malloc(lenBuffer);
		if (buffer == NULL) {
			LOG_ERROR("Not enough free memory space\n");
			return NULL;
		}
		
		sock = elmnt->clSock;
		free(elmnt);

		rcvBytes = recv(sock, buffer, lenBuffer, 0);
		if (rcvBytes < 0) {
			LOG_ERROR("Consumer[%d]: An error occurred while receiving data from the client.\n", id);
			continue;
		} else if (rcvBytes == 0) {
			LOG_ERROR("Consumer[%d]: The client has performed a shutdown.\n", id);
			continue;
		}
		totalSize += rcvBytes;
		
		type = findHttpMessageType(buffer, rcvBytes);
		if (type == HTTP_GET) {
			int webSock;
			LOG_INFO("Consumer[%d]: Get Message received\n", id);
			
			ret = parseHttpGetMsg(buffer, rcvBytes, &httpGet);
			
			if (ret == HTTP_PARSER_ERROR || ret == NO_MEMORY_ERROR) {
				sendHttpBadReqMsg(sock);
			} else if (ret == HTTP_HOST_NOT_FOUND){
				sendHttpNotFoundMsg(sock);
			} else if (ret == OK) {
				char* response = get_response_cache(cache, httpGet.request, httpGet.reqLen);
				if (response != NULL) {
					LOG_INFO("\tConsumer[%d]: Cache Hit\n", id);
					
					update_cache(cache, httpGet.request, httpGet.reqLen);
					
					ret = sendSingleHttpResponseMsg(response, strlen(response), sock);
					if (ret != OK) {
						LOG_ERROR("Consumer[%d]: Unable to send the response Message\n", id);
					}
				} else {
					LOG_INFO("\tConsumer[%d]: No Cache Hit\n", id);
					
					if (contains_request(httpGet.request, httpGet.reqLen) != FOUND) {
						LOG_INFO("\tConsumer[%d]: Does not contain that request\n", id);								
						webSock = sendHttpGetMsgToServer(&httpGet);
						if (webSock < 0) {
							LOG_ERROR("Consumer[%d]: Did not send the HttpGetMessage to the Webserver\n", id);
						} else {
							if (save_request(httpGet.request, httpGet.reqLen, webSock, sock) != OK) {
								LOG_ERROR("Consumer[%d]: An error occurred while saving the request\n", id);
							}
							if (insertFD(webSock) != OK) {
								LOG_ERROR("Consumer[%d]: An error occurred while saving the socket\n", id);
							}
						}
					} else {
						LOG_INFO("\tConsumer[%d]: Does contain that request\n", id);	
						webSock = get_request(httpGet.request, httpGet.reqLen);
					}
					
					if (map_client_with_webserver(webSock, sock) != OK) {
						LOG_ERROR("Consumer[%d]: An error occurred while making the mapping between client's socket and webserver's one.\n", id);
					}
				}
				
				clear_httpGetMsg(&httpGet);
			}
		} else if (type == HTTP_RESPONSE) {
			LOG_INFO("Consumer[%d]: Response Message received\n", id);
			
			Queue* clSocks = find_appropriate_clients(sock);
			if (clSocks == NULL) {
				LOG_ERROR("Consumer[%d]: Could not find the appropriate clients.\n", id);
				continue;
			}
			
			savedBuffer = (char *) malloc(sizeof(char) * rcvBytes);
			if (savedBuffer == NULL) {
				LOG_ERROR("Consumer[%d]: Not enough free memory space.\n", id);
				continue;
			}
			
			memcpy(savedBuffer, buffer, rcvBytes);
			
			char *t;
			ssize_t prevSize = rcvBytes;
			memset(buffer, 0, lenBuffer);
			while ((rcvBytes = recv(sock, buffer, lenBuffer, 0)) != 0) {
				totalSize += rcvBytes;
				t = realloc(savedBuffer, totalSize);
				if (t == NULL) {
					LOG_ERROR("Consumer[%d]: Not enough free memory space.\n", id);
					break;
				}
				
				savedBuffer = t;
				memcpy(savedBuffer + prevSize, buffer, rcvBytes);
				prevSize = totalSize;
			}
			
			QElement *iter;
			char *req;
			int clSock = -1;
			for (iter = clSocks->head; iter != NULL; iter = iter->next) {
				req = get_hashmap(&cl_req_map, &iter->clSock, sizeof(int));
				if (req != NULL) {
					if (put_response_cache(cache, req, strlen(req) + 1, savedBuffer, totalSize) != OK) {
						LOG_ERROR("Consumer[%d]: Could not insert it in the cache\n", id);
					} 
					clSock = iter->clSock;
					break;
				}
			}
			
			ret = sendHttpResponseMsg(savedBuffer, totalSize, clSocks);
			if (ret != OK) {
				LOG_ERROR("Consumer[%d]: Unable to send the response Message\n", id);
			}
			
			
			if (remove_appropiate_clients(sock) != OK) {
				LOG_ERROR("Consumer[%d]: Could not remove the appropriate clients from the hash map\n", id);
			}
			if (remove_request(sock, clSock) != OK) {
				LOG_ERROR("Consumer[%d]: Could not remove the request from the hash map\n", id);
			}
			
			if (savedBuffer != NULL) {
				free(savedBuffer);
			}
			
		} else {
			LOG_INFO("Consumer[%d]: Unknown type of message\n", id);
			sendHttpBadReqMsg(sock);
		}
		
		free(buffer);
	}
	
	return NULL;
}