1

計算を高速化するために、 Pythonのmultiprocessingモジュールを使用してモンテカルロシミュレーションを起動しています。私が持っているコードは次のようになります:

def main():
    (various parameters are being set up...)

    start = 0
    end = 10
    count = int(1e4)
    time = np.linspace(start, end, num=count)

    num_procs = 12
    mc_iterations_per_proc = int(1e5)
    mc_iterations = num_procs * mc_iterations_per_proc
    mean_estimate, mean_estimate_variance = np.zeros(count), np.zeros(count)
    pool = multiprocessing.Pool(num_procs)
    for index, (estimate, estimate_variance) in enumerate(pool.imap_unordered(mc_linear_estimate,
        ((disorder_mean, intensity, wiener_std, time) for index in xrange(mc_iterations)), chunksize=mc_iterations_per_proc)):
        delta = estimate - mean_estimate
        mean_estimate = mean_estimate + delta / float(index + 1)
        mean_estimate_variance = mean_estimate_variance + delta * (estimate - mean_estimate)

    mean_estimate_variance = mean_estimate_variance / float(index)

さて、これmc_linear_estimateは* argsを受け取り、その中に追加の変数を作成する関数です。次のようになります。

def mc_linear_estimate(*args):
    disorder_mean, intensity, wiener_std, time = args[0]

    theta_process       = source_process(time, intensity, disorder_mean)
    xi_process          = observed_process(time, theta_process, wiener_std)
    gamma               = error_estimate(time, intensity, wiener_std, disorder_mean)
    estimate            = signal_estimate(time, intensity, wiener_std, disorder_mean, gamma, xi_process)

    estimate_variance   = (estimate - theta_process) ** 2
    return estimate, estimate_variance

ご覧のとおり、反復回数はかなり多く(1.2M)、すべての配列のサイズは10Kの2倍です。したがって、ストアを必要としないため、平均と分散を計算するためにウェルフォードのアルゴリズムを使用します。メモリ内の考慮されたシーケンスのすべての要素。ただし、これは役に立ちません。

問題:メモリが不足しています。アプリケーションを起動すると、12個のプロセスが表示されます(topLinuxマシンでプログラムを使用して見た場合)。彼らはすぐに大量のメモリを消費し始めますが、私が使用しているLinuxマシンには49GのRAMがあるので、しばらくの間は問題ありません。次に、各プロセスが最大約4GのRAMを使用するため、そのうちの1つが失敗し、のように表示さ<defunct>topます。次に、別のプロセスが失敗します。これは、プロセスが1つだけ残るまで発生し、最終的に「メモリ不足」の例外で失敗します。

質問:

  1. 私はおそらく何が間違っているのですか?

  2. すべてのメモリを消費しないようにコードを改善するにはどうすればよいですか?

4

0 に答える 0