This file is indexed.

/usr/share/pyshared/deap/dtm/manager.py is in python-deap 0.7.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
#    This file is part of DEAP.
#
#    DEAP is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as
#    published by the Free Software Foundation, either version 3 of
#    the License, or (at your option) any later version.
#
#    DEAP is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with DEAP. If not, see <http://www.gnu.org/licenses/>.

import threading
import time
import math
import random
try:
    import Queue
except ImportError:
    import queue as Queue
import copy
import pickle
import logging
import sys
import os

PRETTY_PRINT_SUPPORT = False
try:
    from lxml import etree
    PRETTY_PRINT_SUPPORT = True
except ImportError:
    try:
        import xml.etree.cElementTree as etree
    except ImportError:
        # Python 2.5
        import xml.etree.ElementTree as etree

from deap.dtm.dtmTypes import *

logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
_logger = logging.getLogger("dtm.control")

# Constantes
DTM_CONTROL_THREAD_LATENCY = 0.05

MSG_COMM_TYPE = 0
MSG_SENDER_INFO = 1
MSG_NODES_INFOS = 2

DTM_LOGDIR_DEFAULT_NAME = "DtmLog"
            
try:
    from math import erf
except ImportError:
    def erf(x):
        # See http://stackoverflow.com/questions/457408/is-there-an-easily-available-implementation-of-erf-for-python
        # save the sign of x
        sign = 1
        if x < 0:
            sign = -1
        x = abs(x)

        # constants
        a1 = 0.254829592
        a2 = -0.284496736
        a3 = 1.421413741
        a4 = -1.453152027
        a5 = 1.061405429
        p = 0.3275911

        # A&S formula 7.1.26
        t = 1.0 / (1.0 + p * x)
        y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * math.exp(-x * x)
        return sign * y # erf(-x) = -erf(x)


class TaskIdGenerator(object):
    """
    A thread-safe ID generator.
    A DTM task ID is a tuple looking like (workerId, uniqId)
    With MPI, workerId is an integer (rank) and the uniqIds start from 0,
    but workerId can be virtually any immutable object (as it is used as
    a dictionnary key)
    """
    def __init__(self, rank, initId=0):
        self.r = rank
        self.wtid = 0
        self.generatorLock = threading.Lock()

    @property
    def tid(self):
        self.generatorLock.acquire()
        newId = self.wtid
        self.wtid += 1
        self.generatorLock.release()
        return (self.r, newId)


class ExecInfo(object):
    """
    Contains the information about the current task
    """
    def __init__(self, tStats, tStatsLock):
        self.eLock = threading.Lock()
        self.tStats = tStats
        self.tStatsLock = tStatsLock
        self.eCurrent = None
        self.mainThread = threading.currentThread()
        self.piSqrt = math.sqrt(math.pi)
        self.sqrt2 = 2 ** 0.5

    def _execTimeRemaining(self, mean, stdDev, alreadyDone):
        if alreadyDone == 0.:
            # If not started yet, return mean
            return mean
        #
        # Else compute the mass center of the remaining part of the gaussian
        # For instance, if we have a gaussian (mu, sigma) = (3, 4)
        # and that the task is executing for 3 seconds, then we may estimate
        # that it will probably finished at :
        # int(x*gaussian) / int(gaussian) over [3, +inf[
        # (where int is integral)
        #
        # We get 6.192 seconds, that is an expected remaining time of
        # 6.192 - 3 = 3.192 seconds
        
        # Evaluate primitive at point 'alreadyDone'
        commonPart = erf(self.sqrt2 * (alreadyDone - mean) / (stdDev * 2))
        areaPart1 = 0.5 * commonPart
        massCenterPart1 = (self.sqrt2 / (4 * self.piSqrt)) * (-2 * stdDev * math.exp(-0.5 * ((alreadyDone - mean) ** 2) / (stdDev ** 2)) + mean * (self.piSqrt) * (self.sqrt2) * commonPart)

        # Evaluate primitive at the infinite
        # erf(+inf) = 1, so
        areaPart2 = 0.5
        # exp(-inf) = 0, and erf(+inf) = 1, so
        massCenterPart2 = mean / 2.

        if areaPart1 == areaPart2:
            # So far from the mean that erf(alreadyDone - moyenne) = 1
            previsionPoint = alreadyDone + 0.5      # Hack : lets assume that there's 0.5 sec left...
        else:
            previsionPoint = (massCenterPart2 - massCenterPart1) / (areaPart2 - areaPart1)

        return previsionPoint - alreadyDone


    def acquire(self, forTask, blocking=True):
        if self.eLock.acquire(blocking):
            self.eCurrent = forTask
            return True
        else:
            return False

    def isLocked(self):
        if self.eLock.acquire(False):
            self.eLock.release()
            return False
        return True

    def release(self):
        self.eCurrent = None
        self.eLock.release()

    def getLoad(self):
        try:
            self.tStatsLock.acquire()
            tInfo = self.tStats.get(self.eCurrent.target, StatsContainer(rAvg=1., rStdDev=1., rSquareSum=0., execCount=0))
            val = self._execTimeRemaining(tInfo.rAvg, tInfo.rStdDev, self.eCurrent.threadObject.timeExec)
            self.tStatsLock.release()
            return val
        except AttributeError:
            self.tStatsLock.release()
            return 0.


