openmp 中部分函数简介
2013-10-10 12:52
295 查看
接下来将考虑对plsa进行并行化,并行化主要包括机器之间的并行和单机多线程的并行,考虑采用omp来实现单机多线程的并行,这样既可以快速验证下算法的效率提升又可以避免把很多时间花在多线程控制上,结合算法特点,需要用到以下函数,在这里简单整理下,更多的函数参考microsoft msdn
1.omp_get_thread_num() function:Returns the thread number of the thread executing within its thread team
2.#pragma omp critical() function:Specifies that code is only be executed on one thread at a time.
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.
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.
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; } }
相关文章推荐
- 部分Dojo常用函数简介——面向对象(OO)及包机制(package system)
- 部分Dojo常用函数简介
- 部分Dojo常用函数简介(一)——Javascript基础扩展函数
- 部分Dojo常用函数简介(一)——Javascript基础扩展函数
- 部分Dojo常用函数简介(二)——面向对象(OO)及包机制(package system)
- 部分Dojo常用函数简介(四)——String及Array扩展
- 部分Dojo常用函数简介(二)——面向对象(OO)及包机制(package system)
- 部分Dojo常用函数简介(四)——String及Array扩展
- 部分Dojo常用函数简介(五)——事件处理以及Ajax I/O函数扩展
- 部分Dojo常用函数简介(五)——事件处理以及Ajax I/O函数扩展
- 部分Dojo常用函数简介(三)——页面生命周期及DOM相关常用函数
- 部分Dojo常用函数简介(三)——页面生命周期及DOM相关常用函数
- 部分Dojo常用函数简介(三)——页面生命周期及DOM相关常用函数
- 部分函数介绍()
- exit()函数简介
- 部分文件操作函数
- AVI函数截取BMP(中间部分代码提示了播放AVI的办法)
- VC开发OPC客户端常用函数简介(一)
- stm32应用部分发生中断,PC跳转到IAP中断向量表处后,如何跳转到应用部分中断函数入口的
- 消息中间件 Apache Qpid 简介,第 1 部分