a possible low-level optimization

http://www.1point3acres.com/bbs/thread-212960-1-1.html

第二轮白人小哥,一开始问了一道至今不懂的问题,好像是一个vector<uint8_t> nums, 然后又一个256位的vector<int> counts,遍nums,然后counts[nums]++如何化,提示要用到CPU cache西(完全不知道)。小白哥见我懵逼,后来又给了一道3sum,迅速做出。

 

uint8_t input[102400];
uint32_t count[256];
void count_it()
{
    for (int i = 0; i < sizeof(input) / sizeof(input[0]); i++) {
        ++count[input[i]];
    }
}

 

how to optimize? possible points to consider:

a) target "count" array size is 4B*256=1KB, which can fit into L1 cache, so no need to worry about that;

b) input array access is sequential, which is actually cache friendly;

c) update to "count" could have false sharing, but given it's all in L1 cache, that's fine;

d) optimization 1: the loop could be unrolled to reduce loop check;

e) optimization 2: input array could be pre-fetched (i.e. insert PREFETCH instructions beforehand);

    for (int i = 0; i < sizeof(input) / sizeof(input[0]);) {
        // typical cache size is 64 bytes
        __builtin_prefetch(&input[i+64], 0, 3); // prefetch for read, high locality
        for (int j = 0; j < 8; j++) {
            int k = i + j * 8;
            ++count[input[k]];
            ++count[input[k+1]];
            ++count[input[k+2]];
            ++count[input[k+3]];
            ++count[input[k+4]];
            ++count[input[k+5]];
            ++count[input[k+6]];
            ++count[input[k+7]];
        }
        i += 64;
    }

(see https://gcc.gnu.org/onlinedocs/gcc-5.4.0/gcc/Other-Builtins.html for __builtin_prefetch)

f) optimization 3: multi-threading, but need to use lock instruction when incrementing the count;

g) optimization 4: vector extension CPU instructions: "gather" instruction to load sparse locations (count[xxx]) to a zmmx register (512bit, 64byte i.e. 16 integers), then it can process 16 input uchar8_t in one go; then add a constant 512bit integer which adds 1 to each integer. corresponding "scatter" instruction will store back the updated count.

 

第二轮白人小哥,一开始问了一道至今不懂的问题,好像是一个vector<uint8_t> nums, 然后又一个256位的vector<int> counts,遍nums,然后counts[nums]++如何化,提示要用到CPU cache西(完全不知道)。小白哥见我懵逼,后来又给了一道3sum,迅速做出。

转载于:https://www.cnblogs.com/qsort/p/6094767.html

研究双层优化在学习和视觉中的应用,是为了改善学习算法和视觉系统的性能。在学习和视觉任务中,我们通常面临两个层面的优化问题。 第一层优化问题涉及到学习算法的优化,即如何通过合适的学习算法来获得最佳的模型参数。学习算法的优化过程通常涉及到定义损失函数和选择合适的优化方法。然而,常规的优化方法在高维问题中可能会面临挑战,导致在学习过程中陷入局部最优解。因此,研究者们开始探索使用双层优化方法来改进学习算法的性能。双层优化方法通过引入内部优化循环来进一步更新学习算法中的超参数,以改善模型性能。这种方法可以更好地探索参数空间,寻找更优的模型参数,从而提高学习算法的效果。 第二层优化问题涉及到视觉任务的优化,即如何通过图像处理和计算机视觉算法来解决具体的视觉问题。视觉任务可以包括目标检测、图像分割、姿态估计等多个方面。传统的视觉算法通常是通过定义特定的目标函数并使用迭代方法来进行优化。然而,这种方法可能会受到参数选择和初始条件的限制。因此,研究者们开始研究使用双层优化技术来提高视觉任务的性能。双层优化方法通过引入内部优化循环来逐步调整算法超参数和模型参数,以更好地适应特定的视觉任务。 总之,研究双层优化在学习和视觉中的应用,旨在改善学习算法和视觉系统的性能。这种方法可以通过优化学习算法的参数和模型参数,以及优化视觉任务的目标函数和算法参数,来改进学习和视觉的效果。这将有助于在学习和视觉领域取得更好的结果和应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值