Skip to content

evanwmart/gpu-rng

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gpu-rng

gpu-rng is a Rust project that leverages GPU compute shaders via wgpu to generate random numbers efficiently. It uses a Linear Congruential Generator (LCG) implemented in a compute shader written in WGSL.

This project was made purely as a learning endeavor to try and get more familiar with wgpu, WGSL shaders, random number generation processes, and rust.

Features

  • GPU-Accelerated Random Number Generation: Generates random numbers in parallel using a compute shader.
  • Linear Congruential Generator (LCG): Implements a simple yet effective algorithm for generating pseudorandom numbers.
  • Scalable Workload: Configurable number of random numbers to generate, divided into GPU workgroups.
  • Cross-Platform: Built on wgpu, ensuring support across Vulkan, Metal, DirectX, and WebGPU.

Prerequisites

Before building and running this project, ensure the following are installed:

  • Rust: Install via rustup.
  • ... tbh, i think that is all you need?

Project Structure

.
├── Cargo.lock
├── Cargo.toml
├── README.md
└── src
    ├── main.rs    # Main Rust program
    └── rng.wgsl   # Compute shader
  • main.rs: Contains the Rust code to set up the GPU, load the shader, and retrieve random numbers.
  • rng.wgsl: Defines the compute shader for random number generation.

Installation

   git clone https://github.com/evanwmart/gpu-rng.git
   cd gpu-rng
   cargo build
   cargo run

How It Works

  1. Compute Shader: The shader (rng.wgsl) is executed on the GPU. It uses the global invocation ID to seed a Linear Congruential Generator (LCG), generating one random number per thread.

  2. Buffers:

    • An output buffer stores the random numbers generated by the shader.
    • A staging buffer is used to transfer data back from the GPU to the CPU.
  3. Workgroups: The shader divides work into groups of 64 threads. The number of threads is determined by the NUM_RANDOMS constant in main.rs.

  4. Data Transfer:

    • The GPU writes random numbers to the output buffer.
    • The output is copied to a staging buffer and mapped back to the CPU for reading.
  5. Rust and wgpu: The Rust code initializes wgpu, compiles the shader, and handles buffer mapping for GPU-CPU communication.

Configuration

You can configure the number of random numbers to generate by modifying NUM_RANDOMS in main.rs:

const NUM_RANDOMS: u32 = 1024; // Adjust as needed

Example Output

When you run the program, it generates and prints a list of random numbers:

$ cargo run
Random Numbers: [2891336453, 3639132858, 91961967, 839758372, ...]

Dependencies

This project uses the following crates:

  • wgpu: Safe and portable GPU abstraction.
  • bytemuck: Utilities for zero-cost casting of slices and arrays.
  • pollster: Blocking executor for async functions.

Learning Opportunities

gpu-rng is a great project for exploring:

  • GPU compute shaders.
  • Parallel programming concepts.
  • Rust's ecosystem for GPU programming.

Contributing

Contributions are welcome! Feel free to:

  • Report issues.
  • Suggest improvements.
  • Submit pull requests.

License

This project is licensed under the MIT License.

Acknowledgments

Shoutout wgpu and Rust for making cool stuff.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published