Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Intel TBB and Microsoft PPL, how to use next_permutation in a parallel loop?
    primarykey
    data
    text
    <p>I have Visual Studio 2012 with Intel parallel studio 2013 installed, so I have Intel TBB.</p> <p>Say I have the following piece of code:</p> <pre><code>const int cardsCount = 12; // will be READ by all threads // the required number of cards of each colour to complete its set: // NOTE that the required number of cards of each colour is not the same as the total number of cards of this colour available int required[] = {2,3,4}; // will be READ by all threads Card cards[cardsCount]; // will be READ by all threads int cardsIndices[cardsCount];// this will be permuted, permutations need to be split among threads ! // set "cards" to 4 cards of each colour (3 colours total = 12 cards) // set cardsIndices to {0,1,2,3...,11} // this variable will be written to by all threads, maybe have one for each thread and combine them later?? or can I use concurrent_vector&lt;int&gt; instead !? int logColours[] = {0,0,0}; int permutationsCount = fact(cardsCount); for (int pNum=0; pNum&lt;permutationsCount; pNum++) // I want to make this loop parallel !! { int countColours[3] = {0,0,0}; // local loop variable, no problem with multithreading for (int i=0; i&lt;cardsCount; i++) { Card c = cards[cardsIndices[i]]; // accessed "cards" countColours[c.Colour]++; // local loop variable, np. // we got the required number of cards of this colour to complete it if (countColours[c.Colour] == required[c.Colour]) // read global variable "required" ! { // log that we completed this colour and go to next permutation logColours[c.Colour] ++; // should I use a concurrent_vector&lt;int&gt; for this shared variable? break; } } std::next_permutation(cardsIndices, cardsIndices+cardsCount); // !! this is my main issue } </code></pre> <p>What I'm calculating is how many times we will complete a colour if we pick randomly from available cards, and that's done exhaustively by going through each permutation possible and picking sequentially, when a colour is "complete" we break and go to the next permutation. Note that we have 4 cards of each colour but the required number of cards to complete each colour is {2,3,4} for Red, Green, Blue. 2 red cards are enough to complete red and we have 4 available, hence red is more likely to be completed than blue which requires all 4 cards to be picked.</p> <p>I want to make this for-loop parallel, but my main problem is how to deal with "cards" permutations? you have ~0.5 billion permutation here (12!), if I have 4 threads how can I split this into 4 different quarters and let every thread go through each of them?</p> <p>What if I don't know the number of cores the machine has and I want the program to automatically choose the right number of concurrent threads? surely there must be a way to do that using Intel or Microsoft tools?</p> <p>This is my Card struct just in case:</p> <pre><code>struct Card { public: int Colour; int Symbol; } </code></pre>
    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.
    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