File size: 21,050 Bytes
3d4d40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf49f13
3d4d40d
cf49f13
3d4d40d
 
 
 
 
 
 
 
 
 
 
 
 
 
cf49f13
3d4d40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf49f13
3d4d40d
cf49f13
3d4d40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf49f13
 
3d4d40d
 
 
 
 
 
 
 
 
cf49f13
3d4d40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf49f13
 
 
 
 
 
 
 
 
 
 
3d4d40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf49f13
3d4d40d
 
 
cf49f13
3d4d40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf49f13
3d4d40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf49f13
3d4d40d
cf49f13
 
3d4d40d
 
 
 
 
 
 
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
pattern_size = 6
from collections import Counter
from dataclasses import dataclass

@dataclass(eq=True, frozen=True)
class ScheduledNode:
    type: str
    chunk: int
    stage: int
    minibatch: int
    start_time: int
    completion_time: int

def transform_schedule(schedule, f, b, w, c):
    result = []

    stage_order = []
    local_prev = {}
    stages = len(schedule)
    
    for sid, stage in enumerate(schedule):
        counter = Counter()
        order = []
        for p in stage:
            if not p.strip():
                continue
            mb = counter.get(p, 0)
            if order:
                local_prev[(sid, p, mb)] = order[-1]
            order.append((p, mb))
            counter.update(p)
        stage_order.append(order)
    nmb = max(counter.values())
    time_map = {}
    cost = {
        'F': f,
        'B': b,
        'W': w,
        'f': f,
        'b': b,
        'w': w,
    }
    def get_time(stage, type, mb):
        if (stage, type, mb) in time_map:
            return time_map.get((stage, type, mb))
        time = 0
        if (stage, type, mb) in local_prev:
            time = get_time(stage, *local_prev[(stage, type, mb)])
        if type in "FB" and stage > 0:
            time = max(time, get_time(stage - 1, type, mb) + c)
        if type in "fb" and stage + 1< len(schedule):
            time = max(time, get_time(stage + 1, type, mb) + c)
        # print(f'{stage} {type}:{mb}', time + cost[type])
        time_map[(stage, type, mb)] = time + cost[type]
        return time_map[(stage, type, mb)]
    r = 0
    for sid, stage in enumerate(schedule):
        r = max(get_time(sid, 'W', nmb - 1) - get_time(sid, 'F', 0) + f, r)
        r = max(get_time(sid, 'w', nmb - 1) - get_time(sid, 'F', 0) + f, r)

    for sid, stage in enumerate(stage_order):
        result_stage = []
        for p, mb in stage:
            result_stage.append(ScheduledNode(
                p.upper(),
                p in "fBW",
                sid,
                mb,
                get_time(sid, p, mb) - cost[p],
                get_time(sid, p, mb)
            )
            )
        result.append(result_stage)
    return result


    


def evaluate_schedule(schedule, f, b, w, c):
    stage_order = []
    local_prev = {}
    stages = len(schedule)
    
    for sid, stage in enumerate(schedule):
        counter = Counter()
        order = []
        for p in stage:
            if not p.strip():
                continue
            mb = counter.get(p, 0)
            if order:
                local_prev[(sid, p, mb)] = order[-1]
            order.append((p, mb))
            counter.update(p)
        stage_order.append(order)
    nmb = max(counter.values())
    time_map = {}
    cost = {
        'F': f,
        'B': b,
        'W': w,
        'f': f,
        'b': b,
        'w': w,
    }
    def get_time(stage, type, mb):
        if (stage, type, mb) in time_map:
            return time_map.get((stage, type, mb))
        time = 0
        if (stage, type, mb) in local_prev:
            time = get_time(stage, *local_prev[(stage, type, mb)])
        if type in "FB" and stage > 0:
            time = max(time, get_time(stage - 1, type, mb) + c)
        if type in "fb" and stage + 1< len(schedule):
            time = max(time, get_time(stage + 1, type, mb) + c)
        # print(f'{stage} {type}:{mb}', time + cost[type])
        time_map[(stage, type, mb)] = time + cost[type]
        return time_map[(stage, type, mb)]
    r = 0
    for sid, stage in enumerate(schedule):
        r = max(get_time(sid, 'W', nmb - 1) - get_time(sid, 'F', 0) + f, r)
        r = max(get_time(sid, 'w', nmb - 1) - get_time(sid, 'F', 0) + f, r)
    return r

def get_pattern_str(pos):
    pattern = [" "] * pattern_size
    notations = "FfBbWw"
    for i, v in enumerate(pos):
        if v < 0:
            continue
        pattern[v] = notations[i]
    _str = ""
    for v in pattern:
        _str += v
    return _str


def get_peak_mem(schedules, return_all=False):
    max_peak = 0
    all_peak = []
    for schedule_ in schedules:
        peak, mem = 0, 0
        for v in schedule_:
            if v in "Ff":
                mem += 1
            elif v in "Ww":
                mem -= 1
            peak = max(peak, mem)
        all_peak.append(peak)
        max_peak = max(max_peak, peak)
    if return_all:
        return all_peak
    return max_peak


def calc_bubble(schedules):
    stage_bubbles = []
    for i in range(len(schedules)):
        max_len = 0
        count = 0
        for j in range(len(schedules[i])):
            if schedules[i][j] != ' ':
                max_len = j + 1
                count += 1
        stage_bubbles.append(max_len - count - i)
    return stage_bubbles


def init_repeated_schedule(p, m, patterns):
    repeated = []
    _len = 4 * p + m + 1
    for i in range(p):
        str_i = get_pattern_str(patterns[i]) * _len
        repeated_i = []
        for v in str_i:
            repeated_i.append(v)
        repeated.append(repeated_i)
    return repeated


def clear_invalid(repeated, stage, pos, offset=-1):
    while 0 <= pos < len(repeated[stage]):
        repeated[stage][pos] = ' '
        pos += offset * pattern_size
    return repeated


def clear_invalid_index(repeated, m):
    p = len(repeated)
    index = pattern_size
    for identifier in "FfBb":
        if identifier in "FB":
            _iter = range(p)
        else:
            _iter = range(p - 1, -1, -1)
        for i in _iter:
            for j in range(pattern_size):
                if repeated[i][index] == identifier:
                    clear_invalid(repeated, i, index - pattern_size, offset=-1)
                    clear_invalid(repeated, i, index + pattern_size * m, offset=1)
                    index += 1
                    if identifier in "Bb":
                        w_identifier = {'B': 'W', 'b': 'w'}[identifier]
                        for k in range(pattern_size):
                            if repeated[i][index + k] == w_identifier:
                                clear_invalid(repeated, i, index + k - pattern_size, offset=-1)
                                clear_invalid(repeated, i, index + k + pattern_size * m, offset=1)
                                break
                    break
                index += 1
    return repeated


