Skip to content

Commit 9092c5e

Browse files
Merge pull request #124 from ROOT-Sim/feat/committed_output
Implement committed output
2 parents d6568f6 + d90bbaf commit 9092c5e

14 files changed

Lines changed: 647 additions & 27 deletions

File tree

‎src/CMakeLists.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ set(rscore_srcs
55
arch/mem.c
66
arch/thread.c
77
core/core.c
8-
init.c
98
core/sync.c
9+
core/output.c
1010
datatypes/msg_queue.c
1111
distributed/control_msg.c
1212
gvt/fossil.c
1313
gvt/gvt.c
1414
gvt/termination.c
15+
init.c
1516
log/file.c
1617
log/log.c
1718
log/stats.c

‎src/ROOT-Sim.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ typedef void (*ProcessEvent_t)(lp_id_t me, simtime_t now, unsigned event_type, c
6666
*/
6767
typedef bool (*CanEnd_t)(lp_id_t me, const void *snapshot);
6868

69+
/**
70+
* @brief Perform output operations.
71+
* @param me The logical process ID of the LP performing the output
72+
* @param output_type Numerical output type
73+
* @param output_content The output content
74+
* @param output_size The size (in bytes) of the output content
75+
*
76+
* This function is called by the simulation kernel whenever an event is committed, during the handling of which
77+
* an output operation had been scheduled by the simulation model using the ScheduleOutput() function.
78+
* This function will receive the same data that was passed to the ScheduleOutput() function.
79+
*
80+
* @warning The memory pointed by output_content is not a copy, and is owned by the simulation kernel. It will be
81+
* freed by the simulation kernel after the function returns.
82+
* @warning The function shall not perform any memory-managed allocation (e.g. using rs_malloc).
83+
*/
84+
typedef void (*OutputCallback_t)(lp_id_t me, unsigned output_type, const void *output_content, unsigned output_size);
85+
6986
/// @brief Internal event types used by the simulation kernel.
7087
///
7188
/// These event types are automatically scheduled to the model according to the following logic:
@@ -91,6 +108,19 @@ extern void ScheduleNewEvent(lp_id_t receiver, simtime_t timestamp, unsigned eve
91108

92109
extern void SetState(void *new_state);
93110

111+
/**
112+
* @brief API to schedule a new output event to execute once the event being currently executed is committed
113+
*
114+
* This function makes a copy of the data in @p output_content , which stays owned by the caller.
115+
*
116+
* @param output_type Numerical output type to be passed to the output handling function
117+
* @param output_content The output content
118+
* @param output_size The size (in bytes) of the output content
119+
*
120+
* @warning This function shall not be called during the handling of LP_FINI events, as it will produce no output.
121+
*/
122+
extern void ScheduleOutput(unsigned output_type, const void *output_content, unsigned output_size);
123+
94124
/**
95125
* @brief Allocates rollbackable memory
96126
*
@@ -206,6 +236,8 @@ struct simulation_configuration {
206236
ProcessEvent_t dispatcher;
207237
/// Function pointer to the termination detection function
208238
CanEnd_t committed;
239+
/// Function pointer to the output handling function
240+
OutputCallback_t output_callback;
209241
};
210242

211243
extern int RootsimInit(const struct simulation_configuration *conf);

‎src/core/output.c‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @file core/output.c
3+
*
4+
* @brief Committed output management functions
5+
*
6+
* This module implements the facilities for committed output
7+
*
8+
* SPDX-FileCopyrightText: 2008-2023 HPDCS Group <rootsim@googlegroups.com>
9+
* SPDX-License-Identifier: GPL-3.0-only
10+
*/
11+
#include <ROOT-Sim.h>
12+
#include <core/core.h>
13+
#include <core/output.h>
14+
#include <lp/common.h>
15+
#include <lp/msg.h>
16+
#include <lp/process.h>
17+
18+
void ScheduleOutput(unsigned output_type, const void *output_content, unsigned output_size)
19+
{
20+
if(unlikely(silent_processing))
21+
return;
22+
23+
if(unlikely(global_config.synchronization == SERIAL)) {
24+
global_config.output_callback(current_msg->dest, output_type, output_content, output_size);
25+
return;
26+
}
27+
28+
char *content = mm_alloc(output_size);
29+
memcpy(content, output_content, output_size);
30+
31+
struct output_data data = {.type = output_type, .content = content, .size = output_size};
32+
33+
output_array_t *outputs = current_msg->outputs;
34+
if(!outputs) {
35+
outputs = mm_alloc(sizeof(output_array_t));
36+
array_init(*outputs);
37+
current_msg->outputs = outputs;
38+
}
39+
40+
array_push(*outputs, data);
41+
}
42+
43+
void execute_outputs(struct lp_msg *msg)
44+
{
45+
output_array_t *outputs = msg->outputs;
46+
if(!outputs)
47+
return;
48+
49+
for(array_count_t i = 0; i < array_count(*outputs); ++i) {
50+
struct output_data data = array_get_at(*outputs, i);
51+
global_config.output_callback(msg->dest, data.type, data.content, data.size);
52+
mm_free(data.content);
53+
}
54+
55+
/* Use array_clear() rather than array_fini(): the message buffer remains alive in the
56+
* system and will eventually reach free_msg_outputs(), which performs the final
57+
* array_fini() + mm_free(). When called right before msg_allocator_free() (e.g. from
58+
* fossil_lp_collect()), the array_clear() is redundant but harmless. */
59+
array_clear(*outputs);
60+
}
61+
62+
void free_msg_outputs(output_array_t *output_array)
63+
{
64+
if(!output_array)
65+
return;
66+
67+
for(array_count_t i = 0; i < array_count(*output_array); ++i) {
68+
struct output_data data = array_get_at(*output_array, i);
69+
mm_free(data.content);
70+
}
71+
72+
array_fini(*output_array);
73+
mm_free(output_array);
74+
}
75+
76+
void committed_output_on_rollback(struct lp_msg *msg)
77+
{
78+
output_array_t *outputs = msg->outputs;
79+
if(!outputs)
80+
return;
81+
82+
for(array_count_t i = 0; i < array_count(*outputs); ++i) {
83+
struct output_data data = array_get_at(*outputs, i);
84+
mm_free(data.content);
85+
}
86+
87+
array_clear(*outputs);
88+
}

‎src/core/output.h‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @file core/output.h
3+
*
4+
* @brief Committed output management functions
5+
*
6+
* Committed output management functions
7+
*
8+
* SPDX-FileCopyrightText: 2008-2023 HPDCS Group <rootsim@googlegroups.com>
9+
* SPDX-License-Identifier: GPL-3.0-only
10+
*/
11+
#pragma once
12+
13+
#include <datatypes/array.h>
14+
15+
struct output_data {
16+
unsigned type;
17+
void *content;
18+
unsigned size;
19+
};
20+
21+
typedef array_declare(struct output_data) output_array_t;
22+
23+
/**
24+
* @brief Free the outputs stored for later from a message
25+
* @param output_array the output_data from the message
26+
*/
27+
void free_msg_outputs(output_array_t *output_array);
28+
29+
struct lp_msg;
30+
31+
/**
32+
* @brief Invoke the output callback on all outputs stored in the message.
33+
* @param msg the message to execute outputs from
34+
*/
35+
void execute_outputs(struct lp_msg *msg);
36+
37+
/**
38+
* @brief Handle rolling-back of a message
39+
* @param msg the rolled-back
40+
*
41+
* This function is called when a message is rolled back. It frees the output data
42+
* stored in the message, as they are no longer valid.
43+
* The output array is not freed, as it can still be used in the future.
44+
*/
45+
void committed_output_on_rollback(struct lp_msg *msg);

‎src/gvt/fossil.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <gvt/fossil.h>
1111

1212
#include <mm/msg_allocator.h>
13+
#include <core/output.h>
1314

1415
_Thread_local unsigned fossil_epoch_current;
1516
/// The value of the last GVT, kept here for easier fossil collection operations
@@ -50,12 +51,14 @@ void fossil_lp_collect(struct lp_ctx *lp)
5051

5152
past_i = model_allocator_fossil_lp_collect(&lp->mm_state, past_i + 1);
5253

53-
for(array_count_t k = past_i; k;) {
54-
struct pes_entry e = array_get_at(proc_ctx->pes, --k);
54+
for(array_count_t k = 0; k < past_i; ++k) {
55+
struct pes_entry e = array_get_at(proc_ctx->pes, k);
56+
5557
if(pes_entry_is_sent_local(e))
5658
continue;
5759

5860
struct lp_msg *m = pes_entry_msg(e);
61+
execute_outputs(m);
5962
msg_allocator_free(m);
6063
}
6164
array_truncate_first(proc_ctx->pes, past_i);

‎src/lp/common.c‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
/**
2-
* @file lp/common.c
3-
*
4-
* @brief Common LP and message functionalities
5-
*
6-
* SPDX-FileCopyrightText: 2008-2025 HPCS Group <rootsim@googlegroups.com>
7-
* SPDX-License-Identifier: GPL-3.0-only
8-
*/
2+
* @file lp/common.c
3+
*
4+
* @brief Common LP and message functionalities
5+
*
6+
* SPDX-FileCopyrightText: 2008-2025 HPCS Group <rootsim@googlegroups.com>
7+
* SPDX-License-Identifier: GPL-3.0-only
8+
*/
99
#include <lp/common.h>
1010

1111
#include <serial/serial.h>
1212

13-
#ifndef NDEBUG
14-
_Thread_local const struct lp_msg *current_msg;
15-
#endif
13+
_Thread_local struct lp_msg *current_msg;
1614

17-
void ScheduleNewEvent(const lp_id_t receiver, const simtime_t timestamp, const unsigned event_type,
18-
const void *payload, const unsigned payload_size)
15+
void ScheduleNewEvent(const lp_id_t receiver, const simtime_t timestamp, const unsigned event_type, const void *payload,
16+
const unsigned payload_size)
1917
{
2018
#ifndef NDEBUG
2119
if(unlikely(event_type >= LP_INIT)) {

‎src/lp/common.h‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515
#include <log/stats.h>
1616
#include <mm/msg_allocator.h>
1717

18-
#ifndef NDEBUG
1918
/// The currently processed message
20-
/** This is not necessary for normal operation, but it's useful in debug */
21-
extern __thread const struct lp_msg *current_msg;
22-
#endif
19+
extern _Thread_local struct lp_msg *current_msg;
2320

2421
/**
2522
* @brief Process a message for a given LP (Logical Process)
@@ -30,16 +27,12 @@ extern __thread const struct lp_msg *current_msg;
3027
* @param lp A pointer to the LP associated with the message.
3128
* @param msg A pointer to the message to be processed.
3229
*/
33-
static inline void common_msg_process(const struct lp_ctx *lp, const struct lp_msg *msg)
30+
static inline void common_msg_process(const struct lp_ctx *lp, struct lp_msg *msg)
3431
{
3532
timer_uint t = timer_hr_new();
36-
#ifndef NDEBUG
3733
current_msg = msg;
38-
#endif
3934
global_config.dispatcher(msg->dest, msg->dest_t, msg->m_type, msg->pl, msg->pl_size, lp->state_pointer);
40-
#ifndef NDEBUG
4135
current_msg = NULL;
42-
#endif
4336
stats_take(STATS_MSG_PROCESSED_TIME, timer_hr_value(t));
4437
stats_take(STATS_MSG_PROCESSED, 1);
4538
}

‎src/lp/msg.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#pragma once
1212

1313
#include <core/core.h>
14+
#include <core/output.h>
1415

1516
#include <limits.h>
1617
#include <stdatomic.h>
@@ -59,6 +60,8 @@
5960
struct lp_msg {
6061
/// The next element in the message list (used in the message queue)
6162
struct lp_msg *next;
63+
/// Data for committed output — kept in the preamble so it is not transmitted over MPI
64+
output_array_t *outputs;
6265
/// The id of the recipient LP
6366
lp_id_t dest;
6467
/// The intended destination logical time of this message

‎src/lp/process.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
#include <lp/lp.h>
2222
#include <mm/checkpoint/checkpoint.h>
2323
#include <mm/msg_allocator.h>
24-
#include <serial/serial.h>
2524

2625
/// The flag used in ScheduleNewEvent() to keep track of silent execution
27-
static _Thread_local bool silent_processing = false;
26+
_Thread_local bool silent_processing = false;
2827

2928
/**
3029
* @brief Schedule a new event. Parallel (Time Warp) version.
@@ -96,6 +95,8 @@ void process_lp_fini(struct lp_ctx *lp)
9695
if(pes_entry_is_sent_local(e))
9796
continue;
9897

98+
execute_outputs(pes_entry_msg(e));
99+
99100
if(pes_entry_is_sent_remote(e) ||
100101
!(atomic_load_explicit(&pes_entry_msg_received(e)->flags, memory_order_relaxed) & MSG_FLAG_ANTI))
101102
msg_allocator_free(pes_entry_msg(e));
@@ -166,6 +167,7 @@ static inline void send_anti_messages(struct process_ctx *msg_processing, const
166167

167168
struct lp_msg *msg = pes_entry_msg_received(e);
168169
const uint64_t f = atomic_fetch_add_explicit(&msg->flags, -MSG_FLAG_PROCESSED, memory_order_relaxed);
170+
committed_output_on_rollback(msg);
169171
if(!(f & MSG_FLAG_ANTI))
170172
msg_queue_insert_self(msg);
171173

‎src/lp/process.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ struct process_ctx {
9797

9898
struct lp_ctx; // forward declaration
9999

100+
extern _Thread_local bool silent_processing;
101+
100102
extern void process_lp_init(struct lp_ctx *lp);
101103
extern void process_lp_fini(struct lp_ctx *lp);
102104

0 commit comments

Comments
 (0)