问题
I have a program that solves sudoku puzzles and I've gotten it to work sequentially, but now I'm trying to parallelize it using openMP. The function solvePuzzle()
includes the algorithm and I want to parallelize the for loop within it however when I add #pragma omp parallel for
statement before my for loop i get this error: fatal error C1001: An internal error has occurred in the compiler.
The code for the function is solvePuzzle()
:
bool sudoku::solvePuzzle(int grid[CELL][CELL]) {
int row, col;
if (!findEmptyCell(grid, row, col))
return true;
#pragma omp parallel for
for (int num = 1; num <= 9; num++) {
if (checkAccuracy(grid, row, col, num)) {
grid[row][col] = num;
if (solvePuzzle(grid))
return true;
grid[row][col] = EMPTY_CELL;
}
}
return false;
}
Here is the driver with main if it helps:
#include "SudokuGrid.h"
using namespace std;
int main() {
sudoku sudoku;
clock_t t1, t2;
t1 = clock();
if (sudoku.readPuzzle("1.txt")) {
sudoku.printGrid(sudoku.grid);
}
else {
cout << "Incorrect file" << endl;
}
cout << endl;
if (sudoku.solvePuzzle(sudoku.grid) == true)
sudoku.printGrid(sudoku.grid);
else
printf("No solution exists");
t2 = clock();
printf("Time to execute = %1d ms\n", (t2 - t1));
return 0;
}
Full error when built:
1>------ Build started: Project: Sudoku, Configuration: Debug Win32 ------
1>SudokuGrid.cpp
1>c:\users\john\source\repos\sudoku\sudoku\sudokugrid.cpp(8): fatal error
C1001: An internal error has occurred in the compiler.
1>(compiler file 'f:\dd\vctools\compiler\utc\src\p2\main.c', line 258)
1> To work around this problem, try simplifying or changing the program near
the locations listed above.
1>Please choose the Technical Support command on the Visual C++
1> Help menu, or open the Technical Support help file for more information
1>
1>Done building project "Sudoku.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
回答1:
To summarize, the problem is returning from inside the parallelized for
loop.
#pragma omp parallel for
for (int num = 1; num <= 9; num++) {
...
if (solvePuzzle(grid))
return true; // BAD
...
}
}
"OpenMP requires that a loop construct processes each iteration. Breaking out of the loop (using return, goto, break, throw or other means) is not allowed."
-- https://bisqwit.iki.fi/story/howto/openmp/
So the solution is to let the loop loop through all iterations (See also Breaking Out of Loops).
bool solvePuzzle(int grid[CELL][CELL]) {
bool solved = false;
int row, col;
if (!findEmptyCell(grid, row, col))
return true;
#pragma omp parallel for
for (int num = 1; num <= 9; num++) {
#pragma omp flush (solved)
if (!solved && checkAccuracy(grid, row, col, num)) {
grid[row][col] = num;
if (solvePuzzle(grid)) {
solved = true;
#pragma omp flush (solved)
} else {
grid[row][col] = EMPTY_CELL;
}
}
}
return solved;
}
Repro
Demo
来源:https://stackoverflow.com/questions/49540292/c-fatal-error-c1001-an-internal-error-has-occurred-in-the-compiler-with-openm