Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's wrong with this threads pool / multi-core simulation
    primarykey
    data
    text
    <p>Hy everyone. I've been trying to make a kind of threads pool, meant to simulate a multi-core processor, where I have a number of threads running all the time( the core ), that I later dispatch to process a ( fixed for now) function. The idea to have the threads running at all time is that I don't have the thread creation/destruction overhead. There are three problems with what I'm doing now. First, the results are all wrong. Second, the function measuring the time is reporting 0 ms Third, the program calls abort at exit. Here's the code I'm using:</p> <pre><code> auto fakeExpensiveOperation = [](int i) -&gt; float { Sleep(10); return sqrt(i); }; // Performance test int size = 4000; float* out = new float[size]; #define THREAD_RUNNING 0 #define THREAD_FINISHED (1 &lt;&lt; 0) #define THREAD_EXIT (1 &lt;&lt; 1) #define THREAD_STALL (1 &lt;&lt; 2) const int coreCount = 8; thread cores[coreCount]; atomic&lt;unsigned int&gt; msgArray[coreCount]; for (auto&amp; msg : msgArray) msg.store(THREAD_STALL); auto kernel = [out, &amp;fakeExpensiveOperation](int idx){ out[idx] = fakeExpensiveOperation(idx); }; for (int i = 0; i &lt; coreCount; i++) { cores[i] = thread([&amp;msgArray, i, kernel]() { while (true) { unsigned int msg = msgArray[i].load(); if((msg &amp; THREAD_STALL) == THREAD_STALL) continue; if ((msg &amp; THREAD_EXIT) == THREAD_EXIT) break; if ((msg &amp; THREAD_RUNNING) == THREAD_RUNNING) { int idx = (msg &gt;&gt; 3) + i; // Do the function kernel(idx); msgArray[i].store(THREAD_FINISHED); } } }); } auto t2 = time_call([&amp;]() { for (int i = 0; i &lt; size; i += coreCount) { for (int n = 0; n &lt; coreCount; n++) { if((msgArray[n].load() &amp; THREAD_RUNNING) == THREAD_RUNNING) continue; // The core is still working unsigned int msg = THREAD_RUNNING; msg |= (i &lt;&lt; 3); msgArray[n].store(msg); } } }); for (int n = 0; n &lt; coreCount; n++) msgArray[n].store(THREAD_EXIT); cout &lt;&lt; "sqrt 0 : " &lt;&lt; out[0] &lt;&lt; endl; cout &lt;&lt; "sqrt 1 : " &lt;&lt; out[1] &lt;&lt; endl; cout &lt;&lt; "sqrt 2 : " &lt;&lt; out[2] &lt;&lt; endl; cout &lt;&lt; "sqrt 4 : " &lt;&lt; out[4] &lt;&lt; endl; cout &lt;&lt; "sqrt 16 : " &lt;&lt; out[16] &lt;&lt; endl; cout &lt;&lt; "Parallel : " &lt;&lt; t2 &lt;&lt; endl; system("pause"); delete[] out; return 0; </code></pre> <p>I'm really out of ideas. Can anyone point out what's wrong here?</p> <p>EDIT: I did the changes i mentioned, and still get wrong values. I changed the values of the flags, and detached the threads after creating them.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload