The ROme OpTimistic Simulator  3.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
application.c
Go to the documentation of this file.
1 
10 
11 #include <test.h>
12 
13 #include <inttypes.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 
17 struct ap_option model_options[] = {{0}};
18 
19 #define do_random() (lcg_random(state->rng_state))
20 
21 void ProcessEvent(lp_id_t me, simtime_t now, unsigned event_type, const void *event_content, unsigned event_size, void *st)
22 {
23  lp_state *state = st;
24  if (state && state->events >= COMPLETE_EVENTS) {
25  if (event_type == LP_FINI) {
26  test_printf("%" PRIu32 "\n", state->total_checksum);
27  while(state->head)
28  state->head = deallocate_buffer(state->head, 0);
29  free(state);
30  }
31  return;
32  }
33 
34  if (!state && event_type != LP_INIT && event_type != MODEL_INIT &&
35  event_type != MODEL_FINI) {
36  printf("[ERR] Requested to process an weird event\n");
37  abort();
38  }
39  switch (event_type) {
40  case MODEL_INIT:
41  crc_table_init();
42  break;
43 
44  case LP_INIT:
45  state = malloc(sizeof(lp_state));
46  if (state == NULL) {
47  exit(-1);
48  }
49  memset(state, 0, sizeof(lp_state));
50 
51  lcg_init(state->rng_state, ((test_rng_state)me + 1) *
52  4390023366657240769ULL);
53  SetState(state);
54 
55  unsigned buffers_to_allocate = do_random() * MAX_BUFFERS;
56  for (unsigned i = 0; i < buffers_to_allocate; ++i) {
57  unsigned c = do_random() * MAX_BUFFER_SIZE / sizeof(uint64_t);
58  state->head = allocate_buffer(state, NULL, c);
59  state->buffer_count++;
60  }
61 
62  ScheduleNewEvent(me, 20 * do_random(), LOOP, NULL, 0);
63  break;
64 
65  case LOOP:
66  state->events++;
67  ScheduleNewEvent(me, now + do_random() * 10, LOOP, NULL, 0);
68  lp_id_t dest = do_random() * n_lps;
69  if(do_random() < DOUBLING_PROBABILITY && dest != me)
70  ScheduleNewEvent(dest, now + do_random() * 10, LOOP, NULL, 0);
71 
72  if (state->buffer_count) {
73  state->total_checksum = read_buffer(state->head, do_random() * state->buffer_count, state->total_checksum);
74  }
75 
76  if (state->buffer_count < MAX_BUFFERS && do_random() < ALLOC_PROBABILITY) {
77  unsigned c = do_random() * MAX_BUFFER_SIZE / sizeof(uint64_t);
78  state->head = allocate_buffer(state, NULL, c);
79  state->buffer_count++;
80  }
81 
82  if (state->buffer_count && do_random() < DEALLOC_PROBABILITY) {
83  state->head = deallocate_buffer(state->head, do_random() * state->buffer_count);
84  state->buffer_count--;
85  }
86 
87  if (state->buffer_count && do_random() < SEND_PROBABILITY) {
88  unsigned i = do_random() * state->buffer_count;
89  buffer *to_send = get_buffer(state->head, i);
90 
91  dest = do_random() * n_lps;
92  ScheduleNewEvent(dest, now + do_random() * 10, RECEIVE, to_send->data, to_send->count * sizeof(uint64_t));
93 
94  state->head = deallocate_buffer(state->head, i);
95  state->buffer_count--;
96  }
97  break;
98 
99  case RECEIVE:
100  if(state->buffer_count >= MAX_BUFFERS)
101  break;
102  state->head = allocate_buffer(state, event_content, event_size / sizeof(uint64_t));
103  state->buffer_count++;
104  break;
105 
106  case MODEL_FINI:
107  break;
108 
109  default:
110  printf("[ERR] Requested to process an unknown event\n");
111  abort();
112  break;
113  }
114 }
115 
116 bool CanEnd(lp_id_t me, const void *snapshot)
117 {
118  (void)me;
119  const lp_state *state = snapshot;
120  return state->events >= COMPLETE_EVENTS;
121 }
simtime_t
double simtime_t
The type used to represent logical time in the simulation.
Definition: core.h:62
ProcessEvent
void ProcessEvent(lp_id_t me, simtime_t now, unsigned event_type, const void *event_content, unsigned event_size, void *st)
The total number of threads running in the node.
Definition: application.c:21
n_lps
lp_id_t n_lps
The total number of LPs in the simulation.
Definition: core.c:13
ap_option
A single parsable command line option.
Definition: arg_parse.h:14
test.h
Test framework header.
lcg_init
#define lcg_init(rng_state, initseq)
Initializes the random number generator.
Definition: test_rng.h:30
_lp_state_type
Definition: application.h:34
test_printf
int test_printf(const char *restrict fmt,...)
Registers a formatted string to compare against the expected output.
Definition: test.c:169
_buffer
Definition: application.h:28
lp_id_t
uint64_t lp_id_t
Used to uniquely identify LPs in the simulation.
Definition: core.h:75
test_rng_state
__uint128_t test_rng_state
The type of this pseudo random number generator state.
Definition: test_rng.h:19
application.h
Header of the model used to verify the runtime correctness.