Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
 
    reorder.cpp
 
Abstract:
 
    This module implements routines to reorder buffers to and from blocked
    formats.
 
--*/
 
#include "mlasi.h"
 
//
// Define the parameters to execute segments of a NCHW output reordering
// operation on worker threads.
//
 
struct MLAS_REORDER_OUTPUT_NCHW_BLOCK {
    ptrdiff_t TargetThreadCount;
    const float* S;
    float* D;
    size_t OutputChannels;
    size_t OutputSize;
    size_t TasksCount;
};
 
MLAS_FORCEINLINE
void
MlasReorderGatherFloat32x4(
    const float* S,
    float* D,
    size_t GatherStride
    )
/*++
 
Routine Description:
 
    This routine gathers floats from the source buffer and writes a vector to
    the destination buffer.
 
Arguments:
 
    S - Supplies the address of the source buffer.
 
    D - Supplies the address of the destination buffer.
 
    GatherStride - Supplies the stride to read elements from the source buffer.
 
Return Value:
 
    None.
 
--*/
{
#if defined(MLAS_SSE41_INTRINSICS)
    __m128 v = _mm_load_ss(&S[0 * GatherStride]);
    v = _mm_insert_ps(v, _mm_load_ss(&S[1 * GatherStride]), 0x10);
    v = _mm_insert_ps(v, _mm_load_ss(&S[2 * GatherStride]), 0x20);
    v = _mm_insert_ps(v, _mm_load_ss(&S[3 * GatherStride]), 0x30);
 
    _mm_storeu_ps(D, v);
#else
    float f0 = S[0 * GatherStride];
    float f1 = S[1 * GatherStride];
    float f2 = S[2 * GatherStride];
    float f3 = S[3 * GatherStride];
 
    D[0] = f0;
    D[1] = f1;
    D[2] = f2;
    D[3] = f3;
#endif
}
 
MLAS_FORCEINLINE
void
MlasReorderScatterFloat32x4(
    const float* S,
    float* D,
    size_t ScatterStride
    )
/*++
 
Routine Description:
 
    This routine scatters a vector read from the source buffer to the
    destination buffer.
 
Arguments:
 
    S - Supplies the address of the source buffer.
 
    D - Supplies the address of the destination buffer.
 
    ScatterStride - Supplies the stride to write elements to the destination
        buffer.
 
Return Value:
 
    None.
 
--*/
{
#if defined(MLAS_SSE41_INTRINSICS) || defined(MLAS_NEON_INTRINSICS)
    MLAS_FLOAT32X4 v = MlasLoadFloat32x4(S);
 
    MlasStoreLaneFloat32x4<0>(&D[ScatterStride * 0], v);
    MlasStoreLaneFloat32x4<1>(&D[ScatterStride * 1], v);
    MlasStoreLaneFloat32x4<2>(&D[ScatterStride * 2], v);
    MlasStoreLaneFloat32x4<3>(&D[ScatterStride * 3], v);
#else
    float f0 = S[0];
    float f1 = S[1];
    float f2 = S[2];
    float f3 = S[3];
 
    D[ScatterStride * 0] = f0;
    D[ScatterStride * 1] = f1;
    D[ScatterStride * 2] = f2;
    D[ScatterStride * 3] = f3;
#endif
}
 
MLAS_FORCEINLINE
void
MlasReorderTransposeFloat32x4x4(
    const float* S,
    float* D,
    size_t GatherStride,
    size_t ScatterStride
    )