class TaskQueue(object):
    """
    """
    def __init__(self, tasksStatsStruct, tasksStatsLock):
        self.tStats = tasksStatsStruct
        self.tStatsLock = tasksStatsLock
        self.previousLoad = 0.
        self.piSqrt = math.sqrt(math.pi)
        self.sqrt2 = 2 ** 0.5
        self._tQueue = Queue.PriorityQueue()
        self._tDict = {}
        self._actionLock = threading.Lock()
        self.changed = True

    def _getTimeInfoAbout(self, task):
        if task.threadObject is None:
            timeDone = 0.
        else:
            timeDone = task.threadObject.timeExec

        self.tStatsLock.acquire()
        tInfo = self.tStats.get(task.target, StatsContainer(rAvg=1., rStdDev=1., rSquareSum=0., execCount=0))
        self.tStatsLock.release()

        return self._execTimeRemaining(tInfo.rAvg, tInfo.rStdDev, timeDone)

    def _execTimeRemaining(self, mean, stdDev, alreadyDone):
        if alreadyDone == 0. or stdDev == 0:
            return mean

        #
        # Else compute the mass center of the remaining part of the gaussian
        # For instance, if we have a gaussian (mu, sigma) = (3, 4)
        # and that the task is executing for 3 seconds, then we may estimate
        # that it will probably finished at :
        # int(x*gaussian) / int(gaussian) over [3, +inf[
        # (where int is integral)
        #
        # We get 6.192 seconds, that is an expected remaining time of
        # 6.192 - 3 = 3.192 seconds

        # Evaluate primitive at point 'alreadyDone'
        commonPart = erf(self.sqrt2 * (alreadyDone - mean) / (stdDev * 2))
        areaPart1 = 0.5 * commonPart
        massCenterPart1 = (self.sqrt2 / (4 * self.piSqrt)) * (-2 * stdDev * math.exp(-0.5 * ((alreadyDone - mean) ** 2) / (stdDev ** 2)) + mean * (self.piSqrt) * (self.sqrt2) * commonPart)

        # Evaluate primitive at the infinite
        # erf(+inf) = 1, so
        areaPart2 = 0.5
        # exp(-inf) = 0, and erf(+inf) = 1, so
        massCenterPart2 = mean / 2.

        if areaPart1 == areaPart2:
            # So far from the mean that erf(alreadyDone - moyenne) = 1
            previsionPoint = alreadyDone + 0.5      # Hack : lets assume that there's 0.5 sec left...
        else:
            previsionPoint = (massCenterPart2 - massCenterPart1) / (areaPart2 - areaPart1)

        return previsionPoint - alreadyDone

    def put(self, taskObject):
        self.putList((taskObject,))

    def putList(self, tasksList):
        self._actionLock.acquire()

        for taskObject in tasksList:
            self._tDict[taskObject.tid] = taskObject
            self._tQueue.put(taskObject)
        
        self.changed = True
        self._actionLock.release()

    def getTask(self):
        self._actionLock.acquire()
        while True:
            try:
                taskObject = self._tQueue.get_nowait()
            except Queue.Empty:
                self._actionLock.release()
                raise Queue.Empty

            if taskObject.tid in self._tDict:
                del self._tDict[taskObject.tid]
                self.changed = True
                self._actionLock.release()
                return taskObject

    def isTaskIn(self, taskId):
        return taskId in self._tDict

    def _getApproximateLoad(self):
        return self.previousLoad
    
    def getLen(self):
        return len(self._tDict)
    
    def getLoad(self):
        tmpLoad = 0.
        self._actionLock.acquire()
        if not self.changed:
            self._actionLock.release()
            return self.previousLoad
	  
        for tid, tObj in self._tDict.items():
            tmpLoad += self._getTimeInfoAbout(tObj)

        self.previousLoad = tmpLoad
        self.changed = False
        self._actionLock.release()
        return self.previousLoad

    def getSpecificTask(self, taskId):
        self._actionLock.acquire()
        if taskId in self._tDict:
            task = self._tDict[taskId]
            del self._tDict[taskId]
            self.changed = True
        else:
            task = None
        self._actionLock.release()
        return task

    def getTaskByExecTime(self, execTimeWanted, maxDiff= -1.):
        # maxDiff is the max difference wanted
        # If there is no task with the execTimeWanted +- maxDiff,
        # return None
        # maxDiff = -1 means that there is no max difference
        mostClose = None
        self._actionLock.acquire()

        for tid, tObj in self._tDict.items():
            timeDiff = math.abs(execTimeWanted - self._getTimeInfoAbout(tObj))
            if mostClose is None or mostClose[1] > timeDiff:
                mostClose = (tid, timeDiff)

        self._actionLock.release()

        if mostClose is None or (maxDiff >= 0 and mostClose[1] > maxDiff):
            return None
        else:
            return self.getSpecificTask(mostClose[0])

    def getTasksIDsWithExecTime(self, execTimeWanted, maxDiff= -1.):
        # Return task list containing tasks which the durations approximately
        # sum to execTimeWanted
        returnList = []
        totalTime = 0.
        self._actionLock.acquire()

        for tid, tObj in self._tDict.items():
            timeInfo = self._getTimeInfoAbout(tObj)
            if totalTime + timeInfo <= execTimeWanted:
                returnList.append(tid)
                totalTime += timeInfo
        #
        # Hack to avoid the starting bug (few jobs but very long)
        # 
        if len(returnList) == 0 and maxDiff == -1 and len(self._tDict) > 1:
            it = list(filter(lambda x: self._getTimeInfoAbout(x[1]) != 0, self._tDict.items()))
            if len(it) != 0:
                returnList.append(it[0][0])
                totalTime += self._getTimeInfoAbout(it[0][1])
        self._actionLock.release()
        return returnList, totalTime



