void traffic_light_intersection_startup(intersection_state* SV, tw_lp* LP) {
    tw_stime ts = 0;
    tw_event* current_event;
    message_data* new_message;

    // Initialize the total number of cars arrived:
    SV->total_cars_arrived = 0;
    SV->total_cars_finished = 0;

    // Initialize the number of cars arriving into the intersection:
    SV->num_cars_south = 0;
    SV->num_cars_west = 0;
    SV->num_cars_north = 0;
    SV->num_cars_east = 0;

    SV->num_cars_south_left = 0;
    SV->num_cars_west_left = 0;
    SV->num_cars_north_left = 0;
    SV->num_cars_east_left = 0;

    SV->north_south_green_until = -1;
    SV->north_south_left_green_until = -1;
    SV->east_west_green_until = -1;
    SV->east_west_left_green_until = -1;

    // Initialize a random direction to be green
    SV->traffic_direction = tw_rand_ulong(LP->rng, NORTH_SOUTH, EAST_WEST_LEFT);

    // Schedule the first light change
    current_event = tw_event_new(LP->gid, ts, LP);
    new_message = (message_data*)tw_event_data(current_event);
    new_message->event_type = LIGHT_CHANGE;
    tw_event_send(current_event);

    // Put cars on the road
    int i;
    for(i = 0; i < g_traffic_start_events; i++) {
        // Arrival time
        ts = tw_rand_exponential(LP->rng, INITIAL_ARRIVAL_MEAN);

        current_event = tw_event_new(LP->gid, ts, LP);
        new_message = (message_data*)tw_event_data(current_event);

        new_message->event_type = CAR_ARRIVES;

        assign_rand_dest:
        new_message->car.x_to_go = tw_rand_integer(LP->rng, -MAX_TRAVEL_DISTANCE, MAX_TRAVEL_DISTANCE);
        new_message->car.y_to_go = tw_rand_integer(LP->rng, -MAX_TRAVEL_DISTANCE, MAX_TRAVEL_DISTANCE);
        new_message->car.x_to_go_original = new_message->car.x_to_go;
        new_message->car.y_to_go_original = new_message->car.y_to_go;
        if (new_message->car.y_to_go == 0)
            new_message->car.has_turned = 1;
        else
            new_message->car.has_turned = 0;
        new_message->car.start_time = tw_now(LP);

        tw_event_send(current_event);
    }
}
static void svr_init(
    svr_state * ns,
    tw_lp * lp)
{
    ns->server_idx = lp->gid / 2;
    if (ns->server_idx < NUM_SERVERS-1){
        for (int i = 0; i < NUM_PRIOS; i++){
            ns->random_order[i] = -1;
        }
        for (int i = 0; i < NUM_PRIOS; i++){
            for (;;){
                int idx = tw_rand_integer(lp->rng, 0, NUM_PRIOS-1);
                // not sure whether rand_integer is inclusive or not...
                assert(idx < NUM_PRIOS);
                if (ns->random_order[idx] == -1){
                    ns->random_order[idx] = i;
                    break;
                }
            }
        }
        tw_event *e = codes_event_new(lp->gid, codes_local_latency(lp), lp);
        svr_msg * m = tw_event_data(e);
        msg_set_header(666, KICKOFF, lp->gid, &m->h);
        tw_event_send(e);
    }
    else {
        memset(ns->num_recv, 0, NUM_SERVERS*sizeof(*ns->num_recv));
    }
}
Esempio n. 3
0
void
mem_event_handler(mem_state * s, tw_bf * bf, mem_message * m, tw_lp * lp)
{
    tw_lpid		 dest;
    tw_event	*e;
    tw_memory	*b;

    int		 i;

    s->stats.s_recv++;

    // read membufs off inbound event, check it and free it
    for(i = 0; i < nbufs; i++)
    {
        b = tw_event_memory_get(lp);

        if(!b)
            tw_error(TW_LOC, "Missing memory buffers: %d of %d",
                     i+1, nbufs);

        mem_verify(b);
        tw_memory_free(lp, b, my_fd);

        s->stats.s_mem_free++;
        s->stats.s_mem_get++;
    }

    if(tw_rand_unif(lp->rng) <= percent_remote)
    {
        bf->c1 = 1;
        dest = tw_rand_integer(lp->rng, 0, ttl_lps - 1);

        dest += offset_lpid;

        if(dest >= ttl_lps)
            dest -= ttl_lps;
    } else
    {
        bf->c1 = 0;
        dest = lp->gid;
    }

    e = tw_event_new(dest, tw_rand_exponential(lp->rng, mean), lp);

    // allocate membufs and attach them to the event
    for(i = 0; i < nbufs; i++)
    {
        b = mem_alloc(lp);

        if(!b)
            tw_error(TW_LOC, "no membuf allocated!");

        mem_fill(b);
        tw_event_memory_set(e, b, my_fd);
        s->stats.s_mem_alloc++;
    }

    tw_event_send(e);
}
Esempio n. 4
0
void
tmr_event_handler(tmr_state * s, tw_bf * bf, tmr_message * m, tw_lp * lp)
{
	tw_lpid	 dest;

	* (int *) bf = 0;

	if(s->timer)
	{
		// if current event being processed is the timer
		if(tw_event_data(s->timer) == m)
		{
			bf->c1 = 1;

			m->old_timer = s->timer;
			s->timer = NULL;

			fprintf(f, "%lld: tmr fired at %lf\n", 
				lp->gid, tw_now(lp));

			return;
		} else
		{
			bf->c2 = 1;

			m->old_time = s->timer->recv_ts;
			tw_timer_reset(lp, &s->timer, tw_now(lp) + 100.0);

			if(s->timer == NULL)
				fprintf(f, "%lld: reset tmr failed %lf\n", 
					lp->gid, tw_now(lp) + 100.0);
			else
				fprintf(f, "%lld: reset tmr to %lf\n", 
					lp->gid, tw_now(lp) + 100.0);
		}
	}

	if(tw_rand_unif(lp->rng) <= percent_remote)
	{
		bf->c3 = 1;
		dest = tw_rand_integer(lp->rng, 0, ttl_lps - 1);

		dest += offset_lpid;

		if(dest >= ttl_lps)
			dest -= ttl_lps;
	} else
	{
		bf->c3 = 0;
		dest = lp->gid;
	}

	if(!lp->gid)
		dest = 0;

	tw_event_send(tw_event_new(dest, tw_rand_exponential(lp->rng, mean), lp));
}
/**
 * Check the number of requests processed (TODO) and computes write overhead for storage and BB
 * @Params ns node state
 * 		   m message
 * 		   lp LP
 */
