309 Params *params_ =
nullptr;
310 const Context *context_ =
nullptr;
315 std::atomic<TaskPool *> task_pool_ =
nullptr;
316#ifdef FN_LAZY_FUNCTION_DEBUG_THREADS
317 std::thread::id current_main_thread_;
322 struct ThreadLocalStorage {
328 std::optional<destruct_ptr<LocalUserData>> local_user_data;
330 std::unique_ptr<threading::EnumerableThreadSpecific<ThreadLocalStorage>> thread_locals_;
335 bool is_first_execution_ =
true;
337 friend GraphExecutorLFParams;
354 BLI_assert(self_.graph_.node_indices_are_valid());
359 if (
TaskPool *task_pool = task_pool_.load()) {
363 for (const int node_index : range) {
364 const Node &node = *self_.graph_.nodes()[node_index];
365 NodeState &node_state = *node_states_[node_index];
366 this->destruct_node_state(node, node_state);
378#ifdef FN_LAZY_FUNCTION_DEBUG_THREADS
379 current_main_thread_ = std::this_thread::get_id();
381 const auto deferred_func = [&]() {
385 is_first_execution_ =
false;
386#ifdef FN_LAZY_FUNCTION_DEBUG_THREADS
387 current_main_thread_ = {};
392 const LocalData local_data = this->get_local_data();
395 if (is_first_execution_) {
397 logging_enabled_state_ = self_.logger_->get_logging_enabled_state(context);
401 char *buffer =
static_cast<char *
>(
402 local_data.allocator->
allocate(self_.preprocess_data_.total_size,
alignof(
void *)));
403 this->initialize_node_states(buffer);
405 loaded_inputs_ =
MutableSpan{
reinterpret_cast<std::atomic<uint8_t> *
>(
406 buffer + self_.preprocess_data_.loaded_inputs_array_offset),
407 self_.graph_inputs_.size()};
409 memset(
static_cast<void *
>(loaded_inputs_.data()), 0, loaded_inputs_.size() *
sizeof(
bool));
411 this->set_always_unused_graph_inputs();
412 this->set_defaulted_graph_outputs(local_data);
416 if (self_.side_effect_provider_ !=
nullptr) {
417 side_effect_nodes = self_.side_effect_provider_->get_nodes_with_side_effects(context);
420 const int node_index =
node->index_in_graph();
421 NodeState &node_state = *node_states_[node_index];
426 this->initialize_static_value_usages(side_effect_nodes);
427 this->schedule_side_effect_nodes(side_effect_nodes, current_task, local_data);
430 this->schedule_for_new_output_usages(current_task, local_data);
431 this->forward_newly_provided_inputs(current_task, local_data);
433 this->run_task(current_task, local_data);
435 if (
TaskPool *task_pool = task_pool_.load()) {
450 offset +=
sizeof(std::atomic<uint8_t>) * self.graph_inputs_.size();
452 offset = (offset +
sizeof(
void *) - 1) & ~(
sizeof(
void *) - 1);
455 for (
const int i :
nodes.index_range()) {
462 static_assert(
sizeof(
NodeState) %
sizeof(
void *) == 0);
463 static_assert(
sizeof(
InputState) %
sizeof(
void *) == 0);
464 static_assert(
sizeof(
OutputState) %
sizeof(
void *) == 0);
471 void initialize_node_states(
char *buffer)
475 reinterpret_cast<NodeState **
>(buffer + self_.preprocess_data_.node_states_array_offset),
479 for (const int i : range) {
480 const Node &node = *nodes[i];
481 char *memory = buffer + self_.preprocess_data_.node_states_offsets[i];
484 NodeState *node_state = reinterpret_cast<NodeState *>(memory);
485 memory += sizeof(NodeState);
486 new (node_state) NodeState();
489 const int num_inputs = node.inputs().size();
490 const int num_outputs = node.outputs().size();
491 node_state->inputs = reinterpret_cast<InputState *>(memory);
492 memory += sizeof(InputState) * num_inputs;
493 node_state->outputs = reinterpret_cast<OutputState *>(memory);
495 default_construct_n(node_state->inputs, num_inputs);
496 default_construct_n(node_state->outputs, num_outputs);
498 node_states_[i] = node_state;
503 void destruct_node_state(
const Node &
node, NodeState &node_state)
505 if (
node.is_function()) {
506 const LazyFunction &
fn =
static_cast<const FunctionNode &
>(
node).function();
507 if (node_state.storage !=
nullptr) {
508 fn.destruct_storage(node_state.storage);
511 for (
const int i :
node.inputs().index_range()) {
512 InputState &input_state = node_state.inputs[i];
513 const InputSocket &input_socket =
node.input(i);
514 this->destruct_input_value_if_exists(input_state, input_socket.type());
516 std::destroy_at(&node_state);
522 void schedule_for_new_output_usages(CurrentTask ¤t_task,
const LocalData &local_data)
524 for (
const int graph_output_index : self_.graph_outputs_.index_range()) {
525 if (params_->output_was_set(graph_output_index)) {
528 const ValueUsage output_usage = params_->get_output_usage(graph_output_index);
529 if (output_usage == ValueUsage::Maybe) {
532 const InputSocket &socket = *self_.graph_outputs_[graph_output_index];
534 NodeState &node_state = *node_states_[
node.index_in_graph()];
535 this->with_locked_node(
536 node, node_state, current_task, local_data, [&](LockedNode &locked_node) {
537 if (output_usage == ValueUsage::Used) {
538 this->set_input_required(locked_node, socket);
541 this->set_input_unused(locked_node, socket);
547 void set_defaulted_graph_outputs(
const LocalData &local_data)
549 for (
const int graph_output_index : self_.graph_outputs_.index_range()) {
550 const InputSocket &socket = *self_.graph_outputs_[graph_output_index];
551 if (socket.origin() !=
nullptr) {
554 const CPPType &type = socket.type();
555 const void *default_value = socket.default_value();
558 if (logging_enabled_state_.socket_values) {
559 const Context
context{context_->storage, context_->user_data, local_data.local_user_data};
560 self_.logger_->log_socket_value(socket, {type, default_value},
context);
563 void *output_ptr = params_->get_output_data_ptr(graph_output_index);
564 type.copy_construct(default_value, output_ptr);
565 params_->output_set(graph_output_index);
569 void set_always_unused_graph_inputs()
571 for (
const int i : self_.graph_inputs_.index_range()) {
572 const OutputSocket &socket = *self_.graph_inputs_[i];
574 const NodeState &node_state = *node_states_[
node.index_in_graph()];
575 const OutputState &output_state = node_state.outputs[socket.index()];
576 if (output_state.usage == ValueUsage::Unused) {
577 params_->set_input_unused(i);
594 Stack<const Node *> reachable_nodes_to_check;
595 Array<bool> reachable_node_flags(all_nodes.
size(),
false);
598 for (
const InputSocket *socket : self_.graph_outputs_) {
600 const int node_index =
node.index_in_graph();
601 if (!reachable_node_flags[node_index]) {
602 reachable_node_flags[node_index] =
true;
603 reachable_nodes_to_check.push(&
node);
608 for (
const FunctionNode *
node : side_effect_nodes) {
609 const int node_index =
node->index_in_graph();
610 reachable_node_flags[node_index] =
true;
611 reachable_nodes_to_check.push(
node);
615 while (!reachable_nodes_to_check.is_empty()) {
616 const Node &
node = *reachable_nodes_to_check.pop();
617 for (
const InputSocket *input_socket :
node.inputs()) {
618 const OutputSocket *origin_socket = input_socket->origin();
619 if (origin_socket !=
nullptr) {
620 const Node &origin_node = origin_socket->node();
621 const int origin_node_index = origin_node.index_in_graph();
622 if (!reachable_node_flags[origin_node_index]) {
623 reachable_node_flags[origin_node_index] =
true;
624 reachable_nodes_to_check.push(&origin_node);
630 for (
const int node_index : reachable_node_flags.index_range()) {
631 const Node &
node = *all_nodes[node_index];
632 NodeState &node_state = *node_states_[node_index];
633 const bool node_is_reachable = reachable_node_flags[node_index];
634 if (node_is_reachable) {
635 for (
const int output_index :
node.outputs().index_range()) {
636 const OutputSocket &output_socket =
node.output(output_index);
637 OutputState &output_state = node_state.outputs[output_index];
639 for (
const InputSocket *target_socket : output_socket.targets()) {
640 const Node &target_node = target_socket->node();
641 const bool target_is_reachable = reachable_node_flags[target_node.index_in_graph()];
643 if (target_is_reachable) {
647 output_state.potential_target_sockets = use_count;
648 if (use_count == 0) {
649 output_state.usage = ValueUsage::Unused;
655 for (
const int input_index :
node.inputs().index_range()) {
656 node_state.inputs[input_index].usage = ValueUsage::Unused;
663 CurrentTask ¤t_task,
664 const LocalData &local_data)
666 for (
const FunctionNode *
node : side_effect_nodes) {
667 NodeState &node_state = *node_states_[
node->index_in_graph()];
668 this->with_locked_node(
669 *
node, node_state, current_task, local_data, [&](LockedNode &locked_node) {
670 this->schedule_node(locked_node, current_task,
false);
675 void forward_newly_provided_inputs(CurrentTask ¤t_task,
const LocalData &local_data)
677 for (
const int graph_input_index : self_.graph_inputs_.index_range()) {
678 std::atomic<uint8_t> &was_loaded = loaded_inputs_[graph_input_index];
679 if (was_loaded.load()) {
682 void *input_data = params_->try_get_input_data_ptr(graph_input_index);
683 if (input_data ==
nullptr) {
686 if (was_loaded.fetch_or(1)) {
690 this->forward_newly_provided_input(current_task, local_data, graph_input_index, input_data);
694 void forward_newly_provided_input(CurrentTask ¤t_task,
695 const LocalData &local_data,
696 const int graph_input_index,
699 const OutputSocket &socket = *self_.graph_inputs_[graph_input_index];
700 const CPPType &type = socket.type();
701 void *buffer = local_data.allocator->allocate(type);
702 type.move_construct(input_data, buffer);
703 this->forward_value_to_linked_inputs(socket, {type, buffer}, current_task, local_data);
706 void notify_output_required(
const OutputSocket &socket,
707 CurrentTask ¤t_task,
708 const LocalData &local_data)
711 const int index_in_node = socket.index();
712 NodeState &node_state = *node_states_[
node.index_in_graph()];
713 OutputState &output_state = node_state.outputs[index_in_node];
717 if (
node.is_interface()) {
718 const int graph_input_index = self_.graph_input_index_by_socket_index_[socket.index()];
719 std::atomic<uint8_t> &was_loaded = loaded_inputs_[graph_input_index];
720 if (was_loaded.load()) {
723 void *input_data = params_->try_get_input_data_ptr_or_request(graph_input_index);
724 if (input_data ==
nullptr) {
727 if (was_loaded.fetch_or(1)) {
731 this->forward_newly_provided_input(current_task, local_data, graph_input_index, input_data);
736 this->with_locked_node(
737 node, node_state, current_task, local_data, [&](LockedNode &locked_node) {
738 if (output_state.usage == ValueUsage::Used) {
741 output_state.usage = ValueUsage::Used;
742 this->schedule_node(locked_node, current_task,
false);
746 void notify_output_unused(
const OutputSocket &socket,
747 CurrentTask ¤t_task,
748 const LocalData &local_data)
751 const int index_in_node = socket.index();
752 NodeState &node_state = *node_states_[
node.index_in_graph()];
753 OutputState &output_state = node_state.outputs[index_in_node];
755 this->with_locked_node(
756 node, node_state, current_task, local_data, [&](LockedNode &locked_node) {
757 output_state.potential_target_sockets -= 1;
758 if (output_state.potential_target_sockets == 0) {
759 BLI_assert(output_state.usage != ValueUsage::Unused);
760 if (output_state.usage == ValueUsage::Maybe) {
761 output_state.usage = ValueUsage::Unused;
762 if (
node.is_interface()) {
763 const int graph_input_index =
764 self_.graph_input_index_by_socket_index_[socket.index()];
765 params_->set_input_unused(graph_input_index);
770 this->schedule_node(locked_node, current_task,
true);
777 void schedule_node(LockedNode &locked_node, CurrentTask ¤t_task,
const bool is_priority)
780 switch (locked_node.node_state.schedule_state) {
781 case NodeScheduleState::NotScheduled: {
782 locked_node.node_state.schedule_state = NodeScheduleState::Scheduled;
783 const FunctionNode &
node =
static_cast<const FunctionNode &
>(locked_node.node);
784 if (this->use_multi_threading()) {
785 std::lock_guard
lock{current_task.mutex};
786 current_task.scheduled_nodes.schedule(
node, is_priority);
789 current_task.scheduled_nodes.schedule(
node, is_priority);
791 current_task.has_scheduled_nodes.store(
true, std::memory_order_relaxed);
794 case NodeScheduleState::Scheduled: {
797 case NodeScheduleState::Running: {
798 locked_node.node_state.schedule_state = NodeScheduleState::RunningAndRescheduled;
801 case NodeScheduleState::RunningAndRescheduled: {
807 void with_locked_node(
const Node &
node,
808 NodeState &node_state,
809 CurrentTask ¤t_task,
810 const LocalData &local_data,
811 const FunctionRef<
void(LockedNode &)> f)
815 LockedNode locked_node{
node, node_state};
816 if (this->use_multi_threading()) {
817 std::lock_guard
lock{node_state.mutex};
818 threading::isolate_task([&]() { f(locked_node); });
824 this->send_output_required_notifications(
825 locked_node.delayed_required_outputs, current_task, local_data);
826 this->send_output_unused_notifications(
827 locked_node.delayed_unused_outputs, current_task, local_data);
831 CurrentTask ¤t_task,
832 const LocalData &local_data)
834 for (
const OutputSocket *socket : sockets) {
835 this->notify_output_required(*socket, current_task, local_data);
840 CurrentTask ¤t_task,
841 const LocalData &local_data)
843 for (
const OutputSocket *socket : sockets) {
844 this->notify_output_unused(*socket, current_task, local_data);
848 void run_task(CurrentTask ¤t_task,
const LocalData &local_data)
850 while (
const FunctionNode *
node = current_task.scheduled_nodes.pop_next_node()) {
851 if (current_task.scheduled_nodes.is_empty()) {
852 current_task.has_scheduled_nodes.store(
false, std::memory_order_relaxed);
854 this->run_node_task(*
node, current_task, local_data);
858 if (current_task.scheduled_nodes.nodes_num() > 128) {
859 if (this->try_enable_multi_threading()) {
860 std::unique_ptr<ScheduledNodes> split_nodes = std::make_unique<ScheduledNodes>();
861 current_task.scheduled_nodes.split_into(*split_nodes);
862 this->push_to_task_pool(std::move(split_nodes));
868 void run_node_task(
const FunctionNode &
node,
869 CurrentTask ¤t_task,
870 const LocalData &local_data)
872 NodeState &node_state = *node_states_[
node.index_in_graph()];
873 LinearAllocator<> &allocator = *local_data.allocator;
874 Context local_context{context_->storage, context_->user_data, local_data.local_user_data};
875 const LazyFunction &fn =
node.function();
877 bool node_needs_execution =
false;
878 this->with_locked_node(
879 node, node_state, current_task, local_data, [&](LockedNode &locked_node) {
880 BLI_assert(node_state.schedule_state == NodeScheduleState::Scheduled);
881 node_state.schedule_state = NodeScheduleState::Running;
883 if (node_state.node_has_finished) {
887 bool required_uncomputed_output_exists =
false;
888 for (
const int output_index :
node.outputs().index_range()) {
889 OutputState &output_state = node_state.outputs[output_index];
890 output_state.usage_for_execution = output_state.usage;
891 if (output_state.usage == ValueUsage::Used && !output_state.has_been_computed) {
892 required_uncomputed_output_exists =
true;
895 if (!required_uncomputed_output_exists && !node_state.has_side_effects) {
899 if (!node_state.always_used_inputs_requested) {
902 for (
const int input_index : fn_inputs.
index_range()) {
903 const Input &fn_input = fn_inputs[input_index];
904 if (fn_input.usage == ValueUsage::Used) {
905 const InputSocket &input_socket =
node.input(input_index);
906 if (input_socket.origin() !=
nullptr) {
907 this->set_input_required(locked_node, input_socket);
912 node_state.always_used_inputs_requested =
true;
915 for (
const int input_index :
node.inputs().index_range()) {
916 InputState &input_state = node_state.inputs[input_index];
917 if (input_state.was_ready_for_execution) {
920 if (input_state.value !=
nullptr) {
921 input_state.was_ready_for_execution =
true;
924 if (!fn.allow_missing_requested_inputs()) {
925 if (input_state.usage == ValueUsage::Used) {
931 node_needs_execution =
true;
934 if (node_needs_execution) {
935 if (!node_state.storage_and_defaults_initialized) {
937 node_state.storage = fn.init_storage(allocator);
940 for (
const int input_index :
node.inputs().index_range()) {
941 const InputSocket &input_socket =
node.input(input_index);
942 if (input_socket.origin() !=
nullptr) {
945 InputState &input_state = node_state.inputs[input_index];
946 const CPPType &type = input_socket.type();
947 const void *default_value = input_socket.default_value();
949 if (logging_enabled_state_.socket_values) {
950 self_.logger_->log_socket_value(input_socket, {type, default_value}, local_context);
953 input_state.value = allocator.allocate(type);
954 type.copy_construct(default_value, input_state.value);
955 input_state.was_ready_for_execution =
true;
958 node_state.storage_and_defaults_initialized =
true;
964 this->execute_node(
node, node_state, current_task, local_data);
967 this->with_locked_node(
968 node, node_state, current_task, local_data, [&](LockedNode &locked_node) {
970 if (node_needs_execution) {
971 this->assert_expected_outputs_have_been_computed(locked_node, local_data);
974 this->finish_node_if_possible(locked_node);
975 const bool reschedule_requested = node_state.schedule_state ==
976 NodeScheduleState::RunningAndRescheduled;
977 node_state.schedule_state = NodeScheduleState::NotScheduled;
978 if (reschedule_requested && !node_state.node_has_finished) {
979 this->schedule_node(locked_node, current_task,
false);
984 void assert_expected_outputs_have_been_computed(LockedNode &locked_node,
985 const LocalData &local_data)
987 const FunctionNode &
node =
static_cast<const FunctionNode &
>(locked_node.node);
988 const NodeState &node_state = locked_node.node_state;
990 if (node_state.missing_required_inputs > 0) {
993 if (node_state.schedule_state == NodeScheduleState::RunningAndRescheduled) {
996 Vector<const OutputSocket *> missing_outputs;
997 for (
const int i :
node.outputs().index_range()) {
998 const OutputState &output_state = node_state.outputs[i];
999 if (output_state.usage_for_execution == ValueUsage::Used) {
1000 if (!output_state.has_been_computed) {
1001 missing_outputs.append(&
node.output(i));
1005 if (!missing_outputs.is_empty()) {
1006 if (self_.logger_ !=
nullptr) {
1007 const Context
context{context_->storage, context_->user_data, local_data.local_user_data};
1008 self_.logger_->dump_when_outputs_are_missing(
node, missing_outputs, context);
1014 void finish_node_if_possible(LockedNode &locked_node)
1016 const Node &
node = locked_node.node;
1017 NodeState &node_state = locked_node.node_state;
1019 if (node_state.node_has_finished) {
1024 for (
const int output_index :
node.outputs().index_range()) {
1025 const OutputState &output_state = node_state.outputs[output_index];
1026 if (output_state.usage != ValueUsage::Unused && !output_state.has_been_computed) {
1031 for (
const int input_index :
node.inputs().index_range()) {
1032 const InputState &input_state = node_state.inputs[input_index];
1033 if (input_state.usage == ValueUsage::Used && !input_state.was_ready_for_execution) {
1038 node_state.node_has_finished =
true;
1040 for (
const int input_index :
node.inputs().index_range()) {
1041 const InputSocket &input_socket =
node.input(input_index);
1042 InputState &input_state = node_state.inputs[input_index];
1043 if (input_state.usage == ValueUsage::Maybe) {
1044 this->set_input_unused(locked_node, input_socket);
1046 else if (input_state.usage == ValueUsage::Used) {
1047 this->destruct_input_value_if_exists(input_state, input_socket.type());
1051 if (node_state.storage !=
nullptr) {
1052 if (
node.is_function()) {
1053 const FunctionNode &fn_node =
static_cast<const FunctionNode &
>(
node);
1054 fn_node.function().destruct_storage(node_state.storage);
1056 node_state.storage =
nullptr;
1060 void destruct_input_value_if_exists(InputState &input_state,
const CPPType &type)
1062 if (input_state.value !=
nullptr) {
1063 type.destruct(input_state.value);
1064 input_state.value =
nullptr;
1068 void execute_node(
const FunctionNode &
node,
1069 NodeState &node_state,
1070 CurrentTask ¤t_task,
1071 const LocalData &local_data);
1073 void set_input_unused_during_execution(
const Node &
node,
1074 NodeState &node_state,
1075 const int input_index,
1076 CurrentTask ¤t_task,
1077 const LocalData &local_data)
1079 const InputSocket &input_socket =
node.input(input_index);
1080 this->with_locked_node(
1081 node, node_state, current_task, local_data, [&](LockedNode &locked_node) {
1082 this->set_input_unused(locked_node, input_socket);
1086 void set_input_unused(LockedNode &locked_node,
const InputSocket &input_socket)
1088 NodeState &node_state = locked_node.node_state;
1089 const int input_index = input_socket.index();
1090 InputState &input_state = node_state.inputs[input_index];
1092 BLI_assert(input_state.usage != ValueUsage::Used);
1093 if (input_state.usage == ValueUsage::Unused) {
1096 input_state.usage = ValueUsage::Unused;
1098 this->destruct_input_value_if_exists(input_state, input_socket.type());
1099 if (input_state.was_ready_for_execution) {
1102 const OutputSocket *origin = input_socket.origin();
1103 if (origin !=
nullptr) {
1104 locked_node.delayed_unused_outputs.append(origin);
1108 void *set_input_required_during_execution(
const Node &
node,
1109 NodeState &node_state,
1110 const int input_index,
1111 CurrentTask ¤t_task,
1112 const LocalData &local_data)
1114 const InputSocket &input_socket =
node.input(input_index);
1116 this->with_locked_node(
1117 node, node_state, current_task, local_data, [&](LockedNode &locked_node) {
1118 result = this->set_input_required(locked_node, input_socket);
1123 void *set_input_required(LockedNode &locked_node,
const InputSocket &input_socket)
1125 BLI_assert(&locked_node.node == &input_socket.node());
1126 NodeState &node_state = locked_node.node_state;
1127 const int input_index = input_socket.index();
1128 InputState &input_state = node_state.inputs[input_index];
1130 BLI_assert(input_state.usage != ValueUsage::Unused);
1132 if (input_state.value !=
nullptr) {
1133 input_state.was_ready_for_execution =
true;
1134 return input_state.value;
1136 if (input_state.usage == ValueUsage::Used) {
1139 input_state.usage = ValueUsage::Used;
1140 node_state.missing_required_inputs += 1;
1142 const OutputSocket *origin_socket = input_socket.origin();
1145 locked_node.delayed_required_outputs.append(origin_socket);
1149 void forward_value_to_linked_inputs(
const OutputSocket &from_socket,
1150 GMutablePointer value_to_forward,
1151 CurrentTask ¤t_task,
1152 const LocalData &local_data)
1154 BLI_assert(value_to_forward.get() !=
nullptr);
1155 const CPPType &type = *value_to_forward.type();
1156 const Context local_context{
1157 context_->storage, context_->user_data, local_data.local_user_data};
1159 if (logging_enabled_state_.socket_values) {
1160 self_.logger_->log_socket_value(from_socket, value_to_forward, local_context);
1164 for (
const InputSocket *target_socket : targets) {
1165 const Node &target_node = target_socket->node();
1166 NodeState &node_state = *node_states_[target_node.index_in_graph()];
1167 const int input_index = target_socket->index();
1168 InputState &input_state = node_state.inputs[input_index];
1169 const bool is_last_target = target_socket == targets.last();
1171 if (input_state.value !=
nullptr) {
1172 if (self_.logger_ !=
nullptr) {
1173 self_.logger_->dump_when_input_is_set_twice(*target_socket, from_socket, local_context);
1178 BLI_assert(!input_state.was_ready_for_execution);
1180 BLI_assert(target_socket->origin() == &from_socket);
1182 if (logging_enabled_state_.socket_values) {
1183 self_.logger_->log_socket_value(*target_socket, value_to_forward, local_context);
1185 if (target_node.is_interface()) {
1187 const int graph_output_index =
1188 self_.graph_output_index_by_socket_index_[target_socket->index()];
1189 if (graph_output_index != -1 &&
1190 params_->get_output_usage(graph_output_index) != ValueUsage::Unused)
1192 void *dst_buffer = params_->get_output_data_ptr(graph_output_index);
1193 if (is_last_target) {
1194 type.move_construct(value_to_forward.get(), dst_buffer);
1197 type.copy_construct(value_to_forward.get(), dst_buffer);
1199 params_->output_set(graph_output_index);
1203 this->with_locked_node(
1204 target_node, node_state, current_task, local_data, [&](LockedNode &locked_node) {
1205 if (input_state.usage == ValueUsage::Unused) {
1208 if (is_last_target) {
1210 this->forward_value_to_input(
1211 locked_node, input_state, value_to_forward, current_task);
1212 value_to_forward = {};
1215 void *buffer = local_data.allocator->allocate(type);
1216 type.copy_construct(value_to_forward.get(), buffer);
1217 this->forward_value_to_input(locked_node, input_state, {type, buffer}, current_task);
1221 if (value_to_forward.get() !=
nullptr) {
1222 value_to_forward.destruct();
1226 void forward_value_to_input(LockedNode &locked_node,
1227 InputState &input_state,
1228 GMutablePointer value,
1229 CurrentTask ¤t_task)
1231 NodeState &node_state = locked_node.node_state;
1234 BLI_assert(!input_state.was_ready_for_execution);
1235 input_state.value = value.get();
1237 if (input_state.usage == ValueUsage::Used) {
1238 node_state.missing_required_inputs -= 1;
1239 if (node_state.missing_required_inputs == 0 ||
1240 (locked_node.node.is_function() &&
static_cast<const FunctionNode &
>(locked_node.node)
1242 .allow_missing_requested_inputs()))
1244 this->schedule_node(locked_node, current_task,
false);
1249 bool use_multi_threading()
const
1251 return task_pool_.load() !=
nullptr;
1254 bool try_enable_multi_threading()
1262 if (this->use_multi_threading()) {
1265#ifdef FN_LAZY_FUNCTION_DEBUG_THREADS
1268 if (current_main_thread_ != std::this_thread::get_id()) {
1273 if (!params_->try_enable_multi_threading()) {
1280 this->ensure_thread_locals();
1285 void ensure_thread_locals()
1287#ifdef FN_LAZY_FUNCTION_DEBUG_THREADS
1288 if (current_main_thread_ != std::this_thread::get_id()) {
1292 if (!thread_locals_) {
1293 thread_locals_ = std::make_unique<threading::EnumerableThreadSpecific<ThreadLocalStorage>>();
1300 void push_all_scheduled_nodes_to_task_pool(CurrentTask ¤t_task)
1303 std::unique_ptr<ScheduledNodes> scheduled_nodes = std::make_unique<ScheduledNodes>();
1305 std::lock_guard
lock{current_task.mutex};
1306 if (current_task.scheduled_nodes.is_empty()) {
1309 *scheduled_nodes = std::move(current_task.scheduled_nodes);
1310 current_task.has_scheduled_nodes.store(
false, std::memory_order_relaxed);
1312 this->push_to_task_pool(std::move(scheduled_nodes));
1315 void push_to_task_pool(std::unique_ptr<ScheduledNodes> scheduled_nodes)
1322 GenericGraphExecutor &executor = *static_cast<GenericGraphExecutor *>(
1323 BLI_task_pool_user_data(pool));
1324 ScheduledNodes &scheduled_nodes = *static_cast<ScheduledNodes *>(data);
1325 CurrentTask new_current_task;
1326 new_current_task.scheduled_nodes = std::move(scheduled_nodes);
1327 new_current_task.has_scheduled_nodes.store(true, std::memory_order_relaxed);
1328 const LocalData local_data = executor.get_local_data();
1329 executor.run_task(new_current_task, local_data);
1331 scheduled_nodes.release(),
1333 [](
TaskPool * ,
void *data) { delete static_cast<ScheduledNodes *>(data); });
1338 if (!this->use_multi_threading()) {
1339 return {&main_allocator_, context_->local_user_data};
1341 ThreadLocalStorage &local_storage = thread_locals_->local();
1342 if (!local_storage.local_user_data.has_value()) {
1343 local_storage.local_user_data = context_->user_data->get_local(local_storage.allocator);
1345 return {&local_storage.allocator, local_storage.local_user_data->get()};