-
-
Notifications
You must be signed in to change notification settings - Fork 475
Expand file tree
/
Copy pathenv_vectorizer.cpp
More file actions
305 lines (253 loc) · 9.07 KB
/
Copy pathenv_vectorizer.cpp
File metadata and controls
305 lines (253 loc) · 9.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "env_vectorizer.hpp"
#if defined(__linux__)
#include <pthread.h>
#elif defined(_WIN32)
#define NOMINMAX // Prevent Windows.h from defining min/max macros
#include <windows.h>
#elif defined(__APPLE__)
#include <mach/thread_policy.h>
#include <mach/thread_act.h>
#include <pthread.h>
#endif
namespace ale::vector {
EnvVectorizer::EnvVectorizer(
const std::vector<fs::path>& rom_paths,
int batch_size,
int num_threads,
int thread_affinity_offset,
AutoresetMode autoreset_mode,
int img_height,
int img_width,
int stack_num,
bool grayscale,
int frame_skip,
bool maxpool,
int noop_max,
bool use_fire_reset,
bool episodic_life,
bool life_loss_info,
bool reward_clipping,
int max_episode_steps,
float repeat_action_probability,
bool full_action_space
) : num_envs_(static_cast<int>(rom_paths.size())),
batch_size_(batch_size > 0 ? batch_size : num_envs_),
img_height_(img_height),
img_width_(img_width),
stack_num_(stack_num),
grayscale_(grayscale),
autoreset_mode_(autoreset_mode),
last_recv_env_ids_(batch_size_ > 0 ? batch_size_ : num_envs_)
{
// Create environments
envs_.reserve(num_envs_);
action_sets_.reserve(num_envs_);
for (int i = 0; i < num_envs_; ++i) {
envs_.push_back(std::make_unique<PreprocessedEnv>(
i, rom_paths[i], img_height, img_width, frame_skip, maxpool,
grayscale, stack_num, noop_max, use_fire_reset, episodic_life,
life_loss_info, reward_clipping, max_episode_steps,
repeat_action_probability, full_action_space, -1
));
action_sets_.push_back(envs_.back()->action_set());
}
stacked_obs_size_ = envs_[0]->stacked_obs_size();
// Create action queue (capacity = 2x num_envs for safety)
action_queue_ = std::make_unique<ActionQueue>(num_envs_ * 2);
// Create result staging
bool same_step = (autoreset_mode_ == AutoresetMode::SameStep);
staging_ = std::make_unique<ResultStaging>(batch_size_, num_envs_, stacked_obs_size_, same_step);
// Determine thread count
int hw_threads = static_cast<int>(std::thread::hardware_concurrency());
if (num_threads <= 0) {
num_threads_ = std::min(batch_size_, hw_threads);
} else {
num_threads_ = std::min(num_threads, hw_threads);
}
// Start worker threads
workers_.reserve(num_threads_);
for (int i = 0; i < num_threads_; ++i) {
workers_.emplace_back([this, i] { worker_loop(i); });
}
// Set thread affinity if requested
if (thread_affinity_offset >= 0) {
set_thread_affinity(thread_affinity_offset);
}
}
EnvVectorizer::~EnvVectorizer() {
stop_.store(true);
// Wake workers blocked on the result-staging slot semaphore (unordered mode).
// Without this, destruction deadlocks if any worker is waiting for a free
// slot because release_batch() is only called from recv().
if (staging_) {
staging_->request_stop();
}
// Send dummy actions to wake up workers blocked in the action queue
std::vector<Action> wake_actions(workers_.size());
for (auto& a : wake_actions) {
a.env_id = 0;
a.force_reset = false;
}
action_queue_->enqueue_bulk(wake_actions);
// Join all workers
for (auto& worker : workers_) {
if (worker.joinable()) {
worker.join();
}
}
}
BatchResult EnvVectorizer::reset(const std::vector<int>& env_ids, const std::vector<int>& seeds) {
if (env_ids.size() != seeds.size()) {
throw std::invalid_argument("env_ids and seeds must have same size");
}
first_batch_ = false;
// Set seeds and prepare actions
std::vector<Action> actions;
actions.reserve(env_ids.size());
for (std::size_t i = 0; i < env_ids.size(); ++i) {
int env_id = env_ids[i];
envs_[env_id]->set_seed(seeds[i]);
Action action;
action.env_id = env_id;
action.action_id = 0;
action.paddle_strength = 1.0f;
action.force_reset = true;
actions.push_back(action);
}
// Enqueue reset actions
action_queue_->enqueue_bulk(actions);
// Wait for results
return recv();
}
void EnvVectorizer::send(const std::vector<Action>& actions) {
if (first_batch_) {
throw std::runtime_error(
"send() called before reset(); last_recv_env_ids_ is uninitialized"
);
}
if (actions.size() != static_cast<std::size_t>(batch_size_)) {
throw std::invalid_argument(
"Expected " + std::to_string(batch_size_) + " actions, got " + std::to_string(actions.size())
);
}
// Map actions to correct environments using last_recv_env_ids
std::vector<Action> mapped_actions;
mapped_actions.reserve(actions.size());
for (std::size_t i = 0; i < actions.size(); ++i) {
Action mapped = actions[i];
int actual_env_id = last_recv_env_ids_[i];
mapped.env_id = actual_env_id;
mapped.force_reset = false;
// Set action on environment
envs_[actual_env_id]->set_action(mapped.action_id, mapped.paddle_strength);
mapped_actions.push_back(mapped);
}
// Enqueue actions
action_queue_->enqueue_bulk(mapped_actions);
}
BatchResult EnvVectorizer::recv() {
// Wait for batch to complete
staging_->wait_for_batch();
// Check for errors
check_error();
// Release batch and get results
auto result = staging_->release_batch();
// Remember env_ids for next send()
std::memcpy(last_recv_env_ids_.data(), result.env_ids_data(), batch_size_ * sizeof(int));
return result;
}
void EnvVectorizer::worker_loop(int thread_id) {
(void)thread_id; // For potential future use (logging, etc.)
while (!stop_.load()) {
try {
Action action = action_queue_->dequeue();
if (stop_.load()) {
break;
}
execute_env(action);
} catch (...) {
set_error(std::current_exception());
}
}
}
void EnvVectorizer::execute_env(const Action& action) {
int env_id = action.env_id;
auto& env = *envs_[env_id];
if (autoreset_mode_ == AutoresetMode::NextStep) {
// NextStep mode: reset happens before step if episode was over
if (action.force_reset || env.is_episode_over()) {
env.reset();
} else {
env.step();
}
// Stage result
staging_->stage_result(env_id, [&](OutputSlot& slot) {
env.write_to(slot);
});
} else { // SameStep mode
if (action.force_reset) {
env.reset();
staging_->stage_result(env_id, [&](OutputSlot& slot) {
env.write_to(slot);
});
} else {
env.step();
staging_->stage_result(env_id, [&](OutputSlot& slot) {
if (env.is_episode_over()) {
// Write final observation before reset
env.write_obs_to(slot.final_obs);
// Capture pre-reset metadata
env.write_to(slot);
int pre_reward = *slot.reward;
bool pre_terminated = *slot.terminated;
bool pre_truncated = *slot.truncated;
// Reset and write new observation
env.reset();
env.write_to(slot);
// Restore pre-reset reward/terminated/truncated
*slot.reward = pre_reward;
*slot.terminated = pre_terminated;
*slot.truncated = pre_truncated;
} else {
env.write_to(slot);
}
});
}
}
}
void EnvVectorizer::set_thread_affinity(int thread_affinity_offset) {
int processor_count = static_cast<int>(std::thread::hardware_concurrency());
for (std::size_t i = 0; i < workers_.size(); ++i) {
int core_id = (thread_affinity_offset + static_cast<int>(i)) % processor_count;
#if defined(__linux__)
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
pthread_setaffinity_np(workers_[i].native_handle(), sizeof(cpu_set_t), &cpuset);
#elif defined(_WIN32)
DWORD_PTR mask = (static_cast<DWORD_PTR>(1) << core_id);
SetThreadAffinityMask(workers_[i].native_handle(), mask);
#elif defined(__APPLE__)
thread_affinity_policy_data_t policy = { static_cast<integer_t>(core_id) };
thread_port_t mach_thread = pthread_mach_thread_np(workers_[i].native_handle());
thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY,
(thread_policy_t)&policy, THREAD_AFFINITY_POLICY_COUNT);
#endif
}
}
void EnvVectorizer::set_error(std::exception_ptr e) {
std::lock_guard<std::mutex> lock(error_mutex_);
if (!has_error_.load()) {
error_ = e;
has_error_.store(true);
}
}
void EnvVectorizer::check_error() {
if (has_error_.load()) {
std::lock_guard<std::mutex> lock(error_mutex_);
if (error_) {
std::rethrow_exception(error_);
}
}
}
} // namespace ale::vector