How to porting a rather complicated c++ code (Geant4) into GPU ??

ronaldo1018 2015-11-03 01:41:32
i, I'm pretty new to CUDA programming and I'm having a problem trying to port a part of Geant4 code into GPU.

Geant4 is a particle simulation tool based on c++ program.

The main reasons why we think it difficult is as following:

1. Geant4 simulation uses c++ instead of c programming.

2. Geant4's program structure is a multi-level class ( In other words, it uses class calls class method to
complete the work }

3. In order to let kernel function uses the class function we need to add Tag __device__ __host__ before each function. Could anyone gives some samrt ideas to add this Tag in front of every class's member function ?

4. Geant4's each class uses many class pointer variable. Hence, we need to use CUDA Unified Memory
Mechanism to handle it ( this is a rather difficult part)

And we have use a simple c++ program to test for it




#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;
}





In this code, we can port a 2-level class structure into GPU. And it works.
But you know , Geant4 is a rather big project. I am afraid that our simple idea could not fulfill the job.

Could anyone with good experience in porting c++ code to GPU give some good advice to me??
I really don't have idea how to handle for such a big program.
Thanks for all your help.

Sincerely,
KEVIN
...全文
167 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

579

社区成员

发帖
与我相关
我的任务
社区描述
CUDA™是一种由NVIDIA推出的通用并行计算架构,该架构使GPU能够解决复杂的计算问题。 它包含了CUDA指令集架构(ISA)以及GPU内部的并行计算引擎。
社区管理员
  • CUDA编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