Joh*_*ter 0 c++ openmp visual-studio visual-c++
我有一个可以解决数独难题的程序,并且可以按顺序工作,但是现在我正在尝试使用openMP对其进行并行化。该函数solvePuzzle()包括算法,我想在其中并行化for循环,但是当我#pragma omp parallel for在for循环之前添加语句时,出现此错误: fatal error C1001: An internal error has occurred in the compiler.
该函数的代码是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;
}
Run Code Online (Sandbox Code Playgroud)
这是主要驱动程序,如果有帮助的话:
#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;
}
Run Code Online (Sandbox Code Playgroud)
构建时出现完整错误:
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 ==========
Run Code Online (Sandbox Code Playgroud)
总而言之,问题是从并行for循环内部返回的。
#pragma omp parallel for
for (int num = 1; num <= 9; num++) {
...
if (solvePuzzle(grid))
return true; // BAD
...
}
}
Run Code Online (Sandbox Code Playgroud)
“ OpenMP要求循环构造处理每次迭代。不允许中断循环(使用return,goto,break,throw或其他方式)。”
因此,解决方案是让循环遍历所有迭代(另请参见“ 打破循环”)。
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1038 次 |
| 最近记录: |