Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading Linux SMPs上的Boost线程和不存在的加速 我用SoBo::线程编写了一个C++程序的小例子。因为有215行,所以我把它贴在了pastebin上_Multithreading_Boost_Ubuntu_Boost Thread_Smp - Fatal编程技术网

Multithreading Linux SMPs上的Boost线程和不存在的加速 我用SoBo::线程编写了一个C++程序的小例子。因为有215行,所以我把它贴在了pastebin上

Multithreading Linux SMPs上的Boost线程和不存在的加速 我用SoBo::线程编写了一个C++程序的小例子。因为有215行,所以我把它贴在了pastebin上,multithreading,boost,ubuntu,boost-thread,smp,Multithreading,Boost,Ubuntu,Boost Thread,Smp,该程序创建大量浮点(当前为1gb)并将它们相加,首先按顺序进行,然后使用多个线程(托管在device_matrix类中)。假设机器是SMP,我希望看到代码的加速。在我的Windows机器上,当使用4个device_matrix实例(在我的双核超线程Intel Core2 CPU上提供4个线程)时,我看到了四倍的加速。Windows上的输出如下所示: starting computation device_matrix count 4 elements

该程序创建大量浮点(当前为1gb)并将它们相加,首先按顺序进行,然后使用多个线程(托管在device_matrix类中)。假设机器是SMP,我希望看到代码的加速。在我的Windows机器上,当使用4个device_matrix实例(在我的双核超线程Intel Core2 CPU上提供4个线程)时,我看到了四倍的加速。Windows上的输出如下所示:

starting computation
device_matrix count       4
elements                  268435456
UINT_MAX                  4294967295
data size total           1024 mb
size per device_matrix    256 mb
reference                 134224128.00000
result                    134224128.00000
time taken (init)         12.015 secs
time taken (single)       3.422 secs
time taken (device)       0.859 secs
real    0m7.292s
user    0m10.340s
sys     0m0.340s
但是,当我在我可用的Ubuntu机器上编译相同的代码时,我会看到以下输出:

starting computation
device_matrix count       8
elements                  268435456
UINT_MAX                  4294967295
data size total           1024 mb
size per device_matrix    128 mb
reference                 134215408.00000
result                    134215400.00000
time taken (init)         3.670 secs
time taken (single)       3.030 secs
time taken (threaded)     3.950 secs
cpu:
                       Intel(R) Core(TM) i7 CPU         930  @ 2.80GHz, 1600 MHz
                       ... 7 more times
在这里,没有看到加速(事实上,它慢了很多)

我使用的Ubuntu机器有以下uname-a输出

Linux gpulab03 2.6.32-23-generic #37-Ubuntu SMP Fri Jun 11 08:03:28 UTC 2010 x86_64 GNU/Linux
hwinfo-short提供以下输出:

starting computation
device_matrix count       8
elements                  268435456
UINT_MAX                  4294967295
data size total           1024 mb
size per device_matrix    128 mb
reference                 134215408.00000
result                    134215400.00000
time taken (init)         3.670 secs
time taken (single)       3.030 secs
time taken (threaded)     3.950 secs
cpu:
                       Intel(R) Core(TM) i7 CPU         930  @ 2.80GHz, 1600 MHz
                       ... 7 more times
我把它读作有八个核的机器(好的,四核,带HT)

我使用以下行在Windows上编译程序:

cl /Fe"boost.exe" /EHsc -I. boost.cpp /link /LIBPATH:"C:\boost\boost_1_45_0\stage\lib"
在Ubuntu上,我使用以下行:

g++ -O0 -v -o boost -I$HOME/Code/boost -L$HOME/Code/boost/stage/lib boost.cpp -lboost_thread-gcc44-mt
运行上述行时的输出在这里,以防它能告诉任何人任何事情

因为我不习惯在Linux上开发,所以我不知道该找什么。是否需要将某些标志传递给GCC,或者需要在某个地方启用某些设置,以获得实际的并发线程

我已经在网上查找了一个使用boost::thread的示例程序,它可以为我提供一些基准测试,但我只找到了一些小的生产者-消费者示例,它们不需要处理任何“繁重”的东西

另外,在一个线程中使用time命令会给出以下时间(以防万一boost::timer不可靠):

当使用8个线程时,我看到以下内容:

starting computation
device_matrix count       4
elements                  268435456
UINT_MAX                  4294967295
data size total           1024 mb
size per device_matrix    256 mb
reference                 134224128.00000
result                    134224128.00000
time taken (init)         12.015 secs
time taken (single)       3.422 secs
time taken (device)       0.859 secs
real    0m7.292s
user    0m10.340s
sys     0m0.340s
这似乎并不意味着跑得更快


我还应该提到,我使用的是一个普通的用户帐户,我自己构建了boost(因此,在Linux上为此目的在“普通”文件夹之外链接它)。这也意味着我严重限制了我可以安装的内容,等等。线程是否也存在类似的限制?

我认为问题在于
boost::timer
。如果使用
gettimeofday
而使用减法,则会得到不同的计时结果

看起来好像是
clock()
,这是
boost::timer
使用的,它返回的是整个程序使用的CPU时间量,而不仅仅是一个线程。对我来说,这看起来像一个助推bug

我制作了一个新版本的代码,与CentOS 5机器上的Boost兼容。我将您的do_sum操作修改为一个自由函数,因此我得到保证,对于单线程和多线程,sum的计算方法完全相同。我添加了一个非Windows标题,以便使用gettimeofday


@Chris Becke:好吧,我在帖子里打错了。这显然是boost::thread,否则我的东西甚至无法编译。我开始搞砸了,现在我和你一样困惑。当我将计时器添加到求和函数本身时,即使是小计数,似乎也需要很长时间才能运行。非常感谢您的关注。我确实阅读了timer类的文档,虽然它确实提到依赖时钟,但我不知道Linux上时钟的这种行为(我也看到了timer.hpp中的注释,似乎没有指出Linux上的这种行为)。这是一个巨大的耻辱,UPVOUTS和努力没有腐蚀这个网站,我只有一个UPVOUT给:-)这是令人欣慰的看到,代码实际上做什么,我认为然而。