現在地 >> メニュー >> TBB >> TBB::parallel_for >> TBB::parallel_for::その2

問題


TBB::parallel_forのプログラムを変更して、イテレータでループを並列にまわせ。

答え


//C++系
#include <iostream>
#include <vector>
#include <algorithm>
//TBB系
#include <tbb/task_scheduler_init.h>
#include <tbb/blocked_range.h>
#include <tbb/parallel_for.h>


//-----プロトタイプ宣言 -----//
void AddFunctiond(double &a,const double b);


//--------- TBB用のクラス ---------//
class TbbAddToAry //並列処理で加算する
{
private:
	const double data;

public:
	//コンストラクタ
	TbbAddToAry(double inputdata = 0):data(inputdata){}

	//オペレータ
	void operator()(const tbb::blocked_range< std::vector<double>::iterator > &range) const
	{
		std::vector<double>::iterator it;
		for( it = range.begin(); it != range.end() ; ++it )
		{
			AddFunctiond(*it,data); //各配列にdataを加算する
		}
	}

};

void AddFunctiond(double &a,const double b)
{
	a += b;
}


//-------- ここからメイン関数 ---------//
int main(int argc,char **argv)
{

	std::vector<double> v(500);
	for(int loop = 0; loop < 500;++loop){
		v[loop] = loop; //初期化
	}


	tbb::task_scheduler_init Tbb_Init;//Tbb初期化
	
	tbb::parallel_for( tbb::blocked_range< std::vector<double>::iterator >(v.begin(),v.end()),TbbAddToAry(1000), tbb::auto_partitioner()); //並列化

	Tbb_Init.terminate();//Tbb終了

	std::copy(v.begin(),v.end(),std::ostream_iterator<double>(std::cout,"\n"));

	return EXIT_SUCCESS;
}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






▲よろしければ広告のクリックもお願いします


▲ランキングに参加しました

管理人/副管理人のみ編集できます