.
// Example 3: The Dispatcher Pattern (Like CK Convolution!)
// This is exactly how the convolution dispatcher works!
#include <iostream>
#include <concepts>
// ==========================================
// Step 1: Define algorithm types
// ==========================================
enum class AlgorithmType {
SIMPLE,
FAST,
PARALLEL
};
// Simple algorithm
struct SimpleAlgorithm {
static constexpr AlgorithmType type = AlgorithmType::SIMPLE;
};
// Fast algorithm
struct FastAlgorithm {
static constexpr AlgorithmType type = AlgorithmType::FAST;
int optimization_level = 3;
};
// Parallel algorithm
struct ParallelAlgorithm {
static constexpr AlgorithmType type = AlgorithmType::PARALLEL;
int num_threads = 8;
};
// ==========================================
// Step 2: Algorithm detection functions (like IsReferenceAlgorithm!)
// ==========================================
// Check if algorithm is Simple
template <typename T>
consteval bool IsSimpleAlgorithm() {
return std::is_same_v<T, SimpleAlgorithm>;
}
// Check if algorithm is Fast
template <typename T>
consteval bool IsFastAlgorithm() {
return std::is_same_v<T, FastAlgorithm>;
}
// Check if algorithm is Parallel
template <typename T>
consteval bool IsParallelAlgorithm() {
return std::is_same_v<T, ParallelAlgorithm>;
}
// ==========================================
// Step 3: Factories (only check direction, not algorithm!)
// ==========================================
enum class Direction {
FORWARD,
BACKWARD
};
// Factory for Simple Algorithm
template <Direction DIR, typename Algorithm>
requires (DIR == Direction::FORWARD) // ← Only check direction!
struct SimpleFactory {
static void process() {
std::cout << "SimpleFactory: Processing with basic algorithm\n";
}
};
// Factory for Fast Algorithm
template <Direction DIR, typename Algorithm>
requires (DIR == Direction::FORWARD) // ← Only check direction!
struct FastFactory {
static void process() {
std::cout << "FastFactory: Processing with optimized algorithm\n";
}
};
// Factory for Parallel Algorithm
template <Direction DIR, typename Algorithm>
requires (DIR == Direction::FORWARD) // ← Only check direction!
struct ParallelFactory {
static void process() {
std::cout << "ParallelFactory: Processing with parallel algorithm\n";
}
};
// ==========================================
// Step 4: DISPATCHER - Routes to correct factory
// ==========================================
template <Direction DIR, typename Algorithm>
void dispatch() {
std::cout << "\n=== DISPATCHER START (MareArts.com)===\n";
// Check direction first
if constexpr (DIR == Direction::FORWARD) {
std::cout << "Direction: FORWARD\n";
// Check algorithm type (like the conv dispatcher!)
if constexpr (IsSimpleAlgorithm<Algorithm>()) { // ← Check algorithm here!
std::cout << "Algorithm: SIMPLE\n";
SimpleFactory<DIR, Algorithm>::process();
}
else if constexpr (IsFastAlgorithm<Algorithm>()) { // ← Check algorithm here!
std::cout << "Algorithm: FAST\n";
FastFactory<DIR, Algorithm>::process();
}
else if constexpr (IsParallelAlgorithm<Algorithm>()) { // ← Check algorithm here!
std::cout << "Algorithm: PARALLEL\n";
ParallelFactory<DIR, Algorithm>::process();
}
else {
// Force compile error for unknown algorithms
[]<bool flag = false>() { static_assert(flag, "Unknown algorithm!"); }();
}
}
else {
std::cout << "Direction: BACKWARD\n";
std::cout << "Not implemented yet!\n";
}
std::cout << "=== DISPATCHER END ===\n\n";
}
// ==========================================
// Main - Try different algorithms!
// ==========================================
int main() {
std::cout << "=== Dispatcher Pattern Example ===\n";
std::cout << "This is exactly how CK convolution dispatcher works!\n";
// Try Simple algorithm
dispatch<Direction::FORWARD, SimpleAlgorithm>();
// Try Fast algorithm
dispatch<Direction::FORWARD, FastAlgorithm>();
// Try Parallel algorithm
dispatch<Direction::FORWARD, ParallelAlgorithm>();
return 0;
}
/*
DISPATCHER PATTERN SUMMARY:
┌─────────────────────────────────────────┐
│ USER CODE │
│ dispatch<FORWARD, SimpleAlgorithm>() │
└─────────────────────────────────────────┘
↓
┌─────────────────────────────────────────┐
│ DISPATCHER │
│ ✅ Check direction (if constexpr) │
│ ✅ Check algorithm (if constexpr) │
│ - IsSimpleAlgorithm() │
│ - IsFastAlgorithm() │
│ - IsParallelAlgorithm() │
│ ✅ Route to correct factory │
└─────────────────────────────────────────┘
↓
┌─────────────────────────────────────────┐
│ FACTORY │
│ requires (DIR == FORWARD) │
│ ❌ NO algorithm check! │
│ ✅ Just build the thing │
└─────────────────────────────────────────┘
KEY POINTS:
1. Dispatcher uses consteval functions with "if constexpr"
2. Factories use concepts with "requires"
3. Algorithm checking happens in dispatcher, not factory
4. This is EXACTLY the CK convolution pattern!
*/
..
### 3. Dispatcher Pattern (03_dispatcher_pattern.cpp) ⭐ MOST IMPORTANT
**What it teaches:** Exactly how CK convolution works!
```cpp
// Detection functions (like IsReferenceAlgorithm!)
template <typename T>
consteval bool IsSimpleAlgorithm() {
return std::is_same_v<T, SimpleAlgorithm>;
}
// Dispatcher - checks algorithm and routes
template <Direction DIR, typename Algorithm>
void dispatch() {
if constexpr (DIR == Direction::FORWARD) {
// Check algorithm type here!
if constexpr (IsSimpleAlgorithm<Algorithm>()) {
SimpleFactory<DIR, Algorithm>::process();
}
else if constexpr (IsFastAlgorithm<Algorithm>()) {
FastFactory<DIR, Algorithm>::process();
}
}
}
// Factory - only checks direction!
template <Direction DIR, typename Algorithm>
requires (DIR == Direction::FORWARD) // Only check direction
struct SimpleFactory {
// NO algorithm check here!
};
```
**Output:**
```
=== DISPATCHER START ===
Direction: FORWARD
Algorithm: SIMPLE
SimpleFactory: Processing with basic algorithm
=== DISPATCHER END ===
```
**Key Point:** This is EXACTLY the CK pattern!
Study.marearts.com!!!
No comments:
Post a Comment