Question: #include #include #include #include #include #include #include using namespace std; / / Custom GCD function for device code _ _ device _ _ int gcd
#include
#include
#include
#include
#include
#include
#include
using namespace std;
Custom GCD function for device code
device int gcdint a int b
while b
int temp b;
b a b;
a temp;
return a;
CUDA kernel to simulate the game rounds
global void simulateRoundsint grid, int tankHP int score, int M int N int T int H
shared int sgrid;
shared int stankHP;
int tankIdx blockIdx.x blockDim.x threadIdx.x;
if tankIdx T return;
sgridthreadIdxx gridtankIdx ;
sgridthreadIdxx gridtankIdx ;
stankHPthreadIdxx tankHPtankIdx;
syncthreads;
int x sgridthreadIdxx ;
int y sgridthreadIdxx ;
for int round ; stankHPthreadIdxx; round
int targetTank tankIdx round T;
if targetTank tankIdx continue; Skip null rounds
int tx sgridtargetTank blockIdx.x blockDim.x ;
int ty sgridtargetTank blockIdx.x blockDim.x ;
Calculate the slope of the line between the tanks
int dx tx x;
int dy ty y;
int divisor gcdabsdx absdy;
dx divisor;
dy divisor;
Check for intermediate tanks
int curX x dx;
int curY y dy;
bool hitRegistered false;
while curX tx curY ty
for int i ; i blockDim.x; i
if sgridi curX && sgridi curY && stankHPi
atomicSub&stankHPi;
atomicAdd&scoretankIdx;
hitRegistered true;
break;
if hitRegistered break;
curX dx;
curY dy;
If no intermediate tank was hit, check the target tank
if hitRegistered && stankHPtargetTank blockIdx.x blockDim.x
atomicSub&stankHPtargetTank blockIdx.x blockDim.x;
atomicAdd&scoretankIdx;
syncthreads;
tankHPtankIdx stankHPthreadIdxx;
Thrust reduction function to count alive tanks
struct countalive
hostdevice
int operatorconst int& a const int& b const
return a b : ;
;
int mainint argc, char argv
Variable declarations
int M N T Hxcoordycoordscore;
FILE inputfilepointer;
File Opening for read
char inputfilename argv;
inputfilepointer fopeninputfilenamer;
if inputfilepointer NULL
printfinputtxt file failed to open.";
return ;
fscanfinputfilepointerd &M;
fscanfinputfilepointerd &N;
fscanfinputfilepointerd &T; T is number of Tanks
fscanfinputfilepointerd &H; H is the starting Health point of each Tank
printfInput read successfully. Md Nd Td Hd
M N T H;
Allocate memory on CPU
xcoord intmallocT sizeofint; X coordinate of each tank
ycoord intmallocT sizeofint; Y coordinate of each tank
score intmallocT sizeofint; Score of each tank
Get the Input of Tank coordinates
for int i ; i T; i
fscanfinputfilepointerd &xcoordi;
fscanfinputfilepointerd &ycoordi;
printfTank coordinates read successfully.
;
auto start chrono::highresolutionclock::now;
Allocate memory on GPU using Thrust
thrust::devicevector dgridT ;
thrust::devicevector dtankHPT H;
thrust::devicevector dscoreT;
Copy data from CPU to GPU using Thrust
thrust::copyxcoord xcoord T dgrid.begin;
thrust::copyycoord ycoord T dgrid.begin T;
printfData copied from CPU to GPU.
;
Launch the kernel
int blockSize ;
int numBlocks T blockSize blockSize;
simulateRoundsthrust::rawpointercastdgrid.data
thrust::rawpointercastdtankHPdata
thrust::rawpointercastdscore.data M N T H;
printfKernel launched.
;
Check if the game has finished using Thrust reduction
int aliveTanks thrust::reducedtankHPbegin dtankHPend countalive;
int round ;
while aliveTanks
printfRound d: d t
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
