Compiling TBB programs and examples on Linux Ubuntu

from http://goparallel.sourceforge.net/compiling-tbb-programs-and-examples-on-linux-ubuntu/

This week I’m going to talk about something I haven’t covered before: Linux. Specially, running Threading Building Blocks on Ubuntu. We’ll look at using TBB with the GNU c++ compiler; soon we’ll also cover topics such as the Cilk Plus extensions for the GNU C++ compiler, as well as the actual Intel Parallel Studio on Linux.

I’m using version 12.04 of Ubuntu here. The steps for installing everything are pretty simple. I’ll assume you already have the GNU C++ compiler. (If you don’t, it’s easy to find instructions on that, elsewhere.)

But you need to install the TBB. The great thing is it’s available through the apt-get command-line package manager. You can also get it through the Software Center, by searching for “tbb” and installing the one called libtbb-dev. In either case, the version I’m seeing right now in the package manager is 4.0, release 223.1. The version on the Threading Building Blocks website is 4.1. But it’s not a big difference.   

To use the command line, just type:

  
  
1
sudo apt-get install libtbb-dev

(I’m assuming you’re logged in as a non-root user who has sudo access.)

Amazingly, this set up everything and I didn’t need to do any additional configuration—no setting environment variables or anything. To try it out, I created a quick C++ file like this, which tests out the parallel_for:

  
  
01
#include <iostream>
02
#include <tbb/tbb.h>
03
#include <tbb/parallel_for.h>
04
 
05
 
06
using namespace std;
07
using namespace tbb;
08
 
09
long len = 5000;
10
float *set1 = new float[len];
11
float *set2 = new float[len];
12
float *set3 = new float[len];
13
 
14
class GrainTest {
15
public:
16
    void operator()( const blocked_range<size_t>& r ) const { 
17
        //std::cout << r.begin() << std::endl;
18
        for (long i=r.begin(); i!=r.end(); ++i ) {
19
            set3[i] = (set1[i] * set2[i]) / 2.0 + set2[i];
20
        }
21
    }
22
};
23
 
24
 
25
int main() {
26
    parallel_for(blocked_range<size_t>(0,len, 100), GrainTest() );
27
    return 0;
28
}

You can use your favorite text editor; I’m using scite, which you can install through apt-get with the name scite.

Also, I’m running my Linux in a virtual machine; ideally you wouldn’t want to do this, especially if the virtual machine is limited on what cores it can see. You want to be able to use all the cores! So I lowered the length of my arrays to be really small, primarily to see if it would compile and run.

To compile it, you can execute the g++ command from the command-line like so:

  
  
1
g++ test1.cpp -ltbb

where test1.cpp is what I called the C++ file. I’m not specifying a name for the final executable, so by default it’s a.out. You can change it to something better with the –o option.

There’s also a nice set of examples available. These are the standard TBB examples, and are again available through apt.get. Here’s how you install them:

  
  
1
sudo apt-get install tbb-examples

When you do this, the examples will be put in a strange place, /usr/share/doc/tbb-examples. I recommend moving it to your own home directory (using sudo mv) and then changing both the owner and group to yourself to make it easy to try them. To do that, make sure you’re in the directory containing tbb-examples, and run:

  
  
1
sudo chown -R jeff tbb-examples
2
sudo chgrp -R jeff tbb-examples

but replacing “jeff” with your own username. 

After that, take a look at the README.Debian file in the root example directory, and follow the instructions for building it, including the line showing how to unzip all the .gz files. All the source and Makefiles start out compressed for some reason. Note, though, that running make doesn’t just compile each example; it actually runs it afterwards. You can go poking through the makefiles if you want to change this behavior. (Hint: the “all” target runs both release and test targets.)

Take a close look at these examples. They’re very good, and they all compile and run beautifully on Linux, and they should give you what you need to get started.

Posted on    by  Jeff Cogswell, Geeknet Contributing Editor

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值