gpt4 book ai didi

java - 是否有可能在 4 核 Android 设备中高效地实现并行数学计算? (这个与接口(interface)无关)

转载 作者:行者123 更新时间:2023-12-01 04:42:00 29 4
gpt4 key购买 nike

我知道,为了在 Android 中保持响应式界面,繁重的工作必须在独立线程中完成。我很清楚如何实现这一点(通过使用 AsynTask...等),这不是问题的重点,只是让每个人都知道。

但是我已经在一个非常简单的并行程序上苦苦挣扎了一段时间。该程序搜索长度为 15000000 的数组中的最小整数。

我实现了这个可运行的:

...

run(){

highestInteger = integers[firstIndex];

for(int i = firstIndex; i < secondIndex; i++){
if(highestInteger<integers[i]){
highestInteger = integers[i];
}
}

}

...所以我可以在数组的前半部分(在一个线程中)查找最大整数,并在数组的另一半(在第二个线程中)查找最大整数。

该程序在我的计算机上运行得很好(作为 java/非 android 程序),并且“非常好”是指并行时间比串行时间短(几乎一半)。

但在我的 Android 平板电脑(4 核)上,时间通常是相同的,并且串行时间几乎总是更短。

我确实注意到(使用调试器)我的平板电脑中有多个线程正在运行:

  1. 主/ui 线程(剩余 3 个核心)
  2. Binder 1 线程(剩余 2 个核心)
  3. Binder 2 线程(剩余 1 个核心:( )
  4. Binder 3 线程(有时我在调试器中看到它,有时我不要)。

所以有 3 个线程在运行,我需要至少 2 个空闲核心才能让我的程序高效运行。我读过一些有关 Binder 线程的内容,但我不太了解。

有没有办法解决这个问题?有没有办法可以避免自动创建这些 Binder 线程?或者在我们拥有 6 核设备之前不可能让这种线程工作?

最佳答案

I do have notice (with the debugger) that in my tablet there are several threads running

您已经创建了多个线程。大多数将在等待 I/O 时被阻塞。

Is there a way to solve this or not?

核心分配的决定是由操作系统做出的,并且会考虑其他程序,加上功耗(保持所有四个核心始终运行对电池来说非常糟糕),正如 Andy Fadden(核心的Android 团队)在 this SO comment 中指出和 this SO comment 。请注意,目前约有 7.5 亿 Android 设备在使用,其中绝大多数的核心数量少于四个,而且大多数只有一个核心,因此您也需要考虑到这一点。

Is there a way in which I can avoid the automatic creation of those binder threads or not?

只有不编写 Android 应用程序。这些线程用于进程间通信,这对于运行 Android 应用程序至关重要。

Or it is not possible to get this kind of threading to work until we have like a 6 core device?

这当然是可能的。 Andy Fadden 在 this StackOverflow answer 中演示了它。可能有一些方法可以重新组织您的算法,以便在 Android 上更好地利用 SMP,如 the documentation 中所述。 。您还可以考虑将 Renderscript Compute 作为使用 Java 进行工作的替代方案。

关于java - 是否有可能在 4 核 Android 设备中高效地实现并行数学计算? (这个与接口(interface)无关),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16378056/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com