class Control(object):
    """
    Control is the main DTM class. The dtm object you receive when you use ``from deap import dtm``
    is a proxy over an instance of this class.

    Most of its methods are used by your program, in the execution tasks; however, two of thems (start() and setOptions()) MUST be called
    in the MainThread (i.e. the thread started by the Python interpreter).

    As this class is instancied directly in the module, initializer takes no arguments.
    """
    def __init__(self):
        self.sTime = time.time()
        
        # Key : Target, Value : StatsContainer
        self.tasksStats = {}
        self.tasksStatsLock = threading.Lock()

        # Global execution lock
        self.dtmExecLock = ExecInfo(self.tasksStats, self.tasksStatsLock)

        self.waitingThreadsQueue = TaskQueue(self.tasksStats, self.tasksStatsLock)
        
        # Key : task ID, Value : WaitInfoContainer
        self.waitingThreads = {}
        self.waitingThreadsLock = threading.Lock()


        self.launchTaskLock = threading.Lock()

        self.waitingForRestartQueue = TaskQueue(self.tasksStats, self.tasksStatsLock)
        self.execQueue = TaskQueue(self.tasksStats, self.tasksStatsLock)

        self.recvQueue = Queue.Queue()      # Contains MessageContainer
        self.sendQueue = Queue.Queue()      # Contains MessageContainer

        self.exitStatus = threading.Event()
        self.commExitNotification = threading.Event()
        self.commReadyEvent = threading.Event()

        self.exitState = (None, None)
        self.exitSetHere = False
        
        # Will stop the main thread until an event occurs
        self.runningFlag = threading.Event()
        self.runningFlag.set()

        self.commManagerType = "deap.dtm.commManagerMpi4py"
        self.loadBalancerType = "deap.dtm.loadBalancerPDB"
        self.printExecSummary = True
        
        self.isStarted = False
        
        self.traceMode = False
        self.traceRoot = None   # Root element of the XML log
        self.traceTasks = None  # XML elements
        self.traceComm = None   # defined if traceMode == True
        self.traceLoadB = None
        self.traceLock = threading.Lock()
        
        self.refTime = 1.

        self.loadBalancer = None

        self.lastRetValue = None
        
        self.dtmRandom = random.Random()



    def _doCleanUp(self):
        """
        Clean up function, called at this end of the execution.
        Should NOT be called by the user.
        """
        #if self.printExecSummary:
             #_logger.info("[%s] did %i tasks", str(self.workerId), self._DEBUG_COUNT_EXEC_TASKS)

        if self.exitSetHere:
            for n in self.commThread.iterOverIDs():
                if n == self.workerId:
                    continue
                self.sendQueue.put(MessageContainer(msgType = DTM_MSG_EXIT,
                                    senderWid = self.workerId,
                                    receiverWid = n,
                                    loadsDict = None,
                                    targetsStats = None,
                                    prepTime = time.time(),
                                    sendTime = 0,
                                    ackNbr = -1,
                                    msg = (0, "Exit with success")))

        self.commExitNotification.set()
        self.commThread.join()

        del self.execQueue
        del self.sendQueue
        del self.recvQueue

        countThreads = sum([1 for th in threading.enumerate() if not th.daemon])
        if countThreads > 1:
            _logger.warning("[%s] There's more than 1 active thread (%i) at the exit.", str(self.workerId), threading.activeCount())
        
        
        if self.commThread.isRootWorker:
            if self.printExecSummary:
                msgT = " Tasks execution times summary :\n"
                for target, times in self.tasksStats.items():
                    msgT += "\t" + str(target) + " : Avg=" + str(times.rAvg * self.refTime) + ", StdDev=" + str(times.rStdDev * self.refTime) + "\n\n"
                _logger.info(msgT)

            _logger.info("DTM execution ended (no more tasks)")
            _logger.info("Total execution time : %s", str(time.time() - self.sTime))
        
        if self.traceMode:
            _logger.info("Writing log to the log files...")
            self.traceLock.acquire()
            fstat = etree.SubElement(self.traceRoot, "finalStats")
            for target, times in self.tasksStats.items():
                etree.SubElement(fstat, "taskinfo", {"target" : str(target), "avg" : repr(times.rAvg * self.refTime), "stddev" : repr(times.rStdDev * self.refTime), "execcount" : str(times.execCount)})
            
            flog = open(DTM_LOGDIR_DEFAULT_NAME + "/log" + str(self.workerId) + ".xml", 'w')
            if PRETTY_PRINT_SUPPORT:
                flog.write(etree.tostring(self.traceRoot, pretty_print=True))
            else:
                flog.write(etree.tostring(self.traceRoot))
            flog.close()
            self.traceLock.release()
            
        if self.exitSetHere:
            if self.lastRetValue[0]:
                return self.lastRetValue[1]
            else:
                raise self.lastRetValue[1]


    def _addTaskStat(self, taskKey, timeExec):
        # The execution time is based on the calibration ref time
        comparableLoad = timeExec / self.refTime
        # We do not keep up all the execution times, but
        # update the mean and stddev realtime
        
        self.tasksStatsLock.acquire()
        if not taskKey in self.tasksStats:
            self.tasksStats[taskKey] = StatsContainer(rAvg = timeExec,
                                                rStdDev = 0.,
                                                rSquareSum = timeExec * timeExec,
                                                execCount = 1)
        else:
            oldAvg = self.tasksStats[taskKey].rAvg
            oldStdDev = self.tasksStats[taskKey].rStdDev
            oldSum2 = self.tasksStats[taskKey].rSquareSum
            oldExecCount = self.tasksStats[taskKey].execCount

            self.tasksStats[taskKey].rAvg = (timeExec + oldAvg * oldExecCount) / (oldExecCount + 1)
            self.tasksStats[taskKey].rSquareSum = oldSum2 + timeExec * timeExec
            self.tasksStats[taskKey].rStdDev = abs(self.tasksStats[taskKey].rSquareSum / (oldExecCount + 1) - self.tasksStats[taskKey].rAvg ** 2) ** 0.5
            self.tasksStats[taskKey].execCount = oldExecCount + 1

        self.tasksStatsLock.release()


    def _calibrateExecTime(self, runsN=3):
        """
        Small calibration test, run at startup
        Should not be called by user
        """
        timesList = []
        for r in range(runsN):
            timeS = time.clock()

            a = 0.
            for i in range(10000):
                a = math.sqrt(self.dtmRandom.random() / (self.dtmRandom.uniform(0, i) + 1))

            strT = ""
            for i in range(5000):
                strT += str(self.dtmRandom.randint(0, 9999))

            for i in range(500):
                pickStr = pickle.dumps(strT)
                strT = pickle.loads(pickStr)

            timesList.append(time.clock() - timeS)

        return sorted(timesList)[int(runsN / 2)]


    def _getLoadTuple(self):
        return (self.dtmExecLock.getLoad(), self.execQueue.getLoad(), self.waitingForRestartQueue.getLoad(), self.waitingThreadsQueue.getLoad())


    def _returnResult(self, idToReturn, resultInfo):
        """
        Called by the execution threads when they have to return a result
        Should NOT be called explicitly by the user
        """
        if idToReturn == self.workerId:
            self._dispatchResults((resultInfo,))
        else:
            self.sendQueue.put(MessageContainer(msgType = DTM_MSG_RESULT,
                                            senderWid = self.workerId,
                                            receiverWid = idToReturn,
                                            loadsDict = self.loadBalancer.getNodesDict(),
                                            targetsStats = self.tasksStats,
                                            prepTime = time.time(),
                                            sendTime = 0,
                                            ackNbr = -1,
                                            msg = (resultInfo,)))

    def _updateStats(self, msg):
        """
        Called by the control thread to update its dictionnary
        Should NOT be called explicitly by the user
        """
        self.loadBalancer.mergeNodeStatus(msg.loadsDict)

        self.tasksStatsLock.acquire()

        for key, val in msg.targetsStats.items():
            if not key in self.tasksStats or val.execCount > self.tasksStats[key].execCount:
                self.tasksStats[key] = val

        self.tasksStatsLock.release()


    def _dispatchResults(self, resultsList):
        """
        Called by the control thread when a message is received;
        Dispatch it to the task waiting for it.
        Should NOT be called explicitly by the user
        """
        for result in resultsList:
            self.waitingThreadsLock.acquire()
            
            # We look for the task waiting for each result
            foundKey = None
            for taskKey in self.waitingThreads[result.parentTid].rWaitingDict:
                try:
                    self.waitingThreads[result.parentTid].rWaitingDict[taskKey].tids.remove(result.tid)
                except ValueError:
                    # The ID is not in this waiting list, continue with other worker
                    continue
                
                foundKey = taskKey
                if isinstance(self.waitingThreads[result.parentTid].rWaitingDict[taskKey].result, list):
                    if not result.success:
                        # Exception occured
                        self.waitingThreads[result.parentTid].rWaitingDict[taskKey].result = result.result
                    else:
                        self.waitingThreads[result.parentTid].rWaitingDict[taskKey].result[result.taskIndex] = result.result
                break
            
            assert not foundKey is None, "Parent task not found for result dispatch!"       # Debug

            if len(self.waitingThreads[result.parentTid].rWaitingDict[foundKey].tids) == 0:
                # All tasks done
                self.waitingThreads[result.parentTid].rWaitingDict[foundKey].finished = True
                if isinstance(self.waitingThreads[result.parentTid].rWaitingDict[foundKey].result, list):
                    self.waitingThreads[result.parentTid].rWaitingDict[foundKey].success = True
                
                canRestart = False
                if self.waitingThreads[result.parentTid].rWaitingDict[foundKey].waitingOn == True or self.waitingThreads[result.parentTid].waitingMode == DTM_WAIT_ALL:
                    if (self.waitingThreads[result.parentTid].waitingMode == DTM_WAIT_ALL and len(self.waitingThreads[result.parentTid].rWaitingDict) == 1)\
                      or self.waitingThreads[result.parentTid].waitingMode == DTM_WAIT_ANY:
                        canRestart = True
                    elif self.waitingThreads[result.parentTid].waitingMode == DTM_WAIT_SOME:
                        canRestart = True
                        
                        for rKey in self.waitingThreads[result.parentTid].rWaitingDict: 
                            if self.waitingThreads[result.parentTid].rWaitingDict[rKey].waitingOn and rKey != foundKey:
                                canRestart = False
                
                if not self.waitingThreads[result.parentTid].rWaitingDict[taskKey].callbackFunc is None:
                    self.waitingThreads[result.parentTid].rWaitingDict[taskKey].callbackFunc()
                
                if canRestart:
                    wTask = self.waitingThreadsQueue.getSpecificTask(result.parentTid)
                    assert not wTask is None
                    self.waitingForRestartQueue.put(wTask)
                
            self.waitingThreadsLock.release()
            
        

    def _startNewTask(self):
        """
        Start a new task (if there's one available)
        Return True if so
        Should NOT be called explicitly by the user
        """
        taskLauched = False
        self.launchTaskLock.acquire()
        if not self.dtmExecLock.isLocked():
            try:
                wTask = self.waitingForRestartQueue.getTask()
                self.dtmExecLock.acquire(wTask)
                wTask.threadObject.waitingFlag.set()
                taskLauched = True
            except Queue.Empty:
                pass

            if not taskLauched:
                try:
                    newTask = self.execQueue.getTask()
                    if self.traceMode:
                        self.traceLock.acquire()
                        newTaskElem = etree.SubElement(self.traceTasks, "task",
                                                       {"id" : str(newTask.tid),
                                                        "creatorTid" : str(newTask.creatorTid),
                                                        "creatorWid" : str(newTask.creatorWid),
                                                        "taskIndex" : str(newTask.taskIndex),
                                                        "creationTime" : repr(newTask.creationTime)})
                        try:
                            newTaskTarget = etree.SubElement(newTaskElem, "target",
                                                         {"name" : str(newTask.target.__name__)})
                        except AttributeError:
                            newTaskTarget = etree.SubElement(newTaskElem, "target",
                                                         {"name" : str(newTask.target)})
                            
                        for i, a in enumerate(newTask.args):
                            newTaskTarget.set("arg" + str(i), str(a))
                        for k in newTask.kwargs:
                            newTaskTarget.set("kwarg_" + str(k), str(newTask.kwargs[k]))
                        
                        newTaskPath = etree.SubElement(newTaskElem, "path", {"data" : str(newTask.taskRoute)})
                        self.traceLock.release()
                        
                        newThread = DtmThread(newTask, self, newTaskElem)
                    else:
                        newThread = DtmThread(newTask, self)
                    self.dtmExecLock.acquire(newTask)
                    newThread.start()
                    taskLauched = True
                except Queue.Empty:
                    pass
            
        self.launchTaskLock.release()
        return taskLauched
    
    def _main(self):
        """
        Main loop of the control thread
        Should NOT be called explicitly by the user
        """
        
        timeBegin = time.time()
        while True:
            
            self.runningFlag.wait()         # WARNING, may deadlock on very specific conditions
            self.runningFlag.clear()        # (if the _last_ task do a set() between those 2 lines, nothing will wake up the main thread)
            
            while True:
                try:
                    recvMsg = self.recvQueue.get_nowait()
                    if recvMsg.msgType == DTM_MSG_EXIT:
                        self.exitStatus.set()
                        self.exitState = (recvMsg.msg[1], recvMsg.msg[0])
                        break
                    elif recvMsg.msgType == DTM_MSG_TASK:
                        self.execQueue.putList(recvMsg.msg)
                        self.loadBalancer.updateSelfStatus(self._getLoadTuple())
                        self.sendQueue.put(MessageContainer(msgType = DTM_MSG_ACK_RECEIVED_TASK,
                                                        senderWid = self.workerId,
                                                        receiverWid = recvMsg.senderWid,
                                                        loadsDict = self.loadBalancer.getNodesDict(),
                                                        targetsStats = self.tasksStats,
                                                        prepTime = time.time(),
                                                        sendTime = 0,
                                                        ackNbr = -1,
                                                        msg = recvMsg.ackNbr))
                        self._updateStats(recvMsg)
                    elif recvMsg.msgType == DTM_MSG_RESULT:
                        self._dispatchResults(recvMsg.msg)
                        self._updateStats(recvMsg)
                    elif recvMsg.msgType == DTM_MSG_REQUEST_TASK:
                        self._updateStats(recvMsg)
                    elif recvMsg.msgType == DTM_MSG_ACK_RECEIVED_TASK:
                        self.loadBalancer.acked(recvMsg.senderWid, recvMsg.msg)
                        self._updateStats(recvMsg)
                    else:
                        _logger.warning("[%s] Unknown message type %s received will be ignored.", str(self.workerId), str(recvMsg.msgType))
                except Queue.Empty:
                    break

	    
            if self.exitStatus.is_set():
                break
            
            currentNodeStatus = self._getLoadTuple()
            self.loadBalancer.updateSelfStatus(currentNodeStatus)

            sendUpdateList, sendTasksList = self.loadBalancer.takeDecision()
            self.tasksStatsLock.acquire()
            for sendInfo in sendTasksList:
                self.sendQueue.put(MessageContainer(msgType = DTM_MSG_TASK,
                                            senderWid = self.workerId,
                                            receiverWid = sendInfo[0],
                                            loadsDict = self.loadBalancer.getNodesDict(),
                                            targetsStats = self.tasksStats,
                                            prepTime = time.time(),
                                            sendTime = 0,
                                            ackNbr = sendInfo[2],
                                            msg = sendInfo[1]))

            for updateTo in sendUpdateList:
                self.sendQueue.put(DtmMessageContainer(msgType = DTM_MSG_REQUEST_TASK,
                                            senderWid = self.workerId,
                                            receiverWid = updateTo,
                                            loadsDict = self.loadBalancer.getNodesDict(),
                                            targetsStats = self.tasksStats,
                                            prepTime = time.time(),
                                            sendTime = 0,
                                            ackNbr = -1,
                                            msg = None))
            self.tasksStatsLock.release()
            self._startNewTask()
        return self._doCleanUp()


    def setOptions(self, *args, **kwargs):
        """
        Set a DTM global option.
        
        .. warning::
            This function must be called BEFORE ``start()``. It is also the user responsability to ensure that the same option is set on every worker.

        Currently, the supported options are :
            * **communicationManager** : can be *deap.dtm.mpi4py* (default) or *deap.dtm.commManagerTCP*.
            * **loadBalancer** : currently only the default *PDB* is available.
            * **printSummary** : if set, DTM will print a task execution summary at the end (mean execution time of each tasks, how many tasks did each worker do, ...)
            * **setTraceMode** : if set, will enable a special DTM tracemode. In this mode, DTM logs all its activity in XML files (one by worker). Mainly for DTM debug purpose, but can also be used for profiling.
        
        This function can be called more than once. Any unknown parameter will have no effect.
        """
        if self.isStarted:
            if self.commThread.isRootWorker:
                _logger.warning("dtm.setOptions() was called after dtm.start(); options will not be considered")
            return
            
        for opt in kwargs:
            if opt == "communicationManager":
                self.commManagerType = kwargs[opt]
            elif opt == "loadBalancer":
                self.loadBalancerType = kwargs[opt]
            elif opt == "printSummary":
                self.printExecSummary = kwargs[opt]
            elif opt == "setTraceMode":
                self.traceMode = kwargs[opt]
            elif self.commThread.isRootWorker:
                _logger.warning("Unknown option '%s'", opt)


    def start(self, initialTarget, *args, **kwargs):
        """
        Start the execution with the target `initialTarget`.
        Calling this function create and launch the first task on the root worker
        (defined by the communication manager, for instance, with MPI, the root worker is the worker with rank 0.).

        .. warning::
            This function must be called only ONCE, and after the target has been parsed by the Python interpreter.
        """
        self.isStarted = True
       
        try:
            tmpImport = __import__(self.commManagerType, globals(), locals(), ['CommThread'], 0)
            if not hasattr(tmpImport, 'CommThread'):
                raise ImportError
            CommThread = tmpImport.CommThread
        except ImportError:
            _logger.warning("Warning : %s is not a suitable communication manager. Default to commManagerMpi4py.", self.commManagerType)
            tmpImport = __import__('deap.dtm.commManagerMpi4py', globals(), locals(), ['CommThread'], 0)
            CommThread = tmpImport.CommThread

        try:
            tmpImport = __import__(self.loadBalancerType, globals(), locals(), ['LoadBalancer'], 0)
            if not hasattr(tmpImport, 'LoadBalancer'):
                raise ImportError
            LoadBalancer = tmpImport.LoadBalancer
        except ImportError:
            _logger.warning("Warning : %s is not a suitable load balancer. Default to loadBalancerPDB.", self.loadBalancerType)
            tmpImport = __import__('deap.dtm.loadBalancerPDB', globals(), locals(), ['LoadBalancer'], 0)
            LoadBalancer = tmpImport.LoadBalancer
        
        self.commThread = CommThread(self.recvQueue, self.sendQueue, self.runningFlag, self.commExitNotification, self.commReadyEvent, self.dtmRandom, sys.argv)

        self.commThread.start()
        self.refTime = self._calibrateExecTime()

        self.commReadyEvent.wait()
        
        if self.commThread.isLaunchProcess:
            sys.exit()

        self.poolSize = self.commThread.poolSize
        self.workerId = self.commThread.workerId

        self.idGenerator = TaskIdGenerator(self.workerId)

        self.loadBalancer = LoadBalancer(self.commThread.iterOverIDs(), self.workerId, self.execQueue, self.dtmRandom)
        
        if self.traceMode:
            self.traceLock.acquire()
            self.traceRoot = etree.Element("dtm", {"version" : str(0.7), "workerId" : str(self.workerId), "timeBegin" : repr(self.sTime)})
            self.traceTasks = etree.SubElement(self.traceRoot, "tasksLog")
            self.traceComm = etree.SubElement(self.traceRoot, "commLog")
            self.traceLoadB = etree.SubElement(self.traceRoot, "loadBalancerLog")
            self.traceLock.release()
            self.commThread.setTraceModeOn(self.traceComm)
            self.loadBalancer.setTraceModeOn(self.traceLoadB)
        
        if self.commThread.isRootWorker:
            
            if self.traceMode:
                # Create the log folder
                try:
                    os.mkdir(DTM_LOGDIR_DEFAULT_NAME)
                except OSError:
                    _logger.warning("Log folder '" + DTM_LOGDIR_DEFAULT_NAME + "' already exists!")
                        
            
            _logger.info("DTM started with %i workers", self.poolSize)
            _logger.info("DTM load balancer is %s, and communication manager is %s", self.loadBalancerType, self.commManagerType)
            
            initTask = TaskContainer(tid = self.idGenerator.tid,
                                    creatorWid = self.workerId,
                                    creatorTid = None,
                                    taskIndex = 0,
                                    taskRoute = [self.workerId],
                                    creationTime = time.time(),
                                    target = initialTarget,
                                    args = args,
                                    kwargs = kwargs,
                                    threadObject = None,
                                    taskState = DTM_TASK_STATE_IDLE)
            self.execQueue.put(initTask)

        return self._main()



    # The following methods are NOT called by the control thread, but by the EXECUTION THREADS
    # All the non-local objects used MUST be thread-safe

    def map(self, function, *iterables, **kwargs):
        """
        A parallel equivalent of the :func:`map` built-in function. It blocks till the result is ready.
        This method chops the iterables into a number of chunks determined by DTM in order to get the most efficient use of the workers.
        It takes any number of iterables (though it will shrink all of them to the len of the smallest one), and any others kwargs that will be
        transmitted as is to the *function* target.
        """
        cThread = threading.currentThread()
        currentId = cThread.tid
        
        zipIterable = list(zip(*iterables))
        
        listResults = [None] * len(zipIterable)
        listTasks = []
        listTids = []
        
        for index, elem in enumerate(zipIterable):
            task = TaskContainer(tid = self.idGenerator.tid,
                                    creatorWid = self.workerId,
                                    creatorTid = currentId,
                                    taskIndex = index,
                                    taskRoute = [self.workerId],
                                    creationTime = time.time(),
                                    target = function,
                                    args = elem,
                                    kwargs = kwargs,
                                    threadObject = None,
                                    taskState = DTM_TASK_STATE_IDLE)
            listTasks.append(task)
            listTids.append(task.tid)
        
        if self.traceMode:
            self.traceLock.acquire()
            newTaskElem = etree.SubElement(cThread.xmlTrace, "event",
                                           {"type" : "map",
                                            "time" : repr(time.time()),
                                            "target" : str(function.__name__),
                                            "childTasks" : str(listTids)})
            self.traceLock.release()
        
        self.waitingThreadsLock.acquire()        
        if currentId not in self.waitingThreads.keys():
            self.waitingThreads[currentId] = WaitInfoContainer(threadObject = cThread,
                                                    eventObject = cThread.waitingFlag,
                                                    waitBeginningTime = 0,
                                                    tasksWaitingCount = 0,
                                                    waitingMode = DTM_WAIT_NONE,
                                                    rWaitingDict = {})
        
        resultKey = listTids[0]
        self.waitingThreads[currentId].rWaitingDict[resultKey] = ExceptedResultContainer(tids = listTids,
                                waitingOn = True,
                                finished = False,
                                success = False,
                                callbackFunc = None,
                                result = listResults)
                                
        self.waitingThreads[currentId].tasksWaitingCount += len(listTasks)
        self.waitingThreads[currentId].waitingMode = DTM_WAIT_SOME
        
        
        self.waitingThreadsQueue.put(cThread.taskInfo)
        
        self.waitingThreads[currentId].waitBeginningTime = time.time()
        self.waitingThreadsLock.release()
        
        self.execQueue.putList(listTasks)
        
        
        cThread.waitForResult()

        self.waitingThreadsLock.acquire()
        ret = self.waitingThreads[currentId].rWaitingDict[resultKey].result
        if self.waitingThreads[currentId].rWaitingDict[resultKey].success == False:
            # Exception occured
            del self.waitingThreads[currentId].rWaitingDict[resultKey]
            self.waitingThreadsLock.release()
            raise ret
        else:           
            del self.waitingThreads[currentId].rWaitingDict[resultKey]
            self.waitingThreadsLock.release()
            return ret


    def map_async(self, function, iterable, callback=None):
        """
        A non-blocking variant of the :func:`~deap.dtm.taskmanager.Control.map` method which returns a :class:`~deap.dtm.taskmanager.AsyncResult` object.
        
        .. note::
            As on version 0.2, callback is not implemented.
        """
        
        cThread = threading.currentThread()
        currentId = cThread.tid

        listResults = [None] * len(iterable)
        listTasks = []
        listTids = []

        for index, elem in enumerate(iterable):
            task = TaskContainer(tid = self.idGenerator.tid,
                                    creatorWid = self.workerId,
                                    creatorTid = currentId,
                                    taskIndex = index,
                                    taskRoute = [self.workerId],
                                    creationTime = time.time(),
                                    target = function,
                                    args = (elem,),
                                    kwargs = {},
                                    threadObject = None,
                                    taskState = DTM_TASK_STATE_IDLE)
            listTasks.append(task)
            listTids.append(task.tid)
        
        resultKey = listTids[0]
        
        if self.traceMode:
            newTaskElem = etree.SubElement(cThread.xmlTrace, "event",
                                           {"type" : "map_async",
                                            "time" : repr(time.time()),
                                            "target" : str(function.__name__),
                                            "childTasks" : str(listTids)})
        
        self.waitingThreadsLock.acquire()
        
        if currentId not in self.waitingThreads.keys():
            self.waitingThreads[currentId] = WaitInfoContainer(threadObject = cThread,
                                                    eventObject = cThread.waitingFlag,
                                                    waitBeginningTime = 0,
                                                    tasksWaitingCount = 0,
                                                    waitingMode = DTM_WAIT_NONE,
                                                    rWaitingDict = {})
                                                    
        self.waitingThreads[currentId].rWaitingDict[resultKey] = ExceptedResultContainer(tids = listTids,
                                waitingOn = False,
                                finished = False,
                                success = False,
                                callbackFunc = None,
                                result = listResults)

        self.waitingThreads[currentId].waitingMode = DTM_WAIT_NONE
        
        asyncRequest = AsyncResult(self, self.waitingThreads[currentId], resultKey)
        self.waitingThreads[currentId].rWaitingDict[resultKey].callbackFunc = asyncRequest._dtmCallback
        
        self.waitingThreadsLock.release()
        
        self.execQueue.putList(listTasks)
        
        self.runningFlag.set()
        
        return asyncRequest


    def apply(self, function, *args, **kwargs):
        """
        Equivalent of the :func:`apply` built-in function. It blocks till the result is ready.
        Given this blocks, :func:`~deap.dtm.taskmanager.Control.apply_async()` is better suited for performing work in parallel.
        Additionally, the passed in function is only executed in one of the workers of the pool.
        """
        cThread = threading.currentThread()
        currentId = cThread.tid
        
        task = TaskContainer(tid = self.idGenerator.tid,
                                    creatorWid = self.workerId,
                                    creatorTid = currentId,
                                    taskIndex = 0,
                                    taskRoute = [self.workerId],
                                    creationTime = time.time(),
                                    target = function,
                                    args = args,
                                    kwargs = kwargs,
                                    threadObject = None,
                                    taskState = DTM_TASK_STATE_IDLE)
        
        if self.traceMode:
            newTaskElem = etree.SubElement(cThread.xmlTrace, "event",
                                           {"type" : "apply",
                                            "time" : repr(time.time()),
                                            "target" : str(function.__name__),
                                            "childTasks" : str([task.tid])})
        
        self.waitingThreadsLock.acquire()
        if currentId not in self.waitingThreads.keys():
            self.waitingThreads[currentId] = WaitInfoContainer(threadObject = cThread,
                                                    eventObject = cThread.waitingFlag,
                                                    waitBeginningTime = 0,
                                                    tasksWaitingCount = 0,
                                                    waitingMode = DTM_WAIT_NONE,
                                                    rWaitingDict = {})
        
        resultKey = task.tid
        
        self.waitingThreads[currentId].rWaitingDict[resultKey] = ExceptedResultContainer(tids = [task.tid],
                                waitingOn = True,
                                finished = False,
                                success = False,
                                callbackFunc = None,
                                result = [None])
        
        self.waitingThreads[currentId].tasksWaitingCount += 1
        self.waitingThreads[currentId].waitingMode = DTM_WAIT_SOME
        
        self.waitingThreadsQueue.put(cThread.taskInfo)
        
        self.waitingThreads[currentId].waitBeginningTime = time.time()
        self.waitingThreadsLock.release()
        
        self.execQueue.put(task)
        

        cThread.waitForResult()

        self.waitingThreadsLock.acquire()
        ret = self.waitingThreads[currentId].rWaitingDict[resultKey].result
        if self.waitingThreads[currentId].rWaitingDict[resultKey].success == False:
            # Exception occured
            del self.waitingThreads[currentId].rWaitingDict[resultKey]
            self.waitingThreadsLock.release()
            raise ret
        else:           
            del self.waitingThreads[currentId].rWaitingDict[resultKey]
            self.waitingThreadsLock.release()
            return ret[0]

    def apply_async(self, function, *args, **kwargs):
        """
        A non-blocking variant of the :func:`~deap.dtm.taskmanager.Control.apply` method which returns a :class:`~deap.dtm.taskmanager.AsyncResult` object.
        """
        cThread = threading.currentThread()
        currentId = cThread.tid
        
        task = TaskContainer(tid = self.idGenerator.tid,
                                    creatorWid = self.workerId,
                                    creatorTid = currentId,
                                    taskIndex = 0,
                                    taskRoute = [self.workerId],
                                    creationTime = time.time(),
                                    target = function,
                                    args = args,
                                    kwargs = kwargs,
                                    threadObject = None,
                                    taskState = DTM_TASK_STATE_IDLE)
        
        if self.traceMode:
            newTaskElem = etree.SubElement(cThread.xmlTrace, "event",
                                           {"type" : "apply_async",
                                            "time" : repr(time.time()),
                                            "target" : str(function.__name__),
                                            "childTasks" : str([task.tid])})
        
        self.waitingThreadsLock.acquire()        
        if currentId not in self.waitingThreads.keys():
            self.waitingThreads[currentId] = WaitInfoContainer(threadObject = cThread,
                                                    eventObject = cThread.waitingFlag,
                                                    waitBeginningTime = 0,
                                                    tasksWaitingCount = 0,
                                                    waitingMode = DTM_WAIT_NONE,
                                                    rWaitingDict = {})
        
        resultKey = task.tid
        
        self.waitingThreads[currentId].rWaitingDict[resultKey] = ExceptedResultContainer(tids = [task.tid],
                                waitingOn = False,
                                finished = False,
                                success = False,
                                callbackFunc = None,
                                result = [None])
        
        self.waitingThreads[currentId].waitingMode = DTM_WAIT_NONE
        
        asyncRequest = AsyncResult(self, self.waitingThreads[currentId], resultKey)
        self.waitingThreads[currentId].rWaitingDict[resultKey].callbackFunc = asyncRequest._dtmCallback
        
        self.waitingThreadsLock.release()
        
        self.execQueue.put(task)
        
        self.runningFlag.set()
        return asyncRequest

    def imap(self, function, iterable, chunksize=1):
        """
        An equivalent of :func:`itertools.imap`.

        The chunksize argument can be used to tell DTM how many elements should be computed at the same time.
        For very long iterables using a large value for chunksize can make make the job complete much faster than using the default value of 1.
        """
        currentIndex = 0

        while currentIndex < len(iterable):
            maxIndex = currentIndex + chunksize if currentIndex + chunksize < len(iterable) else len(iterable)
            asyncResults = [None] * (maxIndex - currentIndex)
            for i in range(currentIndex, maxIndex):
                asyncResults[i % chunksize] = self.apply_async(function, iterable[i])

            for result in asyncResults:
                ret = result.get()
                yield ret

            currentIndex = maxIndex


    def imap_unordered(self, function, iterable, chunksize=1):
        """
        Not implemented yet.
        """
        raise NotImplementedError


    def filter(self, function, iterable):
        """
        Same behavior as the built-in :func:`filter`. The filtering is done localy, but the computation is distributed.
        """
        results = self.map(function, iterable)
        return [item for result, item in zip(results, iterable) if result]

    def repeat(self, function, n, *args, **kwargs):
        """
        Repeat the function *function* *n* times, with given args and keyworded args.
        Return a list containing the results.
        """
        cThread = threading.currentThread()
        currentId = cThread.tid

        listResults = [None] * n
        listTasks = []
        listTids = []
        
        for index in range(n):
            task = TaskContainer(tid = self.idGenerator.tid,
                                    creatorWid = self.workerId,
                                    creatorTid = currentId,
                                    taskIndex = index,
                                    taskRoute = [self.workerId],
                                    creationTime = time.time(),
                                    target = function,
                                    args = args,
                                    kwargs = kwargs,
                                    threadObject = None,
                                    taskState = DTM_TASK_STATE_IDLE)
            listTasks.append(task)
            listTids.append(task.tid)
        
        self.waitingThreadsLock.acquire()        
        if currentId not in self.waitingThreads.keys():
            self.waitingThreads[currentId] = WaitInfoContainer(threadObject = cThread,
                                                    eventObject = cThread.waitingFlag,
                                                    waitBeginningTime = 0,
                                                    tasksWaitingCount = 0,
                                                    waitingMode = DTM_WAIT_NONE,
                                                    rWaitingDict = {})
        
        resultKey = listTids[0]
        self.waitingThreads[currentId].rWaitingDict[resultKey] = ExceptedResultContainer(tids = listTids,
                                waitingOn = True,
                                finished = False,
                                success = False,
                                callbackFunc = None,
                                result = listResults)
                                
        self.waitingThreads[currentId].tasksWaitingCount += len(listTasks)
        self.waitingThreads[currentId].waitingMode = DTM_WAIT_SOME
        
        
        self.waitingThreadsQueue.put(cThread.taskInfo)
        
        self.waitingThreads[currentId].waitBeginningTime = time.time()
        self.waitingThreadsLock.release()
        
        self.execQueue.putList(listTasks)
        
        
        cThread.waitForResult()

        self.waitingThreadsLock.acquire()
        ret = self.waitingThreads[currentId].rWaitingDict[resultKey].result
        if self.waitingThreads[currentId].rWaitingDict[resultKey].success == False:
            # Exception occured
            del self.waitingThreads[currentId].rWaitingDict[resultKey]
            self.waitingThreadsLock.release()
            raise ret
        else:           
            del self.waitingThreads[currentId].rWaitingDict[resultKey]
            self.waitingThreadsLock.release()
            return ret

    def waitForAll(self):
        """
        Wait for all pending asynchronous results. When this function returns,
        DTM guarantees that all ready() call on asynchronous tasks will
        return true.
        """
        threadId = threading.currentThread().tid
        
        self.waitingThreadsLock.acquire()
        if threadId in self.waitingThreads and len(self.waitingThreads[threadId].rWaitingDict) > 0:
            self.waitingThreads[threadId].waitingMode = DTM_WAIT_ALL
            self.waitingThreadsQueue.put(threading.currentThread().taskInfo)
            self.waitingThreadsLock.release()
            threading.currentThread().waitForResult()
            
            self.waitingThreadsLock.acquire()
            self.waitingThreads[threadId].waitingMode = DTM_WAIT_NONE           
            self.waitingThreadsLock.release()
        else:            
            self.waitingThreadsLock.release()
            return        
        return None

    def testAllAsync(self):
        """
        Check whether all pending asynchronous tasks are done.
        It does not lock if it is not the case, but returns false.
        """
        threadId = threading.currentThread().tid
        self.waitingThreadsLock.acquire()
        if threadId in self.waitingThreads:
            ret = len(self.waitingThreads[threadId].rWaitingDict)
            self.waitingThreadsLock.release()
            return False
        else:
            self.waitingThreadsLock.release()
            return True


    def getWorkerId(self):
        """
        Return a unique ID for the current worker. Depending of the
        communication manager type, it can be virtually any Python
        immutable type.
        
        .. note::
            With MPI, the value returned is the MPI slot number.
        """
        return self.workerId




