@@ -991,38 +991,33 @@ def local_optimization_mproc(self, xtals, ncpu, ids=None, qrs=False, pool=None):
991991 if ids is None :
992992 ids = range (len (xtals ))
993993
994- N_cycle = int ( np . ceil ( len ( xtals ) / ncpu ))
995- # Generator to create arg_lists for multiprocessing tasks
994+ # Interleaved assignment: worker i handles indices i, i+ ncpu, i+2*ncpu, …
995+ # so all workers stay busy and results can be sorted to 0,1,2,3… order.
996996 def generate_args_lists ():
997997 for i in range (ncpu ):
998- id1 = i * N_cycle
999- id2 = min ([id1 + N_cycle , len (xtals )])
1000- _ids = ids [id1 : id2 ]
998+ _indices = list (range (i , len (xtals ), ncpu ))
999+ _ids = [ids [j ] for j in _indices ]
10011000 job_tags = [self .tag + "-g" + str (gen )
10021001 + "-p" + str (id ) for id in _ids ]
1003- _xtals = [xtals [id ][0 ] for id in range ( id1 , id2 ) ]
1002+ _xtals = [xtals [j ][0 ] for j in _indices ]
10041003 mutates = []
10051004 labels = []
1006- for i in range ( id1 , id2 ) :
1007- orig_tag = xtals [i ][1 ]
1005+ for j in _indices :
1006+ orig_tag = xtals [j ][1 ]
10081007 if qrs :
10091008 mutates .append (False )
10101009 labels .append (orig_tag if orig_tag != "Random" else None )
10111010 else :
1012- if orig_tag == "Mutation" :
1013- mutates .append (True )
1014- else :
1015- mutates .append (False )
1011+ mutates .append (orig_tag == "Mutation" )
10161012 labels .append (None )
10171013 my_args = [_xtals , _ids , mutates , job_tags , labels , * args , self .rank , self .timeout ]
1018- yield tuple (my_args ) # Yield args instead of appending to a list
1014+ yield tuple (my_args )
10191015
10201016 gen_results = []
10211017 for result in pool .imap_unordered (process_task , generate_args_lists ()):
10221018 if result is not None :
10231019 for _res in result :
10241020 gen_results .append (_res )
1025-
10261021 return gen_results
10271022
10281023 def gen_summary (self , t0 , gen_results , xtals ):
0 commit comments