void node_finalize(node_state * ns, tw_lp * lp) {
	// do some error checking - here, we ensure we got the expected number of
	// messages
	printf("In node_finalize\n");
	int mult;
	if (ns->is_in_client) {
		mult = 1;
	} else /*if(ns->is_in_server)*/{
		//printf("num_svr_nodes is %d\n",num_svr_nodes);
		mult = (num_client_nodes / num_svr_nodes)
				+ ((num_client_nodes % num_svr_nodes) > ns->id_clust);
	}
	if (ns->num_processed != num_reqs * mult) {
		fprintf(stderr, "%s node %d, lp %lu: processed %d (expected %d)\n",
				ns->is_in_client ? "client" : "svr", ns->id_clust, lp->gid,
				ns->num_processed, num_reqs * mult);
	}

	//float io_noise = 0.05 * tw_rand_integer(lp->rng,ns->pvfs_ts_remote_write,ns->pvfs_ts_remote_write);
	float io_noise = ns->pvfs_ts_remote_write
			* ((float) tw_rand_integer(lp->rng, 0.0, 5.0)) / 100.0;

	/*  printf("--------	Random number : %f	------\n",((float) tw_rand_integer(lp->rng,0.0,100.0))/100.0);
	 printf("--------	Remote write latecy : %f	-------\n",(float) ns->pvfs_ts_remote_write );
	 printf("--------	IO Noise : %f	------\n",io_noise);*/
	float io_noise_bb = 0.05
			* tw_rand_integer(lp->rng, ns->bb_ts_local_write,
					ns->bb_ts_local_write);

	long rand_idx = 0;
	//printf("num_svr_nodes is %d\n",num_svr_nodes);
	int dest_id = (lp->gid + rand_idx * 2) % (num_svr_nodes * 2);
	printf("Server %llu time = %f seconds.\n", (unsigned long long) lp->gid,
			ns_to_s(tw_now(lp) - ns->start_ts) + io_noise);

	return;
}
Esempio n. 6
0
void
ospf_random_weights(ospf_state * state, tw_lp * lp)
{
	tw_memory	*b;
	tw_event	*e;
	tw_stime	 next_status;

	ospf_message	*m;

	int	 	 percent;
	long int	 i;

	for(i = 0; i < state->m->nlinks; i++)
	{
		percent = tw_rand_integer(lp->rng, 0, 100);

		if(percent > 10 + 1)
			return;

		next_status = tw_rand_integer(lp->rng, 1, g_tw_ts_end);

		if(next_status >= g_tw_ts_end)
			continue;

		e = NULL;
		e = rn_timer_simple(lp, next_status);

		b = tw_memory_alloc(lp, g_ospf_fd);
		m = tw_memory_data(b);

		m->type = OSPF_WEIGHT_CHANGE;
		m->data = (void *) i;

		tw_event_memory_set(e, b, g_ospf_fd);
	}
}
static void handle_kickoff_event(
	    svr_state * ns,
	    tw_bf * b,
	    svr_msg * m,
	    tw_lp * lp)
{
    char* anno;
    tw_lpid local_dest = -1, global_dest = -1;
   
    svr_msg * m_local = malloc(sizeof(svr_msg));
    svr_msg * m_remote = malloc(sizeof(svr_msg));

    m_local->svr_event_type = LOCAL;
    m_local->src = lp->gid;

    memcpy(m_remote, m_local, sizeof(svr_msg));
    m_remote->svr_event_type = REMOTE;

    assert(net_id == DRAGONFLY); /* only supported for dragonfly model right now. */

    ns->start_ts = tw_now(lp);
    
   codes_mapping_get_lp_info(lp->gid, group_name, &group_index, lp_type_name, &lp_type_index, anno, &rep_id, &offset);
   /* in case of uniform random traffic, send to a random destination. */
   if(traffic == UNIFORM)
   {
   	local_dest = tw_rand_integer(lp->rng, 0, num_nodes - 1);
//	printf("\n LP %ld sending to %d ", lp->gid, local_dest);
   }
   else if(traffic == NEAREST_GROUP)
   {
	local_dest = (rep_id * 2 + offset + num_nodes_per_grp) % num_nodes;
//	printf("\n LP %ld sending to %ld num nodes %d ", rep_id * 2 + offset, local_dest, num_nodes);
   }	
   else if(traffic == NEAREST_NEIGHBOR)
   {
	local_dest =  (rep_id * 2 + offset + 2) % num_nodes;
//	 printf("\n LP %ld sending to %ld num nodes %d ", rep_id * 2 + offset, local_dest, num_nodes);
   }
   assert(local_dest < num_nodes);
   codes_mapping_get_lp_id(group_name, lp_type_name, anno, 1, local_dest / num_servers_per_rep, local_dest % num_servers_per_rep, &global_dest);
  
   ns->msg_sent_count++;
   model_net_event(net_id, "test", global_dest, PAYLOAD_SZ, 0.0, sizeof(svr_msg), (const void*)m_remote, sizeof(svr_msg), (const void*)m_local, lp);
   issue_event(ns, lp);
   return;
}
Esempio n. 8
0
void
phold_pre_run(phold_state * s, tw_lp * lp)
{
    tw_lpid	 dest;

	if(tw_rand_unif(lp->rng) <= percent_remote)
	{
		dest = tw_rand_integer(lp->rng, 0, ttl_lps - 1);
	} else
	{
		dest = lp->gid;
	}

	if(dest >= (g_tw_nlp * tw_nnodes()))
		tw_error(TW_LOC, "bad dest");

	tw_event_send(tw_event_new(dest, tw_rand_exponential(lp->rng, mean) + lookahead, lp));
}
Esempio n. 9
0
void
ospf_experiment_weights(ospf_state * state, long int i, tw_lp * lp)
{
	ospf_db_entry	*dbe;

	int		 metric;
	int		 low;
	int		 high;

	low = 50 - g_ospf_link_weight;
	high = 50 + g_ospf_link_weight;

	if(low <= 0)
		low = 1;

	metric = tw_rand_integer(lp->rng, low, high);

	if(metric == state->m->link[i].cost)
		return;

#if VERIFY_OSPF_EXPERIMENT
	printf("%ld: updating link %ld-%d metric: %d -> %d \n", 
		lp->gid, state->m->id, state->m->link[i].addr, 
		state->m->link[i].cost, metric);
#endif

	state->m->link[i].cost = metric;
	dbe = &state->db[lp->gid - state->ar->low];

	dbe->lsa = ospf_lsa_find(state, NULL, lp->gid - state->ar->low, lp);
	dbe->cause_ospf = 1;
	dbe->cause_bgp = 0;

	ospf_rt_build(state, lp->gid);
	ospf_lsa_refresh(state, dbe, lp);

	dbe->cause_ospf = 0;
}
Esempio n. 10
0
void
phold_event_handler(phold_state * s, tw_bf * bf, phold_message * m, tw_lp * lp)
{
	tw_lpid	 dest;

	if(tw_rand_unif(lp->rng) <= percent_remote)
	{
		bf->c1 = 1;
		dest = tw_rand_integer(lp->rng, 0, ttl_lps - 1);
		// Makes PHOLD non-deterministic across processors! Don't uncomment
		/* dest += offset_lpid; */
		/* if(dest >= ttl_lps) */
		/* 	dest -= ttl_lps; */
	} else
	{
		bf->c1 = 0;
		dest = lp->gid;
	}

	if(dest >= (g_tw_nlp * tw_nnodes()))
		tw_error(TW_LOC, "bad dest");

	tw_event_send(tw_event_new(dest, tw_rand_exponential(lp->rng, mean) + lookahead, lp));
}
Esempio n. 11
0
void
phold_event_handler(phold_state * s, tw_bf * bf, phold_message * m, tw_lp * lp)
{
	tw_lpid	 dest;

	if(tw_rand_unif(lp->rng) <= percent_remote)
	{
		bf->c1 = 1;
		dest = tw_rand_integer(lp->rng, 0, ttl_lps - 1);

		dest += offset_lpid;

		if(dest >= ttl_lps)
			dest -= ttl_lps;
	} else
	{
		bf->c1 = 0;
		dest = lp->gid;
	}

	rn_event_send(
		rn_event_new(dest, tw_rand_exponential(lp->rng, mean), 
				lp, DOWNSTREAM, 0));
}
Esempio n. 12
0
void
epi_xml(epi_state * state, tw_lp * lp)
{
	xmlNodePtr	 agent;
	xmlNodePtr	 move;
	rn_machine	*m = rn_getmachine(lp->id);

	epi_agent	*a;
	epi_ic_stage	*s;

	double		 x;

	int		 ct;
	int		 i;

	char		*sick;

	// create census tract table if it does not exist
	// need 1 more than number of stages to save dead agents
	// dead is not a stage.
	if (!g_epi_ct)
	{
		g_epi_nct = 2200; //rn_getarea(m)->nsubnets;
		g_epi_ct = tw_vector_create(sizeof(unsigned int *), g_epi_nct);

		for (i = 0; i < g_epi_nct; i++)
			g_epi_ct[i] = tw_vector_create(sizeof(unsigned int), g_epi_nstages);
	}

	state->stats = tw_vector_create(sizeof(epi_statistics), 1);
	state->pq = pq_create();

	ct = rn_getsubnet(m)->id - rn_getarea(m)->subnets[0].id;

	// spin through agents 'homed' at this location and allocate + init them
	//
	// NOTE: May not have *any* agents homed at a given location
	for(agent = node->children; agent; agent = agent->next)
	{
		if(0 != strcmp((char *) agent->name, "agent"))
			continue;

		a = tw_vector_create(sizeof(epi_agent), 1);

		a->ct = m->uid; //ct;
		a->num = tw_getlp(atoi(xml_getprop(agent, "num")));
		//a->a_type = atoi(xml_getprop(agent,"type"));

		sick = xml_getprop(agent, "sick");
		if(g_epi_psick > EPSILON)
		{
			if ( tw_rand_unif(lp->rng) < g_epi_psick )
				sick = "1";
		}

		if(0 != strcmp(sick, "0"))
		{
			//a->ts_infected = atof(sick);
			a->stage = EPI_INCUBATING;

			s = &g_epi_stages[a->stage];

			x = ((double) tw_rand_integer(lp->rng, 0, INT_MAX) / (double) INT_MAX);
		
			a->ts_stage_tran = (s->min_duration + 
						(x * (s->max_duration - s->min_duration)));

#if EPI_XML_VERIFY
			printf("\tstage: %d, time %5f \n",
				s->stage_index, a->ts_stage_tran);
#endif

			g_epi_nsick++;
		} else
		{
			a->stage = EPI_SUSCEPTIBLE;
			a->ts_stage_tran = DBL_MAX;
			//a->ts_infected = DBL_MAX;
		}

		g_epi_ct[a->ct][a->stage]++;

		//a->id = g_epi_nagents++;
		g_epi_nagents++;
		a->curr = 0;
		//a->ts_last_tran = 0.0;
		a->ts_next = a->ts_remove = (double) 86400.0;
		//a->n_infected = 0;

		for(move = agent->children; move; move = move->next)
		{
			if(0 != strcmp((char *) move->name, "move"))
				continue;

			a->nloc++;
		}

		if(!a->nloc)
			tw_error(TW_LOC, "Agent has no locations!");

		a->nloc++; // add a location to return home at end of day

		a->loc = tw_vector_create(sizeof(int) * a->nloc, 1);
		a->dur = tw_vector_create(sizeof(tw_stime) * a->nloc, 1);

		int seconds_used = 0; // totals duration to check for 24 hours

		for(i = 0, move = agent->children; move; move = move->next)
		{
			if(0 != strcmp((char *) move->name, "move"))
				continue;

			a->loc[i] = atoi(xml_getprop(move, "loc"));
			a->dur[i] = atoi(xml_getprop(move, "dur")) * 3600;
			seconds_used += a->dur[i];
			i++;
		}

		if (seconds_used > 86400)
			tw_error(TW_LOC, "Agent has duration more than 24 hours\n");

		else if (seconds_used == 86400)
			a->nloc--; // do not need last location
		else {
			a->loc[a->nloc-1] = a->loc[0]; // return home
			a->dur[a->nloc-1] = 86400 - seconds_used;
		}

		a->behavior_flags = 0;
		if (g_epi_worried_well_rate > 0)
		{
			if (tw_rand_unif(lp->rng) < g_epi_worried_well_rate)
				a->behavior_flags = 1;
		} else if (g_epi_work_while_sick_p > EPSILON)
			if (tw_rand_unif(lp->rng) < g_epi_work_while_sick_p)
				a->behavior_flags = 2;

		// Set ts_next and enqueue
		a->ts_remove = a->dur[a->curr];
		if(a->ts_remove < a->ts_stage_tran)
			a->ts_next = a->ts_remove;
		else
			a->ts_next = a->ts_stage_tran;

		pq_enqueue(state->pq, a);
	}
}
Esempio n. 13
0
/**
 * Initializer function for SRW.  Here we set all the necessary
 * initial values we assume for the beginning of the simulation.
 * We also bootstrap the event queue with some random data.
 */
