Generating Ulam numbers with OpenMP and single-thread versions I'm making a program that takes an integer n and generates the first n Ulam numbers. I followed this guide about OpenMP.

This is the core function, single thread version:

bool isulam (int n, int size) {
    int count = 0;
    for (int i = 0; i &lt; size; i++)
        for (int j = 0; j &lt; size; j++) {
            if (i != j &amp;&amp; ulam[i]+ulam[j] == n) count++;
            if (count &gt; 2) return false;
        }
    return count;
}


And this is my attempt at optimizing it with OpenMP:

bool isulam (int n, int size) {
    int count = 0;
    bool toomany = false;
    for (int i = 0; i &lt; size; i++)
        #pragma omp parallel for reduction (|:toomany)
        for (int j = 0; j &lt; size; j++) {
            if (i != j &amp;&amp; ulam[i]+ulam[j] == n) count++;
            if (count &gt; 2) toomany = true;
        }
    if (count &gt; 2) return false;
    return count;
}


I'm compiling with g++ -Ofast -fopenmp. The output is correct, but the OpenMP version is much slower:


$ time ompulam &lt;&lt;&lt; 1000 &gt; /dev/null

real  0m22.211s
user  0m39.697s
sys   0m3.202s
$ time ulam &lt;&lt;&lt; 1000 &gt; /dev/null

real  0m7.073s
user  0m7.017s
sys   0m0.008s



What's happening? My CPU is an AMD E1 2500 (2 cores, 1400Mhz) which may not be the best, but I was hoping for a much different result. Is OpenMP only worth on 4+ cores?

FWIW with a regular #pragma omp parallel for (thus without the toomany), the code is running in 18.583s.
My CPU is an AMD E1 2500


That CPU is a dual core CPU, so with perfect multithreading that introduces exactly zero overhead, which at best could double the performance. As the comments already pointed out, the overhead is more than zero. Other than that, your CPU is a Kabini low power CPU with 15W TDP, so it's not unlikely that thermal throttling lowers the clock speed when running multithreaded - try to look at the actual clock speed (not % use of CPU) when running the test again. 

With that out of the way, the problem in your code is that you introduce absolutely massive overhead. In addition to the overhead due to having threads, the variable count is shared between threads, which forces the threads to wait for each other all the time. Also, your code will always run the entire loop, while the single-threaded version aborts early. A much faster version is the following, although that one still does not abort early (disclaimer: not tested, as I don't have OpenMP installed).

bool isulam (int n, int size) {
    int count = 0;
    bool toomany = false;
    for (int i = 0; i &lt; size; i++)
        #pragma omp parallel for reduction (+:count)
        for (int j = 0; j &lt; size; j++) {
            if (i != j &amp;&amp; ulam[i]+ulam[j] == n) count++;                
        }
    return count &lt;= 2;
}