Python Parallel Computing - Scoop

主宰稳场 提交于 2019-12-07 12:32:09

问题


I am trying to get familiar with the library Scoop (documentation here: https://media.readthedocs.org/pdf/scoop/0.7/scoop.pdf) to learn how to perform statistical computations in parallel, using in particular the futures.map function.

As such, at first, I would like to try to run a simple linear regression, and assess the difference in performance between serial and parallel computations, using 10000000 data point (4 features, 1 target variable) randomly generated from a Normal Distribution.

This is my code:

import pandas as pd
import numpy as np
import random
from scoop import futures
import statsmodels.api as sm
from time import time

def linreg(vals):
    global model
    model = sm.OLS(y_vals,X_vals).fit()
    return model
    print(model.summary())    

if __name__ == '__main__':

    random.seed(42)
    vals = pd.DataFrame(np.random.normal(loc = 3, scale = 100, size =(10000000,5)))
    vals.columns = ['dep', 'ind1', 'ind2', 'ind3', 'ind4']
    y_vals = vals['dep']
    X_vals = vals[['ind1', 'ind2', 'ind3', 'ind4']]

    bt = time()
    model_vals = list(map(linreg, [1,2,3]))
    mval = model_vals[0]
    print(mval.summary())
    serial_time = time() - bt

    bt1 = time()
    model_vals_1 = list(futures.map(linreg, [1,2,3]))
    mval_1 = model_vals_1[0]
    print(mval_1.summary())
    parallel_time = time() - bt1

    print(serial_time, parallel_time)

However, after that the regression summary is indeed produced in serial - via the Python's standard map function - an error:

Traceback (most recent call last): File "C:\Users\niccolo.gentile\AppData\Local\Continuum\anaconda3\envs\tensorenviron\lib\runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "C:\Users\niccolo.gentile\AppData\Local\Continuum\anaconda3\envs\tensorenviron\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:\Users\niccolo.gentile\AppData\Local\Continuum\anaconda3\envs\tensorenviron\lib\site-packages\scoop\bootstrap__main__.py", line 302, in b.main() File "C:\Users\niccolo.gentile\AppData\Local\Continuum\anaconda3\envs\tensorenviron\lib\site-packages\scoop\bootstrap__main__.py", line 92, in main self.run() File "C:\Users\niccolo.gentile\AppData\Local\Continuum\anaconda3\envs\tensorenviron\lib\site-packages\scoop\bootstrap__main__.py", line 290, in run futures_startup() File "C:\Users\niccolo.gentile\AppData\Local\Continuum\anaconda3\envs\tensorenviron\lib\site-packages\scoop\bootstrap__main__.py", line 271, in futures_startup run_name="main" File "C:\Users\niccolo.gentile\AppData\Local\Continuum\anaconda3\envs\tensorenviron\lib\site-packages\scoop\futures.py", line 64, in _startup result = _controller.switch(rootFuture, *args, **kargs) File "C:\Users\niccolo.gentile\AppData\Local\Continuum\anaconda3\envs\tensorenviron\lib\site-packages\scoop_control.py", line 253, in runController raise future.exceptionValue File "C:\Users\niccolo.gentile\AppData\Local\Continuum\anaconda3\envs\tensorenviron\lib\site-packages\scoop_control.py", line 127, in runFuture future.resultValue = future.callable(*future.args, **future.kargs) File "C:\Users\niccolo.gentile\AppData\Local\Continuum\anaconda3\envs\tensorenviron\lib\runpy.py", line 263, in run_path pkg_name=pkg_name, script_name=fname) File "C:\Users\niccolo.gentile\AppData\Local\Continuum\anaconda3\envs\tensorenviron\lib\runpy.py", line 96, in _run_module_code mod_name, mod_spec, pkg_name, script_name) File "C:\Users\niccolo.gentile\AppData\Local\Continuum\anaconda3\envs\tensorenviron\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "Scoop_map_linear_regression1.py", line 33, in model_vals_1 = list(futures.map(linreg, [1,2,3])) File "C:\Users\niccolo.gentile\AppData\Local\Continuum\anaconda3\envs\tensorenviron\lib\site-packages\scoop\futures.py", line 102, in _mapGenerator for future in _waitAll(*futures): File "C:\Users\niccolo.gentile\AppData\Local\Continuum\anaconda3\envs\tensorenviron\lib\site-packages\scoop\futures.py", line 358, in _waitAll for f in _waitAny(future): File "C:\Users\niccolo.gentile\AppData\Local\Continuum\anaconda3\envs\tensorenviron\lib\site-packages\scoop\futures.py", line 335, in _waitAny raise childFuture.exceptionValue NameError: name 'y_vals' is not defined

is produced afterwards. This means that the code stops at model_vals_1 = list(futures.map(linreg, [1,2,3])).

I have also tried running it with map both times and indeed the error does not appear.

I also specify that the script has been correctly launched with:

python -m scoop Scoop_map_linear_regression1.py

from Anaconda Prompt command line.

Indeed, should it be launched without the -m scoop parameter, it would not be parallelized and would actually run, but just using two times the built in Python's map function, as how you would get reported in the Warnings. That is, without specifying the -m scoop parameter when launching it, futures.map would be replaced by map.

My goal is instead to run it in parallel, using exactly futures.map, and assess the performance improvement.

Specifying it to avoid any other similar answer and a consequent put on hold.

Any comment is highly appreciated and welcome.

来源:https://stackoverflow.com/questions/54593652/python-parallel-computing-scoop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!