Use of Intel thread library TBB

Source: Internet
Author: User

First download:

Http://www.threadingbuildingblocks.org/uploads/77/111/2.1/tbb21_20080605oss_win.zip

Current Version 2.1

Decompress the package to drive C, open vs2005, and set the project directory of VC ++.

Include:

C:/tbb21oss_win/include

Execution file:

C:/tbb21oss_win/ia32/vc8/bin

Library file:

C:/tbb21oss_win/ia32/vc8/lib

Finally, set my computer-environment variable settings

Add ";": C:/tbb21oss_win/ia32/vc8/bin

Then add a variable:

Tbb21_install_dir is C:/tbb21oss_win

Start the command line tool of vs2005, go to the C:/tbb21oss_win/ia32/vc8/bin path, and run the tbbvars. BAT file.

 

You can start the first program. Remember to bring TBB. lib to the release compiling mode.

/*
Copyright 2005-2008 Intel Corporation. All rights reserved.

This file is 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 in the hope that it will be
Useful, but without any warranty; without even the Implied Warranty
Of merchantability or fitness for a special purpose. See
GNU General Public License for more details.

You shoshould have your ed a copy of the GNU General Public License
Along with threading building blocks; if not, write to the Free Software
Foundation, inc., 51 Franklin St, th Floor, Boston, MA 02110-1301 USA

As a special exception, you may use this file as part of a free software
Library without restriction. Specifically, if other files instantiate
Templates or use macros or inline functions from this file, or you compile
This file and link it with other files to produce an executable, this
File does not by itself cause the resulting executable to be covered
The GNU General Public License. This exception does not however
Invalidate any other reasons why the executable file might be covered
The GNU General Public License.
*/

//
// Example program that 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_previus_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_previus_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_previus_buffer;
Last_char_of_previus_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 each 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 be 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: Pipeline 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 are 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 corresponds to the debug mode, with 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 operation 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 (){}
};

Int _ tmain (INT argc, _ tchar * argv [])
{
// Create a Task Scheduler
// Task_scheduler_init supports a parameter to specify the number of threads used
Task_scheduler_init Init;
Float a [100];
For (INT I = 0; I <100; I ++)
A [I] = (float) I;
// TBB splits the array into several blocks.
// Call the functor applyfoo for the block
Parallel_for (blocked_range <size_t> (0,100), applyfoo ());
Return 0;
}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/ztz0223/archive/2008/09/01/2862662.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.