• Nem Talált Eredményt

4.4 Implementation of the PGlobal Algorithm

4.4.1 SerializedGlobal

Every worker thread executes the same algorithm, PGlobal, that we illustrated on Algorithm 4.1. A thread selects a task in each main optimization cycle. We de-fined five different tasks; each one of them represents a data processing operation or a key part of algorithm control. Threads always choose the task that represents a latter stage in the algorithmic pipeline in order to facilitate the continuous flow of samples.

We discuss the algorithm in execution order; the tasks appear in reverse of the data flow. Each cycle starts with the check of stopping criterion.

The last stage is local search from the data point of view. The threads take sam-ples for starting points from theoriginssynchronized queue. The thread executes the local optimization and then clusters the found local optimum and the starting point with theclusterize optimumalgorithm (Algorithm4.4). Threads return to the main cycle after finishing the process.

The second task is related to the termination of the algorithm. We consider the end of each clustering as the end of an iteration cycle. As soon as the number of iterations reaches the allowed maximum, and we cannot run further local searches, then we stop any sample transfer to the local search module, and the optimization ends for the worker thread.

The third task is the clustering of the samples. If the clusterizer’s state is active, the thread can start the execution. Entering the clustering module is a critical phase of the algorithm, and it is guarded by a mutex synchronization primitive accordingly.

After finishing the clustering, threads try to leave the module as soon as possible, while new threads may not enter. A portion of the unclustered samples is moved into theoriginsqueue after all threads stopped the execution of the clustering algorithm.

4.4 Implementation of the PGlobal Algorithm 49 The previously inactive data path is temporarily activated at this point until these samples are transferred from one module to another.

If enough samples are available, threads can choose the fourth option, setup the clustering. If the conditions are fulfilled, the samples are moved to the clusterizer, and the module state becomes active.

The last option is the sample generation task. If the number of generated samples reaches its limit, the thread stops the execution. Otherwise the threads create a single sample and store it in thesamplesshared container and check if it is a new global optimum.

With the execution of the tasks sooner or later, the system will reach a point where at least one stopping criteria is fulfilled. The main thread organizes and dis-plays the results after all threads are terminated.

The single-threaded algorithm does not have to put too much effort to main-tain consistency and keep the right order of data processing operations. The se-quential execution ensures the deterministic processing of data. Meanwhile, coor-dinating the threads requires a lot of effort in the multithreaded case. The algo-rithm part responsible for keeping the consistency is in size comparable with the actual processing of data. The single thread executing Global is completely aware of the system state during the whole optimization. On the other hand, PGlobal does not have a dedicated thread for this; the system organizes itself through shared variables.

Although the implemented algorithms are totally different, the modules that han-dle them work similarly. The generation of a single sample happens the same way, and the only difference is thread handling in case of multiple samples. A simple cyclic structure is sufficient for the single-threaded execution that executes a prede-fined number of iterations. In multithreaded environment the threads must commu-nicate with each other to achieve the same behavior. We use a shared counter whom a mutex guards. Threads may generate a new sample if and only if this counter did not reach its allowed maximum. The number of generated samples is considered to be a soft termination criterion. The algorithm can exceed the allowed maximum by a value proportional to the number of worker threads.

Local search itself works also identically; again, its controlling changed on the higher level. PGlobal uses the same local search implementations as GlobalJ with a little modification for the sake of compatibility with SerializedGlobal that com-pletely preserves backward compatibility. The samples that can be the starting points of local searches are stored in theoriginsshared container in the parallel version of the algorithm. Threads take samples from this container and run the adapted lo-cal search algorithms started from them. After the lolo-cal searches, threads finish the data processing using theclusterize optimumalgorithm (Algorithm4.4) as the last operation.

Algorithm 4.1PGLOBAL Input

F:RnR

a,bRn: lower and upper bounds N: number of worker threads

maxSampleSize: maximum number of generated samples newSampleSize: number of samples generated for every iteration

reducedSampleSize: number of best samples chosen from the new samples

batch size: number of samples forwarded to local search after clustering (if 0 use the number of currently free threads)

Return value

optimum: best local optimum point found 1: samples,unclustered,origins← {}

2: optimummaximum value 3: startN1new threads 4: whiletruedo

5: if checkstopping criteriathen

6: break

7: else iforiginsis notemptythen 8: originremovefromorigins 9: iforiginisnullthen

10: continue

11: end if

12: localoptlocal searchoverFfromoriginwithin[a,b] 13: optimumminimumof{optimum,localopt} 14: callclusterize optimum (origin,localopt) 15: else if checkiteration count stopping criteriathen

16: break

17: else ifclusterizerisactivethen

18: callclustering samples (critical distance) 19: ifthisislast clustering threadthen

20: originsremovebatch sizefromunclustered 21: if|unclustered|=0then

22: setclusterizer to inactive

23: increaseiteration count

24: end if

25: end if

26: waituntil all clustering threads reach this point

27: else ifclusterizerisinactiveand|samples| ≥newSampleSizethen

28: locksamples

29: samplessortsamplesby ascending order regardingF

30: unclusteredremove[1,...,reducedSampleSize]elementfromsamples 31: updatecritical distance

32: setclusterizer to active

33: unlocksamples

34: else if checksample count stopping criteriathen

35: break

36: else

37: locksamples

38: samplessamplesgeneratea new sample from[a,b]distributed uniformly 39: optimumminimumof{optimum,new sample}

40: unlocksamples

41: end if 42: end while 43: return

4.4 Implementation of the PGlobal Algorithm 51