def process_warmup_without_increasing_peak_mem(schedules, m):
    """
    FFFFFFFFFF     fBWfBWfBWfBWfBW  b
     FFFFFFFFF    f fBWfBWfBWfBWFBWb
      FFFFFFFF   f f fBWfBWfBWFBW b
       FFFFFFF  f f f fBWfBWFBW Bb
        FFFFFF f f f f fBWFBWFBWb
         FFFFFfFf f f f  BWFBW b
          FFFfFfFfFf f    BW Bb
           FfFfFfFfFfF     BWb
    We reorganize the warmup phase in the following way (i -> pipeline stage from 0):
        1. Before the first B, we set #f = min(i+1, peak_mem//2), #F = peak_mem - #f
        2. Before the first b, #f = peak_mem//2
        3. The offset between the first B is 1
        4. Before the first b, we use the pattern of (BWf)*j + (BWF)*k,
           where j = max(0, peak_mem//2 - (i+1)), k = max(0, #W - j - 1)
    """
    # process warmup phase (before the first b)
    p = len(schedules)
    peak_mem = get_peak_mem(schedules)
    peak_mem = min(peak_mem, 2 * p)
    cnt_f, cnt_ff = [], []
    for i in range(p):
        cc_ff = min(i + 1, peak_mem // 2)
        cc_ff = min(cc_ff, m)
        cc_f = min(peak_mem - cc_ff, m)
        cnt_f.append(cc_f)
        cnt_ff.append(cc_ff)
    distance_b2bb = 0
    for j in range(len(schedules[p - 1])):
        if schedules[p - 1][j] == 'B':
            for k in range(j, len(schedules[p - 1])):
                if schedules[p - 1][k] == 'b':
                    distance_b2bb = k - j
                    break
            break
    for i in range(p):
        c_f, c_ff, c_b, c_w = 0, 0, 0, 0
        for j in range(len(schedules[i])):
            char = schedules[i][j]
            if char == 'F':
                c_f += 1
            elif char == 'f':
                c_ff += 1
            elif char == 'B':
                c_b += 1
            elif char == 'W':
                c_w += 1
            elif char == 'b':
                bj = j
                while j < len(schedules[i]):
                    char = schedules[i][j]
                    if char == 'f' and c_ff < cnt_ff[p - 1]:
                        schedules[i][j] = ' '
                        c_ff += 1
                    if char == 'B' and c_b < c_ff:
                        if c_b < (2 * (p - i) + distance_b2bb) // 3 or c_b < cnt_ff[p - 1] - cnt_ff[i]:
                            # there is empty space, or the number of B is not enough to cover extra f
                            schedules[i][j] = ' '
                            c_b += 1
                    if char == 'W' and c_w < c_b:
                        if c_w < (2 * (p - i) + distance_b2bb - 1) // 3 or c_w < cnt_ff[p - 1] - cnt_ff[i]:
                            # there is empty space, or the number of W is not enough to cover extra f
                            schedules[i][j] = ' '
                            c_w += 1
                    j += 1
                j = bj
                while j < len(schedules[i]):
                    if schedules[i][j] == 'F':
                        if c_f < c_ff or c_f < cnt_f[i] or c_f - cnt_f[i] + c_ff - cnt_ff[i] < c_w - 1:
                            # put enough F, or there are some unused BW
                            schedules[i][j] = ' '
                            c_f += 1
                    j += 1
                break
            else:
                assert char == ' '
            schedules[i][j] = ' '
        assert c_f >= cnt_f[i] and c_ff >= cnt_ff[i]
        assert c_w >= cnt_ff[p - 1] - cnt_ff[i] and c_b >= cnt_ff[p - 1] - cnt_ff[i]
        j = i
        u_f, u_ff, u_b, u_w = 0, 0, 0, 0
        for _ in range(2 * (p - 1 - i)):
            if u_f < cnt_f[i] and u_f < c_f:
                schedules[i][j] = 'F'
                u_f += 1
            j += 1
        for _ in range(i + 1):
            if u_f < cnt_f[i] and u_f < c_f:
                schedules[i][j] = 'F'
                u_f += 1
            j += 1
            if u_ff < cnt_ff[i] and u_ff < c_ff:
                schedules[i][j] = 'f'
                u_ff += 1
            j += 1
        while u_f < c_f or u_ff < c_ff or u_b < c_b or u_w < c_w:
            if u_b < c_b:
                schedules[i][j] = 'B'
                u_b += 1
            j += 1
            if u_w < c_w:
                schedules[i][j] = 'W'
                u_w += 1
            j += 1
            if u_ff < c_ff:
                assert u_ff < u_f
                schedules[i][j] = 'f'
                u_ff += 1
            elif u_f < c_f:
                schedules[i][j] = 'F'
                u_f += 1
            j += 1
    return schedules



def squeeze_without_change_order(schedules, m):
    p = len(schedules)
    squeezed = [[' '] * len(schedules[_]) for _ in range(p)]
    max_len = 0
    for seq in squeezed:
        assert max_len == 0 or max_len == len(seq)
        max_len = max(max_len, len(seq))

    identifier_cnt = [{_id: 0 for _id in "FfBbWw"} for _ in range(p)]
    identifier_index = [{_id: -1 for _id in "FfBbWw"} for _ in range(p * m)]
    stage_index = [0 for _ in range(p)]
    for j in range(max_len):
        for _dir in range(2):
            if _dir == 0:
                _iter = range(p)
            else:
                _iter = range(p - 1, -1, -1)
            for i in _iter:
                identifier = schedules[i][j]
                if identifier == ' ':
                    continue
                if _dir == 0 and identifier in "fbw":
                    continue
                if _dir == 1 and identifier in "FBW":
                    continue
                _cnt = identifier_cnt[i][identifier]
                assert _cnt < m, "{} - {}, {}".format(i, identifier, _cnt)
                if identifier in "Ww" or (i == 0 and identifier in "FB") or (i == p - 1 and identifier in "fb"):
                    if i == 0 and identifier == 'B':
                        assert identifier_index[_cnt * p + i]['f'] >= 0
                    if i == p - 1 and identifier == 'f':
                        assert identifier_index[_cnt * p + i]['F'] >= 0
                    if i == p - 1 and identifier == 'b':
                        assert identifier_index[_cnt * p + i]['B'] >= 0
                    index = stage_index[i]
                elif identifier in "FB":
                    assert identifier_index[_cnt * p + i - 1][identifier] >= 0, "{} {} {}".format(i,identifier,_cnt)
                    index = max(identifier_index[_cnt * p + i - 1][identifier] + 1, stage_index[i])
                elif identifier in "fb":
                    assert identifier_index[_cnt * p + i + 1][identifier] >= 0, "{} {} {}".format(i,identifier,_cnt)
                    index = max(identifier_index[_cnt * p + i + 1][identifier] + 1, stage_index[i])
                else:
                    raise
                squeezed[i][index] = identifier
                identifier_cnt[i][identifier] += 1
                identifier_index[_cnt * p + i][identifier] = index
                stage_index[i] = index + 1
    while True:
        if(len(squeezed[0]) == 1):
            break
        allempty = True
        for x in squeezed:
            if x[-1] != ' ':
                allempty = False
        if allempty == False:
            break
        for x in squeezed:
            del x[-1]
    return squeezed


def process_cooldown(schedules, m):
    """
           fBW       bwbwbwbw
          fBWBW     bwbwbwbw
         fBWBWBW   bwbwbwbw
        fBWBWBWBW bwbwbwbw
       f  BWBWBWBbWbwbwbww
      f    BWBWBbBbWbWbwwww
     f      BWBbBbBbWbWWwwww
    f        BbBbBbBbWWWWwwww
    We reorganize the cooldown phase in the following way (i -> pipeline stage from 0):
        1. After the last f, we set #b = (peak_mem+1)//2, and #B = min(i+1, peak_mem - #b)
        2. After the last f, we make all the dependencies as tight as possible
    """
    p = len(schedules)

    peak_mem = get_peak_mem(schedules)
    assert peak_mem <= 2 * p
    max_bb = (peak_mem + 1) // 2
    max_bb = min(max_bb, m)
    max_b = min(peak_mem - max_bb, m)

    # 1: reorganize B/b and remove W/w in cooldown phase
    starting_index = -1
    for i in range(p):
        c_b, c_bb, c_w, c_ww = 0, 0, 0, 0
        last_ff_index = -1
        # collect B/b which can be reorganized
        for j in range(len(schedules[i]) - 1, -1, -1):
            char = schedules[i][j]
            if char == 'f' and last_ff_index == -1:
                last_ff_index = j
            if char == 'B' and c_b < i + 1 and c_b < max_b:
                schedules[i][j] = ' '
                c_b += 1
            if char == 'b' and c_bb < max_bb:
                schedules[i][j] = ' '
                c_bb += 1
        # clear W in the tail (#W + #w = peak_mem)
        for j in range(len(schedules[i]) - 1, -1, -1):
            char = schedules[i][j]
            if char == 'W' and c_w + c_ww < peak_mem:
                schedules[i][j] = ' '
                c_w += 1
            if char == 'w' and c_w + c_ww < peak_mem:
                schedules[i][j] = ' '
                c_ww += 1
        if i == 0:
            starting_index = last_ff_index
        # reorganize B/b in the tail
        for k in range(c_bb):
            index = starting_index - i + 2 * p - 2 * k
            assert schedules[i][index] == ' ', "{} {} {}".format(schedules[i][index], k, i)
            schedules[i][index] = 'b'
        for k in range(c_b):
            index = starting_index + 1 + i - 2 * k
            assert schedules[i][index] == ' ', schedules[i][index]
            schedules[i][index] = 'B'

    # 2: squeeze cooldown phase without change order
    schedules = squeeze_without_change_order(schedules, m)

    # 3: add W back in cooldown phase
    for i in range(p):
        c_w, c_ww = 0, 0
        last_w_index = -2
        for j in range(len(schedules[i]) - 1, -1, -1):
            if schedules[i][j] in "Ww":
                if last_w_index < 0:
                    schedules[i][j] = ' '
                    last_w_index += 1
                else:
                    last_w_index = j
                    break
        for j in range(len(schedules[i])):
            char = schedules[i][j]
            if char == 'B':
                c_w += 1
            elif char == 'b':
                c_ww += 1
            elif char == 'W':
                c_w -= 1
            elif char == 'w':
                c_ww -= 1
            if char == ' ' and j > last_w_index:
                if c_w > 0:
                    schedules[i][j] = 'W'
                    c_w -= 1
                elif c_ww > 0:
                    schedules[i][j] = 'w'
                    c_ww -= 1

    schedules = squeeze_without_change_order(schedules, m)
    return schedules


def schedule_by_pattern(p, m, patterns, max_mem):
    schedules = init_repeated_schedule(p, max(m, 2 * p), patterns)
    schedules = clear_invalid_index(schedules,  max(m, 2 * p))
    init_peak_mem = get_peak_mem(schedules)
    if init_peak_mem > max_mem:
        return None, init_peak_mem, [6 *  max(m, 2 * p)] * p
    schedules = process_warmup_without_increasing_peak_mem(schedules,  max(m, 2 * p))
    
    for sid in range(len(schedules)):
        cnt = {_id: 0 for _id in "FfBbWw"}
        for i in range(len(schedules[sid])):
            if(schedules[sid][i] == ' '):
                continue
            if cnt[schedules[sid][i]] >= m:
                schedules[sid][i] = ' '
            else:
                cnt[schedules[sid][i]] += 1
    peak_mem = get_peak_mem(schedules)
    if peak_mem > init_peak_mem:
        return None, init_peak_mem, [6 * m] * p

    schedules = squeeze_without_change_order(schedules, m)

    schedules = process_cooldown(schedules, m)
    peak_mem = get_peak_mem(schedules)
    if peak_mem > init_peak_mem:
        return None, init_peak_mem, [6 * m] * p
    stage_bubbles = calc_bubble(schedules)
    return schedules, peak_mem, stage_bubbles


def fill_w_in_pattern(pattern):
    f, ff, b, bb, w, ww = 0, 1, 2, 3, 4, 5
    vis = [False] * pattern_size
    for v in pattern:
        if v >= 0:
            vis[v] = True
    assert pattern[b] >= 0 and pattern[bb] >= 0
    for v, vw in [(b, w), (bb, ww)]:
        for j in range(pattern_size):
            pos = (pattern[v] + j) % pattern_size
            if not vis[pos]:
                pattern[vw] = pos
                vis[pos] = True
                break
    return pattern


def get_whole_pattern(pattern_0, offset_0, offset_1, len_0, p):
    whole_pattern = [pattern_0]
    for i in range(p - 1):
        last_pattern = whole_pattern[i]
        new_pattern = [-1] * pattern_size
        vis = [False] * pattern_size
        if i < len_0:
            offset = offset_0
        else:
            offset = offset_1
        for v, v_o in enumerate(offset):
            pos = (last_pattern[v] + v_o + pattern_size) % pattern_size
            assert 0 <= pos < pattern_size
            if vis[pos]:
                return None
            vis[pos] = True
            new_pattern[v] = pos
        new_pattern = fill_w_in_pattern(new_pattern)
        whole_pattern.append(new_pattern)
    return whole_pattern


        
def schedule(p, m, cost, max_mem):
    f, ff, b, bb, w, ww = 0, 1, 2, 3, 4, 5
    available_patterns = []
    for ff_i in range(1, pattern_size):
        for b_i in range(1, pattern_size):
            for bb_i in range(1, pattern_size):
                if ff_i == b_i or ff_i == bb_i or b_i == bb_i:
                    continue
                pattern = [0, ff_i, b_i, bb_i, -1, -1]
                pattern = fill_w_in_pattern(pattern)
                available_patterns.append(pattern)

    print(len(available_patterns))
    available_offsets = [
        [1, -1, 1, -1],
        [2, -1, 2, -1],
        [3, -1, 3, -1],
        [4, -1, 4, -1],
        [5, -1, 5, -1]
    ]

    best_schedule = None
    best_bubble = None
    for pattern_0 in available_patterns:
        for i_0 in range(len(available_offsets)):
            for i_1 in range(i_0 + 1):
                for len_0 in range(1, p):
                    offset_0 = available_offsets[i_0]
                    offset_1 = available_offsets[i_1]
                    whole_pattern = get_whole_pattern(pattern_0, offset_0, offset_1, len_0, p)
                    if whole_pattern is None:
                        continue
                    s, peak_mem, bubbles = schedule_by_pattern(p, m, whole_pattern, min(2 * p, max_mem))
                    if peak_mem > 2 * p or peak_mem > max_mem:
                        break
                    if s is None:
                        continue
                    max_bubble = max(bubbles)
                    max_bubble = evaluate_schedule(s, *cost)
                    if best_schedule is None or max_bubble < best_bubble:
                        best_schedule, best_bubble = s, max_bubble
    res = transform_schedule(best_schedule, *cost)
    return res