[Size=small] First Download:
Http://www.threadingbuildingblocks.org/uploads/77/111/2.1/tbb21_20080605oss_win.zip
Currently 2.1 version
Unzip to C drive, open vs2005, set VC + + project directory
Include
C:\tbb21oss_win\include
Execute file:
C:\tbb21oss_win\ia32\vc8\bin
Library files:
C:\tbb21oss_win\ia32\vc8\lib
Finally set up my computer-environment variable settings
Add the following to the front of the path section, and remember to add a ";": C:\tbb21oss_win\ia32\vc8\bin
Then add a variable:
Tbb21_install_dir to C:\tbb21oss_win
Open vs2005 command line tool, go to C:\tbb21oss_win\ia32\vc8\bin path, execute Tbbvars.bat file
Can start the first program, remember to bring the tbb.lib corresponding to the release compilation mode
/*
Copyright 2005-2008 Intel Corporation. All rights Reserved.
This file was part of threading Building Blocks.
Threading Building Blocks is free software; You can redistribute it
and/or modify it under the terms of the GNU general public License
Version 2 as published by the Free software Foundation.
Threading Building Blocks is distributed on the hope that it'll be
Useful, but without any WARRANTY; Without even the implied warranty
of merchantability or FITNESS for A particular PURPOSE. See the
GNU general public License for more details.
You should has received a copy of the GNU general public License
Along with threading Building Blocks; If not, write to the free software
Foundation, Inc., Wuyi Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, you could use the this file as part of a free software
Library without restriction. Specifically, if other files instantiate
Templates or use macros or inline functions from the This file, or you compile
This file and the link it with the other files to produce a executable, this
File does not by itself cause the resulting executable to being covered by
The GNU general public License. This exception does not however
Invalidate any and reasons why the executable file might is covered by
The GNU general public License.
*/
//
Example program, reads a file of text and changes the first letter
of each word to upper case.
//
#include "tbb/pipeline.h"
#include "Tbb/tick_count.h"
#include "Tbb/task_scheduler_init.h"
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
using namespace Std;
//! Buffer that holds block of characters and last character of previous buffer.
Class Mybuffer {
static const size_t Buffer_size = 10000;
char* My_end;
//! Storage[0] holds the last character of the previous buffer.
Char Storage[1+buffer_size];
Public
//! Pointer to first character in the buffer
Char* begin () {return storage+1;}
Const CHAR* begin () const {return storage+1;}
//! Pointer to one past last character in the buffer
Char* End () const {return my_end;}
//! Set end of buffer.
void Set_end (char* new_ptr) {my_end=new_ptr;}
//! Number of bytes a buffer can hold
size_t max_size () const {return buffer_size;}
//! Number of bytes appended to buffer.
size_t size () const {return my_end-begin ();}
};
Class Myinputfilter:public Tbb::filter {
Public
static const size_t N_buffer = 8;
Myinputfilter (file* input_file_);
Private
File* Input_file;
size_t Next_buffer;
Char Last_char_of_previous_buffer;
Mybuffer Buffer[n_buffer];
/*override*/void* operator (void*);
};
Myinputfilter::myinputfilter (file* input_file_):
Filter (/*is_serial=*/true),
Next_buffer (0),
Input_file (Input_file_),
Last_char_of_previous_buffer (")
{
}
void* Myinputfilter::operator () (void*) {
mybuffer& B = Buffer[next_buffer];
Next_buffer = (next_buffer+1)% N_buffer;
size_t n = fread (B.begin (), 1, B.max_size (), input_file);
if (!n) {
End of File
return NULL;
} else {
B.begin () [-1] = Last_char_of_previous_buffer;
Last_char_of_previous_buffer = B.begin () [n-1];
B.set_end (B.begin () +n);
Return &b;
}
}
//! Filter that changes the first letter of each word from lower case to upper case.
Class Mytransformfilter:public Tbb::filter {
Public
Mytransformfilter ();
/*override*/void* operator () (void* item);
};
Mytransformfilter::mytransformfilter ():
Tbb::filter (/*ordered=*/false)
{}
/*override*/void* Mytransformfilter::operator () (void* item) {
mybuffer& B = *static_cast<mybuffer*> (item);
int prev_char_is_space = B.begin () [ -1]== ';
For (char* S=b.begin (); S!=b.end (); ++s) {
if (Prev_char_is_space && islower ((unsigned char) *s))
*s = ToUpper (*s);
Prev_char_is_space = Isspace ((unsigned char) *s);
}
Return &b;
}
//! Filter that writes the buffer to a file.
Class Myoutputfilter:public Tbb::filter {
File* My_output_file;
Public
Myoutputfilter (file* output_file);
/*override*/void* operator () (void* item);
};
Myoutputfilter::myoutputfilter (file* output_file):
Tbb::filter (/*is_serial=*/true),
My_output_file (output_file)
{
}
void* Myoutputfilter::operator () (void* item) {
mybuffer& B = *static_cast<mybuffer*> (item);
Fwrite (B.begin (), 1, B.size (), my_output_file);
return NULL;
}
static int nthread = Tbb::task_scheduler_init::automatic;
static const char* InputFileName = "Input.txt";
static const char* OutputFileName = "Output.txt";
static bool Is_number_of_threads_set = FALSE;
void Usage ()
{
fprintf (stderr, "Usage:\ttext_filter [Input-file [Output-file] [nthread]]]\n];
}
int parsecommandline (int argc, char* argv[]) {
Parse command Line
if (argc> 4) {
Usage ();
return 0;
}
if (argc>=2) inputfilename = argv[1];
if (argc>=3) outputfilename = argv[2];
if (argc>=4) {
Nthread = Strtol (argv[3],0,0);
if (nthread<1) {
fprintf (stderr, "nthread set to%d, but must is at least 1\n", nthread);
return 0;
}
Is_number_of_threads_set = true; Number of threads is set explicitly
}
return 1;
}
int run_pipeline (int nthreads)
{
file* input_file = fopen (InputFileName, "R");
if (!input_file) {
Perror (InputFileName);
Usage ();
return 0;
}
file* output_file = fopen (OutputFileName, "w");
if (!output_file) {
Perror (OutputFileName);
return 0;
}
Create the pipeline
TBB::p Ipeline pipeline;
Create file-reading Writing stage and add it to the pipeline
Myinputfilter Input_filter (input_file);
Pipeline.add_filter (Input_filter);
Create capitalization stage and add it to the pipeline
Mytransformfilter Transform_filter;
Pipeline.add_filter (Transform_filter);
Create file-writing Stage and add it to the pipeline
Myoutputfilter Output_filter (output_file);
Pipeline.add_filter (Output_filter);
Run the pipeline
Tbb::tick_count t0 = Tbb::tick_count::now ();
Pipeline.run (Myinputfilter::n_buffer);
Tbb::tick_count T1 = Tbb::tick_count::now ();
Remove filters from pipeline before they is implicitly destroyed.
Pipeline.clear ();
Fclose (output_file);
Fclose (Input_file);
if (Is_number_of_threads_set) {
printf ("threads =%d time =%g\n", Nthreads, (t1-t0). seconds ());
} else {
if (nthreads = = 1) {
printf ("Serial Run time =%g\n", (t1-t0). seconds ());
} else {
printf ("Parallel Run time =%g\n", (t1-t0). seconds ());
}
}
return 1;
}
int main (int argc, char* argv[]) {
if (! ParseCommandLine (argc, argv))
return 1;
if (Is_number_of_threads_set) {
Start Task Scheduler
Tbb::task_scheduler_init init (nthread);
if (!run_pipeline (Nthread))
return 1;
} else {//number of threads wasn ' t set explicitly. Run Serial and parallel version
{//serial Run
Tbb::task_scheduler_init init_serial (1);
if (!run_pipeline (1))
return 1;
}
{//Parallel run (number of threads is selected automatically)
Tbb::task_scheduler_init Init_parallel;
if (!run_pipeline (0))
return 1;
}
}
return 0;
}
The second program, corresponding to the debug mode, with the Tbb_debug.lib:
#include "Tbb/task_scheduler_init.h"
#include "tbb/blocked_range.h"
#include "Tbb/parallel_for.h"
Link Tbb_debug.lib
#pragma comment (lib, "Tbb_debug.lib")
using namespace TBB;
Perform this action on each item
void Foo (float value)
{
printf ("%.2f\n", value);
}
Class Applyfoo
{
float * Const MY_A;
Public
void operator () (const blocked_range<size_t> & R) const
{
float * a = my_a;
for (size_t i = R.begin (); I! = R.end (); + + i)
Foo (A[i]);
}
Applyfoo (float a[]): My_a (a) {}
};
int _tmain (int argc, _tchar* argv[])
{
Create a Task Scheduler
Task_scheduler_init supports a parameter to specify the number of threads to use
Task_scheduler_init Init;
float a[100];
for (int i = 0; i <; i + +)
A[i] = (float) i;
TBB will divide the array into blocks.
Call Applyfoo to block this functor
parallel_for (blocked_range<size_t> (0), Applyfoo (a));
return 0;
}
Use of Intel Line libraries TBB