我可以并行化这个嵌套for循环吗?(Python 3.5版)

2024-09-27 07:30:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近发现我的代码的瓶颈是下面的代码块。N的阶数为10000,L(10000)^2。RQ\u func只是一个接受索引(元组)并返回float V和{index:probability}格式的dictionary sp\u dist的函数。你知道吗

有什么方法可以并行化这个代码吗?我可以访问群集计算,从中我可以使用多达20个核心的时间,并希望使用该选项。你知道吗

    R = np.empty((L,))
    Q = scipy.sparse.lil_matrix((L, N))

    traverser = 0        # Populate R and Q by traversing the array
    for s_index in state_indices:
        for a_index in action_indices:
            V, sp_dist = RQ_func(s_index, a_index)
            R[traverser] = V
            for sp_index, prob in sp_dist.items():
                Q[traverser, sp_index] = prob
            traverser += 1

Tags: 代码inforindexdistfloatsprq

热门问题