Compiling OpenMP program with Qt: HelloWorld + dual loop + cyclic test
Compiling OpenMP program with Qt: HelloWorld
This program is compiled in Ubuntu Linux. The C ++ and C Programs are included here. The two are slightly different in the parameters set in the Qt project.
OpenMp provides parallel descriptions to make full use of the CPU resources of computer systems.
Step 1. Create a C ++ project for Qt. The project file is as follows:
TEMPLATE = app
CONFIG + = console c ++ 11
CONFIG-= app_bundle
# CONFIG-= qt
QMAKE_CXXFLAGS + =-fopenmp
LIBS + =-fopenmp
SOURCES + = main. cpp
Note the line "CONFIG-= qt. Add the following two rows at the same time (as shown above ):
QMAKE_CXXFLAGS + =-fopenmp
LIBS + =-fopenmp
Step2. compile the C ++ source program as follows:
# Include <iostream>
Using namespace std;
Int main ()
{
# Pragma omp parallel
For (char I = 'a'; I <= 'Z'; I ++)
Cout <"Hello World! "<I <endl;
Return 0;
}
In the program, add the following statement before the for statement to parallelize the for Loop:
# Pragma omp parallel
Step3. run the program
The running result is as follows (it can be seen that the output sequence is different from that of the general program because the loop runs concurrently ):
Hello World! A
Hello World! B
Hello World! C
Hello World! D
Hello World! E
Hello World! F
Hello World! G
Hello World! O
Hello World! P
Hello World! Q
Hello World! R
Hello World! S
Hello World! T
Hello World! U
Hello World! V
Hello World! W
Hello World! X
Hello World! Y
Hello World! Z
Hello World! H
Hello World! I
Hello World! J
Hello World! K
Hello World! L
Hello World! M
Hello World! N
The screenshot of the running result is as follows:
Qt project and C language source code:
1. Qt project file
TEMPLATE = app
CONFIG + = console c ++ 11
CONFIG-= app_bundle
# CONFIG-= qt
QMAKE_CFLAGS + =-fopenmp
LIBS + =-fopenmp
SOURCES + = main. c
Note that the parameter here is QMAKE_CFLAGS (the C ++ project is QMAKE_CXXFLAGS ).
2. C Language Program
# Include <stdio. h>
Int main (void)
{
Char I;
# Pragma omp parallel
For (I = 'a'; I <= 'Z'; I ++)
Printf ("Hello World! % C \ n ", I );
Return 0;
}
Compiling OpenMP program with Qt -- Dual Loop
This program is compiled in Ubuntu Linux. OpenMp provides parallel descriptions to make full use of the CPU resources of computer systems.
Qt project and C language source code:
1. Qt project file
TEMPLATE = app
CONFIG + = console c ++ 11
CONFIG-= app_bundle
# CONFIG-= qt
QMAKE_CFLAGS + =-fopenmp
LIBS + =-fopenmp
SOURCES + = \
Main. c
2. C Language Program
# Include <stdio. h>
# Include <omp. h>
Int main (void)
{
Int I;
# Pragma omp parallel for num_threads (4)
For (I = 0; I <4; I ++ ){
Int j;
For (j = 0; j <4; j ++)
Printf ("(% d, % d) Thread num = % d \ n", I, j, omp_get_thread_num ());
}
Return 0;
}
3. Running result
It can be seen that the output sequence during running is different from the general output sequence, indicating that it is run concurrently. The thread number is also output.
Compiling OpenMP program with Qt -- cyclic testing
This program is compiled in Ubuntu Linux. OpenMp provides parallel descriptions to make full use of the CPU resources of computer systems.
Qt project and C ++ source program:
1. Qt project file
TEMPLATE = app
CONFIG + = console c ++ 11
CONFIG-= app_bundle
# CONFIG-= qt
QMAKE_CXXFLAGS + =-fopenmp
LIBS + =-fopenmp
SOURCES + = \
Main. cpp
2. C ++ language programs
# Include <omp. h>
# Include <stdio. h>
Void test (int n ){
For (int I = 0; I <1000000; ++ I ){
// Do nothing, just waste time
}
Printf ("% d", n );
}
Int main (void)
{
# Pragma omp parallel
For (int I = 0; I <20; ++ I)
Test (I );
Printf ("\ n ");
Return 0;
}
3. Running result
It can be seen that the output sequence during running is different from the general output sequence, indicating that it is run concurrently.
This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151536.htm