• <small id='afSCy'></small><noframes id='afSCy'>

      <tfoot id='afSCy'></tfoot>
    1. <i id='afSCy'><tr id='afSCy'><dt id='afSCy'><q id='afSCy'><span id='afSCy'><b id='afSCy'><form id='afSCy'><ins id='afSCy'></ins><ul id='afSCy'></ul><sub id='afSCy'></sub></form><legend id='afSCy'></legend><bdo id='afSCy'><pre id='afSCy'><center id='afSCy'></center></pre></bdo></b><th id='afSCy'></th></span></q></dt></tr></i><div id='afSCy'><tfoot id='afSCy'></tfoot><dl id='afSCy'><fieldset id='afSCy'></fieldset></dl></div>
        <legend id='afSCy'><style id='afSCy'><dir id='afSCy'><q id='afSCy'></q></dir></style></legend>
          <bdo id='afSCy'></bdo><ul id='afSCy'></ul>
      1. 在 Python 中并行化四个嵌套循环

        时间:2023-05-25
          <tfoot id='sWPpb'></tfoot>

          <small id='sWPpb'></small><noframes id='sWPpb'>

                <bdo id='sWPpb'></bdo><ul id='sWPpb'></ul>
                <i id='sWPpb'><tr id='sWPpb'><dt id='sWPpb'><q id='sWPpb'><span id='sWPpb'><b id='sWPpb'><form id='sWPpb'><ins id='sWPpb'></ins><ul id='sWPpb'></ul><sub id='sWPpb'></sub></form><legend id='sWPpb'></legend><bdo id='sWPpb'><pre id='sWPpb'><center id='sWPpb'></center></pre></bdo></b><th id='sWPpb'></th></span></q></dt></tr></i><div id='sWPpb'><tfoot id='sWPpb'></tfoot><dl id='sWPpb'><fieldset id='sWPpb'></fieldset></dl></div>
                  <tbody id='sWPpb'></tbody>
                <legend id='sWPpb'><style id='sWPpb'><dir id='sWPpb'><q id='sWPpb'></q></dir></style></legend>
                • 本文介绍了在 Python 中并行化四个嵌套循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个相当简单的嵌套 for 循环,它遍历四个数组:

                  I have a fairly straightforward nested for loop that iterates over four arrays:

                  for a in a_grid:
                      for b in b_grid:
                          for c in c_grid:
                              for d in d_grid:
                                  do_some_stuff(a,b,c,d)  # perform calculations and write to file
                  

                  也许这并不是在 4D 网格上执行计算的最有效方式.我知道 joblib 能够并行化两个嵌套的 for 循环,例如 this,但我无法将其推广到四个嵌套循环.有什么想法吗?

                  Maybe this isn't the most efficient way to perform calculations over a 4D grid to begin with. I know joblib is capable of parallelizing two nested for loops like this, but I'm having trouble generalizing it to four nested loops. Any ideas?

                  推荐答案

                  我通常使用这种形式的代码:

                  I usually use code of this form:

                  #!/usr/bin/env python3
                  import itertools
                  import multiprocessing
                  
                  #Generate values for each parameter
                  a = range(10)
                  b = range(10)
                  c = range(10)
                  d = range(10)
                  
                  #Generate a list of tuples where each tuple is a combination of parameters.
                  #The list will contain all possible combinations of parameters.
                  paramlist = list(itertools.product(a,b,c,d))
                  
                  #A function which will process a tuple of parameters
                  def func(params):
                    a = params[0]
                    b = params[1]
                    c = params[2]
                    d = params[3]
                    return a*b*c*d
                  
                  #Generate processes equal to the number of cores
                  pool = multiprocessing.Pool()
                  
                  #Distribute the parameter sets evenly across the cores
                  res  = pool.map(func,paramlist)
                  

                  这篇关于在 Python 中并行化四个嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:我可以以某种方式与子进程共享一个异步队列吗 下一篇:块大小与 Python 中的多处理/pool.map 无关?

                  相关文章

                  最新文章

                  • <bdo id='xIa23'></bdo><ul id='xIa23'></ul>
                    <legend id='xIa23'><style id='xIa23'><dir id='xIa23'><q id='xIa23'></q></dir></style></legend>

                    <small id='xIa23'></small><noframes id='xIa23'>

                    1. <i id='xIa23'><tr id='xIa23'><dt id='xIa23'><q id='xIa23'><span id='xIa23'><b id='xIa23'><form id='xIa23'><ins id='xIa23'></ins><ul id='xIa23'></ul><sub id='xIa23'></sub></form><legend id='xIa23'></legend><bdo id='xIa23'><pre id='xIa23'><center id='xIa23'></center></pre></bdo></b><th id='xIa23'></th></span></q></dt></tr></i><div id='xIa23'><tfoot id='xIa23'></tfoot><dl id='xIa23'><fieldset id='xIa23'></fieldset></dl></div>
                      <tfoot id='xIa23'></tfoot>