我正在努力学习一些OOP实践,同时为我的研究开发模型。我似乎遇到了一些问题,我不太确定这些问题属于我正在使用的书的范围,Stephen Prata的C ++ Primer。
现在,我正在尝试开发一个名为StochasticModel的对象。此SM对象应该能够接受另一个名为LengthDistribution的对象并对其运行模拟。我目前正在使用以下伪代码为这种类型的交互设置框架。作为一个警告,我将类定义组合在一起,因为我最初将它们分别放在两个文件中。你也会在那里看到一些CudaMallocManaged(),因为我打算在GPU上使用这些对象,但CUDA不是这里的问题。
int main()
{
//Read initial conditions, maxlength, maxiterations from input
//Create a length distribution object from IC's and length
LengthDistribution* LD = new LengthDistribution(initialconditions, maxlength)
//Create a model object to operate on LD maxiterations number of times.
StochasticModel* SM = new StochasticModel(LD, maxiterations)
//Test that I've created an LD object as I expected by printing its values out.
LD -> h_printToScreen(); //<- Works fine!
//Test that model has a correct version of LD by printing LD's information to screen through
//SM's print to screen function.
SM->printToScreen(); //<- But when I have an object that contains the object call a function that calls its function (inception), memory access violations occur.
}
我的长度分布课程。
class LengthDistribution : public Managed
{
private:
int m_maxlength;
int* m_lengthDistribution;
public:
//User defined constructor.
LengthDistribution(int* initialconditions, int maxlength)
{
m_maxlength = maxlength;
m_lengthDistribution = new int[maxlength];
m_lengthDistribution = initialconditions;
}
//Default constructor
//Default destructor
//Unified memory copy constructor allows pass-by-value
LengthDistribution::LengthDistribution(const LengthDistribution &LD)
{
//Copy maxlength to new state.
m_maxlength = LD.m_maxlength;
//Allocate CUDA Managed memory for lengthDistribution array
cudaMallocManaged(&m_lengthDistribution, m_maxlength);
//Copy array to new state.
memcpy(m_lengthDistribution, LD.m_lengthDistribution, m_maxlength);
}
__host__ void h_printToScreen()
{
printf("Host maxlength: ");
std::cout<<m_maxlength;
std::cout<<"\n";
printf("Host length distribution: ");
for (int i = 0; i < m_maxlength; i++)
std::cout<<m_lengthDistribution[i];
printf("\n");
}
}
我的随机模型类
class StochasticModel : public Managed
{
private :
int m_numberOfIterations;
LengthDistribution* state;
public:
//User defined constructor
StochasticModel(LengthDistribution* LD, int numberOfIterations)
{
//Copy desired number of iterations.
m_numberOfIterations = numberOfIterations;
//Assign LD to SM's state variable. I think I'm having an issue here.
//Copy LD into SM object's state variable.
LengthDistribution* state = new LengthDistribution(*LD);
}
//User defined copy constructor
Stochastic::Model(const StochasticModel &SM)
{
m_numberOfIterations = SM.m_numberOfIterations;
cudaMallocManaged(&state, sizeof(SM.state));
state = new LengthDistribution(*SM.state);
//memcpy(state, SM.state, sizeof(state));
}
//Print out member length distribution's values.
printToScreen()
{
state->h_printToScreen(); //When I trace the debugger through here, it triggers an access violation when trying to print out the state's array.
}
}
这是另外两个类继承的Managed类。其目的是允许将来移植到显卡。
class Managed {
public:
void *operator new(size_t len) {
void *ptr;
cudaMallocManaged(&ptr, len);
return ptr;
}
void operator delete(void *ptr) {
cudaFree(ptr);
}
};
最终结果是应用程序在运行后“已停止工作”。它编译得很好,但当它遇到状态&lt; -printToScreen调用的内存访问错误时,它“停止工作”。通过调试器,它说它没有定义它试图从LD对象和整数打印的数组。我觉得我在这里缺少一些基本的东西,但我希望能够使用SM对象来操纵和显示来自LD对象的信息。
答案 0 :(得分:2)
在构造函数中,您创建了一个隐藏成员变量state
的新局部变量。这会使成员变量未定义,以后在您尝试访问它时导致崩溃。
您应该更喜欢使用初始化列表:
StochasticModel(LengthDistribution* LD, int numberOfIterations) :
m_numberOfIterations(numberOfIterations),
state(new LengthDistribution(*LD))
{
}