- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 python 中有一个令人尴尬的并行 for 循环(要重复 n
次),每次迭代执行一个复杂的任务并返回 numpy 数组和 dict 的混合(所以不是要填充的单个数字成一个数组 - 否则暂时将它们视为复杂的一堆)。重复不需要按任何特定顺序 - 我只需要能够唯一地识别 n
迭代中的每个 i
(例如,在重复中独立保存结果).事实上,它们甚至不需要通过索引/计数器来识别,而是一种独特的东西,因为它们不需要排序(我可以轻松地将它们填回一个更大的数组。)
举一个更具体的例子,我想并行执行以下任务:
def do_complex_task(complex_input1, input2, input3, input_n):
"all important computation done here - independent of i or n"
inner_result1, inner_result2 = np.zeros(100), np.zeros(100)
for smaller_input in complex_input1:
inner_result1 = do_another_complex_task(smaller_input, input2, input3, input_n)
inner_result2 = do_second_complex_task(smaller_input, input2, input3, input_n)
# do some more to produce few more essential results
dict_result = blah()
unique_identifier = get_unique_identifier_for_this_thread() # I don't know how
# save results for each repetition independently before returning,
# instead of waiting for full computation to be done which can take a while
out_path = os.path.join(out_dir, 'repetition_{}.pkl'.format(unique_identifier))
return inner_result1, inner_result2, inner_result_n, dict_result
def main_compute()
"main method to run the loop"
n = 256 # ideally any number, but multiples of 4 possible, for even parallelization.
result1 = np.zeros([n, 100])
result2 = np.zeros([n, 100])
result_n = np.zeros([n, 100])
dict_result = list()
# this for loop does not need to be computed in any order (range(n) is an illustration)
# although this order would be ideal, as it makes it easy to populate results into a bigger array
for i in range(n):
# this computation has nothing to do with i or n!
result1[i, :], result2[i, :], result_n[i, :], dict_result[i] = do_complex_task(complex_input1, input2, input3, input_n)
# I need to parallelize the above loop to speed up stupidly parallel processing.
if __name__ == '__main__':
pass
我已经相当广泛地阅读了,但不清楚哪种策略更聪明、更简单,而且没有任何可靠性问题。
此外,complex_input1
可能很大 - 所以我不希望酸洗带来大量 I/O 开销。
我当然可以返回一个列表(包含所有复杂部分),该列表附加到主列表,稍后可以将其组合成我喜欢的格式(矩形数组等)。这可以通过 joblib 轻松完成。例如。不过,我正努力向大家学习,以确定好的解决方案。
编辑:我想我正在解决以下解决方案。让我知道它可能出什么问题,或者我怎样才能在速度、无副作用等方面进一步改进它。在我的笔记本电脑上进行了几次非结构化试验后,不清楚是否有明显的加速由于这个。
from multiprocessing import Pool, Manager
chunk_size = int(np.ceil(num_repetitions/num_procs))
with Manager() as proxy_manager:
shared_inputs = proxy_manager.list([complex_input1, input2, another, blah])
partial_func_holdout = partial(key_func_doing_work, *shared_inputs)
with Pool(processes=num_procs) as pool:
results = pool.map(partial_func_holdout, range(num_repetitions), chunk_size)
最佳答案
multiprocessing.Pool.map
import multiprocessing
from functools import partial
def do_task(a, b):
return (42, {'x': a * 2, 'y': b[::-1]})
if __name__ == '__main__':
a_values = ['Hello', 'World']
with multiprocessing.Pool(processes=3) as pool:
results = pool.map(partial(do_task, b='fixed b value'), a_values)
print(results)
在此之后,results
将包含与 a_values
相同顺序的结果。
要求是参数和返回值是 Pickle'able 的。除此之外,它们可能很复杂,但如果数据量很大,可能会有一些性能损失。
我不知道这是否是您认为好的解决方案;我已经使用了很多次,对我来说效果很好。
您可以将返回值放在一个类中,但我个人认为这并没有真正带来好处,因为 Python 没有静态类型检查。
它只是并行启动 #processes
个作业。它们应该是独立的,顺序无关紧要(我认为它们是按提供的顺序开始的,但它们可能以另一个顺序完成)。
示例基于 this answer .
关于python - 令人尴尬的并行 for 循环,每次迭代中都有复杂的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46391191/
有没有办法同时运行 2 个不同的代码块。我一直在研究 R 中的并行包,它们似乎都基于在循环中运行相同的函数。我正在寻找一种同时运行不同函数的方法(循环的 1 次迭代)。例如,我想在某个数据对象上创建一
无论如何增加 Parallel.For 启动后的循环次数?示例如下: var start = 0; var end = 5; Parallel.For(start, end, i => { C
我是 Golang 的新手,正在尝试了解并发和并行。我阅读了下面提到的关于并发和并行的文章。我执行了相同的程序。但没有得到相同的(混合字母和字符)输出。首先获取所有字母,然后获取字符。似乎并发不工作,
我正在寻找同时迭代 R 中两个或多个字符向量/列表的方法,例如。有没有办法做这样的事情: foo <- c('a','c','d') bar <- c('aa','cc','dd') for(i in
我对 Raku 很陌生,我对函数式方法有疑问,尤其是 reduce。 我最初有这样的方法: sub standardab{ my $mittel = mittel(@_); my $foo =
我最近花了很多时间来学习实时音频处理的细节,我发现的大多数库/工具都是c / c++代码或脚本/图形语言的形式,并在其中编译了c / c++代码。引擎盖。 使用基于回调的API,与GUI或App中的其
我正在使用 JMeter 进行图像负载测试。我有一个图像名称数组并遍历该数组,我通过 HTTP 请求获取所有图像。 -> loop_over_image - for loop controller
我整个晚上都在困惑这个问题...... makeflags = ['--prefix=/usr','--libdir=/usr/lib'] rootdir='/tmp/project' ps = se
我正在尝试提高计算图像平均值的方法的性能。 为此,我使用了两个 For 语句来迭代所有图像,因此我尝试使用一个 Parallel For 来改进它,但结果并不相同。 我做错了吗?或者是什么导致了差异?
假设您有一个并行 for 循环实现,例如ConcRT parallel_for,将所有工作放在一个 for 循环体内总是最好的吗? 举个例子: for(size_t i = 0; i < size()
我想并行运行一部分代码。目前我正在使用 Parallel.For 如何让10、20或40个线程同时运行 我当前的代码是: Parallel.For(1, total, (ii) =>
我使用 PAY API 进行了 PayPal 自适应并行支付,其中无论用户(买家)购买什么,都假设用户购买了总计 100 美元的商品。在我的自适应并行支付中,有 2 个接收方:Receiver1 和
我正在考虑让玩家加入游戏的高效算法。由于会有大量玩家,因此算法应该是异步的(即可扩展到集群中任意数量的机器)。有细节:想象有一个无向图(每个节点都是一个玩家)。玩家之间的每条边意味着玩家可以参加同一场
我有一个全局变量 volatile i = 0; 和两个线程。每个都执行以下操作: i++; System.out.print(i); 我收到以下组合。 12、21 和 22。 我理解为什么我没有得到
我有以下称为 pgain 的方法,它调用我试图并行化的方法 dist: /***************************************************************
我有一个 ruby 脚本读取一个巨大的表(约 2000 万行),进行一些处理并将其提供给 Solr 用于索引目的。这一直是我们流程中的一大瓶颈。我打算在这里加快速度,我想实现某种并行性。我对 Ru
我正在研究 Golang 并遇到一个问题,我已经研究了几天,我似乎无法理解 go routines 的概念以及它们的使用方式。 基本上我是在尝试生成数百万条随机记录。我有生成随机数据的函数,并将创建一
我希望 for 循环使用 go 例程并行。我尝试使用 channel ,但没有用。我的主要问题是,我想在继续之前等待所有迭代完成。这就是为什么在它不起作用之前简单地编写 go 的原因。我尝试使用 ch
我正在使用 import Control.Concurrent.ParallelIO.Global main = parallel_ (map processI [1..(sdNumber runPa
我正在尝试通过 makePSOCKcluster 连接到另一台计算机: library(parallel) cl ... doTryCatch -> recvData -> makeSOCKm
我是一名优秀的程序员,十分优秀!