Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

It would have been better if you specified how you used @parallel and what do you exactly mean when you say " it doesn't seem to fit my requirements", but, in any case, I will try to give you a few hints that maybe will help you with your problem. The first thing that I need to be sure that you understand is that, when you use @parallel, your code does not just "run in parallel". Indeed, what happens is that you are executing a new process (with a new python interpreter), you told this process "hey, please run this function with v, r2 and k as arguments" and then, at the end, you retrieve the results. This means, for example, that this new process can not modify the global objects of the previous one. They simply do not share their variables. Therefore, my best advice would be to return three flags from the method check2: b1_must_be_increased, b2_must_be_increased and b3_must_be_increased. Then, in the original process, you proceed to increase the global variables accordingly to the flags you received.

Moreover, you must pay attention to what function you are parallelizing. Let me explain: if you parallelize only check2, for example, then fuse will be executed on the original process and maybe (I don't know if this is that case) it takes more time to execute fuse than to execute check2. In this case, your parallelization will not work (or, actually, will work very very poorly).

So, let me show you a possible parallelization of your code:

# I have no idea of what fuse and check2 do, so I use
# these two placeholders
def fuse(a, b):
    return b

def check2(a, b):
    b1_must_be_increased = True
    b2_must_be_increased = False
    b3_must_be_increased = True

    flags = (
        b1_must_be_increased,
        b2_must_be_increased,
        b3_must_be_increased
    )
    return flags

# This is the function that I want to parallelize
@parallel()
def my_parallel_function(v, r2, k):
    return check2(fuse(v, r2), k)

# Now I prepare in advance the list of all the arguments
# that I want to pass to my_parallel_function
my_parallel_function_args = [
    (v, r2, k) for r2 in Subsets(V2, k - 4)
]

# And here you will find the results of your computation executed
# in parallel.
results = my_parallel_function(my_parallel_function_args)

# Results is a list of couples; the first element of the couple is
# the input of the my_parallel_function, the second element is the
# output. Now I want to take only the output (so, the three flags)
flag_list = [r[1] for r in results]

for flags in flag_list:
    if flags[0]:
        b1 += 1
    if flags[1]:
        b2 += 1
    if flags[2]:
        b3 += 1

print((b1, b2, b3))

Finally, there is one last issue that I want to point out. The function "check2" must be "enough complicated" if you want all this to make sense. Let me explain with an example. Let us suppose that you have 1 million sets and you want to check, for each set, if 1 is inside the set or not. It may seems like a good idea to run this code in parallel. Therefore, what you need to do is to create a few new processes and describe the sets to all the different processes and then retrieve all the results. Unfortunately, this could turn out to be more complicated than actually check if 1 is inside each set or not. So, in principle, your code is parallel but the amount of work that the original process has to perform is increased (and, therefore, your code will become slower than before).