579
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <cuda_runtime.h>
using namespace std;
// Derived class
class Rectangle
{
public:
Rectangle()
{
}
int getArea()
{
return (*width * *height);
}
int* width;
int* height;
};
// Base class
class Shape
{
public:
Shape()
{
}
Rectangle* rect;
};
__global__ void change_width(Shape* sha)
{
*(sha->rect->width) = 210;
*(sha->rect->height) = 10;
return;
}
int main(void)
{
Shape* sha;
cudaMallocManaged(&sha,sizeof(Shape));
cudaMallocManaged(&sha->rect, sizeof(Rectangle));
cudaMallocManaged(&sha->rect->width,sizeof(int));
cudaMallocManaged(&sha->rect->height,sizeof(int));
*(sha->rect->width) = 20;
*(sha->rect->height) = 10;
change_width<<<1,1,0>>>(sha);
cudaDeviceSynchronize();
// Print the area of the object.
cout << "Total area: " << sha->rect->getArea() << endl;
return 0;
}