MENU
Systems & Tools

Cord Path

Repository
Distance matrix< 5ms (5K coords)
2-opt gain4-10% reduction
GPU targetRTX 30-series
BridgeRust FFI + static lib

Cord Path solves the Traveling Salesman Problem by offloading the O(N^2) distance matrix computation to the GPU via a custom CUDA kernel, then running heuristic optimization in Rust. The Rust side calls into the CUDA static library through FFI with zero-cost abstractions. For 5,000 coordinate sets, the distance matrix completes in under 5ms on an RTX 30-series GPU. A 2-opt local search pass then reduces path distance by 4-10% over the greedy nearest-neighbor baseline.

#Rust#CUDA C++

CUDA-Accelerated Distance Matrix

The primary bottleneck in TSP solvers is the O(N^2) calculation of point-to-point distances. I implemented a custom CUDA kernel that computes the entire distance matrix in parallel on the GPU, with each thread calculating one Euclidean distance. The Rust side interacts with the GPU via a static library and FFI, keeping the language boundary at zero overhead.

Pathfinding Heuristics

Once the distance matrix is populated, the Rust engine generates an initial path using the Nearest Neighbor algorithm: start at a user-defined coordinate, then iteratively visit the closest unvisited node. This greedy baseline provides a valid starting route that the refinement pass can improve.

2-opt Local Optimization

To remove edge crossings from the initial path, I implemented the 2-opt algorithm. The algorithm iteratively swaps pairs of edges when the swap produces a shorter total path length. This pass reduces path distance by 4-10% over the nearest-neighbor baseline, using Rust's mutable slice reversal for in-place segment swaps.

Performance Results

For a benchmark with 5,000 coordinates, the CUDA distance matrix completed in under 5ms on an NVIDIA RTX 30-series GPU. CPU-only computation of the same matrix takes orders of magnitude longer. The combined pipeline (GPU preprocessing, Rust nearest-neighbor, 2-opt refinement) makes the tool practical for coordinate sets that would be prohibitively slow on a single-threaded CPU solver.