C++ fatal error C1001 : An internal error has occurred in the compiler with openMP

标签 c++ visual-studio visual-c++ openmp

我有一个解决数独谜题的程序,我已经让它按顺序运行,但现在我正尝试使用 openMP 并行化它。函数 solvePuzzle() 包含算法,我想在其中并行化 for 循环,但是当我在 for 循环之前添加 #pragma omp parallel for 语句时,出现此错误: fatal error C1001:编译器中发生内部错误。

函数的代码是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;
    }

如果有帮助,这里是 main 的驱动程序:

#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;
}

构建时的完整错误:

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 ==========

最佳答案

总而言之,问题是从并行化的 for 循环内部返回。

#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/

因此解决方案是让循环遍历所有迭代(另请参见 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

关于C++ fatal error C1001 : An internal error has occurred in the compiler with openMP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49540292/

相关文章:

asp.net - 从命令行为 Visual Studio 网站项目运行相当于 "Build Page"命令

c++ - 为什么我的 do 循环运行了几次并且不要求输入?

c++ - 在一个线程中等待 2 个不同的事件

visual-studio - 网络客户端问题 : Expectation failed?

ios - ProjectName.iOS 无法注册程序集 'OpenTK-1.0'

c++ - 为什么析构函数挂了

c++ - 无法从对 main() 的函数调用正确返回值

c++ - 使用 C 或 C++ 以八进制形式使用 sscanf 在 char 数组中发出读取

c++ - Python 中 C++ 编译器标志的等效功能是什么?

C++ operator+ 的简单重载不编译