14. Multi-Agent Simulation hard
Implement a program for a multi-agent flocking simulation (boids). The input consists of:
agents containing N agents, where N is the total number of agents- represents the agent's position in 2D space
- represents the agent's velocity vector
4 * N floats, with agent 's data stored at indices [4i, 4i+1, 4i+2, 4i+3]Simulation Rules
- For each agent , identify all neighbors (where ) within radius using:
- Compute average velocity of neighboring agents: where is the set of neighbors for agent
- Update velocity:
- Update position:
Implementation Requirements
- Use only native features (external libraries are not permitted)
- The
solvefunction signature must remain unchanged - The final result must be stored in the
agents_nextarray
Example 1:
Input: N = 2 agents = [ 0.0, 0.0, 1.0, 0.0, // Agent 0: [x, y, vx, vy] 3.0, 4.0, 0.0, -1.0 // Agent 1: [x, y, vx, vy] ] Output: agents_next = [ 1.0, 0.0, 1.0, 0.0, // Agent 0: [x, y, vx, vy] 3.0, 3.0, 0.0, -1.0 // Agent 1: [x, y, vx, vy] ]
Constraints
- 1 ≤
N≤ 100,000 - Each agent's position and velocity components are 32-bit floats
- Performance is measured with
N= 10,000
Starter code (CUDA)
#include <cuda_runtime.h>
// agents, agents_next are device pointers
extern "C" void solve(const float* agents, float* agents_next, int N) {}
From LeetGPU, CC BY-NC-ND 4.0.