Esempio n. 1
0
void rnorm(drand48_data &buf, int n, fvec &v){
	
	v = fvec (n);
	
	int m = -1;
	if(n % 2){
		m = n/2 + 1;
	}else{
		m = n/2;
	}
	
  int k = -1;
	for(int i = 0; i < m; ++i){
		float u1, u2, s;
		double r1, r2;
		do{
			drand48_r(&buf, &r1);
			drand48_r(&buf, &r2);
			u1 = 2.0f * (float) r1 - 1.0f;
			u2 = 2.0f * (float) r2 - 1.0f;
			s = u1 * u1 + u2 * u2;
		}while(s >= 1.0f || (s < 1e-12 && s > -1e-12));
		
		s = (float) sqrt((-2.0 * log(s)) / s);
    ++k;
		v[k] = u1 * s;
    ++k;
    if(k < n){
      v[k] = u2 * s;
    }
	}
	
}
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;
}
Esempio n. 3
0
int roll(float *dartboard, int *aliases, int num_sides, rand_buffer* buffer)
{
        double x, y;
        drand48_r(buffer, &x);
        drand48_r(buffer, &y);
        int col = x * num_sides;
        if (y > dartboard[col]) {
                return aliases[col];
        } else {
                return col;
        }
}
Esempio n. 4
0
void rl_act_binary::act_sample( void ) {
  double tmp, p, ep, gradval;
  //  printf( "binary: %d samples\n", num_samples );

  ep = exp( -params(0) );
  p = 1.0 / ( 1.0 + ep );
  gradval = ep / ( 1.0 + ep );

  //  printf( "p=%.2f; gradval=%.2f\n", p, gradval );

  grad(0) = 0;

  for ( int i=0; i<num_samples; i++ ) {
    drand48_r( &rng_state, &tmp );
    vals[i] = ( tmp < p );
    if ( vals[i] ) {
      grad(0) += gradval;
    } else {
      grad(0) += -gradval;
    }
  }

  //  printf( "Gradient: " ); grad.Print();

}
Esempio n. 5
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);
}
u_int16_t RTPRandom::GetRandom16()
{
	double x;
	drand48_r(&drandbuffer,&x);
	u_int16_t y = (u_int16_t)(x*65536.0);
	return y;
}
u_int8_t RTPRandom::GetRandom8()
{
	double x;
	drand48_r(&drandbuffer,&x);
	u_int8_t y = (u_int8_t)(x*256.0);
	return y;
}
Esempio n. 8
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;
}
Esempio n. 9
0
// Helper function to determine if a split occurs
int doesSplit(struct drand48_data* seedbuf, double* splitProb, int x, int y, int radius) {
    double r;
    drand48_r(seedbuf,&r);
    if(r < splitProb[toOffset(x,y,radius)]) {
        return 1;
    }
    return 0;
}
Esempio n. 10
0
unsigned long
mb_rand_range_ulong(struct drand48_data* rand, unsigned long from, unsigned long to)
{
    unsigned long width = to - from;
    double ret;
    drand48_r(rand, &ret);
    return ret * width + from;
}
Esempio n. 11
0
long
mb_rand_range_long(struct drand48_data* rand, long from, long to)
{
    long width = to - from;
    double ret;
    drand48_r(rand, &ret);
    return ret * width + from;
}
Esempio n. 12
0
static int roll_dice(double p_win)
{
    if (!chaos_enabled_p) {
        return 0;
    }
    double die;
    drand48_r(&dr_buf, &die);
    return p_win > die;
}
Esempio n. 13
0
uint64_t ycsb_query::zipf(uint64_t n, double theta) {
	assert(this->the_n == n);
	assert(theta == g_zipf_theta);
	double alpha = 1 / (1 - theta);
	double zetan = denom;
	double eta = (1 - pow(2.0 / n, 1 - theta)) / 
		(1 - zeta_2_theta / zetan);
	double u; 
	drand48_r(&_query_thd->buffer, &u);
	double uz = u * zetan;
	if (uz < 1) return 1;
	if (uz < 1 + pow(0.5, theta)) return 2;
	return 1 + (uint64_t)(n * pow(eta*u -eta + 1, alpha));
}
Esempio n. 14
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);
}
Esempio n. 15
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 */
Esempio n. 16
0
// Helper function to update a particle location
void updateLocation(struct drand48_data* seedbuf, DirUpdate* area, int* x, int *y, int radius) {
    double r;
    drand48_r(seedbuf,&r);
    DirUpdate d = area[toOffset(*x, *y, radius)];
    if(r<d.prob[0]) {
        *x = *x - 1;    // ul
        *y = *y + 1;
    }
    else if(r<d.prob[1]) {
        *y = *y + 1;    // u
    }
    else if(r<d.prob[2]) {
        *x = *x + 1;    // ur
        *y = *y + 1;
    }
    else if(r<d.prob[3]) {
        *x = *x - 1;                 // l
    }
    else if(r<d.prob[4]) {                           } //
    else if(r<d.prob[5]) {
        *x = *x + 1;                 // r
    }
    else if(r<d.prob[6]) {
        *x = *x - 1;    // ll
        *y = *y - 1;
    }
    else if(r<d.prob[7]) {
        *y = *y - 1;    // l
    }
    //if(d.prob[8]<r) { *x = *x + 1; *y = *y - 1; return; }
    // otherwise, lower right
    else {
        *x = *x + 1;
        *y = *y - 1;
    }

    return;
}
Esempio n. 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);  
}
Esempio n. 18
0
double rand_double(struct drand48_data* ctx) {
  double r;
  assert(!drand48_r(ctx, &r));
  return r;
}
Esempio n. 19
0
void *producer(void *thread_data) {
  //printf("producer coming in\n");
  //sleep(1);
  double mean_customer_wait_time = 0;
  int number_of_waits = 0;
  int number_sleeps = 0;
  pthread_mutex_lock(&count_mutex);
  thread_data_t *td = (&(*(thread_data_t *) thread_data));
  int local_number_consumed = 0;
  local_number_consumed = td->number_consumed;
  pthread_mutex_unlock(&count_mutex);
  struct timeval tv1, tv2;
  int squares_inter_service_wait_sum = 0;
  int square_inter_arrival_time_sum = 0;
  double wait_time = 0;
  double inter_arrival_time = 0;
  while (local_number_consumed < LIMIT_CONSUME) {
    struct timeval tv;
    gettimeofday(&tv,NULL);
    struct drand48_data buffer;
    srand48_r(tv.tv_sec+tv.tv_usec, &buffer);
    double random_value;
    drand48_r(&buffer, &random_value);
    random_value = -1 *(log(1.0-random_value)/td->arrival_rate);
    //printf("inter-arrival rate: %f\n", random_value);
    struct timespec sleepTime;
    sleepTime.tv_sec = (int)floor(random_value);
    sleepTime.tv_nsec = (random_value-sleepTime.tv_sec) * 1000000000L;
    inter_arrival_time += (sleepTime.tv_sec + (sleepTime.tv_nsec / 1000000000.0));
    square_inter_arrival_time_sum += pow(inter_arrival_time, 2);
    number_sleeps++;
    nanosleep(&sleepTime, NULL);
    //printf("producer sleep time: %f\n", (sleepTime.tv_sec + (sleepTime.tv_nsec / 1000000000.0)));

    gettimeofday(&tv1, NULL);
    struct timeval lock_begin, lock_end;
    gettimeofday(&lock_begin, NULL);
    pthread_mutex_lock(&count_mutex);
    gettimeofday(&lock_end, NULL);
    //printf("producer, lock after arrival, mutex lock wait: %f\n", (lock_end.tv_sec - lock_begin.tv_sec + ((lock_end.tv_usec - lock_begin.tv_usec) / 1000000.0)));
    local_number_consumed = td->number_consumed;
    element_t *queue = td->queue;
    //printf("producer queue addy: %p\n", queue);
    element_t *cur_element = queue;
    int num_elements = 0;
    if (cur_element != NULL) {
      while (cur_element->next != NULL) {
	cur_element = cur_element->next;
	num_elements++;
      }
    }
    // at the end of the queue, now
    element_t *new_end = (element_t *)malloc(sizeof(element_t));
    if (cur_element != NULL) {
      cur_element->next = new_end;
    } else {
      cur_element = new_end;
      td->queue = cur_element;
    }
    new_end->next = NULL;
    if (num_elements == 0) {
      // let the consumer know we have something for them.
      pthread_cond_signal(&count_threshold_cv);
    }
    pthread_mutex_unlock(&count_mutex);
    gettimeofday(&tv2, NULL);
    wait_time = (tv2.tv_sec - tv1.tv_sec + ((tv2.tv_usec - tv1.tv_usec) / 1000000.0));
    //printf("situ customer wait time: %f, rolling mean before: %f\n", wait_time, mean_customer_wait_time);
    mean_customer_wait_time += wait_time;
    squares_inter_service_wait_sum += pow(mean_customer_wait_time, 2);
    number_of_waits++;
    //mean_customer_wait_time /= 1.0 * ++number_of_waits;

  }
  pthread_mutex_lock(&count_mutex);
  td->mean_inter_service_wait_time += (mean_customer_wait_time / (1.0*number_of_waits));
  td->mean_inter_arrival_time += (inter_arrival_time/(1.0*number_sleeps));
  td->std_dev_service_wait_time += sqrt((squares_inter_service_wait_sum/number_of_waits) - pow(td->mean_inter_service_wait_time, 2));
  td->std_dev_arrival_time += sqrt((square_inter_arrival_time_sum/number_sleeps) - pow(td->mean_inter_arrival_time, 2));
  pthread_mutex_unlock(&count_mutex);
  //printf("out of producer loop\n");
  //printf("producer exiting, i: %d\n", local_number_consumed);
  pthread_exit(NULL);
}
Esempio n. 20
0
void ycsb_query::gen_requests(uint64_t thd_id, workload * h_wl) {
#if CC_ALG == HSTORE
	assert(g_virtual_part_cnt == g_part_cnt);
#endif
	int access_cnt = 0;
	set<uint64_t> all_keys;
	part_num = 0;
	double r = 0;
	int64_t rint64 = 0;
	drand48_r(&_query_thd->buffer, &r);
	lrand48_r(&_query_thd->buffer, &rint64);
	if (r < g_perc_multi_part) {
		for (UInt32 i = 0; i < g_part_per_txn; i++) {
			if (i == 0 && FIRST_PART_LOCAL)
				part_to_access[part_num] = thd_id % g_virtual_part_cnt;
			else {
				part_to_access[part_num] = rint64 % g_virtual_part_cnt;
			}
			UInt32 j;
			for (j = 0; j < part_num; j++) 
				if ( part_to_access[part_num] == part_to_access[j] )
					break;
			if (j == part_num)
				part_num ++;
		}
	} else {
		part_num = 1;
		if (FIRST_PART_LOCAL)
			part_to_access[0] = thd_id % g_part_cnt;
		else
			part_to_access[0] = rint64 % g_part_cnt;
	}

	int rid = 0;
	for (UInt32 tmp = 0; tmp < g_req_per_query; tmp ++) {		
		double r;
		drand48_r(&_query_thd->buffer, &r);
		ycsb_request * req = &requests[rid];
		if (r < g_read_perc) {
			req->rtype = RD;
		} else if (r >= g_read_perc && r <= g_write_perc + g_read_perc) {
			req->rtype = WR;
		} else {
			req->rtype = SCAN;
			req->scan_len = SCAN_LEN;
		}

		// the request will access part_id.
		uint64_t ith = tmp * part_num / g_req_per_query;
		uint64_t part_id = 
			part_to_access[ ith ];
		uint64_t table_size = g_synth_table_size / g_virtual_part_cnt;
		uint64_t row_id = zipf(table_size - 1, g_zipf_theta);
		assert(row_id < table_size);
		uint64_t primary_key = row_id * g_virtual_part_cnt + part_id;
		req->key = primary_key;
		int64_t rint64;
		lrand48_r(&_query_thd->buffer, &rint64);
		req->value = rint64 % (1<<8);
		// Make sure a single row is not accessed twice
		if (req->rtype == RD || req->rtype == WR) {
			if (all_keys.find(req->key) == all_keys.end()) {
				all_keys.insert(req->key);
				access_cnt ++;
			} else continue;
		} else {
			bool conflict = false;
			for (UInt32 i = 0; i < req->scan_len; i++) {
				primary_key = (row_id + i) * g_part_cnt + part_id;
				if (all_keys.find( primary_key )
					!= all_keys.end())
					conflict = true;
			}
			if (conflict) continue;
			else {
				for (UInt32 i = 0; i < req->scan_len; i++)
					all_keys.insert( (row_id + i) * g_part_cnt + part_id);
				access_cnt += SCAN_LEN;
			}
		}
		rid ++;
	}
	request_cnt = rid;

	// Sort the requests in key order.
	if (g_key_order) {
		for (int i = request_cnt - 1; i > 0; i--) 
			for (int j = 0; j < i; j ++)
				if (requests[j].key > requests[j + 1].key) {
					ycsb_request tmp = requests[j];
					requests[j] = requests[j + 1];
					requests[j + 1] = tmp;
				}
		for (UInt32 i = 0; i < request_cnt - 1; i++)
			assert(requests[i].key < requests[i + 1].key);
	}

}
double RTPRandom::GetRandomDouble()
{
	double x;
	drand48_r(&drandbuffer,&x);
	return x;
}
Esempio n. 22
0
int main(int argc, char **argv)
{
  SQLHENV     hEnv = NULL;
  SQLHDBC     hDbc = NULL;
  SQLHSTMT    hStmt = NULL;
  char*       pConnStr;
  char        pQuery[1000];
  bool        silent = true;

  if (argc != 5) {
    fprintf(stderr, "Usage: %s <ConnString> <pkey range> <ccol range> <rand seed>\n", argv[0]);
    return 1;
  }
  pConnStr = argv[1];
  char *endptr;
  long long numkeys = strtoll(argv[2], &endptr, 10);
  long long rowsperkey = strtoll(argv[3], &endptr, 10);
  int seed = atoi(argv[4]);
  struct drand48_data lcg;
  srand48_r(seed, &lcg);

  // Allocate an environment
  if (!silent)
    fprintf(stderr, "Allocating Handle Enviroment\n");
  if (SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv) == SQL_ERROR)
    {
      fprintf(stderr, "Unable to allocate an environment handle\n");
      exit(-1);
    }

  // Register this as an application that expects 3.x behavior,
  // you must register something if you use AllocHandle
  if (!silent)
    fprintf(stderr, "Setting to ODBC3\n");
  TRYODBC(hEnv,
	  SQL_HANDLE_ENV,
	  SQLSetEnvAttr(hEnv,
			SQL_ATTR_ODBC_VERSION,
			(SQLPOINTER)SQL_OV_ODBC3,
			0));

  // Allocate a connection
  if (!silent)
    fprintf(stderr, "Allocating Handle\n");
  TRYODBC(hEnv,
	  SQL_HANDLE_ENV,
	  SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc));

  // Connect to the driver.  Use the connection string if supplied
  // on the input, otherwise let the driver manager prompt for input.
  if (!silent)
    fprintf(stderr, "Connecting to driver\n");
  TRYODBC(hDbc,
	  SQL_HANDLE_DBC,
	  SQLDriverConnect(hDbc,
			   NULL,
			   pConnStr,
			   SQL_NTS,
			   NULL,
			   0,
			   NULL,
			   SQL_DRIVER_COMPLETE));

  fprintf(stderr, "Connected!\n");

  if (!silent)
    fprintf(stderr, "Allocating statement\n");
  TRYODBC(hDbc,
	  SQL_HANDLE_DBC,
	  SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt));

  RETCODE     RetCode;
  SQLSMALLINT sNumResults;

  // Execute the query
  if (!silent)
    fprintf(stderr, "Executing query\n");
  long long i;
  double rval;
  for (i = 0; i < 100000; i++) {
    drand48_r(&lcg, &rval);
    sprintf(pQuery, "SELECT MAX(col1) FROM otest.test10 WHERE ccol = %lld", (long long)(rval * numkeys));
    RetCode = SQLExecDirect(hStmt, pQuery, SQL_NTS);

    switch(RetCode)
      {
      case SQL_SUCCESS_WITH_INFO:
	{
	  HandleDiagnosticRecord(hStmt, SQL_HANDLE_STMT, RetCode);
	  // fall through
	}
      case SQL_SUCCESS:
	{
	  // If this is a row-returning query, display
	  // results
	  TRYODBC(hStmt,
		  SQL_HANDLE_STMT,
		  SQLNumResultCols(hStmt,&sNumResults));
	  
	  if (sNumResults > 0)
	    {
	      DisplayResults(hStmt,sNumResults, silent);
	    } 
	  else
	    {
	      SQLLEN cRowCount;
	      
	      TRYODBC(hStmt,
		      SQL_HANDLE_STMT,
		      SQLRowCount(hStmt,&cRowCount));
	      
	      if (cRowCount >= 0)
		{
		  printf("%d %s returned\n",
			 (int)cRowCount,
			 (cRowCount == 1) ? "row" : "rows");
		}
	    }
	  break;
	}
	
      case SQL_ERROR:
	{
	  HandleDiagnosticRecord(hStmt, SQL_HANDLE_STMT, RetCode);
	  break;
	}
	
      default:
	fprintf(stderr, "Unexpected return code %hd!\n", RetCode);
	
      }
  }

  TRYODBC(hStmt,
	  SQL_HANDLE_STMT,
	  SQLFreeStmt(hStmt, SQL_CLOSE));

 Exit:

  // Free ODBC handles and exit

  if (hStmt)
    {
      SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
    }

  if (hDbc)
    {
      SQLDisconnect(hDbc);
      SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
    }

  if (hEnv)
    {
      SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
    }

  return 0;

}
Esempio n. 23
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;
}
Esempio n. 24
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;
}
Esempio n. 25
0
/* Threads will start here */
int worker_listen(worker *worker) {

    int i, b;
    monitor *read_mon;              /* temp monitor storage */
    monitor *write_mon;
    int in_file = -1;
    int out_file = -1;     /* the files */
    char *in_buffer, *out_buffer;   /* i/o buffers */
    job *read_msg, *write_msg;       /* temp job storage */

    int res = -1;
    int what_disc = 0;
    int what_block = 0;

    double rand_result;
    struct drand48_data work_space;

#if 0
    char in_buff1[BLOCK_SIZE];  /* read-ahead buffers */
    char in_buff2[BLOCK_SIZE];
    char in_buff3[BLOCK_SIZE];
    char in_buff4[BLOCK_SIZE];

    char out_buff1[BLOCK_SIZE]; /* write-behind buffers */
    char out_buff2[BLOCK_SIZE];
    char out_buff3[BLOCK_SIZE];
    char out_buff4[BLOCK_SIZE];
#endif

    seed_rand(&work_space);
    /* Get in/out file for each repetition desired */
    for (i = 0; i < worker->repetition; i++) {
        in_file = -1;
        out_file= -1;
        while (out_file == in_file) {
            drand48_r(&work_space, &rand_result);
            in_file = (int)(rand_result * 1000);
            drand48_r(&work_space, &rand_result);
            out_file = (int)(rand_result * 1000);
        }

        // make made up buffers
        in_buffer = emalloc(BLOCK_SIZE * sizeof(char));
        memset(in_buffer, 0, BLOCK_SIZE);

        out_buffer = emalloc(BLOCK_SIZE * sizeof (char));
        memset(out_buffer, 0, BLOCK_SIZE);

        for (b=0; b < BLOCKS_PER_FILE; b++) {

            /* Create read monitor */
            read_mon = emalloc(sizeof(monitor)); 
            read_mon->buffer = in_buffer;
            read_mon->request_time = worker->time;

            /* Create read job. mainly used for original implementation */
            read_msg = emalloc(sizeof(job));
            read_msg->message = READ;
            read_msg->communication_monitor = read_mon;

            /* Create write monitor */
            write_mon = emalloc(sizeof(monitor)); 
            write_mon->buffer = out_buffer;
            write_mon->request_time = worker->time;

            /* Create write job. mainly used for original implementation */
            write_msg = emalloc(sizeof(job));
            write_msg->message = WRITE;
            write_msg->communication_monitor = write_mon;

            compute_physical_address(in_file, b, &what_disc, &what_block, worker->number_of_discs);
            read_mon->block_number = what_block;

            compute_physical_address(out_file, b, &what_disc, &what_block, worker->number_of_discs);
            write_mon->block_number = what_block;

            if (in_file < out_file) {
                // read
                pthread_mutex_lock(&worker->all_discs[what_disc].read_lock);

                cbuffer_add(read_msg,&worker->all_discs[what_disc].read_cbuf);

                pthread_mutex_unlock(&worker->all_discs[what_disc].read_lock);

                // write
                pthread_mutex_lock(&worker->all_discs[what_disc].write_lock);

                cbuffer_add(write_msg,&worker->all_discs[what_disc].write_cbuf);

                pthread_mutex_unlock(&worker->all_discs[what_disc].write_lock);

            } else {

                // write
                pthread_mutex_lock(&worker->all_discs[what_disc].write_lock);

                while (is_cb_full(&worker->all_discs[what_disc].write_cbuf) == 0) {
                    sched_yield();
                }
                cbuffer_add(write_msg,&worker->all_discs[what_disc].write_cbuf);

                pthread_mutex_unlock(&worker->all_discs[what_disc].write_lock);

                // read 
                pthread_mutex_lock(&worker->all_discs[what_disc].read_lock);

                cbuffer_add(read_msg,&worker->all_discs[what_disc].read_cbuf);

                pthread_mutex_unlock(&worker->all_discs[what_disc].read_lock);
            }

        }

        printf("Sent write requests for file: %d\n",out_file);
        printf("Sent read requests for file: %d\n",in_file);

        // TODO TODO TODO - maybe not the below solution. may have to do it for each block 
        // checks replies after all blocks for the file have been queued
        // Figured its better to send the requests to disc first as it is the choke point
        // check reply queue and read replies here 

    }
    printf("Worker took: %ld\n", worker->time);
    //    printf("Worker took %ld\n", );
    return 0;
}
Esempio n. 26
0
void *consumer(void *thread_data) {
  double random_value;
  double mean_customer_wait_time = 0;
  int squares_service_time_sum = 0;
  //double std_dev_service_time = 0;
  int number_of_waits = 0;
  pthread_mutex_lock(&count_mutex);
  thread_data_t *td = &(*(thread_data_t *)thread_data);
  //int local_consumer = td->consumers_that_have_started++;
  int i = td->number_consumed;
  pthread_mutex_unlock(&count_mutex);
  struct timespec abstime;
  abstime.tv_sec = 2;
  abstime.tv_nsec = 3000;


  double free_time = 0;
  double total_time = 0;
  while(i < LIMIT_CONSUME) {
    //printf("consumer waiting for lock, at 0, local: %d\n", local_consumer);
    struct timeval lock_begin, lock_end;
    gettimeofday(&lock_begin, NULL);
    pthread_mutex_lock(&count_mutex);
    gettimeofday(&lock_end, NULL);
    //printf("consumer, mutex lock 1 wait: %f\n", (lock_end.tv_sec - lock_begin.tv_sec + ((lock_end.tv_usec - lock_begin.tv_usec) / 1000000.0)));
    i = td->number_consumed;
    //printf("consumer top, num: %d\n", i);
    element_t *queue = td->queue;
    if (queue != NULL) {
      element_t *old_head = queue;
      td->queue = old_head->next;
      free(old_head);
      td->number_consumed = ++i; 
      pthread_mutex_unlock(&count_mutex);
      struct timeval tv1, tv2;
      gettimeofday(&tv1, NULL);

      struct timeval tv;
      gettimeofday(&tv,NULL);
      struct drand48_data buffer;
      srand48_r(tv.tv_sec+tv.tv_usec, &buffer);
      drand48_r(&buffer, &random_value);
      //thread_data_t td = (*(thread_data_t *)thread_data);
      random_value = -1 *(log(1.0-random_value)/td->service_rate);
      //printf("inter-service rate: %f\n", random_value);
      struct timespec sleepTime;
      sleepTime.tv_sec = (int)floor(random_value);
      sleepTime.tv_nsec = (random_value-sleepTime.tv_sec) * 1000000000L;


      nanosleep(&sleepTime, NULL);
      gettimeofday(&tv2, NULL);
      double wait_time = tv2.tv_sec - tv1.tv_sec + (tv2.tv_usec - tv1.tv_usec) / 1000000.0;
      mean_customer_wait_time += wait_time;
      squares_service_time_sum += pow(mean_customer_wait_time, 2);
      number_of_waits++;

      //printf("consumer waiting for lock at 1, local: %d\n", local_consumer);
      pthread_mutex_lock(&count_mutex);
      gettimeofday(&lock_end, NULL);
      total_time += (lock_end.tv_sec - lock_begin.tv_sec + ((lock_end.tv_usec - lock_begin.tv_usec) / 1000000.0));
      
      //printf("consumer received for lock at 1, local: %d\n", local_consumer);
    } else {
      // let's wait for the producer to inform us there is an item waiting for us.
      gettimeofday(&lock_begin, NULL);
      pthread_cond_timedwait(&count_threshold_cv, &count_mutex, &abstime);
      gettimeofday(&lock_end, NULL);
      free_time += (lock_end.tv_sec - lock_begin.tv_sec + ((lock_end.tv_usec - lock_begin.tv_usec) / 1000000.0));
      //printf("consumer, cond wait: %f\n", (lock_end.tv_sec - lock_begin.tv_sec + ((lock_end.tv_usec - lock_begin.tv_usec) / 1000000.0)));
    }
    i = td->number_consumed;
    if (i >= LIMIT_CONSUME) {
      td->consumers_that_have_finished += 1;
    }
    pthread_mutex_unlock(&count_mutex);
  }
  //printf("consumer exiting, local: %d\n", local_consumer);
  pthread_mutex_lock(&count_mutex);
  mean_customer_wait_time /= (1.0 * number_of_waits);
  td->mean_inter_service_time += mean_customer_wait_time;
  td->std_dev_service_time += sqrt((squares_service_time_sum/number_of_waits) - pow(td->mean_inter_service_time, 2));
  td->utilization += (total_time - free_time) / total_time;
  pthread_mutex_unlock(&count_mutex);
  pthread_exit(NULL);
}