Issue with OpenMP reduction on std::vector passed by reference

瘦欲@ 提交于 2019-12-24 22:18:05

问题


There is a bug in intel compiler on user-defined reduction in OpenMP which was discussed here (including the wrokaround). Now I want to pass the vector to a function and do the same thing but I get this error:

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

This is the example:

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

Again it works fine with GNU/6.4.0 but fails with intel/2018.1.163. Any ideas?

Update: I changed the values to make it easier to debug. I work on a remote node, so I am using terminal. I used gdb to debug the code that was compiled with intel/2018.1.163. I'm not sure if it is the right thing to do, or if there is a better way to debug the code. This is the error from 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

And, this is the cmake configuration:

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: The result of backtrace in gdb is added in the following. The computing node that I used has the intel compiler module loaded as the default compiler, but it looks into /usr/include/c++/4.8.5 for the header files. Is that normal? I looked into /usr/include/c++/. It only includes 4.4.7, 4.8.2, 4.8.5 folders. Another issue is at line #12, in which the length of the vector is -15, which probably causes the std::allocator to set its n parameter a very large number.

#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

来源:https://stackoverflow.com/questions/50244264/issue-with-openmp-reduction-on-stdvector-passed-by-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!