且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

销毁Openmp中的线程(C ++)

更新时间:2022-06-24 05:39:44

此时,OpenMP规范不给用户任何能力控制何时线程被销毁。你所说的是非常有趣的,并没有在任何OpenMP语言委员会会议期间讨论的规范。你能提供更多关于你在做什么和什么问题的信息吗?

At this point in time, the OpenMP specification doesn't give the user any ability to control when threads are destroyed. What you are saying is very interesting and hasn't been brought up during any of the OpenMP language committee meetings to discuss the specification. Can you give more information about what you are doing and what the problem is? It would be helpful in bringing this discussion to the committee.

编辑已添加的3/7 -

好的 - 这里是一个简单的例子,似乎工作,它可能会解决你的问题:

Okay - here is a simple example that seems to work and it might get around your problem:

$> cat prog.c  
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

int main(void)
{
  void (*f)(void);
  void *handle;

  handle = dlopen("./shared.so", RTLD_NOW);
  if (!handle) {
    printf("*** dlopen error - %s\n", dlerror());
    abort();
  }

  f = dlsym(handle, "foo");
  if (!f) {
    printf("*** dlsym error - %s\n", dlerror());
    abort();
  }
  f();

  if(dlclose(handle)) {
    printf("*** dlclose error - %s\n", dlerror());
    abort();
  }
  return 0;
}

$> cat shared.c 
#include <omp.h> 
#include <stdio.h>

void foo(void)
{
  int i;

  puts("... in foo\n");

  #pragma omp parallel for
  for (i=0; i<10; i++) 
    printf("t#: %i  i: %i\n", omp_get_thread_num(), i);

  puts("... exiting foo");
}

$> gcc -rdynamic -fopenmp prog.c -ldl -o prog                                                                   
$> gcc -c -fopenmp -fPIC -o shared.o shared.c                                                                 
$> ld -shared -o shared.so shared.o                                                                           
$> ./prog
... in foo

t#: 2  i: 4
t#: 2  i: 5
t#: 3  i: 6
t#: 3  i: 7
t#: 0  i: 0
t#: 0  i: 1
t#: 4  i: 8
t#: 4  i: 9
t#: 1  i: 2
t#: 1  i: 3
... exiting foo


$ b b

虽然主程序(prog.c)中没有OpenMP代码,我使用-fopenmp标志编译它。这意味着OpenMP环境将在调用实际使用OpenMP的共享库之前设置。这似乎解决了执行dlclose的问题,因为环境仍然存在。它还允许第一次使用OpenMP获得的线程留在后面使用。这是为了解决你的问题(或这只是工作在这个简单的例子)?

While there is no OpenMP code in the main program (prog.c), I compiled it using the -fopenmp flag. This means that the OpenMP environment will be set up before the call to the shared library that is actually using OpenMP. This seems to get around the problem with doing a dlclose, since the environment is still there. It also allows the threads gotten by the first use of OpenMP to stay around for subsequent use. Does this work for getting around your problem (or does this only work for this simple example)?