-->

Rpy2,DiceKriging,多:内存泄漏(Rpy2, DiceKriging, multipr

2019-10-20 19:31发布

为了使用R包DiceKriging从Python程序多,我写的东西像下面的例子。

多重允许在时间显著增益,但内存消耗快速增长的意想不到的副作用。 当设置flag_pool = False在下面的代码(因此不使用多处理)的存储器是稳定的。

我试着使用Python的都和R的垃圾收集器(如建议在这里 , 在这里和那里 ),但没有成功。

这怎么可能避免?

MWE:

from multiprocessing import Pool

import numpy as np

from rpy2 import robjects
robjects.r("library('DiceKriging')")


# Generate data
def model_to_emulate(X):
    return X[:, 0]**3 - X[:, 1]**2

X = np.random.random_sample((1000, 2))
z = model_to_emulate(X)

list_arg = [[z, X]] * N_gp

# Emulation
flag_pool = True

N_gp = 3
N_repetition = 3

def worker_km(response_design):
    response = response_design[0]
    robjects.globalenv["response"] = robjects.FloatVector(response)

    design = response_design[1]
    df = robjects.r["data.frame"]([robjects.FloatVector(column) for column in
                                   design.T])

    df.names = ["x%d" % ii for ii in xrange(design.shape[1])]
    robjects.globalenv["design"] = df

    return robjects.r("fit = km(design=design, response=response,"
                         "covtype='matern5_2')")

for _ in xrange(N_repetition):
    if flag_pool:
        print "==================== Using Pool."
        pool = Pool(N_gp)
        out = pool.map(worker_km, list_arg)
        pool.close()
        pool.join()
    else:
        print ">>>>>>>>>>>>>>>>>>>> Not using Pool."
        for response_design in list_arg:
            out = worker_km(response_design)
#

编辑:我使用Ubuntu 12.04.4,蟒蛇2.7.3,R 2.14.1和rpy2 2.2.5。

我问过类似的问题, 这里有MWE,应该是更容易地运行。

文章来源: Rpy2, DiceKriging, multiprocessing : memory leak