我希望能够%rbp
在内联asm中使用基指针寄存器().这样的玩具示例是这样的:
void Foo(int &x)
{
asm volatile ("pushq %%rbp;" // 'prologue'
"movq %%rsp, %%rbp;" // 'prologue'
"subq $12, %%rsp;" // make room
"movl $5, -12(%%rbp);" // some asm instruction
"movq %%rbp, %%rsp;" // 'epilogue'
"popq %%rbp;" // 'epilogue'
: : : );
x = 5;
}
int main()
{
int x;
Foo(x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望,因为我使用通常的序幕/结尾函数调用方法来推送和弹出旧的%rbp
,这样就可以了.但是,当我尝试在内x
联asm之后访问时,它会出现故障.
GCC生成的汇编代码(略微剥离)是:
_Foo:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -8(%rbp)
# INLINEASM
pushq %rbp; // prologue
movq %rsp, …
Run Code Online (Sandbox Code Playgroud) 我已经按照https://www.tensorflow.org/versions/r0.11/tutorials/estimators/中的描述用Python编写了Abalone估算器.我希望保存估计器的状态,然后在C++中加载它并使用它来进行预测.
为了从Python中保存它,我使用构造函数中的model_dir
参数tf.contrib.learn.Estimator
,它创建一个(文本)protobuf文件和几个检查点文件.然后我使用该freeze_graph.py
工具(https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py)将检查点和protobuf文件合并到一个独立的GraphDef文件中.
我使用C++ API加载此文件,将一些输入值加载到Tensor中,然后运行会话.protobuf文件中的输入节点称为"输入",输出节点称为"输出",两者都是占位符节点.
// ...
std::vector<std::pair<string, tensorflow::Tensor>> inputs =
{
{"input", inputTensor}
};
std::vector<tensorflow::Tensor> outputs;
status = pSession->Run(inputs, {"output"}, {}, &outputs);
Run Code Online (Sandbox Code Playgroud)
但是,由于输出节点是占位符节点,因此需要为其提供值,因此会失败.但是你不能同时提供和获取节点值,因此我无法访问估算器的输出.为什么输出节点是占位符节点?
从Python中保存经过训练的估算器并加载它以便在C++中进行预测的最佳方法是什么?
我想通过std::tuple
使用std::tie
(或std::forward_as_tuple
)-see玩具代码来初始化从函数返回的多个引用的简洁方法.
#include <tuple>
#include <iostream>
class Foo
{
public:
Foo () : m_memberInt(5), m_anotherMemberInt(7) {}
void IncrementMembers() {++m_memberInt; ++m_anotherMemberInt;}
std::tuple<int &, int &> GetMembers() {return std::tie(m_memberInt, m_anotherMemberInt);}
private:
int m_memberInt;
int m_anotherMemberInt;
};
int main()
{
Foo foo;
// Can't have dangling references.
// int &x, &y;
// std::tie(x, y) = foo.GetMembers();
std::tuple<int &, int &> tmpTuple = foo.GetMembers();
int &x = std::get<0>(tmpTuple);
int &y = std::get<1>(tmpTuple);
std::cout << x << " " << …
Run Code Online (Sandbox Code Playgroud) 通常可以在模板类的上下文中看到完美转发.对于非模板类,是否值得将构造函数作为模板方法,以便它可以使用完美转发?像下面这样的东西:
class Foo()
{
public:
template<typename T>
Foo(T &&vec) : memberVec(std::forward<T>(vec)) {};
private:
std::vector memberVec;
};
Run Code Online (Sandbox Code Playgroud)
优点基本相同,但是当我们知道真正的类型时,它们有什么不同?什么时候这是好的做法,何时不是?