如何设计class对可反弹的大对象的依赖?
How to design class dependence on large objects which can be rebound?
我需要更改内部变量以引用新对象,然后做一些事情,然后再次更改对象,然后对新值做一些事情。
class dml
{
void setTrain(matrix<T> train_x , matrix<T> train_y)
{
train_x_ = train_x;
train_y_ = train_y;
}
void train();
private:
matrix<T> train_x_;
matrix<T> train_y_;
}
所以,我会先设置 train ,然后调用 train ,它使用我刚刚设置的值来训练。然后稍后我可能会更改值,通过对新值调用 setTrain 并且我想再次 运行 训练该数据。
(对于 ML 的人,我想实现小批量,我输入数据,训练它,然后输入下一批数据,然后训练它)
我现在拥有的简单解决方案很愚蠢,因为它涉及复制一个非常大的矩阵。我的指针是想到的简单解决方案,但我不想使用它们。我的程序很复杂,指针和内存分配只会更麻烦。
我想到的一个想法是:
private:
matrix<T>& train_x_;
matrix<T>& train_y_;
}
当然这是行不通的,因为引用永远不能指向空值,我必须在构造函数中初始化它们,因为它们不能重新绑定,所以没有必要使用它们。
我认为解决方案是使用一些智能指针。但是智能指针用于管理堆中的内存,但我不是在堆中分配内存。矩阵对象是在堆栈中创建的,但它是使用 std::vector 实现的,它初始化堆中的所有对象。
我认为逻辑上 shared_ptr 应该可行,因为我们共享一个变量。 std::make_shared
似乎是个好主意,但我读到,当您尝试从堆栈中的现有对象创建 shared_ptr 时,它也会创建一个副本。但这并没有解决必须复制这么大对象的问题。
另一个明显的事情是 std::move
,但是移动在这里不适用,因为我可能需要在函数调用之外使用训练数据 agian。
我确定我遗漏了一些明显的东西。我现在有点睡眠不足,所以任何解决方案都会对我有很大帮助!
澄清一下:
LargeObj a=9;
auto a_ptr=std::make_shared(a);
如果我这样做,那么 a_ptr 将只指向 a,而不创建副本?但这不是这里的答案之一所说的:
Create a boost::shared_ptr to an existing variable
int a = 3; // 现有变量
shared_ptr aCopy = make_shared(a); //创建值为a
的副本
LargeObj a=9;
auto a_ptr=std::make_shared(a);
If I do this, then a_ptr will just point to a, without creating a
copy?
如果您通过复制现有对象(允许)来创建共享指针,那么您将拥有两个对象。这可以通过删除 class and/or 中的相关函数来避免,方法是永远不要将对象定义为 shared_ptr<matrix<T>>
.
以外的任何对象。
如果使用 std::shared_ptr
,您可以处理大对象并防止复制。
以下是对问题中示例的改编。它将对象创建为 std::shared_ptr
s 并显示如何将它们分配给示例中的 class。
#include <memory>
#include <iostream>
#include <string>
template <typename T>
class matrix {
public:
matrix() = default;
matrix(const matrix&) = delete; // no copy
matrix(matrix&&) = default; // allow move
matrix& operator=(const matrix&) = delete; // no copy
matrix& operator=(matrix&&) = default; // allow move
};
template <typename T>
class dml
{
public:
void setTrain(std::shared_ptr<matrix<T>> train_x_ptr, std::shared_ptr<matrix<T>> train_y_ptr)
{
train_x_ = train_x_ptr;
train_y_ = train_y_ptr;
}
void train() {};
private:
std::shared_ptr<matrix<T>> train_x_{}; // starts out as an empty shared pointer (contains nullptr)
std::shared_ptr<matrix<T>> train_y_{}; // starts out as an empty shared pointer (contains nullptr)
};
int main()
{
// no explicit new, object is created on the heap (train_x_ptr is created on the stack,
// but will be copied to dml without copying the underlying object).
auto train_x_ptr{std::make_shared<matrix<int>>()};
auto train_y_ptr{std::make_shared<matrix<int>>()};
dml<int> d;
d.setTrain(train_x_ptr, train_y_ptr);
}
注意new
不是直接使用
I think the solution would be in using some of the smart pointers. But
smart pointers are used to manage the memory in heap, but I am not
allocating an memory in the heap. The matrix object is created in the
stack, but it is implemented using a std::vector, which initializes
all of its objects in the heap.
在此示例中,std::shared_ptr
将在堆上创建 matrix
并为您管理生命周期。同时,如果 matrix
包含一个 std::vector
那些元素也会在堆上的某个地方,由 std::vector
.
管理
I think logically shared_ptr should work, as we are sharing a
varaible. std::make_shared
seems to be a good idea, but I read that it
too creates a copy when you try create a shared_ptr from an existing
object in the stack. But this does not solve the problem of having to
copy such a large object.
不会创建副本。它确实解决了问题。
我需要更改内部变量以引用新对象,然后做一些事情,然后再次更改对象,然后对新值做一些事情。
class dml
{
void setTrain(matrix<T> train_x , matrix<T> train_y)
{
train_x_ = train_x;
train_y_ = train_y;
}
void train();
private:
matrix<T> train_x_;
matrix<T> train_y_;
}
所以,我会先设置 train ,然后调用 train ,它使用我刚刚设置的值来训练。然后稍后我可能会更改值,通过对新值调用 setTrain 并且我想再次 运行 训练该数据。 (对于 ML 的人,我想实现小批量,我输入数据,训练它,然后输入下一批数据,然后训练它) 我现在拥有的简单解决方案很愚蠢,因为它涉及复制一个非常大的矩阵。我的指针是想到的简单解决方案,但我不想使用它们。我的程序很复杂,指针和内存分配只会更麻烦。 我想到的一个想法是:
private:
matrix<T>& train_x_;
matrix<T>& train_y_;
}
当然这是行不通的,因为引用永远不能指向空值,我必须在构造函数中初始化它们,因为它们不能重新绑定,所以没有必要使用它们。
我认为解决方案是使用一些智能指针。但是智能指针用于管理堆中的内存,但我不是在堆中分配内存。矩阵对象是在堆栈中创建的,但它是使用 std::vector 实现的,它初始化堆中的所有对象。
我认为逻辑上 shared_ptr 应该可行,因为我们共享一个变量。 std::make_shared
似乎是个好主意,但我读到,当您尝试从堆栈中的现有对象创建 shared_ptr 时,它也会创建一个副本。但这并没有解决必须复制这么大对象的问题。
另一个明显的事情是 std::move
,但是移动在这里不适用,因为我可能需要在函数调用之外使用训练数据 agian。
我确定我遗漏了一些明显的东西。我现在有点睡眠不足,所以任何解决方案都会对我有很大帮助!
澄清一下:
LargeObj a=9;
auto a_ptr=std::make_shared(a);
如果我这样做,那么 a_ptr 将只指向 a,而不创建副本?但这不是这里的答案之一所说的:
Create a boost::shared_ptr to an existing variable
int a = 3; // 现有变量 shared_ptr aCopy = make_shared(a); //创建值为a
的副本LargeObj a=9; auto a_ptr=std::make_shared(a);
If I do this, then a_ptr will just point to a, without creating a copy?
如果您通过复制现有对象(允许)来创建共享指针,那么您将拥有两个对象。这可以通过删除 class and/or 中的相关函数来避免,方法是永远不要将对象定义为 shared_ptr<matrix<T>>
.
如果使用 std::shared_ptr
,您可以处理大对象并防止复制。
以下是对问题中示例的改编。它将对象创建为 std::shared_ptr
s 并显示如何将它们分配给示例中的 class。
#include <memory>
#include <iostream>
#include <string>
template <typename T>
class matrix {
public:
matrix() = default;
matrix(const matrix&) = delete; // no copy
matrix(matrix&&) = default; // allow move
matrix& operator=(const matrix&) = delete; // no copy
matrix& operator=(matrix&&) = default; // allow move
};
template <typename T>
class dml
{
public:
void setTrain(std::shared_ptr<matrix<T>> train_x_ptr, std::shared_ptr<matrix<T>> train_y_ptr)
{
train_x_ = train_x_ptr;
train_y_ = train_y_ptr;
}
void train() {};
private:
std::shared_ptr<matrix<T>> train_x_{}; // starts out as an empty shared pointer (contains nullptr)
std::shared_ptr<matrix<T>> train_y_{}; // starts out as an empty shared pointer (contains nullptr)
};
int main()
{
// no explicit new, object is created on the heap (train_x_ptr is created on the stack,
// but will be copied to dml without copying the underlying object).
auto train_x_ptr{std::make_shared<matrix<int>>()};
auto train_y_ptr{std::make_shared<matrix<int>>()};
dml<int> d;
d.setTrain(train_x_ptr, train_y_ptr);
}
注意new
不是直接使用
I think the solution would be in using some of the smart pointers. But smart pointers are used to manage the memory in heap, but I am not allocating an memory in the heap. The matrix object is created in the stack, but it is implemented using a std::vector, which initializes all of its objects in the heap.
在此示例中,std::shared_ptr
将在堆上创建 matrix
并为您管理生命周期。同时,如果 matrix
包含一个 std::vector
那些元素也会在堆上的某个地方,由 std::vector
.
I think logically shared_ptr should work, as we are sharing a varaible.
std::make_shared
seems to be a good idea, but I read that it too creates a copy when you try create a shared_ptr from an existing object in the stack. But this does not solve the problem of having to copy such a large object.
不会创建副本。它确实解决了问题。