void srw_init(srw_state *s, tw_lp *lp)
{
  int i;
  int num_radios;
  tw_event *e;
  /* global_id is required to make sure all nodes get unique IDs */
  static long global_id = 0;
  /* current_id is just the starting ID for this particular iteration */
  long current_id = global_id;

  /* Initialize the state of this LP (or master) */
  num_radios    = tw_rand_integer(lp->rng, 1, SRW_MAX_GROUP_SIZE);
  s->num_radios = num_radios;
  s->movements  = 0;
  s->comm_fail  = 0;
  s->comm_try   = 0;

  /* Initialize nodes */
  for (i = 0; i < num_radios; i++) {
    (s->nodes[i]).node_id   = global_id++;
    (s->nodes[i]).lng       = 0.0;
    (s->nodes[i]).lat       = 0.0;
    (s->nodes[i]).movements =   0;
    (s->nodes[i]).comm_fail =   0;
    (s->nodes[i]).comm_try  =   0;
  }

  /* Priming events */
  for (i = 0; i < num_radios; i++) {
    tw_stime ts;
    srw_msg_data *msg;

    // GPS event first, as it's the simplest
    ts = tw_rand_unif(lp->rng);
    ts = ts * SRW_GPS_RATE;

    e = tw_event_new(lp->gid, ts, lp);
    msg = tw_event_data(e);
    msg->node_id = current_id + i;
    msg->type = GPS;
    tw_event_send(e);

    // Now schedule some movements
    ts = tw_rand_exponential(lp->rng, SRW_MOVE_MEAN);
    e = tw_event_new(lp->gid, ts, lp);
    msg = tw_event_data(e);
    msg->node_id = current_id + i;
    msg->type = MOVEMENT;
    // We have to figure out a good way to set the initial lat/long
    tw_event_send(e);

    // Now some communications
    ts = tw_rand_exponential(lp->rng, SRW_COMM_MEAN);
    e = tw_event_new(lp->gid, ts, lp);
    msg = tw_event_data(e);
    msg->node_id = current_id + i;
    msg->type = COMMUNICATION;
    // Something should probably go here as well...
    tw_event_send(e);
  }
}
Esempio n. 14
0
void
epi_init_agent_default(void)
{
    tw_memory	*b;

    epi_agent	*a;

    int		 i;
    int		 j;

    int		 id;
    int		 lid;
    int		 rid;

    if(!g_epi_nagents)
        tw_error(TW_LOC, "No agents specified!");

    if(!g_tw_nlp)
        tw_error(TW_LOC, "No locations specified!");

    if(!g_epi_nregions)
        tw_error(TW_LOC, "No regions specified!");

    g_epi_regions = tw_calloc(TW_LOC, "", sizeof(unsigned int *), g_epi_nregions);

    // create reporting tables for each region, for each disease
    for(i = 0; i < g_epi_nregions; i++)
    {
        g_epi_regions[i] = tw_calloc(TW_LOC, "", sizeof(*g_epi_regions[i]),
                                     g_epi_ndiseases);

        for(j = 0; j < g_epi_ndiseases; j++)
            g_epi_regions[i][j] = tw_calloc(TW_LOC, "",
                                            sizeof(*g_epi_regions[i][j]),
                                            g_epi_diseases[j].nstages);
    }

    // allocate the location priority queues for this node
    g_epi_pq = tw_calloc(TW_LOC, "", sizeof(*g_epi_pq), g_tw_nlp);

    for(i = 0; i < g_tw_nlp; i++)
        g_epi_pq[i] = pq_create();

    // round-robin mapping of agents to locations, and locations to regions
    for(i = 0; i < g_epi_nagents; i++)
    {
        lid = i % g_tw_nlp;
        rid = lid % g_epi_nregions;

        b = tw_memory_alloc(g_tw_lp[lid], g_epi_fd);
        a = tw_memory_data(b);

        a->id = id++;
        a->region = rid;

        a->pathogens = NULL;
        a->curr = 0;
        a->nloc = tw_rand_integer(g_tw_lp[lid]->rng, 0, 10);

        // setup "home" location
        a->loc[0] = lid;
        a->dur[0] = tw_rand_exponential(g_tw_lp[lid]->rng, g_epi_mean);
        a->ts_next = a->ts_remove = a->dur[0];
        pq_enqueue(g_epi_pq[a->loc[0]], b);

        printf("A %d nloc %d: (%d, %lf) ",
               a->id, a->nloc, a->loc[0], a->dur[0]);

        for(j = 1; j < a->nloc; j++)
        {
            a->loc[j] = tw_rand_integer(g_tw_lp[lid]->rng, 0,
                                        (g_tw_nlp * tw_nnodes()) - 2);
            a->dur[j] = tw_rand_exponential(g_tw_lp[lid]->rng,
                                            g_epi_mean);

            printf("(%d %lf) ", a->loc[j], a->dur[j]);
        }

        printf("\n");
        ga = a;
    }
}
Esempio n. 15
0
/*Each MPI LP in this model generates a MPI message until a certain message count is reached.
This method 
          (i) keeps generating MPI messages, 
	 (ii) breaks a MPI message down to torus packets 
         (iii) sends those packets to the underlying torus node LP */
