我想使用TBB(线程构建模块)编译R包。我在互联网上使用了一个简单的例子来测试是否可以用TBB开发R包。
我创建了Makevars文件,其中包含gcc:
的这些标志PKG_CPPFLAGS=-I/opt/tbb41_20121003oss/include/ -O3 -g3 -Wall -c -fmessage-length=0
PKG_LIBS=-L/opt/tbb41_20121003oss/build/linux_intel64_gcc_cc4.4.6_libc2.12_kernel2.6.32_release/
源代码是:
#include <iostream>
#include <string>
#include <algorithm>
#include "tbb/parallel_for.h"
#include "tbb/blocked_range.h"
using namespace tbb;
using namespace std;
static const size_t N = 23;
class SubStringFinder {
const string str;
size_t *max_array;
size_t *pos_array;
public:
void operator() ( const blocked_range<size_t>& r ) const {
for ( size_t i = r.begin(); i != r.end(); ++i ) {
size_t max_size = 0, max_pos = 0;
for (size_t j = 0; j < str.size(); ++j)
if (j != i) {
size_t limit = str.size()-max(i,j);
for (size_t k = 0; k < limit; ++k) {
if (str[i + k] != str[j + k]) break;
if (k > max_size) {
max_size = k;
max_pos = j;
}
}
}
max_array[i] = max_size;
pos_array[i] = max_pos;
}
}
SubStringFinder(string &s, size_t *m, size_t *p) :
str(s), max_array(m), pos_array(p) { }
};
extern "C" {
int test() {
string str[N] = { string("a"), string("b") };
for (size_t i = 2; i < N; ++i) str[i] = str[i-1]+str[i-2];
string &to_scan = str[N-1];
size_t num_elem = to_scan.size();
size_t *max = new size_t[num_elem];
size_t *pos = new size_t[num_elem];
parallel_for(blocked_range<size_t>(0, num_elem ),
SubStringFinder( to_scan, max, pos ) );
//
for (size_t i = 0; i < num_elem; ++i)
cout << " " << max[i] << "(" << pos[i] << ")" << endl;
delete[] pos;
delete[] max;
}
}
编译代码时出现此错误:
library/2.14/test/libs/test.so: undefined symbol: _ZTIN3tbb4taskE
如果删除此行,我可以编译程序:
parallel_for(blocked_range<size_t>(0, num_elem ),
SubStringFinder( to_scan, max, pos ) );
这使得TBB库无用!
另外我添加了
export /opt/tbb41_20121003oss/build/linux_intel64_gcc_cc4.4.6_libc2.12_kernel2.6.32_release/
到.bashrc
我使用centos。