M
M
Mart_Slaaf2014-04-22 01:46:40
linux
Mart_Slaaf, 2014-04-22 01:46:40

Why doesn't thread parallelization work in gcc?

Hello!
In the process of trying to automatically parallelize the execution of the library code, ccv encountered a problem - the lack of multithreading. For example, I made a separate file and tortured it.
1) File

#include <stdio.h>
#define T 1000000
int main() {
    printf("HELO\n");
    long int i = 0;
    long int j = 0;
    for (i = 0; i < T; i++)
        for (j = 0; j < T; j++);
    printf("EHLO\n");
}

2) Command used for compilation:
gcc -floop-parallelize-all -ftree-parallelize-loops=4 hello.c -o ./hello_habr

3) Output in the presence of the -v switch
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 
COLLECT_GCC_OPTIONS='-floop-parallelize-all' '-ftree-parallelize-loops=4' '-o' './hello_habr' '-v' '-mtune=generic' '-march=x86-64' '-pthread'
 /usr/lib/gcc/x86_64-linux-gnu/4.6/cc1 -quiet -v -imultilib . -imultiarch x86_64-linux-gnu -D_REENTRANT hello.c -quiet -dumpbase hello.c -mtune=generic -march=x86-64 -auxbase hello -version -floop-parallelize-all -ftree-parallelize-loops=4 -fstack-protector -o /tmp/ccCHjLGI.s
GNU C (Ubuntu/Linaro 4.6.3-1ubuntu5) version 4.6.3 (x86_64-linux-gnu)
  compiled by GNU C version 4.6.3, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/4.6/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.6/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C (Ubuntu/Linaro 4.6.3-1ubuntu5) version 4.6.3 (x86_64-linux-gnu)
  compiled by GNU C version 4.6.3, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 75e879ed14f91af504f4150eadeaa0e6
COLLECT_GCC_OPTIONS='-floop-parallelize-all' '-ftree-parallelize-loops=4' '-o' './hello_habr' '-v' '-mtune=generic' '-march=x86-64' '-pthread'
 as --64 -o /tmp/ccqL9j7e.o /tmp/ccCHjLGI.s
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.6/:/usr/lib/gcc/x86_64-linux-gnu/4.6/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.6/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.6/:/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../:/lib/:/usr/lib/
Reading specs from /usr/lib/gcc/x86_64-linux-gnu/4.6/libgomp.spec
COLLECT_GCC_OPTIONS='-floop-parallelize-all' '-ftree-parallelize-loops=4' '-o' './hello_habr' '-v' '-mtune=generic' '-march=x86-64' '-pthread'
 /usr/lib/gcc/x86_64-linux-gnu/4.6/collect2 --sysroot=/ --build-id --no-add-needed --as-needed --eh-frame-hdr -m elf_x86_64 --hash-style=gnu -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro -o ./hello_habr /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.6 -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../.. /tmp/ccqL9j7e.o -lgomp -lgcc --as-needed -lgcc_s --no-as-needed -lpthread -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crtn.o

The execution takes more than 10 seconds, while only one core is loaded. From which I boldly concluded that parallelization did not work.
Could you tell me what I'm doing wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
alexanius, 2014-04-22
@Mart_Slaaf

Unfortunately, I currently have gcc built without parallelization support, so I turn on telepathy.
First, read how auto-parallelization works. For example here .
Secondly, there is a suspicion that without the -O3 key, these flags have no meaning.
Third, look at the dumps. For example, by the -fdump-tree-parloops key in this case. And in general, look in the man'e gcc what dumps can be dumped. They are prefixed with -fdump.
If it doesn't work with -O3, or you don't find anything useful in the dumps, then ask in the gcc-help mailing list, they will explain it there.

J
jcmvbkbc, 2014-04-22
@jcmvbkbc

for (i = 0; i < T; i++)
        for (j = 0; j < T; j++);

On such a test example, you will never see the work of the optimizer, because with -O0 the optimizer does not start, and with -O1,2,3 this cycle will be thrown out entirely and replaced with i = T, j = T.

O
oleksandr_veles, 2014-04-22
@oleksandr_veles

Maybe it makes sense to look towards OpenMP?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question