本人在开发项目过程中发现openmp并行处理std::vector<bool>赋值时会存在问题,一开始感觉非常奇怪,以下是本人的一开始代码(赋值存在问题)
int num_crop_cols = 9;
int num_crop_rows = 7;
std::vector<bool>detect_rect_left_old(num_crop_rows*num_crop_cols);
std::vector<bool>detect_rect_up_old(num_crop_rows*num_crop_cols);
for (int i = 0; i < num_crop_rows; i++)
{
bool left = false;
bool up = false;
if (0 == i)
{
up = true;
}
else
{
up = false;
}
for (size_t j = 0; j < num_crop_cols; j++)
{
if (0 == j)
{
left = true;
}
else
{
left = false;
}
detect_rect_left_old[i*num_crop_cols + j] = left;
detect_rect_up_old[i*num_crop_cols + j] = up;
}
}
int sum = 0;
while (true)
{
//切图详情
/*std::cout << "detect_rect.size() = " << detect_rect.size() << std::endl;*/
std::vector<bool>detect_rect_left(num_crop_rows*num_crop_cols);
std::vector<bool>detect_rect_up(num_crop_rows*num_crop_cols);
{
#if USE_OPENMP
#pragma omp parallel for num_threads(THREAD_NUM)
#endif
for (int i = 0; i < num_crop_rows; i++)
{
bool up = false;
bool left = false;
if (0 == i)
{
up = true;
}
else
{
up = false;
}
for (size_t j = 0; j < num_crop_cols; j++)
{
if (0 == j)
{
left = true;
}
else
{
left = false;
}
detect_rect_left[i*num_crop_cols + j] = left;
detect_rect_up[i*num_crop_cols + j] = up;
}
}
for (size_t i = 0; i < detect_rect_left_old.size(); i++)
{
if (detect_rect_left_old[i] != detect_rect_left[i] || detect_rect_up_old[i] != detect_rect_up[i])
{
std::cout << string("i = ") << i << std::endl;
std::cout << string("detect_rect_left_old[i] != detect_rect_left[i] || detect_rect_up_old[i] != detect_rect_up[i]") << std::endl;
}
}
sum++;
if (0 == sum%1000)
{
std::cout << "sum = " << sum << std::endl;
}
}
}
上面这个循环未开启并行化的处理的赋值,下面这个while循环是开启了并行化处理的赋值,令人感到意外的是这两种方式赋值有非常高的频率出现结果不一致的情况(说来本人也感到奇怪在main函数中赋值结果是一致的,在动态库中结果不一致,个人猜测这可能是因为在main函数中实际未成功开启openmp处理)。后面经过分析发现std::vector<bool>是没有data的函数的,就想起std::vector<bool>在内存中跟std::vector<int>的方式并不一样,具体可以个人百度。所以对代码进行了以下修改来验证。把std::vector<bool>替换成std::vector<int>
int num_crop_cols = 9;
int num_crop_rows = 7;
std::vector<int>detect_rect_left_old(num_crop_rows*num_crop_cols);
std::vector<int>detect_rect_up_old(num_crop_rows*num_crop_cols);
for (int i = 0; i < num_crop_rows; i++)
{
bool left = false;
bool up = false;
if (0 == i)
{
up = true;
}
else
{
up = false;
}
for (size_t j = 0; j < num_crop_cols; j++)
{
if (0 == j)
{
left = true;
}
else
{
left = false;
}
detect_rect_left_old[i*num_crop_cols + j] = left ? 1 : 0;
detect_rect_up_old[i*num_crop_cols + j] = up ? 1 : 0;
}
}
int sum = 0;
while (true)
{
//切图详情
std::vector<int>detect_rect_left(num_crop_rows*num_crop_cols);
std::vector<int>detect_rect_up(num_crop_rows*num_crop_cols);
{
#if USE_OPENMP
#pragma omp parallel for num_threads(THREAD_NUM)
#endif
for (int i = 0; i < num_crop_rows; i++)
{
bool up = false;
bool left = false;
if (0 == i)
{
up = true;
}
else
{
up = false;
}
for (size_t j = 0; j < num_crop_cols; j++)
{
if (0 == j)
{
left = true;
}
else
{
left = false;
}
detect_rect_left[i * num_crop_cols + j] = left ? 1 : 0;
detect_rect_up[i * num_crop_cols + j] = up ? 1 : 0;
}
}
for (size_t i = 0; i < detect_rect_left_old.size(); i++)
{
if (detect_rect_left_old[i] != detect_rect_left[i] || detect_rect_up_old[i] != detect_rect_up[i])
{
std::cout << string("i = ") << i << std::endl;
std::cout << string("detect_rect_left_old[i] != detect_rect_left[i] || detect_rect_up_old[i] != detect_rect_up[i]") << std::endl;
}
}
sum++;
if (0 == sum % 1000)
{
std::cout << "sum = " << sum << std::endl;
}
}
}
这次的结果就一致。在openmp并行化处理中操作(获取和赋值)std::vector<bool>类型的变量是非常危险的。