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:
- # Include <stdio. h>
- # Include <OMP. h>
- # Include <time. h>
- Using namespace STD;
- Void eg_print ()
- {
- # Pragma OMP parallel
- Printf ("[% d] Hello/N", omp_get_thread_num ());
- }
- Void eg_for ()
- {
- # Pragma OMP parallel
- For (INT I = 0; I <10; I ++)
- {
- Printf ("I = % d/N", I );
- }
- }
- Void eg_long_for ()
- {
- Int C = 0;
- Clock_t T1 = clock ();
- For (INT I = 0; I <1000000000; I ++)
- {
- C ++;
- }
- Clock_t t2 = clock ();
- Printf ("Count = % d, T2 = % d, T1 = % d, time = % d/N", C, T2, T1, t2-t1 );
- }
- Int main ()
- {
- // Eg_print ();
- // Eg_for ();
- // Test
- Clock_t T1 = clock ();
- # Pragma OMP parallel
- For (Int J = 0; j <2; j ++)
- {
- Eg_long_for ();
- }
- Clock_t t2 = clock ();
- Printf ("Total time = % d/N", t2-t1 );
- Eg_long_for ();
- Return 0;
- }