简体   繁体   中英

How to parallelize this embarrassingly parallel loop with Python

I have an embarrassingly parallel loop:

# Definitions

def exhaustiveExplorationsWithSimilarityAll(inputFolder, outputFolder, similarityMeasure):
    phasesSpeedupDictFolder=parsePhasesSpeedupDictFolder(inputFolder)
    avgSpeedupProgramDict=computeAvgSpeedupProgram(phasesSpeedupDictFolder)
    parameters={
        PROGRAMSPHASESSPEEDUPDICTS:phasesSpeedupDictFolder,
        PROGRAMSAVGSPEEDUPDICT:avgSpeedupProgramDict
    }
similarityHandler= SimilarityHandler(similarityMeasure,parameters)



# Sequential running

for fileName in os.listdir(inputFolder):
    print fileName
    exhaustiveExplorationsWithSimilarity(inputFolder + fileName, outputFolder + fileName, similarityHandler)

and I would like to make it parallel using Joblib Parallel:

# Parallel version

num_cores = multiprocessing.cpu_count()

parallel= Parallel(n_jobs=num_cores)
    for fileName in os.listdir(inputFolder):
        print fileName
        parallel(delayed(exhaustiveExplorationsWithSimilarity(inputFolder + fileName, outputFolder + fileName, similarityHandler)))

OR other version:

arg_generator = ((inputFolder + fileName, outputFolder + fileName, similarityHandler) for fileName in os.listdir(inputFolder))
parallel(delayed(exhaustiveExplorationsWithSimilarity)(arg_generator))

But upon running it complaints with :

parallel(delayed(exhaustiveExplorationsWithSimilarity(inputFolder + fileName, outputFolder + fileName, similarityHandler)))
  File "/usr/lib/pymodules/python2.7/joblib/parallel.py", line 516, in __call__
    for function, args, kwargs in iterable:
TypeError: 'function' object is not iterable

What am I missing here? Any help is appreciated.

You are still calling exhaustiveExplorationsWithSimilarity (serially) inside your loop, but then you are passing the result to delayed

From the docs https://pythonhosted.org/joblib/parallel.html#common-usage , it looks like you need to do something like:

parallel = Parallel(n_jobs=num_cores)
parallel(delayed(exhaustiveExplorationsWithSimilarity)(inputFolder + fileName, outputFolder + fileName, similarityHandler) for fileName in os.listdir(inputFolder))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM