`

intel线程库tbb的使用

阅读更多
[size=small]首先下载:

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

当前是2.1版本

解压到c盘,打开vs2005,设置vc++的项目目录

include:

C:\tbb21oss_win\include

执行文件:

C:\tbb21oss_win\ia32\vc8\bin

库文件:

C:\tbb21oss_win\ia32\vc8\lib

最后设置 我的电脑--环境变量设置

添加下面到path部分的最前面,记得加上一个“;”:C:\tbb21oss_win\ia32\vc8\bin

然后添加一个变量:

TBB21_INSTALL_DIR 为C:\tbb21oss_win

开启vs2005的命令行工具,进入到C:\tbb21oss_win\ia32\vc8\bin路径,执行tbbvars.bat文件



可以开始第一个程序了,记得带上tbb.lib对应的是release编译模式

/*
    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 PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received 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, Fifth 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 by
    the GNU General Public License.  This exception does not however
    invalidate any other reasons why the executable file might be covered by
    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_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 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;
}


第二个程序,对应debug模式,带上tbb_debug.lib:

#include "tbb/task_scheduler_init.h"
#include "tbb/blocked_range.h"
#include "tbb/parallel_for.h"

// 链接tbb_debug.lib
//#pragma comment(lib, "tbb_debug.lib")

using namespace tbb;

// 对每个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[])
{
    // 创建task scheduler
    // task_scheduler_init支持一个参数,以指定使用的线程数
    task_scheduler_init init;
    float a[100];
    for (int i = 0; i < 100; i ++)
        a[i] = (float)i;
    // TBB会把数组分成若干的block
    // 对block调用ApplyFoo这个functor
    parallel_for(blocked_range<size_t>(0, 100), ApplyFoo(a));
    return 0;
}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ztz0223/archive/2008/09/01/2862662.aspx
[/size]
分享到:
评论

相关推荐

    TBB多线程库

    **TBB多线程库详解** Intel的Threading Building Blocks(TBB)是一个高效、灵活的C++库,专门设计用于解决多核处理器环境下的并行编程问题。TBB库提供了一组高级接口,帮助开发者编写出可扩展、高性能的多线程应用...

    inteltbb.rar

    在给定的“inteltbb.rar”压缩包中,包含了一系列资源,用于在Windows 7环境下,使用Visual Studio 2013编译和配置TBB库,以便与OpenCV等其他库共用。 **并行计算** 是现代计算机科学中的关键概念,特别是在多核...

    intel-tbb-20190525 经典并行库最新源代码

    **Intel TBB(Threading Building Blocks)** 是一个强大的并行编程库,由Intel公司开发,采用标准C++编写,旨在提升多核处理器上的数据并行计算能力。TBB库的核心理念是提供一种抽象的方式,使得程序员可以更容易地...

    tbb库Thread Building Blocks

    TBB(Thread Building Blocks)是由Intel开发的一个开源库,全称为“线程构建模块”。它旨在提供一种高效、灵活的方式来管理和利用多核处理器的并行计算能力,使开发者能够更容易地编写出高性能、可扩展的多线程应用...

    tbb库 源代码 20150424

    TBB库,全称为Threading Building Blocks,是由Intel公司开发的一款高效、易用的多线程编程库。它为C++程序员提供了一组丰富的模板类和函数,使得在多核处理器上实现并行计算变得简单而高效。20150424版本的TBB库是...

    tbb-Intel Threading Building Blocks 线程构建模块

    **Intel Threading Building Blocks (TBB) 线程构建模块** Intel Threading Building Blocks (TBB) 是Intel公司推出的一款强大的开源库,专门用于帮助C++开发者进行高效的并行编程。TBB的设计目标是简化多核处理器...

    tbb CPU 并行计算库,aarch64 6.5.0交叉编译移植

    TBB,全称为Threading Building Blocks,是由Intel开发的一个开源库,旨在简化多核处理器和多线程编程。它提供了一套C++模板类,用于高效地管理和执行并行任务,帮助开发者在CPU的多个核心上实现数据并行和任务并行...

    TBB使用手册

    **Intel® Threading Building Blocks (TBB)** 是一款由Intel开发并开源的C++模板库,它为多线程编程提供了一套高效且易用的解决方案。该库旨在帮助开发者轻松地编写可扩展的应用程序,以充分利用现代多核处理器的...

    Intel TBB Library

    TBB,Thread Building Blocks,线程构建模块,是Intel公司开发的并行编程开发的工具。 OSCON 上,Intel 宣布,Threading Building Blocks,Intel 众多软件开发工具中的一个,open source了。协议是 GPLv2。 TBB 获得过...

    并行计算 TBB库 多线程

    英特尔的Threading Building Blocks(TBB)库是实现并行计算的一种高效工具,尤其在多线程编程方面表现出色。TBB库提供了高级别的抽象,使得开发者能够更方便地利用多核处理器的性能,而无需深入理解底层的线程管理...

    intel的tbb库,版本是2018 update5

    TBB,Thread Building Blocks,线程构建模块,是Intel公司开发的并行编程开发的工具。 OSCON 上,Intel 宣布,Threading Building Blocks,Intel 众多软件开发工具中的一个,open source了。协议是 GPLv2。 TBB 获得过...

    TBB并发容器 学习笔记

    TBB(Thread Building Blocks)是Intel开发的一个开源C++模板库,专为并行编程设计,旨在帮助开发者充分利用多核处理器的性能。TBB的主要目标是简化并行编程,提供高效、灵活的工具,使程序员可以更容易地创建可扩展...

    TBB30_intel64+ia32_sdk.7z

    Threading Building Blocks是由Intel开发的一个并行编程库,它提供了一种C++模板库,用于帮助开发者编写多线程应用程序。TBB的主要目标是简化并行编程,让开发者能够利用多核处理器的计算能力,提高程序性能,而无需...

    TBB tutorial.pdf

    《Intel Threading Building Blocks Tutorial》是一份详细的指南,旨在教授如何使用Intel的Threading Building Blocks (TBB)库来开发高效、多线程的应用程序。TBB是一个开源库,它为C++程序员提供了高级别的并行编程...

    tbb-2020.1-win.zip

    Threading Building Blocks(TBB)是Intel开发的一个C++库,它提供了一组模板类和函数,帮助开发者轻松地编写高性能的并行代码,无需深入理解底层的线程管理细节。TBB的核心概念包括任务调度器、并行算法和数据并行...

    tbb-tbb_2020.zip

    标题 "tbb-tbb_2020.zip" 指示的是 Intel 的 Threading Building Blocks (TBB) 库的一个版本,这是针对 C++ 开发的并行编程框架,特别适用于优化多线程应用程序。在 OpenCV 中,TBB 被广泛使用来提升图像处理和...

    tbb40_20120408oss_win

    TBB是一个C++库,它提供了并行算法、数据结构和线程管理工具,旨在帮助开发者充分利用多核处理器的性能。 描述中提到的“tbb_debug.dll”是TBB库的一个调试版本动态链接库。在开发过程中,调试版本的库通常包含额外...

    intel tbb src

    **Intel TBB (Threading Building Blocks)** 是一个开源的并行编程库,由Intel公司开发,主要用于提升多核处理器上应用程序的性能。TBB的主要目标是简化并行编程,让开发者能够更容易地利用多核处理器的计算能力。在...

    TBB是cmake编译opencv所需的工具。

    标题中的"TBB"指的是英特尔Threading Building Blocks(线程构建块),它是一个开源库,用于C++编程,旨在简化并行编程,特别是多线程应用的开发。在OpenCV这样的大型图像处理库中,TBB提供了高效的任务调度和数据...

    tbb2018年最新版64bit

    **正文** Intel TBB(Thread Building Blocks)是Intel公司推出的一款高效的并行编程库,专为多核处理器设计,旨在简化并行编程的...使用TBB,开发者可以更专注于解决业务问题,而不是关注底层的线程管理和同步细节。

Global site tag (gtag.js) - Google Analytics