Compared with Python multi-threaded can not operate multicore, Java multithreading will only blindly occupy all the kernel count. With the support of OpenMP in Visual Studio 2005, C + + provides more precise control over how many cores to compute, and is simple to use, to truly multi-core multithreaded programming. Compared to Python, Java multithreading can not substantially improve the program speed, C + + 's OpenMP from the CPU number of cores to improve program speed. As we all know, now dual-core, i3, i5, i7 are multiple CPU parallel, we write out the program, if not set up, is not available to all CPUs, and only with single-core computing, for the quad-core CPU computer, the rest of the core is idle, not to mention the high-performance computer. Using OpenMP can be a very high solution.
First, the version of Visual Studio 2005 or above opens OpenMP
Take vs2010 as an example, after you create a new/C + + project (a project in another language is not available), after you create a new CPP file in the source file, click Properties on the project, and then on the left side of the property page, select "Configuration Properties", "C + +" language, and then on the right " OpenMP supports "After Select is (/OPENMP)", as shown in:
It is important to note that if you do not add a CPP file to the source file after you create a new project, it is not configured in C + +.
Second, the HelloWorld of OpenMP
This allows you to use OpenMP on your project. Just fill in the # pragma omp parallel num_threads (the number of CPU cores participating in the calculation) where you want multicore calculations, and you can use OpenMP to extract the CPU of the corresponding cores. Note that #与pragma之间是否有空格, the relationship is small, and the variable declaration can not be used before this sentence, or can not be compiled. If it is before for, write: # pragma omp parallel for num_threads (number of CPU cores participating in the calculation). For example, the following programs:
#include <omp.h> #include <iostream> #include <time.h>using namespace std;void hello (int i) {if (i==0) { # pragma omp parallel num_threads (2) cout<< "hello!";} else{# pragma omp parallel num_threads (4) cout<< "hello!";} cout<<endl<< "finish!" <<endl;} int main () {hello (0); hello (1); return 0;}
The results of the operation are as follows:
The Hello function enters a different loop depending on the parameters obtained. # pragma omp parallel num_threads (the number of CPU cores participating in the calculation) is limited to its appearance until the end of the struct, i.e. to the first closing brace}. When the number of CPU cores participating in the calculation is 2 and 4, the output is 2 times and 4 times respectively. Each CPU is to complete the cout<< "Hello" statement. However finish! Output has only one CPU to participate, so each hello execution has only one.
You can use multi-threaded concept to understand this thing, but it is different multithreading is only the same resources under the time slice operation, OpenMP is the real CPU to participate in the calculation.
It is important to note that if your computer has only 2 cores of CPU, the number of CPU cores participating in the calculation can only be 1, 2, and not more than the CPU's core count.
Third, what is the meaning of OpenMP?
The above example may be said that there is no meaning at all, it is possible to use a For loop to finish the thing, do not want to pull on OpenMP do?
So let's do a 0 to 2,000,000,000 (4 billion) self-increment operation to illustrate the problem and record the elapsed time.
#include <iostream> #include <time.h>using namespace Std;int main () {clock_t t_start=clock (); for (int i=0;i <2000000000;i++) {}time_t t_end = clock ();cout<< "Run Time:" << (Double) (T_end-t_start)/clocks_per_sec& lt;< "S" <<endl;return 0;}
If you do not use OpenMP, on my computer, the above program run time is as follows:
The CPU usage during the run is as follows:
You can see that the four-core CPU is occupied by only 1 CPUs, with a reference usage of 25%!
If OpenMP is used, the number of CPU cores required to participate in the operation is 2:
#include <omp.h> #include <iostream> #include <time.h>using namespace Std;int main () {clock_t t_start= Clock (); # pragma omp parallel for num_threads (2) for (int i=0;i<2000000000;i++) {}time_t t_end = clock ();cout<< " Run Time: "<< (Double) (t_end-t_start)/clocks_per_sec<<" S "<<endl;return 0;}
The run time is reduced by almost one-fold:
The four-core CPU utilization during the run also went to 50%:
So what if you set up a CPU running with four cores?
#include <omp.h> #include <iostream> #include <time.h>using namespace Std;int main () {clock_t t_start= Clock (); # pragma omp parallel for num_threads (4) for (int i=0;i<2000000000;i++) {}time_t t_end = clock ();cout<< " Run Time: "<< (Double) (t_end-t_start)/clocks_per_sec<<" S "<<endl;return 0;}
The answer is also natural, running time:
CPU Usage:
So, as you can see, if your program is going to perform a large-scale operation, using OpenMP can completely run out of CPU resources and increase the speed of your program!
"OpenMP" Helloworld