@@ -268,7 +268,8 @@ std::unique_ptr<Node> find_best_split(const std::vector<flat_set>& sorted_sets,
268268 int split_step,
269269 size_t min_node_size,
270270 const Data* data,
271- std::vector<std::vector<double >>& sum_array) {
271+ std::vector<std::vector<double >>& sum_array,
272+ const std::function<void ()>& interrupt_handler) {
272273 if (level == 0 ) {
273274 // this base case will only be hit if `find_best_split` is called directly with level = 0
274275 return level_zero_learning (sorted_sets, data);
@@ -291,6 +292,9 @@ std::unique_ptr<Node> find_best_split(const std::vector<flat_set>& sorted_sets,
291292 int split_counter = 0 ;
292293 size_t samples_counter = 0 ;
293294 for (size_t n = 0 ; n < num_points - 1 ; n++) {
295+ if ((n & 1023 ) == 0 ) {
296+ interrupt_handler (); // check for ctrl-c interrupt every 1024 iterations
297+ }
294298 auto point = right_sorted_sets[p].cbegin (); // O(1)
295299 Point point_bk = *point; // store the Point instance since the iterator will be invalid after erase
296300 right_sorted_sets[p].erase (point); // O(1)
@@ -317,8 +321,8 @@ std::unique_ptr<Node> find_best_split(const std::vector<flat_set>& sorted_sets,
317321 } else {
318322 continue ;
319323 }
320- auto left_child = find_best_split (left_sorted_sets, level - 1 , split_step, min_node_size, data, sum_array);
321- auto right_child = find_best_split (right_sorted_sets, level - 1 , split_step, min_node_size, data, sum_array);
324+ auto left_child = find_best_split (left_sorted_sets, level - 1 , split_step, min_node_size, data, sum_array, interrupt_handler );
325+ auto right_child = find_best_split (right_sorted_sets, level - 1 , split_step, min_node_size, data, sum_array, interrupt_handler );
322326 if ((best_left_child == nullptr ) ||
323327 (left_child->reward + right_child->reward >
324328 best_left_child->reward + best_right_child->reward )) {
@@ -350,7 +354,8 @@ std::unique_ptr<Node> find_best_split(const std::vector<flat_set>& sorted_sets,
350354 }
351355}
352356
353- std::unique_ptr<Node> tree_search (int depth, int split_step, size_t min_node_size, const Data* data) {
357+ std::unique_ptr<Node> tree_search (int depth, int split_step, size_t min_node_size, const Data* data,
358+ const std::function<void ()>& interrupt_handler) {
354359 size_t num_rewards = data->num_rewards ();
355360 size_t num_points = data->num_rows ;
356361 auto sorted_sets = create_sorted_sets (data);
@@ -362,5 +367,5 @@ std::unique_ptr<Node> tree_search(int depth, int split_step, size_t min_node_siz
362367 v.resize (num_points + 1 , 0.0 );
363368 }
364369
365- return find_best_split (sorted_sets, depth, split_step, min_node_size, data, sum_array);
370+ return find_best_split (sorted_sets, depth, split_step, min_node_size, data, sum_array, interrupt_handler );
366371}
0 commit comments