使用openMP进行多核处理与多线程 [英] Using openMP for multicore processing vs multithreading

查看:304
本文介绍了使用openMP进行多核处理与多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题听起来很基础,但是我找不到对此的任何具体答案.因此,现在说我们有一个像corei5 680这样的多核处理器(2个物理内核,并为HT启用了4个OS可用内核). 我的问题是,openMP完全适合图片中的什么位置? 1-当我们说使用openMP进行多线程处理时,它会自动利用所有可用的内核(在这种情况下为4个虚拟内核)并根据可用的CPU周期执行线程吗? 2-openmp是否控制如何使用物理/虚拟内核?还是将其抽象化并提供像Java这样的多线程环境?

The question may sound basic but, I could not find any concrete answer to this. So now say we have a multicore processor like a corei5 680 (2 physical cores and with HT enabled 4 usable cores to the OS). My question is where does openMP exactly fit in the picture? 1 - When we say multithreading using openMP does it automatically make use of all the available cores (4 virtual cores in this case) and executes the thread depending on the CPU cycles available? 2 - Does openmp give the control on how to use the physical/virtual cores? or is it abstracted and gives the mutlithreading environment like say a java?

如果这听起来很简单,请原谅,但是我试图在网上找到答案,但找不到满意的答案.

Please excuse me if this sounds basic, but I have tried to find the answer online but could not find anything satisfactory.

谢谢

推荐答案

这取决于您正在考虑的OpenMP版本/功能,因为我相信以后的版本可能会为您提供更多功能,但是原始库是围绕数据并行构建的for原语.通常,OpenMP和其他数据并行编程模型会尝试抽象出底层硬件,然后程序员将其计算声明为对数据的一系列操作,然后由OMP对其进行调度.

It depends a bit which version/features of OpenMP you're considering as I believe later versions may give you more features but the original library was build around data parallel for primatives. In general OpenMP and other data parallel programming models try and abstract away the underlying hardware and the programmer declares their computation as a series of operations on data that are then scheduled by OMP.

要回答您的第一个问题,OS调度程序将跨内核调度线程,OMP调度程序将跨可用线程调度工作.

To answer your first question the OS scheduler will schedule threads across cores, the OMP scheduler will schedule work across the available threads.

#pragma omp parallel for
for (i = 0; i < N; i++)
    a[i] = 2 * i;

OMP调度程序将根据许多因素(包括它们的负载,分配给它的工作量以及您可能提供的任何提示)来选择要使用的内核(实际内核或HT).有人希望上面的代码能在所有可用内核上运行(您的示例中为4个)

The OMP scheduler will choose which cores (real or HT) to use depending on a number of factors including their load, the amount of work being given to it and any hints you might have provided. One would expect the code above to run on all the available cores (4 in your example)

您可以使用schedule关键字来控制调度程序如何分配工作.

You can use the schedule keyword to control how the scheduler allocates work.

计划(类型,块):如果工作共享结构是 do循环或for循环.工作共享结构中的迭代 根据定义的调度方法分配给线程 本条款.调度的三种类型是:

schedule(type, chunk): This is useful if the work sharing construct is a do-loop or for-loop. The iteration(s) in the work sharing construct are assigned to threads according to the scheduling method defined by this clause. The three types of scheduling are:

静态:在这里,所有 线程在执行循环之前被分配迭代 迭代.迭代在各个线程之间平均分配如下: 默认.但是,为参数块指定一个整数将 为特定线程分配块数量的连续迭代.

static: Here, all the threads are allocated iterations before they execute the loop iterations. The iterations are divided among threads equally by default. However, specifying an integer for the parameter chunk will allocate chunk number of contiguous iterations to a particular thread.

动态:这里,一些迭代分配给了较小的迭代 线程数.一旦特定线程完成其分配 迭代,它返回从迭代中获得另一个 左边.参数块定义了连续迭代的次数 一次分配给一个线程的线程.

dynamic: Here, some of the iterations are allocated to a smaller number of threads. Once a particular thread finishes its allocated iteration, it returns to get another one from the iterations that are left. The parameter chunk defines the number of contiguous iterations that are allocated to a thread at a time.

指导:很大一部分 连续迭代被动态分配给每个线程(如 以上).块大小随着每个连续的块呈指数减小 分配到参数块中指定的最小大小

guided: A large chunk of contiguous iterations are allocated to each thread dynamically (as above). The chunk size decreases exponentially with each successive allocation to a minimum size specified in the parameter chunk

来自维基百科

解决第二个问题.您还可以使用num_threads属性指定要使用的线程数.在示例中,在#pragma omp parallel for上方添加以下内容将使OMP限制为三个线程,无论是否有更多线程可用.

To address your second question. You can also use the num_threads attribute to specify the number of threads to be used. Adding the following above the #pragma omp parallel for in the example would limit OMP to three threads, regardless of whether more were available.

#pragma omp parallel num_threads(3)
#pragma omp for
for (i = 0; i < N; i++)
    a[i] = 2 * i;

还可以在某种程度上控制在多处理器(多个插槽)系统中的不同处理器上如何调度工作. OpenMP和NUMA关系?

It is also possible to control to some extent how work is schedule across different processors in a multi-processor (more than one socket) system. OpenMP and NUMA relation?

您可能还会发现以下指南很有用, OpenMP指南:轻松进行多线程编程对于C ++ .

You might also find the following guide useful, Guide into OpenMP: Easy multithreading programming for C++.

这篇关于使用openMP进行多核处理与多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