加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

多线程 – 检测CPU和线程的OpenMP行为

发布时间:2020-12-15 02:29:42 所属栏目:Java 来源:网络整理
导读:我刚开始使用OpenMP,我刚刚使用 gcc -fopenmp openmp_c_helloworld.c编译了以下代码: #include omp.h#include stdio.h#include stdlib.hint main (int argc,char *argv[]) { int th_id,nthreads; #pragma omp parallel private(th_id) { th_id = omp_get_th
我刚开始使用OpenMP,我刚刚使用 gcc -fopenmp openmp_c_helloworld.c编译了以下代码:

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc,char *argv[]) {
  int th_id,nthreads;
  #pragma omp parallel private(th_id)
  {
    th_id = omp_get_thread_num();
    printf("Hello World from thread %dn",th_id);
    #pragma omp barrier
    if ( th_id == 0 ) {
      nthreads = omp_get_num_threads();
      printf("There are %d threadsn",nthreads);
    }
  }
  return EXIT_SUCCESS;
}

我只是在具有超线程的四核Intel CPU上运行可执行文件,并获得以下输出:

Hello World from thread 2
Hello World from thread 0
Hello World from thread 3
Hello World from thread 1
There are 4 threads

从技术上讲,我的CPU上有8个线程和4个CPU内核,为什么OpenMP只显示4个线程?

解决方法

简单地说,我认为这是因为OpenMP查找CPU(核心)的数量而不是处理器线程的数量.
见 this页:`

Implementation default – usually the number of CPUs on a node,though
it could be dynamic (see next bullet).

您可以尝试的是将程序中的线程数设置为等于处理器线程数,并查看是否有性能改进(您必须创建自己的基准测试程序).在并行编程中,当工作线程的数量等于处理器线程的数量时,获得了良好的性能.您也可以为I / O保留一两个额外的线程.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读