MCPcopy Create free account
hub / github.com/a2flo/floor / execute_device

Method execute_device

src/device/host/host_function.cpp:1052–1166  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1050}
1051
1052void host_function::execute_device(const host_function_entry& func_entry,
1053 const uint32_t& cpu_count,
1054 const uint3& group_dim,
1055 const uint3& local_dim,
1056 const uint32_t& work_dim,
1057 const std::vector<const void*>& vptr_args) const {
1058 // #work-groups
1059 const auto group_count = group_dim.x * group_dim.y * group_dim.z;
1060 // #work-items per group
1061 const uint32_t local_size = local_dim.x * local_dim.y * local_dim.z;
1062 // group ticketing system, each worker thread will grab a new group id, once it's done with one group
1063 std::atomic<uint32_t> group_idx { 0 };
1064
1065 // start worker threads
1066#if defined(FLOOR_HOST_FUNCTION_ENABLE_TIMING)
1067 const auto time_start = floor_timer::start();
1068#endif
1069 std::atomic<bool> success { true };
1070 std::vector<std::unique_ptr<std::thread>> worker_threads(cpu_count);
1071 for (uint32_t cpu_idx = 0; cpu_idx < cpu_count; ++cpu_idx) {
1072 worker_threads[cpu_idx] = std::make_unique<std::thread>([this, &success, cpu_idx, &func_entry, &vptr_args,
1073 &group_idx, group_count, group_dim,
1074 local_size, local_dim, work_dim] {
1075 // set CPU affinity for this thread to a particular CPU to prevent this thread from being constantly moved/scheduled
1076 // on different CPUs (starting at index 1, with 0 representing no affinity)
1077 set_thread_affinity(cpu_idx + 1);
1078
1079 // retrieve the instance for this CPU + reset/init it
1080 auto instance = func_entry.program->get_instance(cpu_idx);
1081 if (!instance) {
1082 log_error("no instance for CPU #$ (for $)", cpu_idx, function_name);
1083 success = false;
1084 return;
1085 }
1086 instance->reset(local_dim * group_dim, local_dim, group_dim, work_dim);
1087
1088 auto& exec_ctx = device_exec_context;
1089 exec_ctx.ids = &instance->ids;
1090 exec_ctx.linear_local_work_size = local_size;
1091 auto& ids = instance->ids;
1092
1093 // get and set the function for this instance
1094 const auto& func_info = *func_entry.info;
1095 const auto func_iter = instance->functions.find(func_info.name);
1096 if (func_iter == instance->functions.end()) {
1097 log_error("failed to find function \"$\" for CPU #$", function_name, cpu_idx);
1098 success = false;
1099 return;
1100 }
1101 exec_ctx.func = make_callable_host_function(func_iter->second, vptr_args);
1102 if (!exec_ctx.func) {
1103 log_error("failed to create function \"$\" for CPU #$", function_name, cpu_idx);
1104 success = false;
1105 return;
1106 }
1107
1108 // init contexts (aka fibers)
1109 floor_fiber_context main_ctx;

Callers

nothing calls this directly

Calls 9

set_thread_affinityFunction · 0.85
run_execFunction · 0.85
get_instanceMethod · 0.80
findMethod · 0.80
resetMethod · 0.45
endMethod · 0.45
initMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected