Mixing OpenMP and pthreads

General OpenMP discussion
Forum rules
The OpenMP Forums are now closed to new posts. Please visit Stack Overflow if you are in need of help: https://stackoverflow.com/questions/tagged/openmp
Locked
snarkhunter
Posts: 5
Joined: Fri Sep 18, 2009 1:09 pm

Mixing OpenMP and pthreads

Post by snarkhunter »

Hi --

Quick question: Should I be able to launch a posix thread and then start an OpenMP parallel region from within the thread?

Long Motivation: I'm trying to use OpenMP to parallelize a program that runs on Linux and OS X. The program uses posix threads to call functions invoked by a GUI, and I'd like to use OpenMP in some of those functions. On OS X (10.5.8) using gcc 4.4 (from fink) the program dumps core when using OpenMP on a subthread. On Linux there's no problem. I'd like to know if the problem is in gcc, Apple's pthread implementation, or my expectations of OpenMP.

I might be able to do what I want to do with OpenMP3 and tasks, but not all systems that I will be using support OpenMP3 yet, and using tasks won't be straightforward because the GUI and top level functions are written in Python.

My C++ test code is below. This version crashes on OS X, but if you swap the commented out lines in subthread() and mainthread() it works (but isn't nearly as useful).

Many thanks for any advice.

-- Steve

Code: Select all

#include <omp.h>
#include <pthread.h>
#include <iostream>

void serialTask() {
 std::cerr << "Serial." << std::endl;
}

void parallelTask() {
#pragma omp parallel
  {
#pragma omp single
    {
      std::cerr << "Parallel: There are " << omp_get_num_threads()
		<< " threads." << std::endl;
    }
  }  
}

void *subthread(void*threadid) {
  parallelTask();
  //  serialTask();
  return 0;
}

void mainthread() {
  //  parallelTask();
  serialTask();
}



int main(int, char**) {
 pthread_t subth;
 void *subth_status;
 omp_set_nested(1);
 int res =  pthread_create(&subth,NULL,&subthread,NULL); 
 mainthread();
 pthread_join(subth,&subth_status); 
}


mwolfe

Re: Mixing OpenMP and pthreads

Post by mwolfe »

Neither OpenMP nor pthreads has defined the behavior when you use one under the other. The OpenMP committee has on its long-term goals to define the interaction of OpenMP threads with other threading models, in particular Posix threads, but we haven't done that yet.

It's not a bug to not support OpenMP from within pthreads, so at this point, the problem is your expectations, unfortunately.

The same problem can arise when using pthreads to create threads within an OpenMP parallel region. It all depends on what the particular implementation supports, and that depends both on the pthread implementation and the OpenMP compiler and its runtime.

Sorry, I'm not much help here.
-Michael Wolfe

snarkhunter
Posts: 5
Joined: Fri Sep 18, 2009 1:09 pm

Re: Mixing OpenMP and pthreads

Post by snarkhunter »

Thanks. That's disappointing, but good to know.

What do people do when using OpenMP in an interactive program? Make the main program single threaded and let the GUI be unresponsive while OpenMP threads are running? Spawn a separate process to run the OpenMP threads? Run the GUI in one of the OpenMP threads? Or is this just not done?

-- Steve

mwolfe

Re: Mixing OpenMP and pthreads

Post by mwolfe »

About interactive programs: good question. I don't have a good answer, sorry. I would be tempted to spawn another process for the compute-intensive OpenMP part, or perhaps see if my implementation supported OpenMP under pthreads. To run the GUI under one of the OpenMP threads would require splitting the OpenMP threads into subgroups, so the other group (the N-1 threads not running the GUI) could all synchronize and participate in the compute-intensive part, while not waiting for the one thread that is doing the GUI. That's another topic under consideration for OpenMP, but not yet done.

-Michael Wolfe

snarkhunter
Posts: 5
Joined: Fri Sep 18, 2009 1:09 pm

Re: Mixing OpenMP and pthreads

Post by snarkhunter »

mwolfe wrote: I would be tempted to spawn another process for the compute-intensive OpenMP part
That would require quite a bit of data transfer and/or memory sharing that I was hoping to avoid.
mwolfe wrote: or perhaps see if my implementation supported OpenMP
under pthreads.
Debian systems with gcc 4.3 seem to support it, but it may be an accident.
mwolfe wrote: To run the GUI under one of the OpenMP threads would require splitting the OpenMP threads into subgroups, so the other group (the N-1 threads not running the GUI) could all synchronize and participate in the compute-intensive part, while not waiting for the one thread that is doing the GUI. That's another topic under consideration for OpenMP, but not yet done.
I was thinking of something along these lines, schematically:

Code: Select all

#pragma omp parallel
{
#pragma omp single
  {
    while(1) {				// gui main loop
      callbackfn = do_some_gui_stuff(); 
#pragma omp task 
      {
	callbackfn();		// gui callback in its own task, may contain nested omp
      }
    } // end of main loop
#pragma omp taskwait
  }  // end of omp single
}
Would that work with the current OpenMP 3 spec? At least one of my development targets appears to support OpenMP 3. Inserting something like this into the current control structure might be less of a pain than the other alternatives. Relying on features that are in the current spec but aren't yet supported by all compilers appeals to me more than relying on features that aren't yet in the spec but might be implemented by some compilers.

-- Steve

mwolfe

Re: Mixing OpenMP and pthreads

Post by mwolfe »

It looks like your example should work, as long as your implementation supports nested parallelism for the nested OMP.
-michael wolfe

Locked