質問

In a code that uses OpenMP for multithreading, if the number of threads is set to one, will the time performance of the code be the same or very similar to that of the serial code before adding OpenMP or with all OpenMP stuffs removed?

役に立ちましたか?

解決

You should do some tests and find out. But The answer is that the time can be, but is not necessarily, different. See this answer for a good example as to why this can happen May compiler optimizations be inhibited by multi-threading? and also this one OpenMP with 1 thread slower than sequential version

The compiler may optimize the code without the OpenMP statements differently. For this reason, when comparing serial and parallel code with OpenMP you should make two functions in different executable with and without OpenMP and then time them separately. E.g

//foo.c
foo() {
    for(int i=0; i<n; i++) {
}

//foo_omp.c
foo_omp() {
    #pragma omp parallel for
    for(int i=0; i<n; i++) {
}

Another option is to compile without OpenMP enalbled (e.g. without -fopenmp in GCC). Your compile will ignore the pragma statements and optimize as if they did not exsist. However, this method has two disadvantages. If you use and OpenMP stub functions such as omp_get_wtime() then your code won't compile with GCC (but in ICC you can enable only the stub functions and in MSVC they work even if you don't enable OpenMP). The other problem is that to parallelize your function you often want to change it a bit (or sometimes a lot) from the serial version so to do a fair comparison you have to compare two separate functions anyway.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top