首页 \ 问答 \ 在线程之间分割循环迭代(Dividing loop iterations among threads)

在线程之间分割循环迭代(Dividing loop iterations among threads)

我最近编写了一个小数量的计算程序,基本上在N维网格上循环,并在每个点执行一些计算。

for (int i1 = 0; i1 < N; i1++)
  for (int i2 = 0; i2 < N; i2++)
    for (int i3 = 0; i3 < N; i3++)
      for (int i4 = 0; i4 < N; i4++)
        histogram[bin_index(i1, i2, i3, i4)] += 1; // see bottom of question

它工作得很好,yadda yadda yadda,可爱的图表结果;-)但是,后来我想,我的电脑上有2个内核,为什么不让这个程序多线程,所以我可以运行两倍的速度?

现在,我的循环总共运行了大约十亿次计算,我需要一些方法将它们在线程之间分开。 我想我应该将计算分组为“任务” - 比方说最外层循环的每次迭代都是一项任务 - 并将任务交给线程。 我考虑过了

  • 只是给线程#n最外层循环的所有迭代,其中i1 % nthreads == n - 从本质上预先确定哪些任务转到哪个线程
  • 试图设置一些互斥保护的变量,它保存需要执行的下一个任务的参数(本例中为i1 ) - 动态分配任务给线程

有什么理由选择另一种方法? 或者我还没有想过的另一种方法? 它甚至重要吗?

顺便说一下,我用C编写了这个特定的程序,但我想我会在其他语言中再次做同样的事情,所以答案不一定是C语言特定的。 (如果有人知道用于Linux的C库可以做这种事情,但我很想知道它)

编辑 :在这种情况下, bin_index是一个确定性函数,除了它自己的局部变量外,它不会改变任何东西。 像这样的东西:

int bin_index(int i1, int i2, int i3, int i4) {
    // w, d, h are constant floats
    float x1 = i1 * w / N,  x2 = i2 * w / N, y1 = i3 * d / N, y2 = i4 * d / N;
    float l = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + h * h);
    float th = acos(h / l);
    // th_max is a constant float (previously computed as a function of w, d, h)
    return (int)(th / th_max);
}

(尽管我赞赏所有的评论,甚至那些不适用于确定性bin_index的评论)


I recently wrote a small number-crunching program that basically loops over an N-dimensional grid and performs some calculation at each point.

for (int i1 = 0; i1 < N; i1++)
  for (int i2 = 0; i2 < N; i2++)
    for (int i3 = 0; i3 < N; i3++)
      for (int i4 = 0; i4 < N; i4++)
        histogram[bin_index(i1, i2, i3, i4)] += 1; // see bottom of question

It worked fine, yadda yadda yadda, lovely graphs resulted ;-) But then I thought, I have 2 cores on my computer, why not make this program multithreaded so I could run it twice as fast?

Now, my loops run a total of, let's say, around a billion calculations, and I need some way to split them up among threads. I figure I should group the calculations into "tasks" - say each iteration of the outermost loop is a task - and hand out the tasks to threads. I've considered

  • just giving thread #n all iterations of the outermost loop where i1 % nthreads == n - essentially predetermining which tasks go to which threads
  • trying to set up some mutex-protected variable which holds the parameter(s) (i1 in this case) of the next task that needs executing - assigning tasks to threads dynamically

What reasons are there to choose one approach over the other? Or another approach I haven't thought about? Does it even matter?

By the way, I wrote this particular program in C, but I imagine I'll be doing the same kind of thing again in other languages as well so answers need not be C-specific. (If anyone knows a C library for Linux that does this sort of thing, though, I'd love to know about it)

EDIT: in this case bin_index is a deterministic function which doesn't change anything except its own local variables. Something like this:

int bin_index(int i1, int i2, int i3, int i4) {
    // w, d, h are constant floats
    float x1 = i1 * w / N,  x2 = i2 * w / N, y1 = i3 * d / N, y2 = i4 * d / N;
    float l = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + h * h);
    float th = acos(h / l);
    // th_max is a constant float (previously computed as a function of w, d, h)
    return (int)(th / th_max);
}

(although I appreciate all the comments, even those which don't apply to a deterministic bin_index)


原文:https://stackoverflow.com/questions/564577
更新时间:2023-07-12 17:07

最满意答案

这是 Freemarker网页展示技术的语法,类似于jsp(也是网页展示技术),freemarker现在国外用得比较多,好处我就不说了,<#include>这种是freemarker自带的标签命令,类似于jsp的
,
而<@p.>这是通过freemarker的宏自定义出来的命令,相当于jsp 的自定义标签.
ps:当然网页展示技术的背后,都必定会有解析这些命令的,并最终转化成网页能识别的(html)代码(架包)

其他回答

看上去你这个像电脑语言,就是c语言
c开头是include
c#开头是using
他们是调用 引用的意思
你这个引用了一个image_upload.html页面

换句话说 引用好比一个工具箱
假如你要修理电脑 要先把工具包拿出来 然后 在包里拿你所需要的工具
但是你没有这个工具包的时候 你就无法用工具修电脑

相关问答

更多

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)