/*++
 
Routine Description:
 
    This routine transposes a 4x4 float matrix read from the source buffer and
    writes the result to the destination buffer.
 
Arguments:
 
    S - Supplies the address of the source buffer.
 
    D - Supplies the address of the destination buffer.
 
    GatherStride - Supplies the stride to read elements from the source buffer.
 
    ScatterStride - Supplies the stride to write vectors to the destination
        buffer.
 
Return Value:
 
    None.
 
--*/
{
#if defined(MLAS_SSE2_INTRINSICS)
    MLAS_FLOAT32X4 v[4];
    MLAS_FLOAT32X4 t[4];
 
    v[0] = MlasLoadFloat32x4(&S[GatherStride * 0]);
    v[1] = MlasLoadFloat32x4(&S[GatherStride * 1]);
    v[2] = MlasLoadFloat32x4(&S[GatherStride * 2]);
    v[3] = MlasLoadFloat32x4(&S[GatherStride * 3]);
 
    t[0] = _mm_unpacklo_ps(v[0], v[1]);
    t[2] = _mm_unpackhi_ps(v[0], v[1]);
    t[1] = _mm_unpacklo_ps(v[2], v[3]);
    t[3] = _mm_unpackhi_ps(v[2], v[3]);
 
    v[0] = _mm_movelh_ps(t[0], t[1]);
    v[1] = _mm_movehl_ps(t[1], t[0]);
    v[2] = _mm_movelh_ps(t[2], t[3]);
    v[3] = _mm_movehl_ps(t[3], t[2]);
 
    MlasStoreFloat32x4(&D[ScatterStride * 0], v[0]);
    MlasStoreFloat32x4(&D[ScatterStride * 1], v[1]);
    MlasStoreFloat32x4(&D[ScatterStride * 2], v[2]);
    MlasStoreFloat32x4(&D[ScatterStride * 3], v[3]);
#elif  defined(MLAS_LSX_INTRINSICS)
 
    MLAS_FLOAT32X4 v[4];
    MLAS_FLOAT32X4 t[4];
 
    v[0] = MlasLoadFloat32x4(&S[GatherStride * 0]);
    v[1] = MlasLoadFloat32x4(&S[GatherStride * 1]);
    v[2] = MlasLoadFloat32x4(&S[GatherStride * 2]);
    v[3] = MlasLoadFloat32x4(&S[GatherStride * 3]);
 
    t[0] = (__m128)__lsx_vilvl_w((__m128i)v[1], (__m128i)v[0]);
    t[2] = (__m128)__lsx_vilvh_w((__m128i)v[1], (__m128i)v[0]);
    t[1] = (__m128)__lsx_vilvl_w((__m128i)v[3], (__m128i)v[2]);
    t[3] = (__m128)__lsx_vilvh_w((__m128i)v[3], (__m128i)v[2]);
 
 
    v[0] = (__m128)__lsx_vpickev_d((__m128i) t[1],(__m128i) t[0]);
    v[1] = (__m128)__lsx_vpickod_d((__m128i) t[1],(__m128i) t[0]);
    v[2] = (__m128)__lsx_vpickev_d((__m128i) t[3],(__m128i) t[2]);
    v[3] = (__m128)__lsx_vpickod_d((__m128i) t[3],(__m128i) t[2]);
 
    MlasStoreFloat32x4(&D[ScatterStride * 0], v[0]);
    MlasStoreFloat32x4(&D[ScatterStride * 1], v[1]);
    MlasStoreFloat32x4(&D[ScatterStride * 2], v[2]);
    MlasStoreFloat32x4(&D[ScatterStride * 3], v[3]);
#else
    MlasReorderScatterFloat32x4(&S[GatherStride * 0], &D[0], ScatterStride);
    MlasReorderScatterFloat32x4(&S[GatherStride * 1], &D[1], ScatterStride);
    MlasReorderScatterFloat32x4(&S[GatherStride * 2], &D[2], ScatterStride);
    MlasReorderScatterFloat32x4(&S[GatherStride * 3], &D[3], ScatterStride);
#endif
}
 
void
MLASCALL
MlasReorderInputNchw(
    const float* S,
    float* D,
    size_t InputChannels,
    size_t InputSize
    )
/*++
 
Routine Description:
 
    This routine reorders an input buffer from NCHW to NCHWc format.
 
Arguments:
 
    S - Supplies the address of the source tensor.
 
    D - Supplies the address of the destination tensor.
 
    InputChannels - Supplies the number of NCHW channels.
 
    InputSize - Supplies the spatial input size of the tensors.
 
Return Value:
 
    None.
 
--*/
{
    const size_t BlockSize = MlasNchwcGetBlockSize();
 
    const MLAS_FLOAT32X4 ZeroFloat32x4 = MlasZeroFloat32x4();
 
    //
    // Iterate over BlockSize batches of the input channels.
    //
 
    for (size_t i = InputChannels; i > 0;) {
 
        const size_t InputChannelsThisIteration = std::min(i, BlockSize);
        i -= InputChannelsThisIteration;
 
#if defined(MLAS_TARGET_AMD64)
        //
        // Fast path: on AVX-512 (BlockSize == 16) a full 16-channel block is a
        // 16-wide transpose. Bit-exact with the SSE2 4x4 path below.
        //
        if (BlockSize == 16 && InputChannelsThisIteration == 16) {
            MlasReorderInputNchwBlock16Avx512F(S, D, InputSize);
            S += BlockSize * InputSize;
            D += BlockSize * InputSize;
            continue;
        }
#endif
 
        const float* s = S;
        float* d = D;
        size_t InputSizeRemaining = InputSize;
 
        for (; InputSizeRemaining >= 4; InputSizeRemaining -= 4) {
 
            const float* ss = s;
            float* dd = d;
            size_t bc = 0;
 
            for (; bc < InputChannelsThisIteration; bc += 4) {
                MlasReorderTransposeFloat32x4x4(ss, dd, InputSize, BlockSize);
                ss += 4 * InputSize;
                dd += 4;
            }
 
            for (; bc < BlockSize; bc += 4) {
                MlasStoreFloat32x4(&dd[BlockSize * 0], ZeroFloat32x4);
                MlasStoreFloat32x4(&dd[BlockSize * 1], ZeroFloat32x4);
                MlasStoreFloat32x4(&dd[BlockSize * 2], ZeroFloat32x4);
                MlasStoreFloat32x4(&dd[BlockSize * 3], ZeroFloat32x4);
                dd += 4;
            }
 
            s += 4;
            d += 4 * BlockSize;
        }
 
        for (; InputSizeRemaining > 0; InputSizeRemaining--) {
 
            const float* ss = s;
            float* dd = d;
            size_t bc = 0;
 
            for (; bc < InputChannelsThisIteration; bc += 4) {
                MlasReorderGatherFloat32x4(ss, dd, InputSize);
                ss += 4 * InputSize;
                dd += 4;
            }
 
            for (; bc < BlockSize; bc += 4) {
                MlasStoreFloat32x4(dd, ZeroFloat32x4);
                dd += 4;
            }
 
            s += 1;
            d += BlockSize;
        }
 
        S += BlockSize * InputSize;
        D += BlockSize * InputSize;
    }
}
 
void
MLASCALL
MlasReorderInputNhwc(
    const float* S,
    float* D,
    size_t InputChannels,
    size_t RowCount,
    size_t FullRowCount
    )
/*++
 
Routine Description:
 
    This routine reorders an input buffer from NHWC to NCHWc format.
 
Arguments:
 
    S - Supplies the address of the source tensor.
 
    D - Supplies the address of the destination tensor.
 
    InputChannels - Supplies the number of NHWC channels.
 
    RowCount - Supplies the number of NHWC rows to process. This number may be
        less than FullRowCount to support threaded operation.
 
    FullRowCount - Supplies the total number of NHWC rows per image.
 
Return Value:
 
    None.
 
--*/
{
    const size_t BlockSize = MlasNchwcGetBlockSize();
 
    //
    // Iterate over batches of the input size to improve locality.
    //
 
    for (size_t OuterRowCountRemaining = RowCount; OuterRowCountRemaining > 0; ) {
 
        constexpr size_t OuterRowCountBatch = 32;
 
        const size_t OuterRowCountThisIteration = std::min(OuterRowCountRemaining, OuterRowCountBatch);
        OuterRowCountRemaining -= OuterRowCountThisIteration;
 
        //
        // Iterate over BlockSize batches of the input channels.
        //
 
        const float* s = S;
        float* d = D;
 
        for (size_t i = InputChannels; i > 0;) {
 
            const size_t InputChannelsThisIteration = std::min(i, BlockSize);
            i -= InputChannelsThisIteration;
 
            const float* ss = s;
            float* dd = d;
            size_t InnerRowCountRemaining = OuterRowCountThisIteration;
 
            if (InputChannelsThisIteration == BlockSize) {
 
                if (BlockSize == 8) {
 
                    while (InnerRowCountRemaining-- > 0) {
 
                        MLAS_FLOAT32X4 v0 = MlasLoadFloat32x4(&ss[0]);
                        MLAS_FLOAT32X4 v1 = MlasLoadFloat32x4(&ss[4]);
 
                        MlasStoreFloat32x4(&dd[0], v0);
                        MlasStoreFloat32x4(&dd[4], v1);
 
                        ss += InputChannels;
                        dd += 8;
                    }
 
                } else {
 
                    while (InnerRowCountRemaining-- > 0) {
 
                        MLAS_FLOAT32X4 v0 = MlasLoadFloat32x4(&ss[0]);
                        MLAS_FLOAT32X4 v1 = MlasLoadFloat32x4(&ss[4]);
                        MLAS_FLOAT32X4 v2 = MlasLoadFloat32x4(&ss[8]);
                        MLAS_FLOAT32X4 v3 = MlasLoadFloat32x4(&ss[12]);
 
                        MlasStoreFloat32x4(&dd[0], v0);
                        MlasStoreFloat32x4(&dd[4], v1);
                        MlasStoreFloat32x4(&dd[8], v2);
                        MlasStoreFloat32x4(&dd[12], v3);
 
                        ss += InputChannels;
                        dd += 16;
                    }
                }
 
            } else {
 
                size_t BlockPadding = BlockSize - InputChannelsThisIteration;
 
                while (InnerRowCountRemaining-- > 0) {
 
                    std::copy_n(ss, InputChannelsThisIteration, dd);
                    std::fill_n(dd + InputChannelsThisIteration, BlockPadding, 0.0f);
 
                    ss += InputChannels;
                    dd += BlockSize;
                }
            }
 
            s += InputChannelsThisIteration;
            d += BlockSize * FullRowCount;
        }
 
        S += InputChannels * OuterRowCountThisIteration;
        D += BlockSize * OuterRowCountThisIteration;
    }
}
 
void
MlasReorderOutputNchwThreaded(
    void* Context,
    ptrdiff_t Index
    )
/*++
 
Routine Description:
 
    This routine is invoked from a worker thread to execute a segment of a
    NCHW output reordering operation.
 
Arguments:
 
    Context - Supplies the pointer to the context for the threaded operation.
 
    Index - Supplies the current index of the threaded operation.
 
Return Value:
 
    None.
 
--*/
{
    const auto* WorkBlock = (MLAS_REORDER_OUTPUT_NCHW_BLOCK*)Context;
 
    const size_t OutputChannels = WorkBlock->OutputChannels;
    const size_t OutputSize = WorkBlock->OutputSize;
    const float* S = WorkBlock->S;
    float* D =  WorkBlock->D;
 
    const size_t BlockSize = MlasNchwcGetBlockSize();
    const size_t TasksPerBatch = size_t(ceil(((float)OutputChannels) / BlockSize));
    const size_t LastTaskInBatchIndex = TasksPerBatch - 1;
 
    //
    // Compute the range of task indices to use for this thread.
    //
 
    size_t TaskStart;
    size_t TasksRemaining;
 
    MlasPartitionWork(Index, WorkBlock->TargetThreadCount, WorkBlock->TasksCount,
        &TaskStart, &TasksRemaining);
 
    size_t TaskEnd = TaskStart + TasksRemaining;
    //
    // Rebase the pointers to the source and destination buffers for this thread.
    //
 
    size_t FirstBatchIndex = TaskStart / TasksPerBatch;
    size_t FirstTaskInBatchIndex = TaskStart % TasksPerBatch;
    S += BlockSize * OutputSize * (FirstBatchIndex * TasksPerBatch + FirstTaskInBatchIndex);
    D += OutputSize * (FirstBatchIndex * OutputChannels + BlockSize * FirstTaskInBatchIndex);
 
    //
    // Transpose NCHWc blocks associated with tasks in the range [TaskStart, TaskEnd)
    // from the source buffer to the destination buffer.
    //
 
    for (size_t t = TaskStart; t < TaskEnd; t++) {
        size_t TaskInBatchIndex = t % TasksPerBatch;
 
        const size_t OutputChannelsThisIteration = (TaskInBatchIndex < LastTaskInBatchIndex) ?
            BlockSize : OutputChannels - BlockSize * LastTaskInBatchIndex;
        const size_t AlignedOutputChannelsThisIteration = OutputChannelsThisIteration & (~3);
 
#if defined(MLAS_TARGET_AMD64)
        //
        // Fast path: on AVX-512 (BlockSize == 16) a full 16-channel block is a
        // 16-wide transpose. Bit-exact with the SSE2 4x4 path below.
        //
        if (BlockSize == 16 && OutputChannelsThisIteration == 16) {
            MlasReorderOutputNchwBlock16Avx512F(S, D, OutputSize);
            S += BlockSize * OutputSize;
            D += OutputChannelsThisIteration * OutputSize;
            continue;
        }
#endif
 
        const float* s = S;
        float* d = D;
        size_t OutputSizeRemaining = OutputSize;
 
        for (; OutputSizeRemaining >= 4; OutputSizeRemaining -= 4) {
 
            const float* ss = s;
            float* dd = d;
            size_t bc = 0;
 
            for (; bc < AlignedOutputChannelsThisIteration; bc += 4) {
                MlasReorderTransposeFloat32x4x4(ss, dd, BlockSize, OutputSize);
                ss += 4;
                dd += 4 * OutputSize;
            }
 
            for (; bc < OutputChannelsThisIteration; bc += 1) {
                MlasReorderGatherFloat32x4(ss, dd, BlockSize);
                ss += 1;
                dd += OutputSize;
            }
 
            s += 4 * BlockSize;
            d += 4;
        }
 
        for (; OutputSizeRemaining > 0; OutputSizeRemaining--) {
 
            const float* ss = s;
            float* dd = d;
            size_t bc = 0;
 
            for (; bc < AlignedOutputChannelsThisIteration; bc += 4) {
                MlasReorderScatterFloat32x4(ss, dd, OutputSize);
                ss += 4;
                dd += 4 * OutputSize;
            }
 
            for (; bc < OutputChannelsThisIteration; bc += 1) {
                *dd = *ss++;
                dd += OutputSize;
            }
 
            s += BlockSize;
            d += 1;
        }
 
        S += BlockSize * OutputSize;
        D += OutputChannelsThisIteration * OutputSize;
    }
}
 
 
void
MLASCALL
MlasReorderOutputNchw(
    const int64_t* OutputShape,
    const float* S,
    float* D,
    MLAS_THREADPOOL* ThreadPool
    )
/*++
 
Routine Description:
 
    This routine reorders an output buffer from NCHWc to NCHW format.
 
Arguments:
 
    OutputShape - Supplies the shape of the output tensor.
 
    S - Supplies the address of the source tensor.
 
    D - Supplies the address of the destination tensor.
 
Return Value:
 
    None.
 
--*/
{
    MLAS_REORDER_OUTPUT_NCHW_BLOCK WorkBlock;
 
    //
    // Capture the NCHW reorder output operation parameters to the work block.
    //
 
    WorkBlock.S = S;
    WorkBlock.D = D;
    WorkBlock.OutputChannels = size_t(OutputShape[1]);
    WorkBlock.OutputSize = size_t(OutputShape[2]) * size_t(OutputShape[3]);
 
    const size_t BlockSize = MlasNchwcGetBlockSize();
    const size_t TasksPerBatch = size_t(ceil(((float)WorkBlock.OutputChannels) / BlockSize));
    const size_t BatchCount = size_t(OutputShape[0]);
    const size_t TasksCount = BatchCount * TasksPerBatch;
    WorkBlock.TasksCount = TasksCount;
 
    //
    // Schedule the operation across a set of worker threads if the output
    // tensor is sufficienly large. Limit the number of threads to at least
    // the number of available tasks.
    //
 
    ptrdiff_t TargetThreadCount = 1;
    const size_t BufferSize = BatchCount * WorkBlock.OutputChannels * WorkBlock.OutputSize;
    if (BufferSize > 1024 && TasksCount > 1) {
        TargetThreadCount = MlasGetMaximumThreadCount(ThreadPool);
        if (size_t(TargetThreadCount) > TasksCount) {
            TargetThreadCount = ptrdiff_t(TasksCount);
        }
    }
    WorkBlock.TargetThreadCount = TargetThreadCount;
 
    MlasExecuteThreaded(MlasReorderOutputNchwThreaded, &WorkBlock, TargetThreadCount, ThreadPool);
}
 
void
MLASCALL
MlasReorderOutputNhwc(
    const int64_t* OutputShape,
    const float* S,
    float* D
    )
/*++
 
Routine Description:
 
    This routine reorders an output buffer from NCHWc to NHWC format.
 
Arguments:
 
    OutputShape - Supplies the shape of the output tensor.
 
    S - Supplies the address of the source tensor.
 
    D - Supplies the address of the destination tensor.
 
Return Value:
 
    None.
 
--*/
{
    const size_t BlockSize = MlasNchwcGetBlockSize();
 
    const size_t BatchCount = size_t(OutputShape[0]);
    const size_t OutputChannels = size_t(OutputShape[3]);
    const size_t OutputSize = size_t(OutputShape[1]) * size_t(OutputShape[2]);
 
    const size_t AlignedOutputChannels = (OutputChannels + BlockSize - 1) & ~(BlockSize - 1);
 
    //
    // Copy NCHWc blocks from the source buffer to the destination buffer.
    //
 
    for (size_t batch = 0; batch < BatchCount; batch++) {
 
        const float* s = S;
        size_t OutputSizeRemaining = OutputSize;
 
        for (; OutputSizeRemaining > 0; OutputSizeRemaining--) {
 
            const float* ss = s;
 
            for (size_t o = OutputChannels; o > 0;) {
 
                const size_t OutputChannelsThisIteration = std::min(o, BlockSize);
                const size_t AlignedOutputChannelsThisIteration = OutputChannelsThisIteration & (~3);
                o -= OutputChannelsThisIteration;
 
                size_t bc = 0;
 
                for (; bc < AlignedOutputChannelsThisIteration; bc += 4) {
                    MlasStoreFloat32x4(&D[bc], MlasLoadFloat32x4(&ss[bc]));
                }
 
                for (; bc < OutputChannelsThisIteration; bc += 1) {
                    D[bc] = ss[bc];
                }
 
                ss += BlockSize * OutputSize;
                D += OutputChannelsThisIteration;
            }
 
            s += BlockSize;
        }
 
        S += AlignedOutputChannels * OutputSize;
    }
}
 
void
MLASCALL
MlasReorderFilterOIHWBiBo(
    const int64_t* FilterShape,
    const float* S,
    float* D
    )
