openmp 中部分函数简介

接下来将考虑对plsa进行并行化,并行化主要包括机器之间的并行和单机多线程的并行,考虑采用omp来实现单机多线程的并行,这样既可以快速验证下算法的效率提升又可以避免把很多时间花在多线程控制上,结合算法特点,需要用到以下函数,在这里简单整理下,更多的函数参考microsoft msdn

1.omp_get_thread_num() function:Returns the thread number of the thread executing within its thread team#include <stdio h=""> #include <omp h=""> int main() { #pragma omp parallel num_threads(4) { int i = omp_get_thread_num(); printf_s("Hello from thread %d\n", i); } }

2.#pragma omp critical() function:Specifies that code is only be executed on one thread at a time.#include <omp h=""> #include <stdio h=""> #include <stdlib h=""> #define SIZE 10 int main() { int i; int max; int a[SIZE]; for (i = 0; i < SIZE; i++) { a[i] = rand(); printf_s("%d\n", a[i]); } max = a[0]; #pragma omp parallel for num_threads(4) for (i = 1; i < SIZE; i++) { if (a[i] > max) { #pragma omp critical { // compare a[i] and max again because max // could have been changed by another thread after // the comparison outside the critical section if (a[i] > max) max = a[i]; } } } printf_s("max = %d\n", max); }

3.#pragma omp parallel function:Defines a parallel region, which is code that will be executed by multiple threads in parallel.

4.#prarma omp for schedule

5.#pragma omp single function:Lets you specify that a section of code should be executed on a single thread, not necessarily the master thread.#include <stdio h=""> #include <omp h=""> int main() { #pragma omp parallel num_threads(2) { #pragma omp single // Only a single thread can read the input. printf_s("read input\n"); // Multiple threads in the team compute the results. printf_s("compute results\n"); #pragma omp single // Only a single thread can write the output. printf_s("write output\n"); } }

6.#pragma omp barrier function:Synchronizes all threads in a team; all threads pause at the barrier, until all threads execute the barrier.

7.#pragma omp for reduction function:Specifies that one or more variables that are private to each thread are the subject of a reduction operation at the end of the parallel region.#include <stdio h=""> #include <omp h=""> #define NUM_THREADS 4 #define SUM_START 1 #define SUM_END 10 #define FUNC_RETS {1, 1, 1, 1, 1} int bRets[5] = FUNC_RETS; int nSumCalc = ((SUM_START + SUM_END) * (SUM_END - SUM_START + 1)) / 2; int func1( ) {return bRets[0];} int func2( ) {return bRets[1];} int func3( ) {return bRets[2];} int func4( ) {return bRets[3];} int func5( ) {return bRets[4];} int main( ) { int nRet = 0, nCount = 0, nSum = 0, i, bSucceed = 1; omp_set_num_threads(NUM_THREADS); #pragma omp parallel reduction(+ : nCount) { nCount += 1; #pragma omp for reduction(+ : nSum) for (i = SUM_START ; i <= SUM_END ; ++i) nSum += i; #pragma omp sections reduction(&& : bSucceed) { #pragma omp section { bSucceed = bSucceed && func1( ); } #pragma omp section { bSucceed = bSucceed && func2( ); } #pragma omp section { bSucceed = bSucceed && func3( ); } #pragma omp section { bSucceed = bSucceed && func4( ); } #pragma omp section { bSucceed = bSucceed && func5( ); } } } printf_s("The parallel section was executed %d times " "in parallel.\n", nCount); printf_s("The sum of the consecutive integers from " "%d to %d, is %d\n", 1, 10, nSum); if (bSucceed) printf_s("All of the the functions, func1 through " "func5 succeeded!\n"); else printf_s("One or more of the the functions, func1 " "through func5 failed!\n"); if (nCount != NUM_THREADS) { printf_s("ERROR: For %d threads, %d were counted!\n", NUM_THREADS, nCount); nRet |= 0x1; } if (nSum != nSumCalc) { printf_s("ERROR: The sum of %d through %d should be %d, " "but %d was reported!\n", SUM_START, SUM_END, nSumCalc, nSum); nRet |= 0x10; } if (bSucceed != (bRets[0] && bRets[1] && bRets[2] && bRets[3] && bRets[4])) { printf_s("ERROR: The sum of %d through %d should be %d, " "but %d was reported!\n", SUM_START, SUM_END, nSumCalc, nSum); nRet |= 0x100; } }

你可能感兴趣的:(parallel,openmp)