简体   繁体   English

在 OpenMP 并行区域内设置关联

[英]Setting affinity within OpenMP parallel region

I'm trying to see if it's possible to set affinity within an OpenMP region using pthread_setaffinity_np() call, assuming the underlying implementation uses pthreads for OpenMP workers.我正在尝试查看是否可以使用 pthread_setaffinity_np() 调用在 OpenMP 区域内设置关联,假设底层实现为 OpenMP 工作人员使用 pthread。 In the example code below, the call to set affinity does not return an error and the sched_getcpu() call also confirms that core affinity has been set correctly.在下面的示例代码中,对设置关联的调用不会返回错误,并且 sched_getcpu() 调用还确认已正确设置核心关联。 However, this way of setting affinity causes considerable performance degradation compared to using GOMP_CPU_AFFINITY environment variable to set affinity, which indicates some underlying issue with the use of pthread_setaffinity_np().但是,与使用 GOMP_CPU_AFFINITY 环境变量设置亲缘关系相比,这种设置亲缘关系的方式会导致相当大的性能下降,这表明使用 pthread_setaffinity_np() 存在一些潜在问题。 Are there any known issues with using pthread_setaffinity_np() inside OpenMP regions?在 OpenMP 区域内使用 pthread_setaffinity_np() 是否存在任何已知问题? For my use-case, I need to use pthreads that are 'masters' and each pthread will call its own OpenMP regions and will need to set affinity explicitly for the respective OpenMP regions.对于我的用例,我需要使用作为“主”的 pthread,每个 pthread 将调用自己的 OpenMP 区域,并且需要为相应的 OpenMP 区域显式设置关联。

#pragma omp parallel for reduction(+:sum) num_threads(num_drones)
  for (int i=start_N;i<end_N;i++){
    if(set[omp_get_thread_num()] == 0) {
      set[omp_get_thread_num()] = 1;
      cpu_set_t cpuset;
      CPU_ZERO(&cpuset);
      CPU_SET(rank*num_drones+omp_get_thread_num(), &cpuset);

      int error = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
      if (error != 0) {
        cout<< "\nError setting affinity";
        abort();
      }
    } else if (set[omp_get_thread_num()] == 1){
      set[omp_get_thread_num()] = 2;
      assert(rank*num_drones+omp_get_thread_num() == sched_getcpu());
    }
    sum += v1[i];
  }

This is a bad idea.这是一个主意。 The OpenMP runtime will almost certainly be optimising its internal algorithms and data structures based on the affinity of the threads which it has established. OpenMP 运行时几乎肯定会根据它所建立的线程的亲和性来优化其内部算法和数据结构。 (Eg using a hierarchical barrier to minimise cross-cache and cross-socket communication). (例如,使用分层屏障来最小化跨缓存和跨套接字通信)。 You are stamping all over that.你在上面盖章。

You say你说

I need to use pthreads that are 'masters' and each pthread will call its own OpenMP regions and will need to set affinity explicitly for the respective OpenMP regions.我需要使用作为“主”的 pthread,每个 pthread 将调用自己的 OpenMP 区域,并且需要为各自的 OpenMP 区域显式设置关联。

yet you haven't said anything about why you believe you need to do this.但是你还没有说你为什么相信你需要这样做。

This feels very like a classic "I have a problem I'm not going to explain to you, but here's my solution that doesn't work, so please fix that solution for me" question.这感觉非常像一个经典的“我有一个问题我不会向你解释,但这是我的解决方案不起作用,所以请为我修复该解决方案”问题。

If you were to explain your real problem we might be able to provide more help...如果您要解释您的真正问题,我们也许可以提供更多帮助...

(In particular, choosing thread affinity sensibly using OpenMP's mechanisms may be all that you need. See Controlling OpenMP Thread Affinity ). (特别是,您可能只需要使用 OpenMP 的机制明智地选择线程关联。请参阅控制 OpenMP 线程关联)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM