Paddle-Lite icon indicating copy to clipboard operation
Paddle-Lite copied to clipboard

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

Open changbindu opened this issue 4 months ago • 4 comments

这段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;
      }
    }
  }

changbindu avatar Apr 28 '24 09:04 changbindu

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

shentanyue avatar May 07 '24 02:05 shentanyue

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

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

changbindu avatar May 07 '24 04:05 changbindu

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

shentanyue avatar May 07 '24 04:05 shentanyue

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

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

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

ddchenhao66 avatar May 17 '24 07:05 ddchenhao66