Implementation:CARLA simulator Carla MultiGPU PrimaryCommands
| Knowledge Sources | |
|---|---|
| Domains | MultiGPU, CommandProtocol |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
PrimaryCommands provides a high-level command interface for the primary server to communicate with secondary GPU servers, handling frame data broadcasting, map loading, token management, health checks, and ROS integration.
Description
The PrimaryCommands class acts as the command layer between the primary CARLA server and its connected secondary GPU servers. It uses a Router instance to dispatch commands and manage responses.
Key command methods:
- SendFrameData(Buffer): Broadcasts frame data to all connected secondary servers using MultiGPUCommand::SEND_FRAME.
- SendLoadMap(std::string): Broadcasts a map name to all secondary servers using MultiGPUCommand::LOAD_MAP, converting the string to a Buffer with null terminator.
- SendGetToken(stream_id): Sends a token request to the next secondary server (round-robin via Router::WriteToNext) using MultiGPUCommand::GET_TOKEN. Blocks on the future to receive the token and reconstructs a token_type from the response data.
- SendIsAlive(): Sends a health check to the next secondary server using MultiGPUCommand::YOU_ALIVE and blocks for a response.
- SendEnableForROS / SendDisableForROS / SendIsEnabledForROS: Send ROS enable/disable/query commands to the specific secondary server that owns a given sensor (looked up in _servers map), using Router::WriteToOne.
Token and server tracking:
- GetToken(stream_id): Checks _tokens map for a cached token. If not found, selects the next secondary server, calls SendGetToken, and caches both the token (in _tokens) and the server reference (in _servers) for future lookups.
- EnableForROS / DisableForROS / IsEnabledForROS: Public wrappers that first ensure the sensor has been activated (via GetToken if necessary) before delegating to the Send methods.
Usage
PrimaryCommands is owned by the Router and accessed via Router::GetCommander(). The CARLA server uses it to synchronize simulation state across GPU servers.
Code Reference
Source Location
- Repository: CARLA
- Files:
LibCarla/source/carla/multigpu/primaryCommands.hLibCarla/source/carla/multigpu/primaryCommands.cpp
Signature
class PrimaryCommands {
public:
PrimaryCommands();
PrimaryCommands(std::shared_ptr<Router> router);
void set_router(std::shared_ptr<Router> router);
void SendFrameData(carla::Buffer buffer);
void SendLoadMap(std::string map);
void SendIsAlive();
token_type GetToken(stream_id sensor_id);
void EnableForROS(stream_id sensor_id);
void DisableForROS(stream_id sensor_id);
bool IsEnabledForROS(stream_id sensor_id);
private:
token_type SendGetToken(stream_id sensor_id);
void SendEnableForROS(stream_id sensor_id);
void SendDisableForROS(stream_id sensor_id);
bool SendIsEnabledForROS(stream_id sensor_id);
std::shared_ptr<Router> _router;
std::unordered_map<stream_id, token_type> _tokens;
std::unordered_map<stream_id, std::weak_ptr<Primary>> _servers;
};
Import
#include "carla/multigpu/primaryCommands.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| router | std::shared_ptr<Router> | Yes | Router for dispatching commands to secondary servers |
| buffer | carla::Buffer | Yes (SendFrameData) | Frame data buffer to broadcast |
| map | std::string | Yes (SendLoadMap) | Map name to load on all secondary servers |
| sensor_id | stream_id | Yes (token/ROS ops) | Sensor stream identifier |
Outputs
| Name | Type | Description |
|---|---|---|
| GetToken() | token_type | Streaming token for the requested sensor on a secondary server |
| IsEnabledForROS() | bool | Whether the sensor's ROS forwarding is active on its secondary server |
Usage Examples
// Access commands through the router
auto &commander = router->GetCommander();
// Broadcast frame data to all secondary servers
commander.SendFrameData(std::move(frameBuffer));
// Load a new map on all secondary servers
commander.SendLoadMap("Town03");
// Get a streaming token for a sensor (activates on a secondary if needed)
auto token = commander.GetToken(sensorStreamId);
// Enable ROS2 forwarding for a sensor
commander.EnableForROS(sensorStreamId);
// Check if ROS is enabled
bool rosEnabled = commander.IsEnabledForROS(sensorStreamId);