Getting Started with OpenMP*

Abstract

As you probably know by now, to get the maximum performance benefit from a processor with Hyper-Threading Technology, an application needs to be executed in parallel. Parallel execution requires threads, and threading an application is not trivial. What you may not know is that tools like OpenMP* can make the process a lot easier.

This is the first in a series of three white-papers that teach you, an experienced C/C++ programmer, how to use OpenMP to get the most out of Hyper-Threading Technology. This first paper shows you how to parallelize loops, called work sharing. The second paper teaches you how to exploit non-loop parallelism and some additional OpenMP features. The final paper discusses the OpenMP runtime library functions, the Intel® C++ Compiler, and how to debug your application if things go wrong.

A Quick Introduction to OpenMP

The designers of OpenMP wanted to provide an easy method to thread applications without requiring that the programmer know how to create, synchronize, and destroy threads or even requiring him or her to determine how many threads to create. To achieve these ends, the OpenMP designers developed a platform-independent set of compiler pragmas, directives, function calls, and environment variables that explicitly instruct the compiler how and where to insert threads into the application. Most loops can be threaded by inserting only one pragma right before the loop. Further, by leaving the nitty-gritty details to the compiler and OpenMP, you can spend more time determining which loops should be threaded and how to best restructure the algorithms for maximum performance. The maximum performance of OpenMP is realized when it is used to thread "hotspots," the most time-consuming loops in your application.

The power and simplicity of OpenMP is best demonstrated by looking at an example. The following loop converts a 32-bit RGB (red, green, blue) pixel to an 8-bit gray-scale pixel. The one pragma, which has been inserted immediately before the loop, is all that is needed for parallel execution.

#pragma omp parallel for

for (i=0; i < numPixels; i++)

{

   pGrayScaleBitmap[i] = (unsigned BYTE)

            (pRGBBitmap[i].red * 0.299 +

             pRGBBitmap[i].green * 0.587 +

             pRGBBitmap[i].blue * 0.114);

}

Let's take a closer look at the loop. First, the example uses 'work-sha ring,' the general term used in OpenMP to describe distribution of work across threads. When work-sharing is used with the for construct, as shown in the example, the iterations of the loop are distributed among multiple threads so that each loop iteration is executed exactly once and in parallel by one or more threads. OpenMP determines how many threads to create and how to best create, synchronize, and destroy them. All the programmer needs to do is to tell OpenMP which loop should be threaded.

OpenMP places the following five restrictions on which loops can be threaded:

The loop variable must be of type signed integer. Unsigned integers, such as DWORD's, will not work.

The comparison operation must be in the form loop_variable <, <=, >, or >= loop_invariant_integer

The third expression or increment portion of the for loop must be either integer addition or integer subtraction and by a loop invariant value.

If the comparison operation is < or <=, the loop variable must increment on every iteration, and conversely, if the comparison operation is > or >=, the loop variable must decrement on every iteration.

The loop must be a basic block, meaning no jumps from the inside of the loop to the outside are permitted with the exception of the exit statement, which terminates the whole application. If the statements goto or break are used, they must jump within the loop, not outside it. The same goes for exception handling; exceptions must be caught within the loop.

Although these restrictions may sound somewhat limiting, non-conforming loops can easily be rewritten to follow these restrictions.

