/* Copyright (c) 2005-2020 Intel Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include "../../common/utility/utility.h" #include "../../common/utility/get_default_num_threads.h" #if __TBB_MIC_OFFLOAD #pragma offload_attribute (push,target(mic)) #endif // __TBB_MIC_OFFLOAD #include #include #include #include #include "tbb/tick_count.h" #include "tbb/task_group.h" #include "tbb/global_control.h" #pragma warning(disable: 4996) const unsigned BOARD_SIZE=81; const unsigned BOARD_DIM=9; using namespace tbb; using namespace std; std::atomic nSols; bool find_one = false; bool verbose = false; unsigned short init_values[BOARD_SIZE] = {1,0,0,9,0,0,0,8,0,0,8,0,2,0,0,0,0,0,0,0,5,0,0,0,7,0,0,0,5,2,1,0,0,4,0,0,0,0,0,0,0,5,0,0,7,4,0,0,7,0,0,0,3,0,0,3,0,0,0,2,0,0,5,0,0,0,0,0,0,1,0,0,5,0,0,0,1,0,0,0,0}; task_group *g; double solve_time; typedef struct { unsigned short solved_element; unsigned potential_set; } board_element; void read_board(const char *filename) { FILE *fp; int input; fp = fopen(filename, "r"); if (!fp) { fprintf(stderr, "sudoku: Could not open input file '%s'.\n", filename); exit(1); } for (unsigned i=0; i=0; --i) if (b[i].solved_element==0) return false; return true; } bool in_row(board_element *b, unsigned row, unsigned col, unsigned short p) { for (unsigned c=0; ccancel(); if (++nSols==1 && verbose) { print_board(b); } free(b); return; } calculate_potentials(b); bool progress=true; bool success = examine_potentials(b, &progress); if (success && progress) { partial_solve(b, first_potential_set); } else if (success && !progress) { board_element *new_board; while (b[first_potential_set].solved_element!=0) ++first_potential_set; for (unsigned short potential=1; potential<=BOARD_DIM; ++potential) { if (1<<(potential-1) & b[first_potential_set].potential_set) { new_board = (board_element *)malloc(BOARD_SIZE*sizeof(board_element)); copy_board(b, new_board); new_board[first_potential_set].solved_element = potential; #if __TBB_CPP11_LAMBDAS_PRESENT g->run( [=]{ partial_solve(new_board, first_potential_set); } ); #else g->run(PartialSolveBoard(new_board, first_potential_set)); #endif } } free(b); } else { free(b); } } unsigned solve(int p) { tbb::global_control c(tbb::global_control::max_allowed_parallelism, p); nSols = 0; board_element *start_board = (board_element *)malloc(BOARD_SIZE*sizeof(board_element)); init_board(start_board, init_values); g = new task_group; tick_count t0 = tick_count::now(); partial_solve(start_board, 0); g->wait(); solve_time = (tick_count::now() - t0).seconds(); delete g; return nSols; } #if __TBB_MIC_OFFLOAD #pragma offload_attribute (pop) #endif // __TBB_MIC_OFFLOAD int main(int argc, char *argv[]) { try { tbb::tick_count mainStartTime = tbb::tick_count::now(); utility::thread_number_range threads(utility::get_default_num_threads); string filename = ""; bool silent = false; utility::parse_cli_arguments(argc,argv, utility::cli_argument_pack() //"-h" option for displaying help is present implicitly .positional_arg(threads,"n-of-threads",utility::thread_number_range_desc) .positional_arg(filename,"filename","input filename") .arg(verbose,"verbose","prints the first solution") .arg(silent,"silent","no output except elapsed time") .arg(find_one,"find-one","stops after finding first solution\n") ); if ( silent ) verbose = false; if ( !filename.empty() ) read_board( filename.c_str() ); // otherwise (if file name not specified), the default statically initialized board will be used. for(int p = threads.first; p <= threads.last; p = threads.step(p) ) { unsigned number; #if __TBB_MIC_OFFLOAD #pragma offload target(mic) in(init_values, p, verbose, find_one) out(number, solve_time) { #endif // __TBB_MIC_OFFLOAD number = solve(p); #if __TBB_MIC_OFFLOAD } #endif // __TBB_MIC_OFFLOAD if ( !silent ) { if ( find_one ) { printf("Sudoku: Time to find first solution on %d threads: %6.6f seconds.\n", p, solve_time); } else { printf("Sudoku: Time to find all %u solutions on %d threads: %6.6f seconds.\n", number, p, solve_time); } } } utility::report_elapsed_time((tbb::tick_count::now() - mainStartTime).seconds()); return 0; } catch(std::exception& e) { std::cerr<<"error occurred. error text is :\"" <