C ++超快速线程安全rand函数

编程入门 行业动态 更新时间:2024-10-27 09:41:24
本文介绍了C ++超快速线程安全rand函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 void NetClass::Modulate(vector <synapse> & synapses ) { int size = synapses.size(); int split = 200 * 0.5; for(int w=0; w < size; w++) if(synapses[w].active) synapses[w].rmod = ((rand_r(seedp) % 200 - split ) / 1000.0); }

函数 rand_r(seedp)是严重瓶颈我的程序。具体来说,当我运行serialy时,它减慢了3倍,在16个内核上运行时减慢了4.4倍。 rand()不是一个选项,因为它更糟。有什么我可以做的简化这个吗?如果它会有所变化,我认为我可以在统计随机性方面承受损失。将预生成(执行前)随机数列表,然后加载到线程堆栈是一个选项?

The function rand_r(seedp) is seriously bottle-necking my program. Specifically, its slowing me by 3X when run serialy, and 4.4X when run on 16 cores. rand() is not an option because its even worse. Is there anything I can do to streamline this? If it will make a difference, I think I can sustain a loss in terms of statistical randomness. Would pre-generating (before execution) a list of random numbers and then loading to the thread stacks be an option?

推荐答案

问题是 seedp 变量在几个线程之间共享。处理器核心必须在每次访问这种不断变化的值时同步其高速缓存,这会妨碍性能。解决方案是所有线程都使用自己的 seedp ,因此避免缓存同步。

Problem is that seedp variable (and its memory location) is shared among several threads. Processor cores must synchronize their caches each time they access this ever changing value, which hampers performance. The solution is that all threads work with their own seedp, and so avoid cache synchronization.

更多推荐

C ++超快速线程安全rand函数

本文发布于:2023-11-17 11:36:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   函数   快速   rand

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!