Как показано в приведенном ниже коде, у меня есть первый цикл 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()