void mpi_msg_send(mpi_process * p, 
	          tw_bf * bf, 
		  nodes_message * msg, 
		  tw_lp * lp)
{
    tw_stime ts;
    tw_event *e;
    nodes_message *m;
    tw_lpid final_dst;
    int i;
    bf->c3 = 0;
    bf->c4 = 0;

    if(p->message_counter >= injection_limit)
     {
	bf->c4 = 1;

	return;
     }

    switch(TRAFFIC)
	{
          /* UR traffic picks a random destination from the network */
	  case UNIFORM_RANDOM:
		{
                    bf->c3 = 1;

		    final_dst = tw_rand_integer( lp->rng, N_nodes, 2 * N_nodes - 1);

		    /* if the random final destination generated is the same as current LP ID then it is possible
		       that the next randomly generated destination is also the same.
			Therefore if randomly generated destination is the same as source, we use the following
			calculation to make sure that the source and destinations are different */	
		    if( final_dst == lp->gid )
		      {
                        final_dst = N_nodes + (lp->gid + N_nodes/2) % N_nodes;
		      }
		}
	  break;

       /* The nearest neighbor traffic works in a round-robin fashion. The first message is sent to the first nearest neighbor
	of the node, second message is sent to second nearest neighbor and so on. Thats why we use the message counter number
	to calculate the destination neighbor number (Its between 1 to neighbor-1). In the packet_generate function, we calculate
	the torus coordinates of the destination neighbor. */
       case NEAREST_NEIGHBOR:
         {
           final_dst = p->message_counter% (2*N_dims_sim);
         }
        break;

	/* The diagonal traffic pattern sends message to the mirror torus coordinates of the current torus node LP. The
        torus coordinates are not available at the MPI process LP level thats why we calculate destination for this pattern
	in the packet_generate function. */
	case DIAGONAL:
	  {
	    final_dst = -1;
	  }	
     }
      tw_stime base_time = MEAN_PROCESS;
	
      for( i=0; i < num_packets; i++ ) 
       {
	      // Send the packet out
	     ts = 0.1 + tw_rand_exponential(lp->rng, MEAN_INTERVAL/200); 
             msg->saved_available_time = p->available_time;
	     p->available_time = max( p->available_time, tw_now(lp) );
	     p->available_time += ts;

	     e = tw_event_new( getProcID(lp->gid), p->available_time - tw_now(lp), lp );

	     m = tw_event_data( e );
	     m->type = GENERATE;
             m->packet_ID = packet_offset * ( lp->gid * num_mpi_msgs * num_packets ) + p->message_counter;

             p->message_counter++;
	     m->travel_start_time = tw_now( lp ) + ts;

	     if(TRAFFIC == NEAREST_NEIGHBOR || TRAFFIC == DIAGONAL)
		m->dest_lp = final_dst;
	     else
	       {
	        m->dest_lp = getProcID( final_dst );
	 	}

 	     m->next_stop = -1; 
             tw_event_send( e );
     } 
     ts = 0.1 + tw_rand_exponential( lp->rng, MEAN_INTERVAL);
     e = tw_event_new( lp->gid, ts, lp );
     m = tw_event_data( e );
     m->type = MPI_SEND;
     tw_event_send( e );
}
Esempio n. 16
0
/*
 * epi_init - initialize node LPs state variables
 * 
 * state	- our node LP state space
 * lp		- our node LP
 */
