代码段一
// Python object that backs torch.autograd.Variable
struct THPVariable {
PyObject_HEAD
torch::autograd::Variable cdata;
PyObject* backward_hooks;
};
PyObject_HEAD
这个宏用于标准化python对象,其拓展为一个结构体,包含有指向一个type对象(定义了初始化方法、内存分配等)的指针以及一个引用计数器。在python API中还有两个宏Py_INCREF()
和Py_DECREF()
,它们用来增加和减少python对象的引用数量
代码段二
typedef struct THTensor
{
int64_t *size;
int64_t *stride;
int nDimension;
THStorage *storage;
ptrdiff_t storageOffset;
int refcount;
char flag;
} THTensor;
可以看到,THTensor
包含了size
/strides
/dimensions
/offsets
以及THStorage
代码段三
static THStorage* THPStorage_(newFilenameStorage)(ptrdiff_t size)
{
int flags = TH_ALLOCATOR_MAPPED_SHAREDMEM | TH_ALLOCATOR_MAPPED_EXCLUSIVE;
std::string handle =