viva_tensor/optim/pool

TensorPool - Distributed Tensor Computing via OTP

INNOVATIVE CONCEPT: GenServer + GPU

In C/C++: you have threads + mutex + condition vars (NIGHTMARE) In Gleam/OTP: GenServer manages state, NIFs do GPU compute

Architecture:

[Request] -> [GenServer Pool] -> [Worker 1] -> [NIF GPU] -> cuBLAS/cuDNN -> [Worker 2] -> [NIF GPU] -> cuBLAS/cuDNN -> [Worker N] -> [NIF GPU] -> cuBLAS/cuDNN

Each Worker is a lightweight BEAM process (~2KB) that can call GPU NIFs. The GenServer does automatic load balancing. If a Worker crashes, OTP restarts it (free fault tolerance!)

Types

Similarity search result

pub type SearchResult {
  SearchResult(index: Int, similarity: Float)
}

Constructors

  • SearchResult(index: Int, similarity: Float)

Supported tensor operations

pub type TensorOp {
  Scale(Float)
  Normalize
}

Constructors

  • Scale(Float)
  • Normalize

Values

pub fn benchmark_pool() -> Nil
pub fn main() -> Nil
pub fn parallel_map(
  tensors: List(tensor.Tensor),
  op: TensorOp,
) -> List(tensor.Tensor)

Processes a list of tensors in parallel

Each tensor is processed in a separate BEAM process. In C/C++ this would require pthread_create + mutex for each tensor. In Gleam: one line of code and zero data races!

pub fn parallel_sum(tensors: List(tensor.Tensor)) -> Float

Parallel sum of all tensors

pub fn similarity_search(
  query: tensor.Tensor,
  documents: List(tensor.Tensor),
  chunk_size: Int,
) -> List(SearchResult)

Batch similarity search - THE KILLER FEATURE!

Compares a query against thousands of documents in parallel. Returns results sorted by similarity (desc).

pub fn top_k_similar(
  query: tensor.Tensor,
  documents: List(tensor.Tensor),
  k: Int,
) -> List(SearchResult)

Top-K similarity - returns the K most similar

Search Document