我是Boost.Threads的新手,我正在尝试理解如何将函数参数传递给boost::thread_groups::create_thread()函数.在阅读了一些教程和boost文档之后,我明白可以简单地将参数传递给这个函数,但是我无法使这个方法起作用.
我读到的另一个方法是使用函子将参数绑定到我的函数但是会创建参数的副本,我严格要求传递const引用,因为参数将是大矩阵(我计划通过使用boost::cref(Matrix)一次我得到这个简单的例子来工作).
现在,让我们来看看代码:
void printPower(float b, float e)
{
cout<<b<<"\t"<<e<<"\t"<<pow(b,e)<<endl;
boost::this_thread::yield();
return;
}
void thr_main()
{
boost::progress_timer timer;
boost::thread_group threads;
for (float e=0.; e<20.; e++)
{
float b=2.;
threads.create_thread(&printPower,b,e);
}
threads.join_all();
cout << "Threads Done" << endl;
}
Run Code Online (Sandbox Code Playgroud)
这不会编译与以下错误:
mt.cc: In function âvoid thr_main()â:
mt.cc:46: error: no matching function for call to âboost::thread_group::create_thread(void (*)(float, float), float&, float&)â
/usr/local/boost_1_44_0/include/boost/thread/detail/thread.hpp: In member function âvoid boost::detail::thread_data<F>::run() [with F = void (*)(float, float)]â:
mt.cc:55: instantiated from here
/usr/local/boost_1_44_0/include/boost/thread/detail/thread.hpp:61: …Run Code Online (Sandbox Code Playgroud) 我正在为我公司的投资组合模拟器设计一个python插件,当它逐步完成每天的模拟时,它会接收投资组合中所有工具的每日返回数据.
我的模块应该每天在仪器的横截面上更新其协方差矩阵.为了做到这一点,到目前为止我的方法是:
此实现具有以下低效率:
在我的公司,我们正在研究一个相当大的横截面(N~ = 1E6 - 1E7).我需要我的实现可扩展,内存效率和快速.
您能否建议对当前方案进行任何改进,以使这项任务受益?
我编写了一个数据容器类,它基本上包含一个numpy ndarray成员以及生成time_series掩码/横截面掩码的方法,在ring-buffer模式下获取日期索引(row#),处理调整大小时记住数据可能是一个环形缓冲区,并对形状/尺寸等实施限制.
由于我的类实现,现在我将通过显式引用*.data成员来访问此对象包装的数据.这很麻烦,我想在我的类中实现[]运算符,以便在我的类的实例上调用时,它引用底层ndarray对象上的相同操作.我怎样才能做到这一点?
def MyArray(object):
def __init__(self, shape, fill_value, dtype):
self.shape = shape
self.fill_value = fill_value
self.dtype = dtype
self.data = numpy.empty(shape, fill_value=fill_value, dtype=dtype)
def reset(self, fill_value=None):
self.data.fill(fill_value or self.fill_value)
def resize(self, shape):
if self.data.ndim != len(shape): raise Exception("dim error")
if self.data.shape < shape: raise Exception("sizing down not permitted")
# do resizing
Run Code Online (Sandbox Code Playgroud)
现在,如果我想在其他地方使用这个容器,我必须这样使用它:
arr = MyArray(shape=(10000,20), fill_value=numpy.nan, dtype='float')
arr.data[::10] = numpy.NAN
msk = numpy.random.randn(10000,20)<.5
arr.data[~msk] = -1.
Run Code Online (Sandbox Code Playgroud)
我每次使用它时都需要明确引用arr.data的事实太麻烦且容易出错(我忘记了很多地方的.data后缀).
有没有什么方法可以添加一些运算符,使得切片和索引arr实际上是arr.data 隐式操作的?