-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathAgentFunctionDescription.cpp
More file actions
598 lines (583 loc) · 29.4 KB
/
AgentFunctionDescription.cpp
File metadata and controls
598 lines (583 loc) · 29.4 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#include <nvrtc.h>
#include <cuda.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <regex>
#include "flamegpu/model/AgentFunctionDescription.h"
#include "flamegpu/detail/cxxname.hpp"
namespace flamegpu {
CAgentFunctionDescription::CAgentFunctionDescription(std::shared_ptr<AgentFunctionData> data)
: function(std::move(data)) { }
CAgentFunctionDescription::CAgentFunctionDescription(std::shared_ptr<const AgentFunctionData> data)
: function(std::const_pointer_cast<AgentFunctionData>(data)) { }
bool CAgentFunctionDescription::operator==(const CAgentFunctionDescription& rhs) const {
return *this->function == *rhs.function; // Compare content is functionally the same
}
bool CAgentFunctionDescription::operator!=(const CAgentFunctionDescription& rhs) const {
return !(*this == rhs);
}
/**
* Const Accessors
*/
std::string CAgentFunctionDescription::getName() const {
return function->name;
}
std::string CAgentFunctionDescription::getInitialState() const {
return function->initial_state;
}
std::string CAgentFunctionDescription::getEndState() const {
return function->end_state;
}
MessageBruteForce::CDescription CAgentFunctionDescription::getMessageInput() const {
if (auto m = function->message_input.lock())
return MessageBruteForce::CDescription(m);
THROW exception::OutOfBoundsException("Message input has not been set, "
"in AgentFunctionDescription::getMessageInput().");
}
MessageBruteForce::CDescription CAgentFunctionDescription::getMessageOutput() const {
if (auto m = function->message_output.lock())
return MessageBruteForce::CDescription(m);
THROW exception::OutOfBoundsException("Message output has not been set, "
"in AgentFunctionDescription::getMessageOutput().");
}
bool CAgentFunctionDescription::getMessageOutputOptional() const {
return this->function->message_output_optional;
}
CAgentDescription CAgentFunctionDescription::getAgentOutput() const {
if (auto a = function->agent_output.lock())
return CAgentDescription(a);
THROW exception::OutOfBoundsException("Agent output has not been set, "
"in AgentFunctionDescription::getAgentOutput().");
}
std::string CAgentFunctionDescription::getAgentOutputState() const {
if (auto a = function->agent_output.lock())
return function->agent_output_state;
THROW exception::OutOfBoundsException("Agent output has not been set, "
"in AgentFunctionDescription::getAgentOutputState().");
}
bool CAgentFunctionDescription::getAllowAgentDeath() const {
return function->has_agent_death;
}
bool CAgentFunctionDescription::hasMessageInput() const {
return function->message_input.lock() != nullptr;
}
bool CAgentFunctionDescription::hasMessageOutput() const {
return function->message_output.lock() != nullptr;
}
bool CAgentFunctionDescription::hasAgentOutput() const {
return function->agent_output.lock() != nullptr;
}
bool CAgentFunctionDescription::hasFunctionCondition() const {
return function->condition != nullptr;
}
AgentFunctionWrapper* CAgentFunctionDescription::getFunctionPtr() const {
return function->func;
}
AgentFunctionConditionWrapper* CAgentFunctionDescription::getConditionPtr() const {
return function->condition;
}
bool CAgentFunctionDescription::isRTC() const {
return !function->rtc_source.empty();
}
/**
* Constructors
*/
AgentFunctionDescription::AgentFunctionDescription(std::shared_ptr<AgentFunctionData> data)
: CAgentFunctionDescription(std::move(data)) { }
/**
* Accessors
*/
void AgentFunctionDescription::setInitialState(const std::string &init_state) {
if (auto p = function->parent.lock()) {
if (p->states.find(init_state) != p->states.end()) {
// Check if this agent function is already in a layer
auto mdl = function->model.lock();
if (!mdl) {
THROW exception::ExpiredWeakPtr();
}
for (const auto &l : mdl->layers) {
for (const auto &f : l->agent_functions) {
// Agent fn is in layer
if (f->name == this->function->name) {
// search all functions in that layer
for (const auto &f2 : l->agent_functions) {
if (const auto &a2 = f2->parent.lock()) {
if (const auto &a1 = this->function->parent.lock()) {
// Same agent
if (a2->name == a1->name) {
// Skip ourself
if (f2->name == this->function->name)
continue;
if (f2->initial_state == init_state ||
f2->end_state == init_state) {
THROW exception::InvalidAgentFunc("Agent functions's '%s' and '%s', within the same layer "
"cannot share any input or output states, this is not permitted, "
"in AgentFunctionDescription::setInitialState().",
f2->name.c_str(), this->function->name.c_str());
}
}
}
}
}
}
}
}
// Checks passed, make change
this->function->initial_state = init_state;
} else {
THROW exception::InvalidStateName("Agent ('%s') does not contain state '%s', "
"in AgentFunctionDescription::setInitialState().",
p->name.c_str(), init_state.c_str());
}
} else {
THROW exception::InvalidParent("Agent parent has expired, "
"in AgentFunctionDescription::setInitialState().");
}
}
void AgentFunctionDescription::setEndState(const std::string &exit_state) {
if (auto p = function->parent.lock()) {
if (p->states.find(exit_state) != p->states.end()) {
// Check if this agent function is already in a layer
auto mdl = function->model.lock();
if (!mdl) {
THROW exception::ExpiredWeakPtr();
}
for (const auto &l : mdl->layers) {
for (const auto &f : l->agent_functions) {
// Agent fn is in layer
if (f->name == this->function->name) {
// search all functions in that layer
for (const auto &f2 : l->agent_functions) {
if (const auto &a2 = f2->parent.lock()) {
if (const auto &a1 = this->function->parent.lock()) {
// Same agent
if (a2->name == a1->name) {
// Skip ourself
if (f2->name == this->function->name)
continue;
if (f2->initial_state == exit_state ||
f2->end_state == exit_state) {
THROW exception::InvalidAgentFunc("Agent functions's '%s' and '%s', within the same layer "
"cannot share any input or output states, this is not permitted, "
"in AgentFunctionDescription::setEndState().",
f2->name.c_str(), this->function->name.c_str());
}
}
}
}
}
}
}
}
// Checks passed, make change
this->function->end_state = exit_state;
} else {
THROW exception::InvalidStateName("Agent ('%s') does not contain state '%s', "
"in AgentFunctionDescription::setEndState().",
p->name.c_str(), exit_state.c_str());
}
} else {
THROW exception::InvalidParent("Agent parent has expired, "
"in AgentFunctionDescription::setEndState().");
}
}
void AgentFunctionDescription::setMessageInput(const std::string &message_name) {
if (auto other = function->message_output.lock()) {
if (message_name == other->name) {
THROW exception::InvalidMessageName("Message '%s' is already bound as message output in agent function %s, "
"the same message cannot be input and output by the same function, "
"in AgentFunctionDescription::setMessageInput().",
message_name.c_str(), function->name.c_str());
}
}
auto mdl = function->model.lock();
if (!mdl) {
THROW exception::ExpiredWeakPtr();
}
auto a = mdl->messages.find(message_name);
if (a != mdl->messages.end()) {
// Just compare the classname is the same, to allow for the various approaches to namespace use. This should only be required for RTC functions.
auto message_in_classname = detail::cxxname::getUnqualifiedName(this->function->message_in_type);
auto demangledClassName = detail::cxxname::getUnqualifiedName(detail::curve::CurveRTCHost::demangle(a->second->getType()));
if (message_in_classname == demangledClassName) {
this->function->message_input = a->second;
} else {
THROW exception::InvalidMessageType("Message ('%s') type '%s' does not match type '%s' applied to FLAMEGPU_AGENT_FUNCTION ('%s'), "
"in AgentFunctionDescription::setMessageInput().",
message_name.c_str(), demangledClassName.c_str(), message_in_classname.c_str(), this->function->name.c_str());
}
} else {
THROW exception::InvalidMessageName("Model ('%s') does not contain message '%s', "
"in AgentFunctionDescription::setMessageInput().",
mdl->name.c_str(), message_name.c_str());
}
}
void AgentFunctionDescription::setMessageInput(MessageBruteForce::CDescription message) {
if (message.message->model.lock() != function->model.lock()) {
THROW exception::DifferentModel("Attempted to use agent description from a different model, "
"in AgentFunctionDescription::setAgentOutput().");
}
if (auto other = function->message_output.lock()) {
if (message.getName() == other->name) {
THROW exception::InvalidMessageName("Message '%s' is already bound as message output in agent function %s, "
"the same message cannot be input and output by the same function, "
"in AgentFunctionDescription::setMessageInput().",
message.getName().c_str(), function->name.c_str());
}
}
auto mdl = function->model.lock();
if (!mdl) {
THROW exception::ExpiredWeakPtr();
}
auto a = mdl->messages.find(message.getName());
if (a != mdl->messages.end()) {
if (a->second == message.message) {
// Just compare the classname is the same, to allow for the various approaches to namespace use. This should only be required for RTC functions.
auto message_in_classname = detail::cxxname::getUnqualifiedName(this->function->message_in_type);
auto demangledClassName = detail::cxxname::getUnqualifiedName(detail::curve::CurveRTCHost::demangle(a->second->getType()));
if (message_in_classname == demangledClassName) {
this->function->message_input = a->second;
} else {
THROW exception::InvalidMessageType("Message ('%s') type '%s' does not match type '%s' applied to FLAMEGPU_AGENT_FUNCTION ('%s'), "
"in AgentFunctionDescription::setMessageInput().",
a->second->name.c_str(), demangledClassName.c_str(), message_in_classname.c_str(), this->function->name.c_str());
}
} else {
THROW exception::InvalidMessage("Message '%s' is not from Model '%s', "
"in AgentFunctionDescription::setMessageInput().",
message.getName().c_str(), mdl->name.c_str());
}
} else {
THROW exception::InvalidMessageName("Model ('%s') does not contain message '%s', "
"in AgentFunctionDescription::setMessageInput().",
mdl->name.c_str(), message.getName().c_str());
}
}
void AgentFunctionDescription::setMessageOutput(const std::string &message_name) {
if (auto other = function->message_input.lock()) {
if (message_name == other->name) {
THROW exception::InvalidMessageName("Message '%s' is already bound as message output in agent function %s, "
"the same message cannot be input and output by the same function, "
"in AgentFunctionDescription::setMessageOutput().",
message_name.c_str(), function->name.c_str());
}
}
// Clear old value
if (this->function->message_output_optional) {
if (auto b = this->function->message_output.lock()) {
b->optional_outputs--;
}
}
auto mdl = function->model.lock();
if (!mdl) {
THROW exception::ExpiredWeakPtr();
}
auto a = mdl->messages.find(message_name);
if (a != mdl->messages.end()) {
// Just compare the classname is the same, to allow for the various approaches to namespace use. This should only be required for RTC functions.
auto message_out_classname = detail::cxxname::getUnqualifiedName(this->function->message_out_type);
auto demangledClassName = detail::cxxname::getUnqualifiedName(detail::curve::CurveRTCHost::demangle(a->second->getType()));
if (message_out_classname == demangledClassName) {
this->function->message_output = a->second;
if (this->function->message_output_optional) {
a->second->optional_outputs++;
}
} else {
THROW exception::InvalidMessageType("Message ('%s') type '%s' does not match type '%s' applied to FLAMEGPU_AGENT_FUNCTION ('%s'), "
"in AgentFunctionDescription::setMessageOutput().",
message_name.c_str(), demangledClassName.c_str(), message_out_classname.c_str(), this->function->name.c_str());
}
} else {
THROW exception::InvalidMessageName("Model ('%s') does not contain message '%s', "
"in AgentFunctionDescription::setMessageOutput().",
mdl->name.c_str(), message_name.c_str());
}
}
void AgentFunctionDescription::setMessageOutput(MessageBruteForce::CDescription message) {
if (message.message->model.lock() != function->model.lock()) {
THROW exception::DifferentModel("Attempted to use agent description from a different model, "
"in AgentFunctionDescription::setAgentOutput().");
}
if (auto other = function->message_input.lock()) {
if (message.getName() == other->name) {
THROW exception::InvalidMessageName("Message '%s' is already bound as message input in agent function %s, "
"the same message cannot be input and output by the same function, "
"in AgentFunctionDescription::setMessageOutput().",
message.getName().c_str(), function->name.c_str());
}
}
// Clear old value
if (this->function->message_output_optional) {
if (auto b = this->function->message_output.lock()) {
b->optional_outputs--;
}
}
auto mdl = function->model.lock();
if (!mdl) {
THROW exception::ExpiredWeakPtr();
}
auto a = mdl->messages.find(message.getName());
if (a != mdl->messages.end()) {
if (a->second == message.message) {
// Just compare the classname is the same, to allow for the various approaches to namespace use. This should only be required for RTC functions.
auto message_out_classname = detail::cxxname::getUnqualifiedName(this->function->message_out_type);
auto demangledClassName = detail::cxxname::getUnqualifiedName(detail::curve::CurveRTCHost::demangle(a->second->getType()));
if (message_out_classname == demangledClassName) {
this->function->message_output = a->second;
if (this->function->message_output_optional) {
a->second->optional_outputs++;
}
} else {
THROW exception::InvalidMessageType("Message ('%s') type '%s' does not match type '%s' applied to FLAMEGPU_AGENT_FUNCTION ('%s'), "
"in AgentFunctionDescription::setMessageOutput().",
a->second->name.c_str(), demangledClassName.c_str(), message_out_classname.c_str(), this->function->name.c_str());
}
} else {
THROW exception::InvalidMessage("Message '%s' is not from Model '%s', "
"in AgentFunctionDescription::setMessageOutput().",
message.getName().c_str(), mdl->name.c_str());
}
} else {
THROW exception::InvalidMessageName("Model ('%s') does not contain message '%s', "
"in AgentFunctionDescription::setMessageOutput()\n",
mdl->name.c_str(), message.getName().c_str());
}
}
void AgentFunctionDescription::setMessageOutputOptional(const bool output_is_optional) {
if (output_is_optional != this->function->message_output_optional) {
this->function->message_output_optional = output_is_optional;
if (auto b = this->function->message_output.lock()) {
if (output_is_optional)
b->optional_outputs++;
else
b->optional_outputs--;
}
}
}
void AgentFunctionDescription::setAgentOutput(const std::string &agent_name, const std::string state) {
// Set new
auto mdl = function->model.lock();
if (!mdl) {
THROW exception::ExpiredWeakPtr();
}
auto a = mdl->agents.find(agent_name);
if (a != mdl->agents.end()) {
// Check agent state is valid
if (a->second->states.find(state)!= a->second->states.end()) { // Clear old value
if (auto b = this->function->agent_output.lock()) {
b->agent_outputs--;
}
this->function->agent_output = a->second;
this->function->agent_output_state = state;
a->second->agent_outputs++; // Mark inside agent that we are using it as an output
} else {
THROW exception::InvalidStateName("Agent ('%s') does not contain state '%s', "
"in AgentFunctionDescription::setAgentOutput().",
agent_name.c_str(), state.c_str());
}
} else {
THROW exception::InvalidAgentName("Model ('%s') does not contain agent '%s', "
"in AgentFunctionDescription::setAgentOutput().",
mdl->name.c_str(), agent_name.c_str());
}
}
void AgentFunctionDescription::setAgentOutput(AgentDescription &agent, const std::string state) {
if (agent.agent->model.lock() != function->model.lock()) {
THROW exception::DifferentModel("Attempted to use agent description from a different model, "
"in AgentFunctionDescription::setAgentOutput().");
}
// Set new
auto mdl = function->model.lock();
if (!mdl) {
THROW exception::ExpiredWeakPtr();
}
auto a = mdl->agents.find(agent.getName());
if (a != mdl->agents.end()) {
if (*a->second == agent) {
// Check agent state is valid
if (a->second->states.find(state) != a->second->states.end()) {
// Clear old value
if (auto b = this->function->agent_output.lock()) {
b->agent_outputs--;
}
this->function->agent_output = a->second;
this->function->agent_output_state = state;
a->second->agent_outputs++; // Mark inside agent that we are using it as an output
} else {
THROW exception::InvalidStateName("Agent ('%s') does not contain state '%s', "
"in AgentFunctionDescription::setAgentOutput().",
agent.getName().c_str(), state.c_str());
}
} else {
THROW exception::InvalidMessage("Agent '%s' is not from Model '%s', "
"in AgentFunctionDescription::setAgentOutput().",
agent.getName().c_str(), mdl->name.c_str());
}
} else {
THROW exception::InvalidMessageName("Model ('%s') does not contain agent '%s', "
"in AgentFunctionDescription::setAgentOutput().",
mdl->name.c_str(), agent.getName().c_str());
}
}
void AgentFunctionDescription::setAllowAgentDeath(const bool has_death) {
function->has_agent_death = has_death;
}
void AgentFunctionDescription::setRTCFunctionCondition(std::string func_cond_src) {
// Use Regex to get agent function name
std::regex rgx(R"###(.*FLAMEGPU_AGENT_FUNCTION_CONDITION\([ \t]*(\w+)[ \t]*)###");
std::smatch match;
std::string func_cond_name;
if (std::regex_search(func_cond_src, match, rgx)) {
if (match.size() == 2) {
func_cond_name = match[1];
// set the runtime agent function condition source in agent function data
function->rtc_func_condition_name = func_cond_name;
function->rtc_condition_source = func_cond_src;
// TODO: Does this need emplacing in CUDAAgent?
} else {
THROW exception::InvalidAgentFunc("Runtime agent function condition is missing FLAMEGPU_AGENT_FUNCTION_CONDITION arguments e.g. 'FLAMEGPU_AGENT_FUNCTION_CONDITION(func_name)', "
"in AgentDescription::setRTCFunctionCondition().");
}
} else {
THROW exception::InvalidAgentFunc("Runtime agent function('%s') is missing FLAMEGPU_AGENT_FUNCTION_CONDITION, "
"in AgentDescription::setRTCFunctionCondition().");
}
// append jitify program string and include
std::string func_cond_src_str = std::string(func_cond_name + "_program\n");
#ifdef FLAMEGPU_OUTPUT_RTC_DYNAMIC_FILES
func_cond_src_str.append("#line 1 \"").append(function->rtc_func_name).append("_impl_condition.cu\"\n");
#endif
func_cond_src_str.append("#include \"flamegpu/runtime/DeviceAPI.cuh\"\n");
// Append line pragma to correct file/line number in same format as FLAMEGPU_OUTPUT_RTC_DYNAMIC_FILES
#ifndef FLAMEGPU_OUTPUT_RTC_DYNAMIC_FILES
func_cond_src_str.append("#line 1 \"").append(function->rtc_func_name).append("_impl_condition.cu\"\n");
#endif
// If src begins (\r)\n, trim that
// Append the function source
if (func_cond_src.find_first_of("\n") <= 1) {
func_cond_src_str.append(func_cond_src.substr(func_cond_src.find_first_of("\n") + 1));
} else {
func_cond_src_str.append(func_cond_src);
}
// update the agent function data
function->rtc_func_condition_name = func_cond_name;
function->rtc_condition_source = func_cond_src_str;
}
void AgentFunctionDescription::setRTCFunctionConditionFile(const std::string& file_path) {
// Load file and forward to regular RTC method
std::ifstream file;
file.open(file_path);
if (file.is_open()) {
std::stringstream sstream;
sstream << file.rdbuf();
const std::string func_src = sstream.str();
setRTCFunctionCondition(func_src);
}
THROW exception::InvalidFilePath("Unable able to open file '%s', "
"in AgentDescription::newRTCFunctionFile().",
file_path.c_str());
}
MessageBruteForce::Description AgentFunctionDescription::MessageInput() {
if (auto m = function->message_input.lock())
return MessageBruteForce::Description(m);
THROW exception::OutOfBoundsException("Message input has not been set, "
"in AgentFunctionDescription::MessageInput().");
}
MessageBruteForce::Description AgentFunctionDescription::MessageOutput() {
if (auto m = function->message_output.lock())
return MessageBruteForce::Description(m);
THROW exception::OutOfBoundsException("Message output has not been set, "
"in AgentFunctionDescription::MessageOutput().");
}
bool &AgentFunctionDescription::MessageOutputOptional() {
return function->message_output_optional;
}
bool &AgentFunctionDescription::AllowAgentDeath() {
return function->has_agent_death;
}
AgentDescription AgentFunctionDescription::AgentOutput() {
if (auto a = function->agent_output.lock())
return AgentDescription(a);
THROW exception::OutOfBoundsException("Agent output has not been set, "
"in AgentFunctionDescription::AgentOutput().");
}
AgentFunctionDescription AgentDescription::newRTCFunction(const std::string& function_name, const std::string& func_src) {
if (agent->functions.find(function_name) == agent->functions.end()) {
// Use Regex to get agent function name, and input/output message type
std::regex rgx(R"###(.*FLAMEGPU_AGENT_FUNCTION\([ \t]*(\w+),[ \t]*([:\w]+),[ \t]*([:\w]+)[ \t]*\))###");
std::smatch match;
if (std::regex_search(func_src, match, rgx)) {
if (match.size() == 4) {
std::string code_func_name = match[1]; // not yet clear if this is required
std::string in_type_name = match[2];
std::string out_type_name = match[3];
if (in_type_name == "flamegpu::MessageSpatial3D" || in_type_name == "flamegpu::MessageSpatial2D" || out_type_name == "flamegpu::MessageSpatial3D" || out_type_name == "flamegpu::MessageSpatial2D") {
if (agent->variables.find("_auto_sort_bin_index") == agent->variables.end()) {
agent->variables.emplace("_auto_sort_bin_index", Variable(1, std::vector<unsigned int> {0}));
}
}
// set the runtime agent function source in agent function data
std::string func_src_str = std::string(function_name + "_program\n");
#ifdef FLAMEGPU_OUTPUT_RTC_DYNAMIC_FILES
func_src_str.append("#line 1 \"").append(code_func_name).append("_impl.cu\"\n");
#endif
func_src_str.append("#include \"flamegpu/runtime/DeviceAPI.cuh\"\n");
// Include the required headers for the input message type.
std::string in_type_include_name = in_type_name.substr(in_type_name.find_last_of("::") + 1);
func_src_str = func_src_str.append("#include \"flamegpu/runtime/messaging/"+ in_type_include_name + "/" + in_type_include_name + "Device.cuh\"\n");
// If the message input and output types do not match, also include the input type
if (in_type_name != out_type_name) {
std::string out_type_include_name = out_type_name.substr(out_type_name.find_last_of("::") + 1);
func_src_str = func_src_str.append("#include \"flamegpu/runtime/messaging/"+ out_type_include_name + "/" + out_type_include_name + "Device.cuh\"\n");
}
// Append line pragma to correct file/line number in same format as FLAMEGPU_OUTPUT_RTC_DYNAMIC_FILES
#ifndef FLAMEGPU_OUTPUT_RTC_DYNAMIC_FILES
func_src_str.append("#line 1 \"").append(code_func_name).append("_impl.cu\"\n");
#endif
// If src begins (\r)\n, trim that
// Append the function source
if (func_src.find_first_of("\n") <= 1) {
func_src_str.append(func_src.substr(func_src.find_first_of("\n") + 1));
} else {
func_src_str.append(func_src);
}
auto rtn = std::shared_ptr<AgentFunctionData>(new AgentFunctionData(this->agent->shared_from_this(), function_name, func_src_str, in_type_name, out_type_name, code_func_name));
agent->functions.emplace(function_name, rtn);
return AgentFunctionDescription(rtn);
} else {
THROW exception::InvalidAgentFunc("Runtime agent function('%s') is missing FLAMEGPU_AGENT_FUNCTION arguments e.g. (func_name, message_input_type, message_output_type), "
"in AgentDescription::newRTCFunction().",
agent->name.c_str());
}
} else {
THROW exception::InvalidAgentFunc("Runtime agent function('%s') is missing FLAMEGPU_AGENT_FUNCTION, "
"in AgentDescription::newRTCFunction().",
agent->name.c_str());
}
}
THROW exception::InvalidAgentFunc("Agent ('%s') already contains function '%s', "
"in AgentDescription::newRTCFunction().",
agent->name.c_str(), function_name.c_str());
}
AgentFunctionDescription AgentDescription::newRTCFunctionFile(const std::string& function_name, const std::string& file_path) {
if (agent->functions.find(function_name) == agent->functions.end()) {
// Load file and forward to regular RTC method
std::ifstream file;
file.open(file_path);
if (file.is_open()) {
std::stringstream sstream;
sstream << file.rdbuf();
const std::string func_src = sstream.str();
return newRTCFunction(function_name, func_src);
}
THROW exception::InvalidFilePath("Unable able to open file '%s', "
"in AgentDescription::newRTCFunctionFile().",
file_path.c_str());
}
THROW exception::InvalidAgentFunc("Agent ('%s') already contains function '%s', "
"in AgentDescription::newRTCFunctionFile().",
agent->name.c_str(), function_name.c_str());
}
} // namespace flamegpu