PaddlePaddle / Paddle-Lite

PaddlePaddle High Performance Deep Learning Inference Engine for Mobile and Edge (飞桨高性能深度学习端侧推理引擎)
https://www.paddlepaddle.org.cn/lite
Apache License 2.0
6.89k stars 1.6k forks source link

[BUG] sort_cpuid_by_max_freq()中的bubble sort代码逻辑错误 #10507

Open changbindu opened 2 months ago

changbindu commented 2 months ago

这段bubble sort代码是错误的,一个冒泡排序却操作了两个数组。

void sort_cpuid_by_max_freq(const std::vector<int>& max_freqs,
                            std::vector<int>* cpu_ids,
                            std::vector<int>* cluster_ids) {
  ...
  // sort cpuid as big core first
  // simple bubble sort
  for (int i = 0; i < cpu_num; i++) {
    for (int j = i + 1; j < cpu_num; j++) {
      if (max_freqs[i] < max_freqs[j]) {
        // swap
        int tmp = cpu_ids->at(i);
        cpu_ids->at(i) = cpu_ids->at(j);
        cpu_ids->at(j) = tmp;
      }
    }
  }
shentanyue commented 1 month ago

您好,没明白您的意思,贴出的冒泡排序代码只针对 cpu_ids 数组进行了排序,没有操作两个数组。

changbindu commented 1 month ago

您好,没明白您的意思,贴出的冒泡排序代码只针对 cpu_ids 数组进行了排序,没有操作两个数组。

列几个数据演算一下就明白了。代码调整了cpu_ids数组的元素顺序,但是比较大小确实参考另一个数组的值,但这个数组的元素位置没变动,因此参考的值实际上不匹配了。

shentanyue commented 1 month ago

好的,我明白您的意思了。 非常感谢您的指出~

ddchenhao66 commented 1 month ago

您好,非常抱歉这么晚才合入您的PR。在这个PR中 https://github.com/PaddlePaddle/Paddle-Lite/pull/10518 ,我们采纳了您的代码解决了这个问题,合入develop分支后您就可以关闭这个issue了。再次感谢您对Paddle-Lite的关注与贡献!

您好,没明白您的意思,贴出的冒泡排序代码只针对 cpu_ids 数组进行了排序,没有操作两个数组。

列几个数据演算一下就明白了。代码调整了cpu_ids数组的元素顺序,但是比较大小确实参考另一个数组的值,但这个数组的元素位置没变动,因此参考的值实际上不匹配了。