pool.map函数在for循环的3级内部被忽略

时间:2019-07-11 22:16:12

标签: python python-2.7 python-multiprocessing numba

如下面的代码所示,我有第一个for循环,用于设置调用函数的级别,但是当级别为3(如果是循环的n变量)时,pool.map将被忽略并且不执行任何操作,由于列表创建为空列表,导致程序停止工作。 我一直在研究其他相关问题,但找不到解决方案,我真的不知道那里到底发生了什么。

这是重现该错误的代码。

import time
import itertools
import numpy
import copy
import multiprocessing 
import logging 
from functools import partial
import numba
import random

# pool1 = multiprocessing.Pool(multiprocessing.cpu_count()) # pool 
nel = 4
norb = 6 # 'norb' tem que ser maior que 'nel'


def for2(swap_in_set,swap_out_set, reference_C, reference_occ):
    print "entrei for2"
    # copy the references and perform swaps
    C = copy.copy(reference_C)
    occ = list(reference_occ)

    # for swap_out, swap_in in zip(swap_out_set, swap_in_set):
    #     C[swap_out, :] = reference_C[swap_in, :]
    #     C[swap_in, :]  = reference_C[swap_out, :] # may be unnecessary
    #     occ[swap_out]  = reference_occ[swap_in]
    #     occ[swap_in] = reference_occ[swap_out]
    swap_list2 = zip(swap_out_set, swap_in_set)

    if swap_list2:
        C, occ = for1(swap_in_set, swap_out_set, C, occ, reference_C, reference_C, swap_list2)

    # get the condition
    new_cond = cond_num(C)

    return (new_cond, C, occ) 

@numba.njit
def for1(swap_in_set, swap_out_set, C, occ, reference_C, reference_occ, swap_list):
    for swap_out, swap_in in swap_list:
        C[swap_out, :] = reference_C[swap_in, :]
        C[swap_in, :]  = reference_C[swap_out, :] # may be unnecessary
        occ[swap_out]  = reference_occ[swap_in]
        occ[swap_in] = reference_occ[swap_out]
    return (C, occ)

# retorna soma da linha principal da matriz
@numba.njit(parallel=True)
def cond_num(matriz):
    sum = 0
    for i in numba.prange(len(matriz)):
        j = i
        sum = sum + matriz[i][j]
    return sum


def main():
    start_rot = 0
    final_rotation_level = 4
    pool1 = multiprocessing.Pool(multiprocessing.cpu_count())
    reference_C_b = []

    for i in range(norb):
        reference_C_b.append([])
        for j in range(norb):
            reference_C_b[i].append(random.randint(1,10))

    reference_C = numpy.array(reference_C_b)  
    reference_occ = reference_C
    # start_time = time.time()

    # iterate over rotation levels
    for n in range(start_rot, final_rotation_level+1, 1):  # tentar otimizar esse loop
        print "Level",n,"/",final_rotation_level

        # choose a number of occupied orbitals to swap
        for swap_out_set in itertools.combinations(range(nel), n):

            # choose the same number of unoccupied orbitals to swap in

            #print "Found a better reference (",best_cond,")"
            #new_cond = cond_num(C) 
            # swap_list = itertools.combinations(range(nel, norb, 1), n)
            swap_list = []
            for s in itertools.combinations(range(nel, norb, 1), n):
                swap_list.append(s)
            print "antes do pool"  
            Cs_occs = pool1.map(partial(for2, swap_out_set=swap_out_set, reference_C=reference_C, reference_occ=reference_occ), swap_list)
            print Cs_occs
            print "depois do pool"
            if not Cs_occs:
                print "ta vazia"
                print reference_C
                print reference_occ
                print swap_out_set
            # check for betterness
            # no level 3 por algum motivo nao esta entrando no pool?
            Cond_list, Cs, occs = zip(*Cs_occs) # transform list of tuples into lists



main()             

0 个答案:

没有答案