2

Here is the structure of the code snippet that creates the graph and meets certain conditions:

for i in range(1, len(binary_combinations)):

    # ...something          

    h=0
    while len(added)<i+1:   

         #...something  

         for j in it.combinations(good, 8):

             #...something

         h=h+1

Where binary_combinations, added and good they are some lists. I'm trying to implement multiprocessing for it for the whole for loop. Or only for the function it.combinations, but to no avail, because I can not reconcile this with the execution of the while-loop. How to approach it?

5
  • Before multiprocessing have you considered vectorizing your code? Commented Jan 5, 2018 at 9:11
  • I did not think about it because I'm not very good at vectorization yet. but if it would help, I will have to read more about it :) Commented Jan 5, 2018 at 9:14
  • secondly, I was interested in 'ordinary' loops because of the large number of values in the lists, so as not to remember the complete results in the operating memory. Commented Jan 5, 2018 at 9:17
  • Could you provide a reproducible example? Commented Jan 5, 2018 at 9:21
  • is an example of a complete code ufile.io/1v419 Commented Jan 5, 2018 at 9:25

1 Answer 1

2

As far as can be seen, you can abstract the first for loop easily:

from multiprocessing import Pool, cpu_count
import itertools as it

def foo(i):
    global added
    global good
    h=0
    while len(added)<i+1:   
        #...something  
        for j in it.combinations(good, 8):
            #...something
            h=h+1

pool = Pool(cpu_count())
results = pool.map(foo, range())

The main problem you will have here is that if you are mutating the lists inside your #...something code blocks it would be worthless, since the processes will not be sharing their stack state.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.