简体   繁体   中英

C++ OpenMP not running in parallel

I am making a program that utilizes GTK and in the program a server is created. The GUI and server work well. I am also able to thread it. OMP is compiled and I have tested it with the OMP Hello World which is computed in C. It works and from the outputs come out obviously randomized. However when I try to run the server in parallel after I press a button with this code:

char * ip;
int por;
#pragma omp parallel num_threads(1) private(ip, por)
{
    ip = (char *)gtk_entry_get_text(GTK_ENTRY(IP));
    por = atoi((char *)gtk_entry_get_text(GTK_ENTRY(port)));
    startServer(ip, por);
}

And obviously the include file #include <omp.h> .

GTK does what it is supposed to do and the server starts and takes connections. However it isn't run in parallel. The reason I know this is because the GUI becomes unresponsive and frozen. This is the command I use to compile it:
clang++ -std=c++11 -Xpreprocessor -fopenmp -L/usr/local/opt/llvm/lib httpServerv2.cpp `pkg-config --cflags --libs gtk+-3.0` -lomp
httpServerv2.cpp is the name of my c++ file. I have to use clang++ with the tag -std=c++11 so that threading will work. Which does work. The rest of the tags are for gtk and openmp. Is there something wrong with the way I am starting it? Because it is not running in parrallel.

UPDATE

So I figured out that it is running in parallel, just not the way I thought it should. What I thought was going to happen was that the program would just call the omp parallel and then the rest of the program would be able to continue to run while the program I wanted to run in parallel, in this case my server, just ran on another CPU. What I didn't realize was that after you call the parallel program, it actually waits for the processes to finish before the code can continue. Is there a way to make it so that the program doesn't wait for the processes to finish?

Something like the thread module has for the thread to detach, just for OMP.

After, some trying and more research I found that OpenMP doesn't do what I want it to do. However, the simple fork and pipe does. This is my new code using fork:

char * ip;
int por;
pid_t pid = fork();
if (pid == 0){
    ip = (char *)gtk_entry_get_text(GTK_ENTRY(IP));
    por = atoi((char *)gtk_entry_get_text(GTK_ENTRY(port)));
    startServer(ip, por);
}

First of all you force the parallel block to use only one thread, ie actually the master thread. So effectively, nothing will be done here in parallel. Further on, the fork-join model of OpenMP is generally not very well fitting for this class of problems, in doubt not really applicable since at the end of the parallel block, the threads are rejoined always. In (almost) any case here, you'd have to "help" OpenMP with further asynchronous detaching schemes. Since C++11, there's a huge standard library support available for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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