一旦我在我正在研究的项目(MMORPG集群,linux 2.6,最近的gcc,8核Intel Xeon)中尝试了TBB内存分配器,并且在收到更差的性能时感到惊讶.可能是什么原因?我预计至少没有更好的性能(所有核心都被加载并且分配定期发生).任何人都可以与TBB共享自己的经验,特别是它的内存分配器?tnx你的时间
我无法编译我的C++程序.如您所见,我正在编写Node.js插件.这是代码(Eamorr_addon.cpp):
#include <iostream>
#include <v8.h>
#include <node.h>
#include <gmp.h>
#include "Definitions.h"
#include "Rk.h"
#include "tbb/concurrent_hash_map.h"
//#include <tbb/concurrent_vector.h>
using namespace std;
using namespace v8;
static Handle<Value> Echo(const Arguments& args) {
HandleScope scope;
if (args.Length() < 1) {
return ThrowException(Exception::TypeError(String::New("Bad argument")));
}
return scope.Close(args[0]);
}
extern "C" void init (Handle<Object> target)
{
HandleScope scope;
target->Set(String::New("hello"), String::New("world"));
NODE_SET_METHOD(target, "echo", Echo);
}
Run Code Online (Sandbox Code Playgroud)
我删除线条时一切正常#include "tbb/concurrent_hash_map.h".
这是我的gyp make文件:
{
'targets': [
{
'target_name':'Eamorr_addon',
'sources':['src/Eamorr_addon.cpp'],
'include_dirs': ['/usr/include','/usr/local/include/'],
'cflags': ['-Wall','-fopenmp'],
'ldflags': ['-ltbb','-lgmp']
}
]
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时 …
有没有办法在C/C++中并行减少CPU上的数组?我最近了解到使用openmp是不可能的.还有其他选择吗?
我发现英特尔的Thread Building Blocks库令人困惑.例如,我想使用TBB并行化以下计算:
int CountNegatives(std::vector<Trigraph> input)
{
int count = 0;
for(int i = 0; i< input.size(); i++)
{
if(input[i].VisibleFrom(viewPoint))
{
count++;
}
}
return count;
}
Run Code Online (Sandbox Code Playgroud)
我知道你必须operator()在TBB中使用一个类来做到这一点; 真的吗?我本来希望在TBB上阅读一些"初学者教程",以帮助我解决这个问题,但似乎没有任何初学者教程.
你能帮我把TBB应用到这个计算中吗?
我刚刚在新的OpenCV 2.4.3中看到他们添加了一个通用的parallel_for.所以按照这个例子,我试着自己实现它.我用我的代码完成了所有功能,但是当我按照常规"for"命令以典型的串行方式对其处理时间进行计时时,结果显着更快,或者通常稍慢一点!
我想也许这与我推入向量或其他东西有关(我对并行处理我是一个非常大的菜鸟),所以我设置了一个只运行一个大数字的测试循环,它仍然无法正常工作.
码:
class Parallel_Test : public cv::ParallelLoopBody
{
private:
double* const mypointer;
public:
Parallel_Test(double* pointer)
: mypointer(pointer){
}
void operator() (const Range& range) const
{
//This constructor needs to be here otherwise it is considered an abstract class.
// qDebug()<<"This should never be called";
}
void operator ()(const cv::BlockedRange& range) const
{
for (int x = range.begin(); x < range.end(); ++x){
mypointer[x]=x;
}
}
};
//TODO Loop pixels in parallel
double t = (double)getTickCount();
//TEST PARALELL …Run Code Online (Sandbox Code Playgroud) 我正在使用concurrent_bounded_queueIntel TBB 4.1 Update 3在生产者线程和使用者线程之间进行通信:
队列类有一个叫做方法abort,其抛出tbb::user_abort到上阻塞所有线程pop和push队列实例的。两个线程之间的通信可能如下所示:
ConsThread | ProdThread
-----------+-------------
q.pop | get new data
(wait) | q.push
process | get new data
q.pop | no more data!
(wait) | q.abort
quit | quit
Run Code Online (Sandbox Code Playgroud)
不幸的是,即使在这个简单的示例中,我也不能使用它来可靠地关闭队列,因为如果某些使用者pop在调用之前没有完成对先前ped数据的处理abort,他们将完成迭代并返回阻塞pop:
ConsThread | ProdThread
-----------+-------------
q.pop | get new data
(wait) | q.push
process | get new data
process | no more data!
process | q.abort
process | …Run Code Online (Sandbox Code Playgroud) 我正在开发针对英特尔x86_64机器的C++中需要时间要求的模拟.经过一番研究,我找到了两个有趣的库来实现并行化:
正如文档中所述,它们都针对多核处理器的并行性,但仍未确定哪一个是最好的.AFAIK Cilkplus简单地实现了三个关键字,以实现更轻松的并行性(这会导致重新编译GCC以支持这些关键字); 而TBB只是一个促进更好的并行开发的库.
考虑到我在安装CilkPlus时遇到了很多问题(仍在尝试并仍在尖叫).所以我想知道,我应该先检查TBB吗?Cilkplus比TBB好吗?你会推荐什么?
我是否应该完成安装CilkPlus(仍在为此祈祷),是否可以将TBB与它一起使用?他们可以一起工作吗?是否有人使用CiclkPlus和TBB体验过发展?你会建议和他们一起工作吗?
谢谢
我正在学习英特尔的TBB库.当对std::vector结果中的所有值求和时,tbb::parallel_reduce与std::accumulate向量中超过16.777.220个元素的情况不同(在16.777.320个元素处遇到错误).这是我的最小工作示例:
#include <iostream>
#include <vector>
#include <numeric>
#include <limits>
#include "tbb/tbb.h"
int main(int argc, const char * argv[]) {
int count = std::numeric_limits<int>::max() * 0.0079 - 187800; // - 187900 works
std::vector<float> heights(size);
std::fill(heights.begin(), heights.end(), 1.0f);
float ssum = std::accumulate(heights.begin(), heights.end(), 0);
float psum = tbb::parallel_reduce(tbb::blocked_range<std::vector<float>::iterator>(heights.begin(), heights.end()), 0,
[](tbb::blocked_range<std::vector<float>::iterator> const& range, float init) {
return std::accumulate(range.begin(), range.end(), init);
}, std::plus<float>()
);
std::cout << std::endl << " Heights serial sum: " << ssum …Run Code Online (Sandbox Code Playgroud) 当我使用我的i7-6700HQ处理器的所有8个逻辑内核时,我的并行化版本的fibonacci实现(效率低,只是为了比较库的性能)比普通的低效实现慢得多,这是一个惊喜.与非并行实现相比,处理器风扇开始变得混乱,处理时间非常慢.
这个例子直接来自英特尔的TBB教程 - https://www.threadingbuildingblocks.org/tutorial-intel-tbb-task-based-programming
这是我的代码
#include <tbb/task_group.h>
#include <chrono>
#include <iostream>
#define FIB_NUM 40
long fib1(int n)
{
if(n < 2) return n;
else
{
int x, y;
tbb::task_group g;
g.run([&]{x=fib1(n - 1);});
g.run([&]{y=fib1(n - 2);});
g.wait();
return x + y;
}
}
long fib2(int n)
{
return n < 2? n : fib2(n - 1) + fib2(n - 2);
}
int main()
{
auto t1 = std::chrono::high_resolution_clock::now();
std::cout << fib2(FIB_NUM) << std::endl;
auto t2 = std::chrono::high_resolution_clock::now();
std::cout << (t2 …Run Code Online (Sandbox Code Playgroud) 我正在使用TBB自定义内存分配器.
tbb::memory_pool<std::allocator<char>> shortTermPool;
typedef tbb::memory_pool_allocator<Result*> custom_allocator;
std::vector<Result*,custom_allocator>* results =(std::vector<Result*,custom_allocator>*)shortTermPool.malloc(sizeof(std::vector<Result*,custom_allocator>));
Run Code Online (Sandbox Code Playgroud)
问题是设置分配器是在构造函数中.Malloc不会调用构造函数.默认用法是这样的:
tbb::memory_pool<std::allocator<char>> shortTermPool;
typedef tbb::memory_pool_allocator<Result*> custom_allocator;
std::vector<Result*,custom_allocator> results (custom_allocator(shortTermPool));
Run Code Online (Sandbox Code Playgroud)
有没有办法做一个stl容器的malloc,然后分配一个自定义分配器?