Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocators and Sparse Graphs 🕸️ #19

Merged
merged 2 commits into from
Jan 20, 2025
Merged

Allocators and Sparse Graphs 🕸️ #19

merged 2 commits into from
Jan 20, 2025

Conversation

ashvardanian
Copy link
Owner

@ashvardanian ashvardanian commented Jan 10, 2025

This PR explores high-performance graph implementations optimized for different access patterns, focusing on real-world use cases like recommendation systems and social networks. To implement those, various STL and Abseil-based containers are used to implement sparse Graph structures.
It shows:

  • How to use STL's polymorphic allocators?
  • How do we construct a hybrid nested container that propagates stateful allocators to the inner structures?
  • Up to 300x performance difference between good implementations in different workload patterns!

Of other neat tricks, shows:

  • How can the three-way comparison operator be used with std::tie?
  • What's the difference between std::weak_ordering and the strong one ?
  • Where can the [[no_unique_address]] attribute be used?

Implementation

It extends the Graph API to:

  • upsert_edge(from, to, weight): Inserts or updates an existing edge between two vertices.
  • get_edge(from, to): Retrieves the std::optional weight of the edge between two vertices.
  • remove_edge(from, to): If present, remove the edge between two vertices.
  • for_edges(from, visitor): Applies a callback to all edges starting from a vertex.
  • size(): Returns the graph's number of vertices and edges.
  • reserve(capacity): Reserves memory for the given number of vertices.
  • compact(): Compacts the memory layout of the graph, preparing for read-intensive workloads.

Results

On Intel Sapphire Rapids CPUs in AWS c7i instances:

------------------------------------------------------------------------------------------
Benchmark                                                Time             CPU   Iterations
------------------------------------------------------------------------------------------
graph_make<std::unordered_maps>/min_time:10.000   57329425 ns     57327619 ns          245
graph_make<std::map>/min_time:10.000             109704078 ns    109697310 ns          100
graph_make<absl::flat_set>/min_time:10.000        80598043 ns     80595813 ns          174
graph_rank<std::unordered_maps>/min_time:10.000   35763632 ns     35762406 ns          392
graph_rank<std::map>/min_time:10.000              51658552 ns     51657290 ns          271
graph_rank<absl::flat_set>/min_time:10.000          236938 ns       236933 ns        59137

On AWS Graviton 4 CPUs in AWS r8g instances:

------------------------------------------------------------------------------------------
Benchmark                                                Time             CPU   Iterations
------------------------------------------------------------------------------------------
graph_make<std::unordered_maps>/min_time:10.000  163664945 ns    163660572 ns           86
graph_make<std::map>/min_time:10.000             382543113 ns    382534380 ns           45
graph_make<absl::flat_set>/min_time:10.000       213277341 ns    213272284 ns           64
graph_rank<std::unordered_maps>/min_time:10.000   59579530 ns     59578435 ns          240
graph_rank<std::map>/min_time:10.000              69177429 ns     69175965 ns          191
graph_rank<absl::flat_set>/min_time:10.000          186428 ns       186430 ns        74929

@ashvardanian ashvardanian changed the title Improved Graph Processing Allocators and Sparse Graphs 🕸️ Jan 12, 2025
@ashvardanian ashvardanian merged commit 819ffb1 into main Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant