// File: xbai0624[P]dnn/./include/Layer.h
// Function: Filter2D::GenerateCompleteDropOutSet(unsigned long, double)
std::vector<Filter2D> GenerateCompleteDropOutSet(size_t nBatches, double dropoutRatio)
    {
        // generate a complete set of drop out filters
        // In this set, each element get acitvated at least once, this is to make sure during drop-out every neuron get trained
        // Parameters:
        //     nBatches: number of drop-out branches
        //     dropoutRatio: ratio of elements to drop out
        // 1) basic check
        auto dim = Dimension();

        assert(nBatches >= 1);
        assert(dropoutRatio < 1.0); // one cannot drop all of them
        int total_elements = (int)dim.first * (int)dim.second;
        // make sure there's enough positions to accomodate all elements
        assert(total_elements * (1.0-dropoutRatio) * (int)nBatches >= total_elements);

        // 2) setup a random engine
        std::random_device rd;
        std::mt19937 gen(rd());
        //std::uniform_int_distribution<> row(0, (int)dim.first-1);
        //std::uniform_int_distribution<> col(0, (int)dim.second-1);

        // 3) the result to be returned
        std::vector<Filter2D> Ret;
        for(size_t n=0;n<nBatches;n++)
        {
            Ret.push_back(Filter2D(dim.first, dim.second, false));
        }

        // 4) first save all coords
        std::vector<std::pair<int, int>> all_coords;
        for(size_t i=0;i<dim.first;i++)
        {
            for(size_t j=0;j<dim.second;j++)
                all_coords.push_back(std::pair<int, int>((int)i, (int)j));
        }

        // 5) integer part
        int average = total_elements / (int)nBatches;
        for(auto &M: Ret)
        {
            // for each Filter, set 'average' elements to true
            int True_elements = 0;
            while(True_elements<average && all_coords.size() > 0)
            {
                std::uniform_int_distribution<int> index(0, (int)all_coords.size()-1);
                int pos = index(gen);
                M[all_coords[pos].first][all_coords[pos].second] = true;
                True_elements++;
                all_coords.erase(all_coords.begin()+pos);
            }
        }

        // 6) residue part
        int mod = total_elements % (int)nBatches;
        for(int i=0;i<mod;i++)
        {
            // evenly distribute 'mod' elements to Filters
            int True_elements = 0;
            while(True_elements < 1 && all_coords.size() > 0)
            {
                std::uniform_int_distribution<int> index(0, (int)all_coords.size()-1);
                int pos = index(gen);
                (Ret[i])[all_coords[pos].first][all_coords[pos].second] = true;
                True_elements++;
                all_coords.erase(all_coords.begin()+pos);
            }
        }
        //assert(1 < 0);

        // 7) complete the rest elements
        // a helper
        // complete missing elements
        auto complete = [&](size_t required_active_elements, Filter2D& F)
        {
            int actual_active_elements = F.GetNumberOfTrueElements();
            int residue = (int)required_active_elements - actual_active_elements;
            //assert(residue >= 0); // incorrect
            if(residue < 0) return; // correct, in case number of neurons is odd number

            // first save all inactive coords
            std::vector<std::pair<int, int>> tmp;
            auto dim = F.Dimension();
            for(size_t i=0;i<dim.first;i++)
            {
                for(size_t j=0;j<dim.second;j++)
                    if(!F[i][j]) tmp.push_back(std::pair<int, int>((int)i, (int)j));
            }
            int count = 0;
            while(count < residue && tmp.size() > 0)
            {
                std::uniform_int_distribution<int> index(0, (int)tmp.size()-1);
                int pos = index(gen);
                if(!F[tmp[pos].first][tmp[pos].second])
                {
                    F[tmp[pos].first][tmp[pos].second] = true;
                    tmp.erase(tmp.begin() + pos);
                    count++;
                }
            }
        };

        for(auto &M: Ret)
            complete(total_elements*(1.-dropoutRatio), M);

        return Ret;
    }

// Assembly:
subq $0x2928, %rsp           # imm = 0x2928
movq %rdi, 0x90(%rsp)
movq %rdi, %rax
movq %rax, 0x98(%rsp)
movq %rdi, 0x2920(%rsp)
movq %rsi, 0x2918(%rsp)
movq %rdx, 0x2910(%rsp)
movsd %xmm0, 0x2908(%rsp)
movq 0x2918(%rsp), %rdi
callq 0x200b0
movq %rax, 0x28f8(%rsp)
movq %rdx, 0x2900(%rsp)
cmpq $0x1, 0x2910(%rsp)
jb 0x20255
jmp 0x20274
leaq 0x33eb2(%rip), %rdi     # 0x5410e
leaq 0x33eb9(%rip), %rsi     # 0x5411c
movl $0x9c, %edx
leaq 0x33ebf(%rip), %rcx     # 0x5412e
callq 0x4170
movsd 0x31d8c(%rip), %xmm0    # 0x52008
ucomisd 0x2908(%rsp), %xmm0
jbe 0x20289
jmp 0x202a8
leaq 0x33ee9(%rip), %rdi     # 0x54179
leaq 0x33e85(%rip), %rsi     # 0x5411c
movl $0x9d, %edx
leaq 0x33e8b(%rip), %rcx     # 0x5412e
callq 0x4170
movq 0x28f8(%rsp), %rax
movq 0x2900(%rsp), %rcx
imull %ecx, %eax
movl %eax, 0x28f4(%rsp)
cvtsi2sdl 0x28f4(%rsp), %xmm0
movsd 0x31d35(%rip), %xmm1    # 0x52008
subsd 0x2908(%rsp), %xmm1
mulsd %xmm1, %xmm0
movq 0x2910(%rsp), %rax
cvtsi2sd %eax, %xmm1
mulsd %xmm1, %xmm0
cvtsi2sdl 0x28f4(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x20301
jmp 0x20320
leaq 0x33e84(%rip), %rdi     # 0x5418c
leaq 0x33e0d(%rip), %rsi     # 0x5411c
movl $0xa0, %edx
leaq 0x33e13(%rip), %rcx     # 0x5412e
callq 0x4170
leaq 0x1568(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x21140
movq 0x80(%rsp), %rdi
callq 0x211e0
movl %eax, 0x8c(%rsp)
jmp 0x2034b
movl 0x8c(%rsp), %eax
movl %eax, %eax
movl %eax, %esi
leaq 0x1e0(%rsp), %rdi
callq 0x211f0
jmp 0x20365
movq 0x90(%rsp), %rdi
movb $0x0, 0x1d3(%rsp)
callq 0x1f070
movq $0x0, 0x1c8(%rsp)
movq 0x1c8(%rsp), %rax
cmpq 0x2910(%rsp), %rax
jae 0x2044f
movq 0x28f8(%rsp), %rsi
movq 0x2900(%rsp), %rdx
leaq 0x1b0(%rsp), %rdi
xorl %ecx, %ecx
callq 0x27a60
jmp 0x203bd
movq 0x90(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0x27a30
jmp 0x203d4
leaq 0x1b0(%rsp), %rdi
callq 0x1f9b0
movq 0x1c8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x1c8(%rsp)
jmp 0x20386
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d8(%rsp)
movl %eax, 0x1d4(%rsp)
jmp 0x20ace
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d8(%rsp)
movl %eax, 0x1d4(%rsp)
jmp 0x20ac1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d8(%rsp)
movl %eax, 0x1d4(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x1f9b0
jmp 0x20ac1
leaq 0x198(%rsp), %rdi
callq 0x21250
movq $0x0, 0x190(%rsp)
movq 0x190(%rsp), %rax
cmpq 0x28f8(%rsp), %rax
jae 0x2054b
movq $0x0, 0x188(%rsp)
movq 0x188(%rsp), %rax
cmpq 0x2900(%rsp), %rax
jae 0x20530
movl 0x190(%rsp), %eax
movl %eax, 0x17c(%rsp)
movl 0x188(%rsp), %eax
movl %eax, 0x178(%rsp)
leaq 0x180(%rsp), %rdi
leaq 0x17c(%rsp), %rsi
leaq 0x178(%rsp), %rdx
callq 0x27b90
jmp 0x204db
leaq 0x198(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0x27b60
jmp 0x204f2
jmp 0x204f4
movq 0x188(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x188(%rsp)
jmp 0x2048a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1d8(%rsp)
movl %eax, 0x1d4(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x21430
jmp 0x20ac1
jmp 0x20532
movq 0x190(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x190(%rsp)
jmp 0x20468
movl 0x28f4(%rsp), %eax
movq 0x2910(%rsp), %rcx
cltd
idivl %ecx
movl %eax, %ecx
movq 0x90(%rsp), %rax
movl %ecx, 0x174(%rsp)
movq %rax, 0x168(%rsp)
movq 0x168(%rsp), %rdi
callq 0x20140
movq %rax, 0x160(%rsp)
movq 0x168(%rsp), %rdi
callq 0x20170
movq %rax, 0x158(%rsp)
leaq 0x160(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x201a0
testb $0x1, %al
jne 0x205be
jmp 0x2078d
leaq 0x160(%rsp), %rdi
callq 0x201e0
movq %rax, 0x150(%rsp)
movl $0x0, 0x14c(%rsp)
movl 0x14c(%rsp), %ecx
xorl %eax, %eax
cmpl 0x174(%rsp), %ecx
movb %al, 0x7f(%rsp)
jge 0x2060c
leaq 0x198(%rsp), %rdi
callq 0x21260
cmpq $0x0, %rax
seta %al
movb %al, 0x7f(%rsp)
movb 0x7f(%rsp), %al
testb $0x1, %al
jne 0x20619
jmp 0x20779
leaq 0x198(%rsp), %rdi
callq 0x21260
movl %eax, %edx
decl %edx
leaq 0x144(%rsp), %rdi
xorl %esi, %esi
callq 0x21220
jmp 0x2063b
leaq 0x144(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x21280
movl %eax, 0x78(%rsp)
jmp 0x20656
movl 0x78(%rsp), %eax
movl %eax, 0x140(%rsp)
movq 0x150(%rsp), %rax
movq %rax, 0x50(%rsp)
movslq 0x140(%rsp), %rsi
leaq 0x198(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x27bc0
movq 0x50(%rsp), %rdi
movslq (%rax), %rsi
callq 0x1fff0
movq 0x58(%rsp), %rdi
movq %rax, 0x60(%rsp)
movslq 0x140(%rsp), %rsi
callq 0x27bc0
movq 0x60(%rsp), %rdi
movslq 0x4(%rax), %rsi
callq 0x20020
movq %rdx, 0x68(%rsp)
movq %rax, 0x70(%rsp)
jmp 0x206c6
movq 0x68(%rsp), %rax
movq 0x70(%rsp), %rcx
movq %rcx, 0x130(%rsp)
movq %rax, 0x138(%rsp)
leaq 0x130(%rsp), %rdi
movl $0x1, %esi
callq 0x20ff0
movl 0x14c(%rsp), %eax
incl %eax
movl %eax, 0x14c(%rsp)
leaq 0x198(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x21360
movq %rax, 0x118(%rsp)
movslq 0x140(%rsp), %rsi
leaq 0x118(%rsp), %rdi
callq 0x27c50
movq %rax, 0x120(%rsp)
leaq 0x128(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x27c90
movq 0x40(%rsp), %rdi
movq 0x128(%rsp), %rsi
callq 0x27be0
movq %rax, 0x48(%rsp)
jmp 0x20767
movq 0x48(%rsp), %rax
movq %rax, 0x110(%rsp)
jmp 0x205de
jmp 0x2077b
leaq 0x160(%rsp), %rdi
callq 0x20b60
jmp 0x205a0
movl 0x28f4(%rsp), %eax
movq 0x2910(%rsp), %rcx
cltd
idivl %ecx
movl %edx, 0x10c(%rsp)
movl $0x0, 0x108(%rsp)
movl 0x108(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jge 0x20988
movl $0x0, 0x104(%rsp)
xorl %eax, %eax
cmpl $0x1, 0x104(%rsp)
movb %al, 0x3f(%rsp)
jge 0x207f8
leaq 0x198(%rsp), %rdi
callq 0x21260
cmpq $0x0, %rax
seta %al
movb %al, 0x3f(%rsp)
movb 0x3f(%rsp), %al
testb $0x1, %al
jne 0x20805
jmp 0x20970
leaq 0x198(%rsp), %rdi
callq 0x21260
movl %eax, %edx
decl %edx
leaq 0xfc(%rsp), %rdi
xorl %esi, %esi
callq 0x21220
jmp 0x20827
leaq 0xfc(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x21280
movl %eax, 0x38(%rsp)
jmp 0x20842
movq 0x90(%rsp), %rdi
movl 0x38(%rsp), %eax
movl %eax, 0xf8(%rsp)
movslq 0x108(%rsp), %rsi
callq 0x1ff80
movq %rax, 0x10(%rsp)
movslq 0xf8(%rsp), %rsi
leaq 0x198(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x27bc0
movq 0x10(%rsp), %rdi
movslq (%rax), %rsi
callq 0x1fff0
movq 0x18(%rsp), %rdi
movq %rax, 0x20(%rsp)
movslq 0xf8(%rsp), %rsi
callq 0x27bc0
movq 0x20(%rsp), %rdi
movslq 0x4(%rax), %rsi
callq 0x20020
movq %rdx, 0x28(%rsp)
movq %rax, 0x30(%rsp)
jmp 0x208bf
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rcx, 0xe8(%rsp)
movq %rax, 0xf0(%rsp)
leaq 0xe8(%rsp), %rdi
movl $0x1, %esi
callq 0x20ff0
movl 0x104(%rsp), %eax
incl %eax
movl %eax, 0x104(%rsp)
leaq 0x198(%rsp), %rdi
movq %rdi, (%rsp)
callq 0x21360
movq %rax, 0xd0(%rsp)
movslq 0xf8(%rsp), %rsi
leaq 0xd0(%rsp), %rdi
callq 0x27c50
movq %rax, 0xd8(%rsp)
leaq 0xe0(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x27c90
movq (%rsp), %rdi
movq 0xe0(%rsp), %rsi
callq 0x27be0
movq %rax, 0x8(%rsp)
jmp 0x2095e
movq 0x8(%rsp), %rax
movq %rax, 0xc8(%rsp)
jmp 0x207d0
jmp 0x20972
movl 0x108(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x108(%rsp)
jmp 0x207b1
movq 0x90(%rsp), %rax
leaq 0x1e0(%rsp), %rcx
movq %rcx, 0xc0(%rsp)
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x20140
movq %rax, 0xb0(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x20170
movq %rax, 0xa8(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x201a0
testb $0x1, %al
jne 0x209f0
jmp 0x20a78
leaq 0xb0(%rsp), %rdi
callq 0x201e0
movq %rax, 0xa0(%rsp)
cvtsi2sdl 0x28f4(%rsp), %xmm0
movsd 0x2908(%rsp), %xmm2
movsd 0x315e9(%rip), %xmm1    # 0x52008
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rsi
movq %rsi, %rcx
sarq $0x3f, %rcx
movsd 0x31612(%rip), %xmm1    # 0x52050
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
andq %rcx, %rax
orq %rax, %rsi
movq 0xa0(%rsp), %rdx
leaq 0xc0(%rsp), %rdi
callq 0x27cd0
jmp 0x20a64
jmp 0x20a66
leaq 0xb0(%rsp), %rdi
callq 0x20b60
jmp 0x209d2
movb $0x1, 0x1d3(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x21430
testb $0x1, 0x1d3(%rsp)
jne 0x20aa4
movq 0x90(%rsp), %rdi
callq 0x1f250
leaq 0x1568(%rsp), %rdi
callq 0x21490
movq 0x98(%rsp), %rax
addq $0x2928, %rsp           # imm = 0x2928
retq
movq 0x90(%rsp), %rdi
callq 0x1f250
leaq 0x1568(%rsp), %rdi
callq 0x21490
movq 0x1d8(%rsp), %rdi
callq 0x43f0
nopl (%rax,%rax)