/*++
 
Routine Description:
 
    This routine reorders a filter buffer from OIHW to OIHWBiBo format.
 
Arguments:
 
    FilterShape - Supplies the shape of the filter tensor.
 
    S - Supplies the address of the source tensor.
 
    D - Supplies the address of the destination tensor.
 
Return Value:
 
    None.
 
--*/
{
    const size_t BlockSize = MlasNchwcGetBlockSize();
 
    const size_t OutputChannels = size_t(FilterShape[0]);
    const size_t InputChannels = size_t(FilterShape[1]);
    const size_t KernelHeight = size_t(FilterShape[2]);
    const size_t KernelWidth = size_t(FilterShape[3]);
 
    const size_t KernelSize = KernelHeight * KernelWidth;
    const size_t InputStride = InputChannels * KernelSize;
 
    const MLAS_FLOAT32X4 ZeroFloat32x4 = MlasZeroFloat32x4();
 
    //
    // Transform the filter tensor from format OIHW to OIHWBiBo:
    //
    //  OutputChannelBlock[0] = {
    //      InputChannelBlock[0] = {
    //          Kernel[0][0] = {
    //              InputChannel[0] = { filter[0 filter[1] ... filter[BlockSize-1] },
    //              InputChannel[1] = { filter[0 filter[1] ... filter[BlockSize-1] },
    //              ...
    //              InputChannel[BlockSize-1] = { filter[0] filter[1] ... filter[BlockSize-1] },
    //          },
    //          Kernel[0][1] = {
    //              ...
    //          },
    //          ...
    //          Kernel[KernelHeight-1][KernelWidth-1] = {
    //              ...
    //          },
    //      },
    //      InputChannelBlock[BlockSize] = {
    //          ...
    //      },
    //      ...
    //      InputChannelBlock[InputChannels-BlockSize] = {
    //          ...
    //      },
    //  },
    //  OutputChannelBlock[BlockSize] = {
    //      ...
    //  },
    //  OutputChannelBlock[OutputChannels-BlockSize] = {
    //      ...
    //  };
    //
 
    //
    // Iterate over BlockSize batches of the output channels.
    //
    // The final batch may be less than BlockSize, but must be a multiple of 4.
    // The unaligned count results in zero padding below.
    //
 
    for (size_t o = OutputChannels; o > 0;) {
 
        const size_t OutputChannelsThisIteration = std::min(o, BlockSize);
        const size_t AlignedOutputChannelsThisIteration = OutputChannelsThisIteration & (~3);
        o -= OutputChannelsThisIteration;
 
        //
        // Iterate over BlockSize batches of the input channels.
        //
        // The final batch may be less than BlockSize, but must be a multiple
        // of 4.
        //
 
        const float* S_InputChannels = S;
 
        for (size_t i = InputChannels; i > 0;) {
 
            const size_t InputChannelsThisIteration = std::min(i, BlockSize);
            i -= InputChannelsThisIteration;
 
            //
            // Iterate over each index of the kernel.
            //
 
            const float* S_KernelSize = S_InputChannels;
 
            for (size_t k = 0; k < KernelSize; k++) {
 
                //
                // Construct a filter block of BlockSize by BlockSize.
                //
 
                const float* S_BlockSize = S_KernelSize;
 
                for (size_t bi = 0; bi < InputChannelsThisIteration; bi++) {
 
                    //
                    // Transpose from the source filter buffer to the destination
                    // buffer. Zero pad the filter block if the output channels
                    // is not block aligned.
                    //
 
                    const float* s = S_BlockSize;
                    size_t bo = 0;
 
                    for (; bo < AlignedOutputChannelsThisIteration; bo += 4) {
                        MlasReorderGatherFloat32x4(s, D, InputStride);
                        s += 4 * InputStride;
                        D += 4;
                    }
 
                    for (; bo < OutputChannelsThisIteration; bo += 1) {
                        *D++ = *s;
                        s += InputStride;
                    }
 
                    for (; bo < BlockSize; bo += 1) {
                        *D++ = 0.0f;
                    }
 
                    S_BlockSize += KernelSize;
                }
 
                for (size_t z = 0; z < (BlockSize - InputChannelsThisIteration) * (BlockSize / 4); z++) {
                    MlasStoreFloat32x4(D, ZeroFloat32x4);
                    D += 4;
                }
 
                S_KernelSize += 1;
            }
 
            S_InputChannels += BlockSize * KernelSize;
        }
 
        S += BlockSize * InputStride;
    }
}
 