void
epi_init(epi_state * state, tw_lp * lp)
{
	tw_memory	*b;
	tw_memory	*agent = NULL;
	tw_stime	 ts_min_tran = DBL_MAX;

	epi_agent	*a;
	epi_pathogen	*p;
	epi_ic_stage	*s;

	double		 x;

	int		 i;
	int		 j;
	int		 sz;

	// hard-coded, single hospital LP
	if(lp->id == g_tw_nlp-1)
	{
		fprintf(g_epi_hospital_f, "0 0 %u %u %u\n", 
			g_epi_hospital[0][0], g_epi_hospital_ww[0][0], 
			g_epi_hospital_wws[0][0]);

		g_epi_hospital[0][0] = 0;
		g_epi_hospital_ww[0][0] = 0;
		g_epi_hospital_wws[0][0] = 0;

		return;
	}

	// ID of the hospital I go to (or nearest if office/school)
	state->hospital = 0;
	state->stats = tw_calloc(TW_LOC, "", sizeof(epi_statistics), 1);
	state->ncontagious = tw_calloc(TW_LOC, "", sizeof(unsigned int), g_epi_ndiseases);
	state->ts_seir = tw_calloc(TW_LOC, "", sizeof(tw_stime), g_epi_ndiseases);

	for(i = 0; i < g_epi_ndiseases; i++)
		state->ts_seir[i] = DBL_MAX;

	// if no agents initially at this location, then we are done!
	if(0 == (sz = pq_get_size(g_epi_pq[lp->id])))
		return;

	// determine agents initial parameters / configuration
	for(i = 0; i < sz; i++)
	{
		agent = pq_next(g_epi_pq[lp->id], i);
		a = tw_memory_data(agent);

		for(j = 0; j < g_epi_ndiseases; j++)
		{
			if(!a->id && !j)
			{
				// ALLOCATE PATHOGEN AND FILL IT IN
				b = tw_memory_alloc(lp, g_epi_pathogen_fd);
				p = tw_memory_data(b);

				b->next = a->pathogens;
				a->pathogens = b;

				p->index = j;
				p->stage = EPI_SUSCEPTIBLE;
				p->ts_stage_tran = DBL_MAX;

				g_epi_regions[a->region][p->index][p->stage]--;
				p->stage = EPI_EXPOSED;
				g_epi_regions[a->region][p->index][p->stage]++;
				g_epi_exposed_today[j]++;
	
				// determine if agents start out sick.
				if(tw_rand_unif(lp->rng) < g_epi_sick_rate)
				{

				s = &g_epi_diseases[p->index].stages[p->stage];

				x = ((double) tw_rand_integer(lp->rng, 0, INT_MAX) / 
						(double) INT_MAX);

				p->ts_stage_tran = (s->min_duration + 
						(x * (s->max_duration - s->min_duration)));

				if(0.0 >= p->ts_stage_tran)
					tw_error(TW_LOC, "Immediate stage transition?!");

				//state->stats->s_ninfected++;
				state->ncontagious[p->index]++;
				state->ncontagious_tot++;
				//g_epi_hospital[0][0]++;
				g_epi_sick_init_pop[j]++;

				ts_min_tran = min(ts_min_tran, p->ts_stage_tran);

				if(!a->id)
					printf("%ld: agent exposed %d: %s, transition at %lf\n", 
					lp->id, a->id, g_epi_diseases[j].name, 
					p->ts_stage_tran);
				}
			}
		}

		// reflect ts_next change in PQ ordering
		if(ts_min_tran < a->ts_next)
		{
			a->ts_next = ts_min_tran;
			pq_delete_any(g_epi_pq[lp->id], &agent);
			pq_enqueue(g_epi_pq[lp->id], agent);
		}

		// determine if agents are a part of worried well population.
		if(g_epi_ww_rate)
		{
			if(tw_rand_unif(lp->rng) < g_epi_ww_rate)
			{
				a->behavior_flags = 1;
				g_epi_hospital_ww[0][0]++;
				g_epi_ww_init_pop++;
			}
		} else if(g_epi_wws_rate)
		{
			if(tw_rand_unif(lp->rng) < g_epi_wws_rate)
			{
				a->behavior_flags = 2;
				g_epi_hospital_wws[0][0]++;
				g_epi_wws_init_pop++;
			}
		}

		// define agent's at work contact population
		//g_epi_regions[a->region][a->stage]++;
	}

	epi_location_timer(state, lp);
}
/**
 * Burst Buffer send write request to Storage node if it's full
 * Otherwise local write + write request to storage later
 * @Params ns node state
 * 		   m message
 * 		   lp LP
 */
