Пример #1
0
void
phold_init(phold_state * s, tw_lp * lp)
{
	int              i;

	if( stagger )
	  {
	    for (i = 0; i < g_phold_start_events; i++)
	      {
		tw_event_send(
			      tw_event_new(lp->gid,
					   tw_rand_exponential(lp->rng, mean) + lookahead + (tw_stime)(lp->gid % (unsigned int)g_tw_ts_end),
					   lp));
	      }
	  }
	else
	  {
	    for (i = 0; i < g_phold_start_events; i++)
	      {
		tw_event_send(
			      tw_event_new(lp->gid,
					   tw_rand_exponential(lp->rng, mean) + lookahead,
					   lp));
	      }
	  }
}
Пример #2
0
void Airport_EventHandler(Airport_State *SV, tw_bf *CV, Msg_Data *M, 
		      tw_lp *lp) {
  tw_stime ts;
  tw_event *CurEvent;
  Msg_Data *NewM;

  *(int *)CV = (int)0;

  switch(M->event_type) {
    
  case ARRIVAL: 
    // Schedule a landing in the future
    SV->InTheAir++;
    
    if((CV->c1 = (SV->RunwayFree == 1))){
      SV->RunwayFree = 0;
   
      ts = tw_rand_exponential(lp->id, R);
      CurEvent = tw_event_new(lp, ts, lp);
      NewM = (Msg_Data *)tw_event_data(CurEvent);
      NewM->event_type = LAND;
      tw_event_send(CurEvent);
    }
    break;
      
  case LAND: 
    SV->InTheAir--;
    SV->OnTheGround++;
    SV->NumLanded++;
    
    ts = tw_rand_exponential(lp->id, G);
    CurEvent = tw_event_new(lp, ts, lp);
    NewM = (Msg_Data *)tw_event_data(CurEvent);
    NewM->event_type = DEPARTURE;
    tw_event_send(CurEvent);
    
    if ((CV->c1 = (SV->InTheAir > 0))){
      ts = tw_rand_exponential(lp->id, R);
      CurEvent = tw_event_new(lp, ts, lp);
      NewM = (Msg_Data *)tw_event_data(CurEvent);
      NewM->event_type = LAND;
      tw_event_send(CurEvent);
    }
    else
      SV->RunwayFree = 1;
    break;
    
  case DEPARTURE: 
    SV->OnTheGround--;
    
    ts = tw_rand_exponential(lp->id, A);
    CurEvent = tw_event_new(tw_getlp((lp->id + 1) % 3) , ts, lp);
    NewM = (Msg_Data *) tw_event_data(CurEvent);
    NewM->event_type = ARRIVAL;
    tw_event_send(CurEvent);
    break;
  }
}
Пример #3
0
/*Sends a 8-byte credit back to the torus node LP that sent the message */
static void credit_send( nodes_state * s,
                         tw_bf * bf,
                         tw_lp * lp,
                         nodes_message * msg)
{
#if DEBUG
    //printf("\n (%lf) sending credit tmp_dir %d tmp_dim %d %lf ", tw_now(lp), msg->source_direction, msg->source_dim, s->params->credit_delay );
#endif
    bf->c1 = 0;
    tw_event * buf_e;
    nodes_message *m;
    tw_stime ts;
    int src_dir = msg->source_direction;
    int src_dim = msg->source_dim;

    msg->saved_available_time = s->next_credit_available_time[(2 * src_dim) + src_dir][0];
    s->next_credit_available_time[(2 * src_dim) + src_dir][0] = maxd(s->next_credit_available_time[(2 * src_dim) + src_dir][0], tw_now(lp));
    ts =  s->params->credit_delay +
          tw_rand_exponential(lp->rng, s->params->credit_delay/1000);
    s->next_credit_available_time[(2 * src_dim) + src_dir][0] += ts;

    //buf_e = tw_event_new( msg->sender_lp, s->next_credit_available_time[(2 * src_dim) + src_dir][0] - tw_now(lp), lp);
    //m = tw_event_data(buf_e);
    buf_e = model_net_method_event_new(msg->sender_node,
                                       s->next_credit_available_time[(2*src_dim) + src_dir][0] - tw_now(lp),
                                       lp, TORUS, (void**)&m, NULL);
    m->source_direction = msg->source_direction;
    m->source_dim = msg->source_dim;

    m->type = CREDIT;
    tw_event_send( buf_e );
}
Пример #4
0
static void node_collective_fan_out(nodes_state * s,
                                    tw_bf * bf,
                                    nodes_message * msg,
                                    tw_lp * lp)
{
    int i;
    //TODO: be annotation-aware
    int num_lps = codes_mapping_get_lp_count(grp_name, 1, LP_CONFIG_NM,
                  NULL, 1);
    bf->c1 = 0;
    bf->c2 = 0;

    send_remote_event(s, bf, msg, lp);

    if(!s->is_leaf)
    {
        bf->c1 = 1;
        tw_event* e_new;
        nodes_message * msg_new;
        tw_stime xfer_to_nic_time;

        for( i = 0; i < s->num_children; i++ )
        {
            xfer_to_nic_time = g_tw_lookahead + TORUS_FAN_OUT_DELAY + tw_rand_exponential(lp->rng, (double)TORUS_FAN_OUT_DELAY/10);

            if(s->children[i] > 0)
            {
                tw_lpid child_nic_id;

                /* get global LP ID of the child node */
                codes_mapping_get_lp_id(grp_name, LP_CONFIG_NM, NULL, 1,
                                        s->children[i]/num_lps,
                                        (s->children[i] % num_lps), &child_nic_id);
                //e_new = codes_event_new(child_nic_id, xfer_to_nic_time, lp);
                //msg_new = tw_event_data(e_new);
                //memcpy(msg_new, msg, sizeof(nodes_message) + msg->remote_event_size_bytes);
                void* m_data;
                e_new = model_net_method_event_new(child_nic_id,
                                                   xfer_to_nic_time,
                                                   lp, TORUS, (void**)&msg_new, &m_data);
                memcpy(msg_new, msg, sizeof(nodes_message));
                if (msg->remote_event_size_bytes) {
                    memcpy(m_data, model_net_method_get_edata(TORUS, msg),
                           msg->remote_event_size_bytes);
                }


                msg_new->type = T_COLLECTIVE_FAN_OUT;
                msg_new->sender_node = s->node_id;
                tw_event_send(e_new);
            }
        }
    }
    //printf("\n Fan out phase completed %ld ", lp->gid);
    if(max_collective < tw_now(lp) - s->collective_init_time )
    {
        bf->c2 = 1;
        max_collective = tw_now(lp) - s->collective_init_time;
    }
}
Пример #5
0
void wifi_station_arrival(wifi_access_point_state * s, tw_bf * bf, wifi_message * m, tw_lp * lp)
{
  unsigned int rng_calls=0;
  tw_event *e=NULL;
  wifi_message *m_new=NULL;

  // packets coming from access point have much more power and so better snr
  s->stations[m->station].total_packets++;
  s->stations[m->station].station_snr = tw_rand_normal_sd(lp->rng,4.0,8.0, &rng_calls);
  // New Function - to add prop loss, but not ready yet.
  //s->stations[m->station].station_snr = calcRxPower (txPowerDbm, distance, minDistance, lambda, systemLoss);
  s->stations[m->station].station_success_rate =
    WiFi_80211b_DsssDqpskCck11_SuccessRate(s->stations[m->station].station_snr, num_of_bits);
  if( tw_rand_normal_sd(lp->rng,0.5,0.1, &rng_calls) <
      s->stations[m->station].station_success_rate)
    {
      bf->c1 = 1;
      s->stations[m->station].failed_packets++;
    }

  // schedule event back to AP w/ exponential service time
  e = tw_event_new(lp->gid, tw_rand_exponential(lp->rng, 10.0), lp);
  m_new = (wifi_message *) tw_event_data(e);
  m_new->type = WIFI_PACKET_ARRIVAL_AT_ACCESS_POINT;
  m_new->station = m->station;
  tw_event_send(e);
}
Пример #6
0
void
torus_init(nodes_state * s, tw_lp * lp)
{
  tw_event *e;
  tw_stime ts;
  nodes_message *m;

  /*
   * Set up the initial state for each LP (node)
   * according to the number of dimensions
   */
  torus_setup(s, lp);

  /*
   * Start a GENERATE event on each LP
   */
  //ts = tw_rand_exponential(lp->rng, MEAN_INTERVAL);
  ts = tw_rand_exponential(lp->rng, 10);
  int i;
  for(i=0;i<N_nodes;i++)
    {
      e = tw_event_new(lp->gid, ts, lp);
      m = tw_event_data(e);
      m->type = GENERATE;
      
      m->next_stop = i;
      
      m->dest_lp = lp->gid;
      tw_event_send(e);
    }
}
Пример #7
0
/*Sends a 8-byte credit back to the torus node LP that sent the message */
void 
credit_send( nodes_state * s, 
	    tw_bf * bf, 
	    tw_lp * lp, 
	    nodes_message * msg)
{
    //if(lp->gid == TRACK_LP)
    //	printf("\n (%lf) sending credit tmp_dir %d tmp_dim %d %lf ", tw_now(lp), msg->source_direction, msg->source_dim, credit_delay );
    
    tw_event * buf_e;
    nodes_message *m;
    tw_stime ts;
    
    int src_dir = msg->source_direction;
    int src_dim = msg->source_dim;

    msg->saved_available_time = s->next_credit_available_time[(2 * src_dim) + src_dir][0];
    s->next_credit_available_time[(2 * src_dim) + src_dir][0] = max(s->next_credit_available_time[(2 * src_dim) + src_dir][0], tw_now(lp));
    ts =  credit_delay + tw_rand_exponential(lp->rng, credit_delay/1000);
    s->next_credit_available_time[(2 * src_dim) + src_dir][0] += ts;

    buf_e = tw_event_new( msg->sender_lp, s->next_credit_available_time[(2 * src_dim) + src_dir][0] - tw_now(lp), lp);

    m = tw_event_data(buf_e);
    m->source_direction = msg->source_direction;
    m->source_dim = msg->source_dim;

    m->type = CREDIT;
    tw_event_send( buf_e );
}
Пример #8
0
void packet_buffer_process( nodes_state * s, tw_bf * bf, nodes_message * msg, tw_lp * lp )
{
   msg->wait_loc = -1;
   s->buffer[ msg->source_direction + ( msg->source_dim * 2 ) ][  0 ]-=1;
  
  tw_event * e_h;
  nodes_message * m;
  tw_stime ts;
  bf->c3 = 0;
  int loc=s->wait_count, j=0;
  for(j = 0; j < loc; j++)
   {
    if( s->waiting_list[j].dim == msg->source_dim && s->waiting_list[j].dir == msg->source_direction)
     {
        bf->c3=1;
	ts = tw_rand_exponential(lp->rng, MEAN_INTERVAL/100);
        e_h = tw_event_new( lp->gid, ts, lp );
        m = tw_event_data( e_h );
     	memcpy(m, s->waiting_list[j].packet, sizeof(nodes_message));
        
//        For reverse computation, also copy data to the msg 
   	memcpy(msg, s->waiting_list[j].packet, sizeof(nodes_message));
	msg->wait_loc = j;
	msg->type = CREDIT;
	m->type = SEND;
        tw_event_send(e_h);
        waiting_packet_free(s, j);
        break;
    }
  }
}
Пример #9
0
void
mem_init(mem_state * s, tw_lp * lp)
{
    tw_event	*e;
    tw_memory	*b;

    int              i;

    for (i = 0; i < g_mem_start_events; i++)
    {
        e = tw_event_new(lp->gid, tw_rand_exponential(lp->rng, mean), lp);

        for(i = 0; i < nbufs; i++)
        {
            b = mem_alloc(lp);
            mem_fill(b);
            tw_event_memory_set(e, b, my_fd);

            s->stats.s_mem_alloc++;
        }

        tw_event_send(e);
        s->stats.s_sent++;
    }
}
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);
    }
}
Пример #11
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);
}
Пример #12
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));
}
Пример #13
0
/**
 * Forward event handler for SRW.  Supported operations are currently:
 * - GPS
 * - MOVEMENT
 * - COMMUNICATION
 */
