OpenMP base Use

Source: Internet
Author: User

OpenMP is the CPU parallel acceleration-related compilation processing scheme, which is supported by the VS very early version, but is turned off by default. To enable this support, you only need to set it in the properties of the project, with the following options: Configuration Properties->c/c++-> The language has an "OpenMP support" in the list on the right, and the dropdown selects "Yes (/OPENMP)".
In fact, I was in the last few days to know that there is this thing exists, friends and I said this thing with the normal open multi-threaded to be more convenient, after the study found that it is indeed convenient place. Well, below or write it how to use it, here just write the most basic, of course, the back of the relevant will be added.

After selecting the options above, you can directly use the "#pragma omp parallel for" to mark the For loop acceleration in your code, as follows:

#include <stdio.h> #include <time.h> #include <stdlib.h>   void Test ()//pure waste of time {    int add = 0;    for (int runtime = 0; runtime < 100000000; runtime++)        add++;    printf ("%d\n", add);} void Main () {    int beginclock = Clock ();//record start time #pragma omp parallel for for    (int testtime = 0; testtime<8; testt ime++)    {        test ();//Run Calculation    }    printf ("Run Time:%dms\n", clock ()-beginclock);//Output image processing takes time information      system ("pause");
in my four core eight thread i7 CPU, its running time is 261ms, if delete "#pragma omp parallel for", then the operation takes time 1816ms, the difference is nearly seven times times, and if the number of cycles in main is changed to 9 times , it takes time to become 477ms, which can be seen to be processed using the most multithreaded number (eight threads) that the CPU supports.
of course, many of the code that needs to be parallelized can be painful to write with for, and then use "#pragma omp parallel sections" to mark the code that will be executed in parallel, and then use the "#pragma omp section". Indicate the execution code for each thread, as shown in the following example:

#include <stdio.h> #include <time.h> #include <stdlib.h>   void Test (int time)//a purely wasted period {    int add = 0;    for (int runtime = 0; runtime < 100000000; runtime++)        add++;    printf ("%d\n", Time);} void Main () {    int beginclock = Clock ();//Record start time/    * Parallel contents */#pragma omp parallel sections    {        #pragma omp Section        {            test (1);        }        #pragma omp section        {            test (2);        }        #pragma omp section        {            test (3);        }        #pragma omp section        {            test (4);        }    }    /* Parallel content */    printf ("Run Time:%dms\n", clock ()-beginclock);//Output image processing takes time information      system ("Pause");}
Run time is 211ms, also faster than a single-threaded run to achieve parallel acceleration.
The above mentioned is the two most basic use of OpenMP, of course, its function far more than these, such as how to avoid the memory operation conflict, if useful to put it back.



OpenMP base Use

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.