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 gcd(int a, int b){
while (b !=0){
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// CUDA kernel to simulate the game rounds
__global__ void simulateRounds(int* grid, int* tankHP, int* score, int M, int N, int T, int H){
__shared__ int s_grid[1024];
__shared__ int s_tankHP[1024];
int tankIdx = blockIdx.x * blockDim.x + threadIdx.x;
if (tankIdx >= T) return;
s_grid[threadIdx.x *2]= grid[tankIdx *2];
s_grid[threadIdx.x *2+1]= grid[tankIdx *2+1];
s_tankHP[threadIdx.x]= tankHP[tankIdx];
__syncthreads();
int x = s_grid[threadIdx.x *2];
int y = s_grid[threadIdx.x *2+1];
for (int round =1; s_tankHP[threadIdx.x]>0; round++){
int targetTank =(tankIdx + round)% T;
if (targetTank == tankIdx) continue; // Skip null rounds
int tx = s_grid[targetTank *2- blockIdx.x * blockDim.x *2];
int ty = s_grid[targetTank *2- blockIdx.x * blockDim.x *2+1];
// Calculate the slope of the line between the tanks
int dx = tx - x;
int dy = ty - y;
int divisor = gcd(abs(dx), abs(dy));
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 =0; i < blockDim.x; i++){
if (s_grid[i *2]== curX && s_grid[i *2+1]== curY && s_tankHP[i]>0){
atomicSub(&s_tankHP[i],1);
atomicAdd(&score[tankIdx],1);
hitRegistered = true;
break;
}
}
if (hitRegistered) break;
curX += dx;
curY += dy;
}
// If no intermediate tank was hit, check the target tank
if (!hitRegistered && s_tankHP[targetTank - blockIdx.x * blockDim.x]>0){
atomicSub(&s_tankHP[targetTank - blockIdx.x * blockDim.x],1);
atomicAdd(&score[tankIdx],1);
}
__syncthreads();
}
tankHP[tankIdx]= s_tankHP[threadIdx.x];
}
// Thrust reduction function to count alive tanks
struct count_alive {
__host____device__
int operator()(const int& a, const int& b) const {
return a +(b >0?1 : 0);
}
};
int main(int argc, char** argv){
// Variable declarations
int M, N, T, H,*xcoord,*ycoord,*score;
FILE* inputfilepointer;
// File Opening for read
char* inputfilename = argv[1];
inputfilepointer = fopen(inputfilename,"r");
if (inputfilepointer == NULL){
printf("input.txt file failed to open.");
return 0;
}
fscanf(inputfilepointer,"%d", &M);
fscanf(inputfilepointer,"%d", &N);
fscanf(inputfilepointer,"%d", &T); // T is number of Tanks
fscanf(inputfilepointer,"%d", &H); // H is the starting Health point of each Tank
printf("Input read successfully. M=%d, N=%d, T=%d, H=%d
", M, N, T, H);
// Allocate memory on CPU
xcoord =(int*)malloc(T * sizeof(int)); // X coordinate of each tank
ycoord =(int*)malloc(T * sizeof(int)); // Y coordinate of each tank
score =(int*)malloc(T * sizeof(int)); // Score of each tank
// Get the Input of Tank coordinates
for (int i =0; i < T; i++){
fscanf(inputfilepointer,"%d", &xcoord[i]);
fscanf(inputfilepointer,"%d", &ycoord[i]);
}
printf("Tank coordinates read successfully.
");
auto start = chrono::high_resolution_clock::now();
// Allocate memory on GPU using Thrust
thrust::device_vector d_grid(T *2);
thrust::device_vector d_tankHP(T, H);
thrust::device_vector d_score(T,0);
// Copy data from CPU to GPU using Thrust
thrust::copy(xcoord, xcoord + T, d_grid.begin());
thrust::copy(ycoord, ycoord + T, d_grid.begin()+ T);
printf("Data copied from CPU to GPU.
");
// Launch the kernel
int blockSize =256;
int numBlocks =(T + blockSize -1)/ blockSize;
simulateRounds<<>>(thrust::raw_pointer_cast(d_grid.data()),
thrust::raw_pointer_cast(d_tankHP.data()),
thrust::raw_pointer_cast(d_score.data()), M, N, T, H);
printf("Kernel launched.
");
// Check if the game has finished using Thrust reduction
int aliveTanks = thrust::reduce(d_tankHP.begin(), d_tankHP.end(),0, count_alive());
int round =1;
while (aliveTanks >1){
printf("Round %d: %d t

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!