class DtmThread(threading.Thread):
    """
    DTM execution threads. Those are one of the main parts of DTM.
    They should not be created or called directly by the user.
    """
    def __init__(self, structInfo, controlThread, xmlTrace=None):
        threading.Thread.__init__(self)
        
        self.taskInfo = structInfo      # TaskContainer
        
        self.taskInfo.threadObject = self   # Remind that we are the exec thread
        
        self.tid = structInfo.tid
        self.t = structInfo.target
        self.control = controlThread

        self.waitingFlag = threading.Event()        
        self.waitingFlag.clear()
        
        self.timeExec = 0
        self.timeBegin = 0
        if structInfo.creatorTid is None:
            self.isRootTask = True
        else:
            self.isRootTask = False
        
        self.xmlTrace = xmlTrace

    def run(self):
        # The lock is already acquired for us
        self.taskInfo.taskState = DTM_TASK_STATE_RUNNING
        success = True
        self.timeBegin = time.time()
        if not self.xmlTrace is None:
            # Debug output in xml object
            self.control.traceLock.acquire()
            etree.SubElement(self.xmlTrace, "event", {"type" : "begin", "worker" : str(self.control.workerId), "time" : repr(self.timeBegin)})
            self.control.traceLock.release()
            
        try:
            returnedR = self.t(*self.taskInfo.args, **self.taskInfo.kwargs)
        except Exception as expc:
            returnedR = expc
            strWarn = "An exception of type " + str(type(expc)) + " occured on worker " + str(self.control.workerId) + " while processing task " + str(self.tid)
            _logger.warning(strWarn)
            _logger.warning("This will be transfered to the parent task.")
            _logger.warning("Exception details : " + str(expc))
            success = False
            
        
        self.timeExec += time.time() - self.timeBegin
        
        self.control.dtmExecLock.release()
        
        if not self.xmlTrace is None:
            # Debug output in xml object
            self.control.traceLock.acquire()
            etree.SubElement(self.xmlTrace, "event", {"type" : "end", "worker" : str(self.control.workerId), "time" : repr(time.time()), "execTime" : repr(self.timeExec), "success" : str(success)})
            self.control.traceLock.release()
        
        if success:
            try:
                self.control._addTaskStat(self.t.__name__, self.timeExec)
            except AttributeError:
                self.control._addTaskStat(str(self.t), self.timeExec)

        
        if self.isRootTask:
            # Is this task the root task (launch by dtm.start)? If so, we quit
            self.control.lastRetValue = (success, returnedR)
            self.control.exitSetHere = True
            self.control.exitStatus.set()
        else:
            # Else, tell the communication thread to return the result
            resultStruct = ResultContainer(tid = self.tid,
                                            parentTid = self.taskInfo.creatorTid,
                                            taskIndex = self.taskInfo.taskIndex,
                                            execTime = self.timeExec,
                                            success = success,
                                            result = returnedR)
                                            
            self.control._returnResult(self.taskInfo.creatorWid, resultStruct)

        # Tell the control thread that something happened
        self.control._startNewTask()
        
        if self.isRootTask:
            self.control.runningFlag.set()
        
        self.control.waitingThreadsLock.acquire()
        if self.tid in self.control.waitingThreads.keys():
            del self.control.waitingThreads[self.tid]
        self.control.waitingThreadsLock.release()
        self.taskInfo.taskState = DTM_TASK_STATE_FINISH
    

    def waitForResult(self):
        # Clear the execution lock, and sleep
        beginTimeWait = time.time()
        self.timeExec += beginTimeWait - self.timeBegin
        self.control.dtmExecLock.release()
        
        self.taskInfo.taskState = DTM_TASK_STATE_WAITING
        
        if not self.xmlTrace is None:
            # Debug output in xml object
            self.control.traceLock.acquire()
            etree.SubElement(self.xmlTrace, "event", {"type" : "sleep", "worker" : str(self.control.workerId), "time" : repr(beginTimeWait)})
            self.control.traceLock.release()
        
        self.control._startNewTask()
        self.control.runningFlag.set()
        
        self.waitingFlag.wait()        
        self.waitingFlag.clear()

        # At this point, we already have acquired the execution lock
        self.taskInfo.taskState = DTM_TASK_STATE_RUNNING
        self.timeBegin = time.time()
        
        if not self.xmlTrace is None:
            # Debug output in xml object
            self.control.traceLock.acquire()
            etree.SubElement(self.xmlTrace, "event", {"type" : "wakeUp", "worker" : str(self.control.workerId), "time" : repr(self.timeBegin)})
            self.control.traceLock.release()



