Use minggw [GCC + OpenMP] And codeblocks in Windows to develop basic environment configurations for multi-core applications

Source: Internet
Author: User

Starting from, multi-core development has become an issue that must be considered in all application design. I use minggw + codeblocks to test the OpenMP multi-core computing framework. Although the support for OpenMP is already available in vc8, the VC size is a bit large, and it is well known that the MS compiler has compatibility issues, so we decided to use standard GCC for development.

OpenMP is only a library for parallel development. In essence, supporting multi-core development is to convert a single-threaded program or algorithm into multi-threaded execution. OpenMP adopts the annotation method similar to Java to automatically convert tasks into multiple threads. For example, if your machine is dual-core, A 10000 for loop is automatically divided into two 5000 cycles. If your machine is quad-core, it is automatically divided into four 2500 cycles.

 

1. Install minggw

Currently, the most stable version of GCC is 3.4.5. In addition, OpenMP is supported by 4.3.0 by default. Therefore, it is best to fashion two versions on the machine.

In Windows, GCC can be used to use minggw and cygwin. I personally think cygwin is too large, so minggw is used.

In https://sourceforge.net/project/showfiles.php? Group_id = download minggw from 2435

For gcc3, download the following packages to the C:/mingw3 directory and decompress the package to the current directory.

Mingw-runtime-3.9.tar.gz

Gcc-core-3.4.5-20060117-3.tar.gz

Gcc-g00000000-3.4.5-20060117-3.tar.gz

Gdb-6.8-mingw-3.tar.bz2

W32api-3.12-mingw32-dev.tar.gz

Binutils-2.19-mingw32-rc1-bin.tar.gz

Gdb-6.8-mingw-3.tar.bz2

 

However, to develop OpenMP, we need to use gcc4 again. It generally includes the following packages. We can see from the bold Section that the biggest difference is the version of the GCC core library. Download the package to the C:/mingw4 directory and decompress it to the current directory.

Mingw-runtime-3.9.tar.gz

Gcc-4.3.0-20080502-mingw32-alpha-bin.tar.gz

Gdb-6.8-mingw-3.tar.bz2

W32api-3.12-mingw32-dev.tar.gz

Binutils-2.19-mingw32-rc1-bin.tar.gz

Gdb-6.8-mingw-3.tar.bz2

 

After minggw is installed, add C:/minggw/bin to the PATH variable to use GCC in the command line.

 

2 codeblocks

This is the open-source ide recommended in the official minggw wiki. I tried it and there is no problem with basic compilation and debugging.

Please download http://www.codeblocks.org/downloads/5 at the following address

 

After installation, you can switch to different GCC versions in the toolchain executables in the menu settings> compiler and debugging settings.

 

3. Use OpenMP

To enable the compiler to compile OpenMP, first use C:/mingw4. in the preceding configuration.

Then enter-fopenmp in other options in Compiler settings.

Enter-lgomp-lpthread in other Linker Options in linker settings.

If you compile it in the command line, you can use the command

G ++-fopenmp main. cpp-lgomp-lpthread-O main.exe

 

Note that if some errors cannot be referenced, the-lgomp-lpthread may not be added.

 

 

Okay. Now we can develop the OpenMP application. The following are some examples, all of which are online, but I have slightly changed them:

  1. # Include <stdio. h>
  2. # Include <OMP. h>
  3. # Include <time. h>
  4. Using namespace STD;
  5. Void eg_print ()
  6. {
  7. # Pragma OMP parallel
  8. Printf ("[% d] Hello/N", omp_get_thread_num ());
  9. }
  10. Void eg_for ()
  11. {
  12. # Pragma OMP parallel
  13. For (INT I = 0; I <10; I ++)
  14. {
  15. Printf ("I = % d/N", I );
  16. }
  17. }
  18. Void eg_long_for ()
  19. {
  20. Int C = 0;
  21. Clock_t T1 = clock ();
  22. For (INT I = 0; I <1000000000; I ++)
  23. {
  24. C ++;
  25. }
  26. Clock_t t2 = clock ();
  27. Printf ("Count = % d, T2 = % d, T1 = % d, time = % d/N", C, T2, T1, t2-t1 );
  28. }
  29. Int main ()
  30. {
  31. // Eg_print ();
  32. // Eg_for ();
  33. // Test
  34. Clock_t T1 = clock ();
  35. # Pragma OMP parallel
  36. For (Int J = 0; j <2; j ++)
  37. {
  38. Eg_long_for ();
  39. }
  40. Clock_t t2 = clock ();
  41. Printf ("Total time = % d/N", t2-t1 );
  42. Eg_long_for ();
  43. Return 0;
  44. }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.