通过引用传递的std :: vector上的OpenMP减少问题

时间:2018-05-09 01:28:19

标签: c++ openmp intel gnu reduction

英特尔编译器中有一个关于OpenMP中用户定义的减少的错误,已经讨论过here(包括wrokaround)。现在我想将向量传递给函数并执行相同的操作但是我收到此错误:

terminate called after throwing an instance of 'std::bad_alloc'
what():  std::bad_alloc
Aborted

这是一个例子:

#include <iostream>
#include <vector>
#include <algorithm>
#include "omp.h"

#pragma omp declare reduction(vec_double_plus : std::vector<double> : \
                              std::transform(omp_out.begin(), omp_out.end(), omp_in.begin(), omp_out.begin(), std::plus<double>())) \
                    initializer(omp_priv = omp_orig)

int foo(std::vector<double> &w){

#pragma omp parallel reduction(vec_double_plus:w)
    {
#pragma omp for
        for (int i = 0; i < 2; ++i)
            for (int j = 0; j < w.size(); ++j)
                w[j] += 1;
    };

    return 0;
}

int main() {

    omp_set_num_threads(2);
    std::vector<double> w(10,0);
    foo(w);

    for(auto i:w)
        if(i != 2)
            std::cout << i << std::endl;

    return 0;
}

再次,它适用于GNU / 6.4.0但无法使用intel / 2018.1.163。有什么想法吗?

更新:我更改了值以便于调试。我在远程节点上工作,所以我正在使用终端。我使用gdb来调试用intel / 2018.1.163编译的代码。我不确定这是否是正确的做法,或者是否有更好的方法来调试代码。这是来自gdb的错误:

[New Thread 0x2aaaac68a780 (LWP 15573)]
terminate called recursively
                        terminate called after throwing an instance of 'std::bad_alloc
Program received signal SIGABRT, Aborted.
0x00002aaaabaf91f7 in raise () from /lib64/libc.so.6

而且,这是cmake配置:

cmake_minimum_required(VERSION 3.2)
project(openmp_reduction001)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE Debug)

find_package(OpenMP)
if(OPENMP_FOUND)
    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

add_executable(openmp_reduction001 main.cpp)

Update2:以下添加了gdb中的回溯结果。我使用的计算节点将intel编译器模块作为默认编译器加载,但它查找头文件的 / usr / include / c ++ / 4.8.5 。这是正常的吗?我查看了 / usr / include / c ++ / 。它只包括4.4.7,4.8.2,4.8.5文件夹。另一个问题是#12 ,其中向量的长度为 -15 ,这可能导致std :: allocator设置其 n 参数非常大。

#0  0x00002aaaabaf91f7 in raise () from /lib64/libc.so.6
#1  0x00002aaaabafa8e8 in abort () from /lib64/libc.so.6
#2  0x00002aaaaad2fa55 in __gnu_cxx::__verbose_terminate_handler() () from /lib64/libstdc++.so.6
#3  0x00002aaaaad2da36 in ?? () from /lib64/libstdc++.so.6
#4  0x00002aaaaad2da63 in std::terminate() () from /lib64/libstdc++.so.6
#5  0x00002aaaaad2dc83 in __cxa_throw () from /lib64/libstdc++.so.6
#6  0x00002aaaaad826d2 in std::__throw_bad_alloc() () from /lib64/libstdc++.so.6
#7  0x0000000000404022 in __gnu_cxx::new_allocator<double>::allocate (this=0x2aaaac689af0, __n=18446744073709551602)
    at /usr/include/c++/4.8.5/ext/new_allocator.h:102
#8  0x0000000000403856 in std::_Vector_base<double, std::allocator<double> >::_M_allocate (this=0x2aaaac689af0, __n=18446744073709551602)
    at /usr/include/c++/4.8.5/bits/stl_vector.h:168
#9  0x000000000040394b in std::_Vector_base<double, std::allocator<double> >::_M_create_storage (this=0x7fffffffa370, __n=18446744073709551601)
    at /usr/include/c++/4.8.5/bits/stl_vector.h:181
#10 0x00000000004037a6 in std::_Vector_base<double, std::allocator<double> >::_Vector_base (this=0x7fffffffa370, __n=18446744073709551601, __a=...)
    at /usr/include/c++/4.8.5/bits/stl_vector.h:136
#11 0x00000000004037fa in std::_Vector_base<double, std::allocator<double> >::_Vector_base (this=0x7fffffffa370)
    at /usr/include/c++/4.8.5/bits/stl_vector.h:134
#12 0x0000000000403b15 in std::vector<double, std::allocator<double> >::vector (this=0x7fffffffa370, 
    __x=std::vector of length -15, capacity -17592185515333 = {...}) at /usr/include/c++/4.8.5/bits/stl_vector.h:312
#13 0x0000000000402cd3 in __udr_i_0x914e698 (__omp_priv=0x7fffffffa370, __omp_orig=0x7fffffffa838)
    at /uufs/chpc.utah.edu/common/home/u1013493/openmp_reduction001/main.cpp:8
#14 0x0000000000402e7d in L__Z3fooRSt6vectorIdSaIdEE_14__par_region0_2_4 () at /uufs/chpc.utah.edu/common/home/u1013493/openmp_reduction001/main.cpp:14
#15 0x00002aaaab39e7a3 in __kmp_invoke_microtask ()
   from /uufs/chpc.utah.edu/sys/installdir/intel/compilers_and_libraries_2018.1.163/linux/compiler/lib/intel64/libiomp5.so

0 个答案:

没有答案