Getting Started with OpenMP的更多相关文章

  1. 应用OpenMP的一个简单的设计模式

    小喵的唠叨话:最近很久没写博客了,一是因为之前写的LSoftmax后馈一直没有成功,所以在等作者的源码.二是最近没什么想写的东西.前两天,在预处理图片的时候,发现处理200w张图片,跑了一晚上也才处理 ...

  2. openmp 的使用

    http://blog.csdn.net/gengshenghong/article/details/7003110 说明:这部分内容比较基础,主要是分析几个容易混淆的OpenMP函数,加以理解. ( ...

  3. OpenMP编程总结表

    本文对OpenMP 2.0的全部语法——Macro(宏定义).Environment Variables(环境变量).Data Types(数据类型).Compiler Directives(编译指导 ...

  4. OpenMP共享内存并行编程详解

    实验平台:win7, VS2010 1. 介绍 平行计算机可以简单分为共享内存和分布式内存,共享内存就是多个核心共享一个内存,目前的PC就是这类(不管是只有一个多核CPU还是可以插多个CPU,它们都有 ...

  5. OpenMP并行构造的schedule子句详解 (转载)

    原文:http://blog.csdn.net/gengshenghong/article/details/7000979 schedule的语法为: schedule(kind, [chunk_si ...

  6. 利用OpenMP实现埃拉托斯特尼(Eratosthenes)素数筛法并行化 分类: 算法与数据结构 2015-05-09 12:24 157人阅读 评论(0) 收藏

    1.算法简介 1.1筛法起源 筛法是一种简单检定素数的算法.据说是古希腊的埃拉托斯特尼(Eratosthenes,约公元前274-194年)发明的,又称埃拉托斯特尼筛法(sieve of Eratos ...

  7. 大数据并行计算利器之MPI/OpenMP

    大数据集群计算利器之MPI/OpenMP ---以连通域标记算法并行化为例 1 背景 图像连通域标记算法是从一幅栅格图像(通常为二值图像)中,将互相邻接(4邻接或8邻接)的具有非背景值的像素集合提取出 ...

  8. android studio ndk使用openMP

    好久没碰ndk了,之前都是在eclipse下写makefile配置c++程序的,现在发现主流都是用android studio,eclipse俨然已经被遗弃了,正好最近项目需要用openMP做算法加速 ...

  9. opencv 3.0 DPM Cascade 检测 (附带TBB和openMP加速)

    opencv 3.0 DPM cascade contrib模块 转载请注明出处,楼燚(yì)航的blog,http://www.cnblogs.com/louyihang-loves-baiyan/ ...

  10. openMP的一点使用经验【非原创】

    按照百科上说的,针对于openmp的编程,最简单的就是在开头加个#include<omp.h>,然后在后面的for上加一行#pragma omp parallel for即可,下面的是较为 ...

随机推荐

  1. linux下ejabberd框架搭建

    ejabberd为erlang的IM的开源框架,一直想找个时间研究研究: 1.下载Ejabberd安装包 wget http://www.process-one.net/downloads/ejabb ...

  2. Ubuntu/CentOS下编译Nginx最基本参数

    Ubuntu/CentOS下编译Nginx安装基本参数,做个记录: groupadd www useradd -g www www ./configure --user=www --group=www ...

  3. 目标检测之行人检测(Pedestrian Detection)---行人检测之简介0

    一.论文 综述类的文章 [1]P.Dollar, C. Wojek,B. Schiele, et al. Pedestrian detection: an evaluation of the stat ...

  4. 目标检测之行人检测(Pedestrian Detection)基于hog(梯度方向直方图)--- 梯度直方图特征行人检测、人流检测2

    本文主要介绍下opencv中怎样使用hog算法,因为在opencv中已经集成了hog这个类.其实使用起来是很简单的,从后面的代码就可以看出来.本文参考的资料为opencv自带的sample. 关于op ...

  5. 使用jQuery Ajax功能的时候需要注意的一个问题

    每次jquery的Ajax请求都会创建一个xmlHttprequest对象,理论上讲,长连接(页面需要和服务器保持长连接,而且在连接超时后需要重新请求连接)的请求是一个无限递归,请求数量是非常大的,但 ...

  6. SQL Server里的 ISNULL 与 NULLIF(转)

    SQL Server 中有两个参数,语法:     ISNULL(check_expression, replacement_value) check_expression 与 replacement ...

  7. Chrome性能分析工具Coverage使用方法

    操作路径如下: 打开控制台-->点击‘Sources’-->ctrl+shift+p-->在命令窗口输入coverage-->在下边新出现的窗口中点击左上角刷新按钮. 界面如下 ...

  8. wireshark 学习 3 display filter

    过滤信息,得到想要的帧进行分析. http://www.networkcomputing.com/networking/wifi-troubleshooting-using-wireshark/155 ...

  9. 小程序开发之xxx is not defined

    遇到问题 在小程序开发中直接在函数中调用data中的变量直接赋值给新的变量,就会出现如下错误 VM33895:1 thirdScriptErrorapaymoney is not defined; [ ...

  10. 使用python转换编码格式

    之前有写过一个使用powershell转换文档格式的方法,然而因为powershell支持不是很全,所以并不好用.这里使用python再做一个. 思路 检测源码格式,如果不是utf8,则进行转换,否则 ...