void
MLASCALL
MlasReorderFilterOIHWBo(
    const int64_t* FilterShape,
    const float* S,
    float* D
    )
/*++
 
Routine Description:
 
    This routine reorders a filter buffer from OIHW to OIHWBo format.
 
Arguments:
 
    FilterShape - Supplies the shape of the filter tensor.
 
    S - Supplies the address of the source tensor.
 
    D - Supplies the address of the destination tensor.
 
Return Value:
 
    None.
 
--*/
{
    const size_t BlockSize = MlasNchwcGetBlockSize();
 
    const size_t OutputChannels = size_t(FilterShape[0]);
    const size_t InputChannels = size_t(FilterShape[1]);
    const size_t KernelHeight = size_t(FilterShape[2]);
    const size_t KernelWidth = size_t(FilterShape[3]);
 
    const size_t KernelSize = KernelHeight * KernelWidth;
    const size_t InputStride = InputChannels * KernelSize;
 
    //
    // Transform the filter tensor from format OIHW to OIHWBo:
    //
    //  OutputChannelBlock[0] = {
    //      InputChannel[0] = {
    //          Kernel[0][0] = filter[0 filter[1] ... filter[BlockSize-1] },
    //          Kernel[0][1] = { filter[0 filter[1] ... filter[BlockSize-1] },
    //          ...
    //          Kernel[KernelHeight-1][KernelWidth-1] = { filter[0 filter[1] ... filter[BlockSize-1] },
    //      },
    //      InputChannel[1] = {
    //          ...
    //      },
    //      ...
    //      InputChannel[InputChannels-1] = {
    //          ...
    //      },
    //  },
    //  OutputChannelBlock[BlockSize] = {
    //      ...
    //  },
    //  OutputChannelBlock[OutputChannels-BlockSize] = {
    //      ...
    //  };
    //
 
    //
    // Iterate over BlockSize batches of the output channels.
    //
    // The final batch may be less than BlockSize, but must be a multiple of 4.
    // The unaligned count results in zero padding below.
    //
 
    for (size_t o = OutputChannels; o > 0;) {
 
        const size_t OutputChannelsThisIteration = std::min(o, BlockSize);
        const size_t AlignedOutputChannelsThisIteration = OutputChannelsThisIteration & (~3);
        o -= OutputChannelsThisIteration;
 
        //
        // Iterate over each of the input channels.
        //
 
        const float* S_InputChannels = S;
 
        for (size_t i = 0; i < InputChannels; i += 1) {
 
            //
            // Iterate over each index of the kernel.
            //
 
            const float* S_KernelSize = S_InputChannels;
 
            for (size_t k = 0; k < KernelSize; k++) {
 
                //
                // Transpose a float[4] from the source filter buffer to the
                // destination buffer. Zero pad the filter block if the output
                // channels is not block aligned.
                //
 
                const float* s = S_KernelSize;
                size_t bo = 0;
 
                for (; bo < AlignedOutputChannelsThisIteration; bo += 4) {
                    MlasReorderGatherFloat32x4(s, D, InputStride);
                    s += 4 * InputStride;
                    D += 4;
                }
 
                for (; bo < OutputChannelsThisIteration; bo += 1) {
                    *D++ = *s;
                    s += InputStride;
                }
 
                for (; bo < BlockSize; bo += 1) {
                    *D++ = 0.0f;
                }
 
                S_KernelSize += 1;
            }
 
            S_InputChannels += KernelSize;
        }
 
        S += BlockSize * InputStride;
    }
}