void burst_bufer_send_request(node_state * ns, node_msg * m, tw_lp * lp) {

	assert(m->id_clust_src % num_burst_buffer_nodes == ns->id_clust);

	if (ns->bb_cur_capacity + pvfs_file_sz >= burst_buffer_capacity) { // if full send to storage node
		printf("In Burst Buffer send REQ to storage\n");
		// setup the response message through the forwarder
		forwarder_msg m_fwd;
		msg_set_header(forwarder_magic, FORWARDER_FWD, lp->gid, &m_fwd.h);

		m_fwd.src_node_clust_id = ns->id_clust;
		m_fwd.dest_node_clust_id = (m->id_clust_src % num_burst_buffer_nodes)
				% num_storage_nodes;
		m_fwd.node_event_type = NODE_RECV_req;

		// compute the dest forwarder index, again using a simple modulus
		int dest_fwd_id = ns->id_clust % num_burst_buffer_forwarders;

		// as the relative forwarder IDs are with respect to groups, the group
		// name must be used
		tw_lpid dest_fwd_lpid = codes_mapping_get_lpid_from_relative(
				dest_fwd_id, "bb_FORWARDERS", "forwarder", NULL, 0);
		ns->pvfs_ts_remote_write += pvfs_tp_write_local_mu;
		model_net_event_annotated(net_id_svr, "bb", "req", dest_fwd_lpid,
				pvfs_file_sz, 0.0, sizeof(m_fwd), &m_fwd, 0, NULL, lp);
	} else {								// if enough room for local write
		printf("In Burst Buffer send ACK to Server and REQ to storage later\n");
		forwarder_msg m_fwd;										//send ack
		msg_set_header(forwarder_magic, FORWARDER_FWD, lp->gid, &m_fwd.h);

		m_fwd.src_node_clust_id = ns->id_clust;
		m_fwd.dest_node_clust_id = (m->id_clust_src % num_burst_buffer_nodes)
				% num_svr_nodes;
		m_fwd.node_event_type = NODE_RECV_ack;

		// compute the dest forwarder index, again using a simple modulus
		int dest_fwd_id = ns->id_clust % num_svr_forwarders;

		// as the relative forwarder IDs are with respect to groups, the group
		// name must be used
		tw_lpid dest_fwd_lpid = codes_mapping_get_lpid_from_relative(
				dest_fwd_id, "bb_FORWARDERS", "forwarder", NULL, 0);
		ns->bb_ts_local_write += burst_buffer_local_mu;
		ns->bb_cur_capacity += pvfs_file_sz;
		model_net_event_annotated(net_id_svr, "bb", "ack", dest_fwd_lpid,
				pvfs_file_sz, 0.0, sizeof(m_fwd), &m_fwd, 0, NULL, lp);

		forwarder_msg m_fwd2;
		msg_set_header(forwarder_magic, FORWARDER_FWD, lp->gid, &m_fwd2.h);

		m_fwd2.src_node_clust_id = ns->id_clust;
		m_fwd2.dest_node_clust_id = (m->id_clust_src % num_burst_buffer_nodes)
				% num_storage_nodes;
		m_fwd2.node_event_type = NODE_RECV_req;

		// compute the dest forwarder index, again using a simple modulus
		int dest_fwd_id2 = ns->id_clust % num_burst_buffer_forwarders;

		tw_lpid dest_fwd_lpid2 = codes_mapping_get_lpid_from_relative(
				dest_fwd_id2, "bb_FORWARDERS", "forwarder", NULL, 0);
		ns->pvfs_ts_remote_write += pvfs_tp_write_local_mu;
		tw_stime offset = tw_rand_integer(lp->rng, 0.0, 5.0);
		model_net_event_annotated(net_id_svr, "bb", "req", dest_fwd_lpid2,
				pvfs_file_sz, offset, sizeof(m_fwd2), &m_fwd2, 0, NULL, lp);

	}
}