void srw_event(srw_state *s, tw_bf *bf, srw_msg_data *m, tw_lp *lp)
{
  tw_stime ts;
  tw_event *e;
  srw_msg_data *msg;

  switch(m->type) {
  case GPS:
    
    // Schedule next event
    e = tw_event_new(lp->gid, SRW_GPS_RATE, lp);
    msg = tw_event_data(e);
    msg->node_id = m->node_id;
    msg->type = GPS;
    tw_event_send(e);
    break;

  case MOVEMENT:
    s->movements++;
    (s->nodes[m->node_id]).movements++;

    // Schedule next event
    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 = m->node_id;
    msg->type = MOVEMENT;
    tw_event_send(e);
    break;

  case COMMUNICATION:
    s->comm_try++;
    (s->nodes[m->node_id]).comm_try++;

    // Schedule next event
    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 = m->node_id;
    msg->type = COMMUNICATION;
    tw_event_send(e);
    break;
  }
}
Пример #14
0
void
phold_init(phold_state * s, tw_lp * lp)
{
	int              i;

	for (i = 0; i < g_phold_start_events; i++)
	{
		rn_event_send(
			rn_event_new(lp->gid, 
				     tw_rand_exponential(lp->rng, mean), 
				     lp, DOWNSTREAM, 0));
	}
}
/* Trigger getting next event at LP */
static void codes_issue_next_event(tw_lp* lp)
{
   tw_event *e;
   nw_message* msg;

   tw_stime ts;

   ts = g_tw_lookahead + 0.1 + tw_rand_exponential(lp->rng, noise);
   e = tw_event_new( lp->gid, ts, lp );
   msg = tw_event_data(e);

   msg->msg_type = MPI_OP_GET_NEXT;
   tw_event_send(e);
}
Пример #16
0
void  Intersection_StartUp(Intersection_State *SV, tw_lp * lp) {
	//printf("begin init\n");
	int i;
	tw_event *CurEvent;
	tw_stime ts;
	Msg_Data *NewM;

	SV->total_cars_arrived = 0;
	SV->total_cars_finished = 0;
	SV->num_in_west_left = 0;
	SV->num_in_west_straight = 0;
	SV->num_in_west_right = 0;
	SV->num_in_north_left = 0;
	SV->num_in_north_straight = 0;
	SV->num_in_north_right = 0;
	SV->num_in_south_left = 0;
	SV->num_in_south_straight = 0;
	SV->num_in_south_right = 0;
	SV->num_in_east_left = 0;
	SV->num_in_east_straight = 0;
	SV->num_in_east_right = 0;
	SV->num_out_west_left = 0;
	SV->num_out_west_straight = 0;
	SV->num_out_west_right = 0;
	SV->num_out_north_left = 0;
	SV->num_out_north_straight = 0;
	SV->num_out_north_right = 0;
	SV->num_out_south_left = 0;
	SV->num_out_south_straight = 0;
	SV->num_out_south_right = 0;
	SV->num_out_east_left = 0;
	SV->num_out_east_straight = 0;
	SV->num_out_east_right = 0;

	for(i = 0; i < g_traffic_start_events; i++)
	{
		ts = tw_rand_exponential(lp->rng, MEAN_SERVICE );
		CurEvent = tw_event_new(lp->gid, ts, lp);
		NewM = (Msg_Data *)tw_event_data(CurEvent);
		NewM->event_type = ARIVAL;
		NewM->car.x_to_go = rand() % 200 - 99;
		NewM->car.y_to_go = rand() % 200 - 99;
		NewM->car.current_lane = rand() % 12;
		NewM->car.sent_back = 0;
		NewM->car.in_out = IN;
		tw_event_send(CurEvent);
	}
}
Пример #17
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));
}
Пример #18
0
/* initialize MPI process LP */
void 
mpi_init( mpi_process * p, 
	 tw_lp * lp)
{
    tw_event *e;
    tw_stime ts;
    nodes_message *m;
    p->message_counter = 0;

    /* Start a MPI send event on each MPI LP */
    ts =  tw_rand_exponential(lp->rng, MEAN_INTERVAL);
    e = tw_event_new( lp->gid, ts, lp );
    m = tw_event_data( e );
    m->type = MPI_SEND;

    p->available_time = 0.0;
    tw_event_send( e );
}
Пример #19
0
Файл: hec.c Проект: nliu8/VSsim
void
init(hec_state * s, tw_lp * lp)
{
  int i;
  tw_event *e;
  hec_message *m;

  /*
    N_routers = 4
    N_nodes = 15
    0 1 2 3: routers (group master)
    4 5 6 7     -> 0
    8 9 10 11   -> 1
    12 13 14 15 -> 2
    16 17 18    -> 3
   */
  s->group_master = (lp->gid-N_ROUTERS)/GROUP_SIZE;

  s->landings = 0;
  s->planes_in_the_sky = 0;
  s->planes_on_the_ground = planes_per_hec;
  s->waiting_time = 0.0;
  s->furthest_flight_landing = 0.0;

  //printf("Init, my id is %d\n",lp->gid);

  if (lp->gid >= N_ROUTERS)
    {
      //printf("In init loop, my id is %d\n",lp->gid);
      for(i = 0; i < planes_per_hec; i++)
	{
	  e = tw_event_new(lp->gid, tw_rand_exponential(lp->rng, MEAN_DEPARTURE), lp);
	  m = tw_event_data(e);
	  m->msg_core.type = HEC_MSG_SEND;
	
	  m->msg_core.src_pid = lp->gid;
	  m->msg_core.src_lid = lp->gid - N_ROUTERS;
	  m->msg_core.dst_lid = (i + N_ROUTERS) % N_NODES;
	  m->msg_core.dst_pid = ( i + N_ROUTERS + lp->gid ) % N_NODES + N_ROUTERS;
	
	  tw_event_send(e);
      }
    }
}
Пример #20
0
void  Airport_StartUp(Airport_State *SV, tw_lp * lp) {
  int i;
  tw_event *CurEvent;
  tw_stime ts;
  Msg_Data *NewM;

  SV->OnTheGround = 0;
  SV->InTheAir = 0 ;
  SV->RunwayFree = 1;
  SV->NumLanded = 0;

  for(i = 0; i < 5; i++) {
    ts = tw_rand_exponential(lp->id, A);
    CurEvent = tw_event_new(lp, ts, lp);
    NewM = (Msg_Data *) tw_event_data(CurEvent);
    NewM->event_type = ARRIVAL;
    tw_event_send(CurEvent);
  }
}
static void issue_event(
    svr_state * ns,
    tw_lp * lp)
{
    tw_event *e;
    svr_msg *m;
    tw_stime kickoff_time;

    /* each server sends a dummy event to itself that will kick off the real
     * simulation
     */

    /* skew each kickoff event slightly to help avoid event ties later on */
    kickoff_time = g_tw_lookahead + arrival_time + tw_rand_exponential(lp->rng, (double)arrival_time/100);

    e = tw_event_new(lp->gid, kickoff_time, lp);
    m = tw_event_data(e);
    m->svr_event_type = KICKOFF;
    tw_event_send(e);
}
Пример #22
0
void
tmr_init(tmr_state * s, tw_lp * lp)
{
	int              i;

	if(!lp->rng)
	{
		tw_error(TW_LOC, "No RNG!");
		lp->rng = tw_calloc(TW_LOC, "LP RNG", sizeof(tw_rng_stream), 1);
		tw_rand_initial_seed(lp->pe->rng, lp->rng, lp->gid);
	}

	for (i = 0; i < g_tmr_start_events; i++)
	{
		tw_event_send(
			tw_event_new(lp->gid, 
				     tw_rand_exponential(lp->rng, mean), 
				     lp));
	}

	if((s->timer = tw_timer_init(lp, 100.0)) == NULL)
		tw_error(TW_LOC, "scheduled timer past end time \n");
}
Пример #23
0
void
mr_client_init(mr_client_state * s, tw_lp * lp)
{
  int i,j,flag;
  tw_event *e;
  mr_client_message *m;

  /*
    only one  Job Tracker: PID 0
    the rest are Physical nodes
   */

  char filename[32];

  s->job_tracker_id = 0;

  s->pkt_send_counter = 0;
  s->pkt_recv_counter = 0;

  printf("mr_client Success\n");

  // all clients will submit a job, trigger submit event, 
  // real remote submit is in VS_MR_CLIENT_SUBMIT_JOB
  if (lp->gid >= 1)
    {
      e = tw_event_new(lp->gid, tw_rand_exponential(lp->rng, 10), lp);
      m = tw_event_data(e);
      m->msg_core.type = VS_MR_CLIENT_SUBMIT_JOB;

      // initialize messages 
      prep_src( &m->msg_core);
      prep_dst( &m->msg_core);

      tw_event_send(e);
    }
}
Пример #24
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));
}
Пример #25
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));
}
Пример #26
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);
  }
}
Пример #27
0
void Intersection_EventHandler(Intersection_State *SV, tw_bf *CV, Msg_Data *M, tw_lp *lp)
{
	tw_stime ts=0.0;
	int new_event_direction=0;
	tw_event *CurEvent=NULL;
	Msg_Data *NewM=NULL;
	enum abs_directions temp_direction=0;
	*(int *)CV = (int)0;

	switch(M->event_type) {

	case ARIVAL:

		if(M->car.x_to_go == 0 && M->car.y_to_go == 0){
			SV->total_cars_finished++;
			break;
		}

		// Schedule a departure in the future
		SV->total_cars_arrived++;

		switch(M->car.current_lane){

		case WEST_LEFT:
			SV->num_in_east_left++;
			M->car.current_lane = EAST_LEFT;
			break;
		case WEST_STRAIGHT:
			SV->num_in_east_straight++;
			M->car.current_lane = EAST_STRAIGHT;
			break;
		case WEST_RIGHT:
			SV->num_in_east_right++;
			M->car.current_lane = EAST_RIGHT;
			break;
		case EAST_LEFT:
			SV->num_in_west_left++;
			M->car.current_lane = WEST_LEFT;
			break;
		case EAST_STRAIGHT:
			SV->num_in_west_straight++;
			M->car.current_lane = WEST_STRAIGHT;
			break;
		case EAST_RIGHT:
			SV->num_in_west_right++;
			M->car.current_lane = WEST_RIGHT;
			break;
		case NORTH_LEFT:
			SV->num_in_south_left++;
			M->car.current_lane = SOUTH_LEFT;
			break;
		case NORTH_STRAIGHT:
			SV->num_in_south_straight++;
			M->car.current_lane = SOUTH_STRAIGHT;
			break;
		case NORTH_RIGHT:
			SV->num_in_south_right++;
			M->car.current_lane = SOUTH_RIGHT;
			break;
		case SOUTH_LEFT:
			SV->num_in_north_left++;
			M->car.current_lane = NORTH_LEFT;
			break;
		case SOUTH_STRAIGHT:
			SV->num_in_north_straight++;
			M->car.current_lane = NORTH_STRAIGHT;
			break;
		case SOUTH_RIGHT:
			SV->num_in_north_right++;
			M->car.current_lane = NORTH_RIGHT;
			break;
		}

		M->car.in_out = IN;

		ts = tw_rand_exponential(lp->rng, MEAN_SERVICE);
		CurEvent = tw_event_new(lp->gid, ts, lp);
		NewM = (Msg_Data *)tw_event_data(CurEvent);
		NewM->car.x_to_go = M->car.x_to_go;
		NewM->car.y_to_go = M->car.y_to_go;
		NewM->car.current_lane = M->car.current_lane;
		NewM->car.sent_back = M->car.sent_back;
		NewM->car.arrived_from = M->car.arrived_from;
		NewM->car.in_out = M->car.in_out;
		NewM->event_type = DIRECTION_SELECT;
		//printf("send ari ");
		tw_event_send(CurEvent);


		break;

	case DEPARTURE:

		switch(M->car.current_lane){
		case WEST_LEFT:
			SV->num_out_west_left--;
			new_event_direction = 0;
			break;
		case WEST_STRAIGHT:
			SV->num_out_west_straight--;
			new_event_direction = 0;
			break;
		case WEST_RIGHT:
			SV->num_out_west_right--;
			new_event_direction = 0;
			break;
		case EAST_LEFT:
			SV->num_out_east_left--;
			new_event_direction = 1;
			break;
		case EAST_STRAIGHT:
			SV->num_out_east_straight--;
			new_event_direction = 1;
			break;
		case EAST_RIGHT:
			SV->num_out_east_right--;
			new_event_direction = 1;
			break;
		case NORTH_LEFT:
			SV->num_out_north_left--;
			new_event_direction = 3;
			break;
		case NORTH_STRAIGHT:
			SV->num_out_north_straight--;
			new_event_direction = 3;
			break;
		case NORTH_RIGHT:
			SV->num_out_north_right--;
			new_event_direction = 3;
			break;
		case SOUTH_LEFT:
			SV->num_out_south_left--;
			new_event_direction = 2;
			break;
		case SOUTH_STRAIGHT:
			SV->num_out_south_straight--;
			new_event_direction = 2;
			break;
		case SOUTH_RIGHT:
			SV->num_out_south_right--;
			new_event_direction = 2;
			break;
		}

		lp->gid = Cell_ComputeMove(lp->gid, new_event_direction);
		ts = tw_rand_exponential(lp->rng, MEAN_SERVICE);
		CurEvent = tw_event_new(lp->gid, ts, lp);
		NewM = (Msg_Data *) tw_event_data(CurEvent);
		NewM->car.x_to_go = M->car.x_to_go;
		NewM->car.y_to_go = M->car.y_to_go;
		NewM->car.current_lane = M->car.current_lane;
		NewM->car.sent_back = M->car.sent_back;
		NewM->car.arrived_from = M->car.arrived_from;
		NewM->car.in_out = M->car.in_out;
		NewM->event_type = ARIVAL;
		//printf("send dep ");
		tw_event_send(CurEvent);
		break;

	case DIRECTION_SELECT:


		temp_direction = M->car.current_lane;

		switch(M->car.current_lane){
		case EAST_LEFT:
			SV->num_in_east_left--;
			if(M->car.y_to_go < 0 && SV->num_out_south_straight < MAX_CARS_ON_ROAD){
				M->car.current_lane = SOUTH_STRAIGHT;
				SV->num_out_south_straight ++;
				M->car.sent_back = 0;
				M->car.y_to_go++;
			}
			else if(M->car.x_to_go < 0 && SV->num_out_south_right < MAX_CARS_ON_ROAD){
				M->car.current_lane = SOUTH_RIGHT;
				SV->num_out_south_right ++;
				M->car.sent_back = 0;
				M->car.x_to_go++;
			}
			else if(M->car.x_to_go > 0 && SV->num_out_south_left < MAX_CARS_ON_ROAD){
				M->car.current_lane = SOUTH_LEFT;
				SV->num_out_south_left ++;
				M->car.sent_back = 0;
				M->car.x_to_go--;
			}
			else{
				if(M->car.arrived_from == SOUTH_LEFT){
					M->car.current_lane = EAST_RIGHT;
					SV->num_out_east_right++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == EAST_STRAIGHT){
					M->car.current_lane = EAST_STRAIGHT;
					SV->num_out_east_straight++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == NORTH_RIGHT){
					M->car.current_lane = EAST_LEFT;
					SV->num_out_east_left++;
					M->car.sent_back++;
				}
			}
			break;
		case EAST_STRAIGHT:
			SV->num_in_east_straight--;
			if(M->car.x_to_go < 0 && SV->num_out_west_straight < MAX_CARS_ON_ROAD){
				M->car.current_lane = WEST_STRAIGHT;
				SV->num_out_west_straight ++;
				M->car.sent_back = 0;
				M->car.x_to_go++;
			}
			else if(M->car.y_to_go < 0 && SV->num_out_west_left < MAX_CARS_ON_ROAD){
				M->car.current_lane = WEST_LEFT;
				SV->num_out_west_left ++;
				M->car.sent_back = 0;
				M->car.y_to_go++;
			}
			else if(M->car.y_to_go > 0 && SV->num_out_west_right < MAX_CARS_ON_ROAD){
				M->car.current_lane = WEST_RIGHT;
				SV->num_out_west_right ++;
				M->car.sent_back = 0;
				M->car.y_to_go--;
			}
			else{
				if(M->car.arrived_from == NORTH_RIGHT){
					M->car.current_lane = EAST_LEFT;
					SV->num_out_east_left++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == EAST_STRAIGHT){
					M->car.current_lane = EAST_STRAIGHT;
					SV->num_out_east_straight++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == SOUTH_LEFT){
					M->car.current_lane = EAST_RIGHT;
					SV->num_out_east_right++;
					M->car.sent_back++;
				}
			}

			break;
		case EAST_RIGHT:
			SV->num_in_east_right--;
			if(M->car.y_to_go > 0 && SV->num_out_north_straight < MAX_CARS_ON_ROAD){
				M->car.current_lane = NORTH_STRAIGHT;
				SV->num_out_north_straight ++;
				M->car.sent_back = 0;
				M->car.y_to_go--;
			}
			else if(M->car.x_to_go > 0 && SV->num_out_north_right < MAX_CARS_ON_ROAD){
				M->car.current_lane = NORTH_RIGHT;
				SV->num_out_north_right ++;
				M->car.sent_back = 0;
				M->car.x_to_go --;
			}
			else if(M->car.x_to_go < 0 && SV->num_out_north_left < MAX_CARS_ON_ROAD){
				M->car.current_lane = NORTH_LEFT;
				SV->num_out_north_left ++;
				M->car.sent_back = 0;
				M->car.x_to_go++;
			}
			else{
				if(M->car.arrived_from == SOUTH_LEFT){
					M->car.current_lane = EAST_RIGHT;
					SV->num_out_east_right++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == EAST_STRAIGHT){
					M->car.current_lane = EAST_STRAIGHT;
					SV->num_out_east_straight++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == NORTH_RIGHT){
					M->car.current_lane = EAST_LEFT;
					SV->num_out_east_left++;
					M->car.sent_back++;
				}
			}
			break;
		case WEST_LEFT:
			SV->num_in_west_left--;
			if(M->car.y_to_go > 0 && SV->num_out_north_straight < MAX_CARS_ON_ROAD){
				M->car.current_lane = NORTH_STRAIGHT;
				SV->num_out_north_straight ++;
				M->car.sent_back = 0;
				M->car.y_to_go--;
			}
			else if(M->car.x_to_go > 0 && SV->num_out_north_right < MAX_CARS_ON_ROAD){
				M->car.current_lane = NORTH_RIGHT;
				SV->num_out_north_right ++;
				M->car.sent_back = 0;
				M->car.x_to_go--;
			}
			else if(M->car.x_to_go < 0 && SV->num_out_north_left < MAX_CARS_ON_ROAD){
				M->car.current_lane = NORTH_LEFT;
				SV->num_out_north_left ++;
				M->car.sent_back = 0;
				M->car.x_to_go++;
			}
			else{
				if(M->car.arrived_from == SOUTH_RIGHT){
					M->car.current_lane = WEST_LEFT;
					SV->num_out_west_left++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == WEST_STRAIGHT){
					M->car.current_lane = WEST_STRAIGHT;
					SV->num_out_west_straight++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == NORTH_LEFT){
					M->car.current_lane = WEST_RIGHT;
					SV->num_out_west_right++;
					M->car.sent_back++;
				}
			}
			break;
		case WEST_STRAIGHT:
			SV->num_in_west_straight--;
			if(M->car.x_to_go > 0 && SV->num_out_east_straight < MAX_CARS_ON_ROAD){
				M->car.current_lane = EAST_STRAIGHT;
				SV->num_out_east_straight ++;
				M->car.sent_back = 0;
				M->car.x_to_go--;
			}
			else if(M->car.y_to_go > 0 && SV->num_out_east_left < MAX_CARS_ON_ROAD){
				M->car.current_lane = EAST_LEFT;
				SV->num_out_east_left ++;
				M->car.sent_back = 0;
				M->car.y_to_go --;
			}
			else if(M->car.y_to_go < 0 && SV->num_out_east_right < MAX_CARS_ON_ROAD){
				M->car.current_lane = EAST_RIGHT;
				SV->num_out_east_right ++;
				M->car.sent_back = 0;
				M->car.y_to_go++;
			}
			else{
				if(M->car.arrived_from == SOUTH_RIGHT){
					M->car.current_lane = WEST_LEFT;
					SV->num_out_west_left++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == WEST_STRAIGHT){
					M->car.current_lane = WEST_STRAIGHT;
					SV->num_out_west_straight++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == NORTH_LEFT){
					M->car.current_lane = WEST_RIGHT;
					SV->num_out_west_right++;
					M->car.sent_back++;
				}
			}
			break;
		case WEST_RIGHT:
			SV->num_in_west_right--;
			if(M->car.y_to_go < 0 && SV->num_out_south_straight < MAX_CARS_ON_ROAD){
				M->car.current_lane = SOUTH_STRAIGHT;
				SV->num_out_south_straight ++;
				M->car.sent_back = 0;
				M->car.y_to_go++;
			}
			else if(M->car.x_to_go > 0 && SV->num_out_south_left < MAX_CARS_ON_ROAD){
				M->car.current_lane = SOUTH_LEFT;
				SV->num_out_south_left ++;
				M->car.sent_back = 0;
				M->car.x_to_go--;
			}
			else if(M->car.x_to_go < 0 && SV->num_out_south_right < MAX_CARS_ON_ROAD){
				M->car.current_lane = SOUTH_RIGHT;
				SV->num_out_south_right ++;
				M->car.sent_back = 0;
				M->car.x_to_go++;
			}
			else{
				if(M->car.arrived_from == SOUTH_RIGHT){
					M->car.current_lane = WEST_LEFT;
					SV->num_out_west_left++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == WEST_STRAIGHT){
					M->car.current_lane = WEST_STRAIGHT;
					SV->num_out_west_straight++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == NORTH_LEFT){
					M->car.current_lane = WEST_RIGHT;
					SV->num_out_west_right++;
					M->car.sent_back++;
				}
			}
			break;
		case NORTH_LEFT:
			SV->num_in_north_left--;
			if(M->car.x_to_go > 0 && SV->num_out_east_straight < MAX_CARS_ON_ROAD){
				M->car.current_lane = EAST_STRAIGHT;
				SV->num_out_east_straight ++;
				M->car.sent_back = 0;
				M->car.x_to_go --;
			}
			else if(M->car.y_to_go > 0 && SV->num_out_east_left < MAX_CARS_ON_ROAD){
				M->car.current_lane = EAST_LEFT;
				SV->num_out_east_left ++;
				M->car.sent_back = 0;
				M->car.y_to_go--;
			}
			else if(M->car.y_to_go < 0 && SV->num_out_east_right < MAX_CARS_ON_ROAD){
				M->car.current_lane = EAST_RIGHT;
				SV->num_out_east_right ++;
				M->car.sent_back = 0;
				M->car.y_to_go++;
			}
			else{
				if(M->car.arrived_from == WEST_RIGHT){
					M->car.current_lane = NORTH_LEFT;
					SV->num_out_north_left++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == NORTH_STRAIGHT){
					M->car.current_lane = NORTH_STRAIGHT;
					SV->num_out_north_straight++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == EAST_LEFT){
					M->car.current_lane = NORTH_RIGHT;
					SV->num_out_north_right++;
					M->car.sent_back++;
				}
			}

			break;
		case NORTH_STRAIGHT:
			SV->num_in_north_straight--;
			if(M->car.y_to_go < 0 && SV->num_out_south_straight < MAX_CARS_ON_ROAD){
				M->car.current_lane = SOUTH_STRAIGHT;
				SV->num_out_south_straight ++;
				M->car.sent_back = 0;
				M->car.y_to_go++;
			}
			else if(M->car.x_to_go > 0 && SV->num_out_south_left < MAX_CARS_ON_ROAD){
				M->car.current_lane = SOUTH_LEFT;
				SV->num_out_south_left ++;
				M->car.sent_back = 0;
				M->car.x_to_go--;
			}
			else if(M->car.x_to_go < 0 && SV->num_out_south_right < MAX_CARS_ON_ROAD){
				M->car.current_lane = SOUTH_RIGHT;
				SV->num_out_south_right ++;
				M->car.sent_back = 0;
				M->car.x_to_go++;
			}
			else{
				if(M->car.arrived_from == WEST_RIGHT){
					M->car.current_lane = NORTH_LEFT;
					SV->num_out_north_left++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == NORTH_STRAIGHT){
					M->car.current_lane = NORTH_STRAIGHT;
					SV->num_out_north_straight++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == EAST_LEFT){
					M->car.current_lane = NORTH_RIGHT;
					SV->num_out_north_right++;
					M->car.sent_back++;
				}
			}
			break;
		case NORTH_RIGHT:
			SV->num_in_north_right--;
			if(M->car.x_to_go < 0 && SV->num_out_west_straight < MAX_CARS_ON_ROAD){
				M->car.current_lane = WEST_STRAIGHT;
				SV->num_out_west_straight ++;
				M->car.sent_back = 0;
				M->car.x_to_go++;
			}
			else if(M->car.y_to_go < 0 && SV->num_out_west_left < MAX_CARS_ON_ROAD){
				M->car.current_lane = WEST_LEFT;
				SV->num_out_west_left ++;
				M->car.sent_back = 0;
				M->car.y_to_go++;
			}
			else if(M->car.y_to_go > 0 && SV->num_out_west_right < MAX_CARS_ON_ROAD){
				M->car.current_lane = WEST_RIGHT;
				SV->num_out_west_right ++;
				M->car.sent_back = 0;
				M->car.y_to_go--;
			}
			else{
				if(M->car.arrived_from == WEST_RIGHT){
					M->car.current_lane = NORTH_LEFT;
					SV->num_out_north_left++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == NORTH_STRAIGHT){
					M->car.current_lane = NORTH_STRAIGHT;
					SV->num_out_north_straight++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == EAST_LEFT){
					M->car.current_lane = NORTH_RIGHT;
					SV->num_out_north_right++;
					M->car.sent_back++;
				}
			}
			break;
		case SOUTH_LEFT:
			SV->num_in_south_left--;
			if(M->car.x_to_go < 0 && SV->num_out_west_straight < MAX_CARS_ON_ROAD){
				M->car.current_lane = WEST_STRAIGHT;
				SV->num_out_west_straight ++;
				M->car.sent_back = 0;
				M->car.x_to_go++;
			}
			else if(M->car.y_to_go < 0 && SV->num_out_west_left < MAX_CARS_ON_ROAD){
				M->car.current_lane = WEST_LEFT;
				SV->num_out_west_left ++;
				M->car.sent_back = 0;
				M->car.y_to_go++;
			}
			else if(M->car.y_to_go > 0 && SV->num_out_west_right < MAX_CARS_ON_ROAD){
				M->car.current_lane = WEST_RIGHT;
				SV->num_out_west_right ++;
				M->car.sent_back = 0;
				M->car.y_to_go--;
			}
			else{
				if(M->car.arrived_from == WEST_LEFT){
					M->car.current_lane = SOUTH_RIGHT;
					SV->num_out_south_right++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == SOUTH_STRAIGHT){
					M->car.current_lane = SOUTH_STRAIGHT;
					SV->num_out_south_straight++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == EAST_RIGHT){
					M->car.current_lane = SOUTH_LEFT;
					SV->num_out_south_left++;
					M->car.sent_back++;
				}
			}

			break;
		case SOUTH_STRAIGHT:
			SV->num_in_south_straight--;
			if(M->car.y_to_go > 0 && SV->num_out_north_straight < MAX_CARS_ON_ROAD){
				M->car.current_lane = NORTH_STRAIGHT;
				SV->num_out_north_straight ++;
				M->car.sent_back = 0;
				M->car.y_to_go--;
			}
			else if(M->car.x_to_go < 0 && SV->num_out_north_left < MAX_CARS_ON_ROAD){
				M->car.current_lane = NORTH_LEFT;
				SV->num_out_north_left ++;
				M->car.sent_back = 0;
				M->car.x_to_go++;
			}
			else if(M->car.x_to_go > 0 && SV->num_out_north_right < MAX_CARS_ON_ROAD){
				M->car.current_lane = NORTH_RIGHT;
				SV->num_out_north_right ++;
				M->car.sent_back = 0;
				M->car.x_to_go --;
			}
			else{
				if(M->car.arrived_from == EAST_RIGHT){
					M->car.current_lane = SOUTH_LEFT;
					SV->num_out_south_left++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == SOUTH_STRAIGHT){
					M->car.current_lane = SOUTH_STRAIGHT;
					SV->num_out_south_straight++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == WEST_LEFT){
					M->car.current_lane = SOUTH_RIGHT;
					SV->num_out_south_right++;
					M->car.sent_back++;
				}
			}
			break;
		case SOUTH_RIGHT:
			SV->num_in_south_right--;
			if(M->car.x_to_go > 0 && SV->num_out_east_straight < MAX_CARS_ON_ROAD){
				M->car.current_lane = EAST_STRAIGHT;
				SV->num_out_east_straight ++;
				M->car.sent_back = 0;
				M->car.x_to_go--;
			}
			else if(M->car.y_to_go > 0 && SV->num_out_east_left < MAX_CARS_ON_ROAD){
				M->car.current_lane = EAST_LEFT;
				SV->num_out_east_left ++;
				M->car.sent_back = 0;
				M->car.y_to_go--;
			}
			else if(M->car.y_to_go < 0 && SV->num_out_east_right < MAX_CARS_ON_ROAD){
				M->car.current_lane = EAST_RIGHT;
				SV->num_out_east_right ++;
				M->car.sent_back = 0;
				M->car.y_to_go++;
			}
			else{
				if(M->car.arrived_from == EAST_RIGHT){
					M->car.current_lane = SOUTH_LEFT;
					SV->num_out_south_left++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == SOUTH_STRAIGHT){
					M->car.current_lane = SOUTH_STRAIGHT;
					SV->num_out_south_straight++;
					M->car.sent_back++;
				}
				else if(M->car.arrived_from == WEST_LEFT){
					M->car.current_lane = SOUTH_RIGHT;
					SV->num_out_south_right++;
					M->car.sent_back++;
				}
			}
			break;
		}

		M->car.arrived_from = temp_direction;
		M->car.in_out = OUT;
		ts = tw_rand_exponential(lp->rng, MEAN_SERVICE);
		CurEvent = tw_event_new(lp->gid, ts, lp);
		NewM = (Msg_Data *)tw_event_data(CurEvent);
		NewM->car.x_to_go = M->car.x_to_go;
		NewM->car.y_to_go = M->car.y_to_go;
		NewM->car.current_lane = M->car.current_lane;
		NewM->car.sent_back = M->car.sent_back;
		NewM->car.arrived_from = M->car.arrived_from;
		NewM->car.in_out = M->car.in_out;
		NewM->event_type = DEPARTURE;
		//printf("send dir ");
		tw_event_send(CurEvent);
		break;
	}
}
Пример #28
0
/*Processes the packet after it arrives from the neighboring torus node */
void packet_arrive( nodes_state * s, 
		    tw_bf * bf, 
		    nodes_message * msg, 
		    tw_lp * lp )
{
  bf->c2 = 0;
  int i;
  tw_event *e;
  tw_stime ts;
  nodes_message *m;

  /* send an ack back to the sender node */
  credit_send( s, bf, lp, msg); 
  
  msg->my_N_hop++;
  ts = 0.1 + tw_rand_exponential(lp->rng, MEAN_INTERVAL/200);

  /* if the flit has arrived on the final destination */
  if( lp->gid == msg->dest_lp )
    {   
	/* check if this is the last flit of the packet */
        if( msg->chunk_id == num_chunks - 1 )    
        {
		bf->c2 = 1;
		int index = floor(N_COLLECT_POINTS*(tw_now(lp)/g_tw_ts_end));
    		for(i = 0; i < 2 * N_dims; i++)
	          N_queue_depth[index]+=s->buffer[i][0];

	        e = tw_event_new(lp->gid + N_nodes, ts, lp);
		m = tw_event_data(e);
	        m->type = MPI_RECV;
	        m->travel_start_time = msg->travel_start_time;
		m->my_N_hop = msg->my_N_hop;
		m->packet_ID = msg->packet_ID;
		tw_event_send(e);
        }
    }
  else
    {
      /* forward the flit to another node */
      e = tw_event_new(lp->gid, ts , lp);
      m = tw_event_data( e );
      m->type = SEND;
      
      // Carry on the message info
      for( i = 0; i < N_dims; i++ )
	m->dest[i] = msg->dest[i];

      m->dest_lp = msg->dest_lp;
      
      m->source_dim = msg->source_dim;
      m->source_direction = msg->source_direction;
      
      m->packet_ID = msg->packet_ID;	  
      m->travel_start_time = msg->travel_start_time;

      m->my_N_hop = msg->my_N_hop;
      m->sender_lp = msg->sender_lp;
      m->chunk_id = msg->chunk_id;

      m->next_stop = -1;
      tw_event_send(e);
   }
}
Пример #29
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;
    }
}
Пример #30
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 );
}