https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104722
Bug ID: 104722
Summary: Wrong std::accumulate result with -D_GLIBCXX_PARALLEL
flag
Product: gcc
Version: og11 (devel/omp/gcc-11)
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: hubert.degaudenzi at gmail dot com
Target Milestone: ---
Created attachment 52529
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52529&action=edit
The C++ to compile to reproduce the error
The following code:
#include <iostream>
#include <vector>
#include <numeric>
int main(int /*argc*/, char* /*argv*/[]) {
const std::size_t val_size_t = 1000;
std::vector<double> vals(val_size_t, 1.0);
std::iota(vals.begin(), vals.end(), 0.0);
auto squared = std::accumulate(vals.begin(), vals.end(), 0.0,
[](const double& tot, const double& vA) {
return tot + vA * vA;
});
std::cout << "Squared: " << squared << std::endl;
double squared2 = 0.0;
for (const auto& v : vals) {
squared2 += v * v;
}
std::cout << "Squared 2: " << squared2 << std::endl;
return 0;
}
Give 2 different values for squared and squared2:
Squared: 2.43875e+16
Squared 2: 3.32834e+08
The strange thing is that it is only happening when:
- the code is compiled with -D_GLIBCXX_PARALLEL -fopenmp
- the value of val_size_t is above or equal to 1000. 999 works and gives the
same value for squared and squared2
My box is a fedora 35 x86_64:
- gcc -v
Utilisation des specs internes.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Cible : x86_64-redhat-linux
Configuré avec: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin
--enable-initfini-array
--with-isl=/builddir/build/BUILD/gcc-11.2.1-20220127/obj-x86_64-redhat-linux/isl-install
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-gnu-indirect-function --enable-cet --with-tune=generic
--with-arch_32=i686 --build=x86_64-redhat-linux
--with-build-config=bootstrap-lto --enable-link-serialization=1
Modèle de thread: posix
Algorithmes de compression LTO supportés: zlib zstd
gcc version 11.2.1 20220127 (Red Hat 11.2.1-9) (GCC)
- uname -a
Linux thor 5.16.11-200.fc35.x86_64 #1 SMP PREEMPT Wed Feb 23 17:08:49 UTC 2022
x86_64 x86_64 x86_64 GNU/Linux
- libstdc++-11.2.1-9.fc35.x86_64
- I compiled the code according to the instructions:
/usr/bin/c++ -D_GLIBCXX_PARALLEL -std=c++14 -Wall -Wextra -fopenmp
-fno-strict-aliasing -fwrapv -fno-aggressive-loop-optimizations
-Wl,--enable-new-dtags -Wl,--as-needed -Wl,--no-undefined -rdynamic -o
Accumulate Accumulate.cpp