class AsyncResult(object):
    """
    The class of the result returned by :func:`~deap.dtm.taskmanager.Control.map_async()` and :func:`~deap.dtm.taskmanager.Control.apply_async()`.
    """
    def __init__(self, control, waitingInfo, taskKey):
        self.control = control
        self.resultReturned = False
        self.resultSuccess = False
        self.resultVal = None
        self.taskKey = taskKey
        self.dictWaitingInfo = waitingInfo
        
    
    def _dtmCallback(self):
        # Used by DTM to inform the object that the job is done
        self.resultSuccess = self.dictWaitingInfo.rWaitingDict[self.taskKey].success
        self.resultVal = self.dictWaitingInfo.rWaitingDict[self.taskKey].result
        self.resultReturned = True
        
        del self.dictWaitingInfo.rWaitingDict[self.taskKey]


    def get(self):
        """
        Return the result when it arrives.
        
        .. note::
            This is a blocking call : caller will wait in this function until the result is ready.
            To check for the avaibility of the result, use :func:`~deap.dtm.taskmanager.AsyncResult.ready()`.
        """
        if not self.resultReturned:
            self.wait()
        
        if self.resultSuccess:
            return self.resultVal
        else:
            raise self.resultVal

    def wait(self):
        """
        Wait until the result is available
        """
        
        self.control.waitingThreadsLock.acquire()
        
        if self.ready():
            # This test MUST be protected by the mutex on waitingThreads
            self.control.waitingThreadsLock.release()
            return
        
        self.control.waitingThreads[threading.currentThread().tid].waitingMode = DTM_WAIT_SOME
        self.control.waitingThreads[threading.currentThread().tid].rWaitingDict[self.taskKey].waitingOn = True
        #self.dictWaitingInfo.waitingMode = DTM_WAIT_SOME
        #self.dictWaitingInfo.rWaitingDict[self.taskKey].waitingOn = True
        self.control.waitingThreadsQueue.put(threading.currentThread().taskInfo)
        self.control.waitingThreadsLock.release()

        threading.currentThread().waitForResult()


    def ready(self):
        """
        Return whether the asynchronous task has completed.
        """
        return self.resultReturned

    def successful(self):
        """
        Return whether the task completed without error. Will raise AssertionError if the result is not ready.
        """
        if not self.resultReturned:
            raise AssertionError("Call AsyncResult.successful() before the results were ready!")
        return self.resultSuccess