多线程的内存注意事项 [英] Memory considerations with multithreading

查看:56
本文介绍了多线程的内存注意事项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C/C ++上实现am算法来处理一些矢量,我认为将其并行是一个好主意,因为我正在使用多核CPU.我有一些使用GPGPU的经验,不良的内存访问会破坏整个性能,我是否还需要考虑CPU内核之间的任何特殊访问布局?

I'm implementing am algorithm on C/C++ to process some vectors and I thought it could be a good idea to make it parallel since I'm working with a multicore CPU. I have some experience with GPGPU and there bad memory access can ruin the entire performance, do I need to consider any special access layout between the cores on the CPU also?

谢谢

推荐答案

使用多处理器设置可能会遇到许多与内存相关的问题,其中一些问题会使应用程序的爬网速度变慢.

There are a number of memory-related problems you can run into with a multiprocessor setup, and some of them can slow an application to a crawl.

您需要大致了解盒子上的缓存行大小,然后尝试2种操作:

You need to be roughly aware of the cache line size on your box and attempt 2 things:

  1. 限制单个线程按关闭时间顺序访问的数据高速缓存行(尤其是您写入的高速缓存行)的数量.即,避免不必要地弄脏"更多的缓存行.
  2. 避免像瘟疫那样,有两个单独的线程同时"通过一次写入来访问同一数据高速缓存行.

(如果要处理必须分页的大型数据结构,则以上两个规则也适用于数据页.)

(The above two rules also apply to data pages, if you're dealing with large data structures that must be paged.)

在可能的情况下,为每个线程设置单独的工作数据结构(尤其是堆),而不是共享数据.特别要提防所有线程都会更新一个公共计数器,并且(显然)避免使用锁和信号灯,除非在关键时刻绝对需要同步线程.

Where possible, set up separate working data structures (especially heap) for each thread, rather than sharing the data. Especially beware of having a common counter that all threads update, and (obviously) avoid locks and semaphores except at critical junctures where you absolutely need to synchronize threads.

这篇关于多线程的内存注意事项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