39 पॉइंट द्वारा GN⁺ 2025-04-21 | 1 टिप्पणियां | WhatsApp पर शेयर करें
  • हाई-परफॉर्मेंस C/C++ और assembly coding techniques को व्यावहारिक उदाहरणों के साथ सीखने के लिए एक open source प्रोजेक्ट
  • STL की जगह optimized libraries और विभिन्न hardware optimization techniques के उपयोग के उदाहरण शामिल
  • input generation cost, math function approximation, CPU branch prediction, multicore parallelization जैसी कई performance tricks की व्याख्या
  • CUDA, PTX, ASM, FPGA, JSON processing जैसी platform-specific optimization techniques से लेकर benchmark measurement methods तक का व्यापक कवरेज
  • Google Benchmark आधारित benchmark execution और statistical processing automation सुविधा प्रदान करता है

परफॉर्मेंस-केंद्रित C/C++ और assembly code लिखने का तरीका

  • यह प्रोजेक्ट हाई-परफॉर्मेंस software design के लिए जरूरी intuition और mindset विकसित करने में मदद करने वाला benchmark code collection है
  • आधुनिक code में आम bugs, security issues, performance bottlenecks से बचने के लिए व्यावहारिक coding examples शामिल हैं
  • university lectures या bootcamps में आसानी से न मिलने वाली real-world performance-oriented techniques को व्यवस्थित रूप से पेश करता है
  • अधिकांश code GCC, Clang आधारित Linux environment में चलता है, लेकिन Windows और macOS का भी आंशिक समर्थन है
  • हाई-परफॉर्मेंस code implementation के लिए parallel algorithms, coroutines, polymorphism आदि का भी परिचय देता है

मुख्य विषय

  • क्या random input 100 गुना सस्ता हो सकता है?! यह तथ्य कि input generation algorithm से भी धीमा हो सकता है
  • 1% error पर cost सिर्फ 1/40: std::sin जैसी STL trigonometric functions को केवल 3 lines of code में approximate करके देखें
  • क्या lazy logic 4 गुना तेज हो सकती है? custom std::ranges और iterators के साथ चरम laziness लागू करना
  • -O3 से आगे की compiler optimization: hidden flags और tricks से performance को 2 गुना तक और बढ़ाया जा सकता है
  • क्या matrix multiplication ही समस्या है? operations 60% कम होने पर भी 3x3x3 GEMM, 4x4x4 से 70% धीमा हो सकता है
  • AI scaling की सच्चाई? theoretical ALU throughput और वास्तविक BLAS performance के बीच के अंतर को मापना
  • कितने conditionals बहुत ज़्यादा माने जाएँगे? सिर्फ 10 lines of code में CPU branch predictor की सीमाओं का प्रयोग
  • क्या recursion बेहतर है? खुद मापें कि SEGFAULT कहाँ आता है और stack depth कितनी है
  • exceptions से बचना क्यों ज़रूरी है? std::error_code या std::variant जैसे alternatives आज़माएँ
  • multicore scaling कैसे करें? OpenMP, Intel oneTBB, या खुद बनाए गए thread pool का उपयोग कैसे करें
  • memory allocation के बिना JSON को कैसे process करें? क्या C++20 बेहतर है, या पुराना C99 toolset ज़्यादा सरल है?
  • STL associative containers को सही तरह इस्तेमाल करने के लिए custom keys और transparent comparators का उपयोग कैसे करें
  • अगर hand-written parser से तेज तरीका हो तो? consteval आधारित regular expression engine के साथ सीधी टक्कर
  • क्या pointer size सच में 64-bit है? pointer tagging का उपयोग करके देखें
  • UDP कितने packets drop करता है? user space में io_uring के साथ web requests तक प्रोसेस करके देखें
  • Scatter-Gather के साथ 50% तेज vectorized non-contiguous memory operations लागू करना
  • Intel oneAPI vs Nvidia CCCL? <thrust> और <cub> में क्या खास है?
  • CUDA C++, PTX, SASS CPU code से कैसे अलग हैं?
  • अगर code performance-sensitive है? intrinsics, inline asm, या .S files में से किसे चुनना चाहिए, इसकी तुलना
  • Tensor Core और memory architecture — CPU, Volta, Ampere, Hopper, Blackwell GPUs में क्या अंतर है?
  • FPGA coding, GPU से कैसे अलग है? high-level synthesis (HLS), Verilog, VHDL में क्या अंतर है? 🔜 #36
  • Encrypted Enclave क्या है? Intel SGX, AMD SEV, ARM Realm की latency तुलना 🔜 #31

चलाने का तरीका और environment setup

  • Linux + GCC environment recommended, WSL या Mac के Clang (non-default distribution) का भी उपयोग किया जा सकता है
  • CMake, liburing, OpenBLAS, g++, build-essential install करना आवश्यक
  • less_slow executable को build करके चलाने पर benchmarks अपने आप execute हो जाते हैं
git clone https://github.com/ashvardanian/less_slow.cpp.git  
cd less_slow.cpp  
pip install cmake --upgrade  
sudo apt install -y build-essential g++ liburing-dev libopenblas-base  
cmake -B build_release -D CMAKE_BUILD_TYPE=Release  
cmake --build build_release --config Release  
build_release/less_slow  
  • CUDA और Intel TBB का उपयोग करना है या नहीं, यह चुना जा सकता है (-D USE_INTEL_TBB=OFF जैसे flags का उपयोग)
  • रनटाइम पर केवल खास benchmarks चुनकर चलाना, JSON में save करना, या output format तय करना संभव है
build_release/less_slow --benchmark_filter=std_sort  
build_release/less_slow --benchmark_out=results.json --benchmark_format=json  

performance measurement बेहतर करने के tips

  • noise कम करने के लिए SMT disable करें और random interleaving का उपयोग करें
  • Google Benchmark के --benchmark_perf_counters option से hardware performance counters मापे जा सकते हैं
sudo build_release/less_slow --benchmark_perf_counters="CYCLES,INSTRUCTIONS"  
  • या Linux के perf tool से benchmark measurement किया जा सकता है
sudo perf stat taskset 0xEFFFEFFFEFFFEFFFEFFFEFFFEFFFEFFF build_release/less_slow --benchmark_filter=super_sort  

प्रोजेक्ट फ़ाइल संरचना

  • मुख्य source: less_slow.cpp (मुख्यतः CPU benchmark code)
  • platform-specific optimization files शामिल: x86/ARM के लिए ASM, CUDA .cu, PTX .ptx code शामिल
├── less_slow.cpp           # मुख्य benchmark code  
├── less_slow_amd64.S       # x86 assembly  
├── less_slow_aarch64.S     # ARM assembly  
├── less_slow.cu            # CUDA C++  
├── less_slow_sm70.ptx      # PTX IR (Volta)  
├── less_slow_sm90a.ptx     # PTX IR (Hopper)  

external libraries का उपयोग

  • Google Benchmark: performance measurement
  • Intel oneTBB: parallel STL backend
  • Meta libunifex: asynchronous execution model
  • range-v3: std::ranges replacement
  • fmt: std::format replacement
  • StringZilla: std::string replacement
  • CTRE: std::regex replacement
  • nlohmann/json, yyjson: JSON parsers
  • Abseil: high-performance containers
  • cppcoro: coroutine implementation
  • liburing: Linux kernel-bypass I/O
  • ASIO: asynchronous networking
  • Nvidia CCCL, CUTLASS: GPU algorithms और matrix operations

Google Benchmark उपयोग के tips का सार

  • BENCHMARK() से benchmark register करें, ->Args({x,y}) से parameters पास करें
  • DoNotOptimize(), ClobberMemory() से compiler optimization नियंत्रित करें
  • ->Iterations(n), ->MinTime(n) से iteration count और benchmark time नियंत्रित करें
  • ->Complexity(...), ->SetComplexityN(n) से time complexity निर्दिष्ट करें
  • state.PauseTiming(), ResumeTiming() से timing sections को सीधे नियंत्रित करें
  • state.counters[...] से custom counters register किए जा सकते हैं

memes और humor elements

  • शैक्षणिक सामग्री में technical meme images जोड़कर रुचि बढ़ाई गई है
  • performance बनाम abstraction, IEEE 754 floating point आदि को हास्यपूर्ण ढंग से प्रस्तुत किया गया है

1 टिप्पणियां

 
GN⁺ 2025-04-21
Hacker News राय
  • 40 गुना तेज़ trigonometry: std::sin जैसे standard library functions को 3 लाइन कोड से तेज़ किया जा सकता है

    • कुछ terms तक expansion सीमित करके sin(x) का approximation किया जा सकता है
    • computation cost घटती है, लेकिन accuracy भी कम हो जाती है
    • accuracy में कमी को कम करके बताया गया है. [-2, 2] रेंज के बाहर के inputs के लिए यह बेहद inaccurate है
    • यह एक single sine-wave interval भी handle नहीं कर सकता और periodic nature को भी नहीं संभालता. यह बेकार "optimization" है
  • microcontroller पर अनुभव साझा

    • embedded systems पर काम कर रहा हूँ, जहाँ heap लगभग 256 KiB है और सबसे बड़ा stack 4 KiB है
    • ज़्यादातर modern C++ इस्तेमाल करता हूँ, लेकिन हर trick हर situation के लिए उपयुक्त नहीं होती
    • CTRE तब तक ठीक है जब तक stack overflow से बचा जाए. HTTP proxy configuration की strings validate करने की कोशिश की, लेकिन stack overflow की वजह से system crash हो गया
    • JSON का अंदरूनी तौर पर लगभग उपयोग नहीं करते और अपनी BSON library लिखी है. memory allocation या fragmentation की चिंता नहीं करनी पड़ती
    • newlib की जगह picolibc इस्तेमाल किया और C/C++ standard library का locale code हटा दिया. इससे program size कम हुआ
  • Abseil के चयन पर राय

    • जब यह पहली बार आया था तब यह बड़ा मुद्दा था, लेकिन अब इसकी कमजोरियों को बेहतर करने वाले कई alternatives मौजूद हैं
    • पिछले कुछ वर्षों में Abseil को लेकर शिकायतें बढ़ी हैं. Google में core library maintainers का बाहर जाना हुआ है
  • performance के लिए C++ में होने वाली विकृतियों पर आलोचना

    • CTRE के अच्छे results देने पर आश्चर्य है. इसे और गहराई से देखने की ज़रूरत है
    • OpenMP और TBB threadpool benchmarks की जाँच करना चाहता हूँ और देखना चाहता हूँ कि क्या Boost::ASIO threadpool जोड़ा जा सकता है
  • FPGA और GPU coding के अंतर, साथ ही high-level synthesis, Verilog, VHDL पर अनुरोध

    • इस विषय पर प्राथमिकता से अनुरोध देखना चाहूँगा
  • denormalized floating-point numbers के बारे में नई जानकारी

    • GPU पर matrix multiply करते समय कभी-कभी इस बारे में सोचता हूँ
  • Google Benchmark पोस्ट पर सकारात्मक प्रतिक्रिया

    • performance benchmarking पर फोकस अच्छा है. repository अच्छी तरह से व्यवस्थित है
  • "कम धीमा C, C++, assembly code" से अपेक्षाएँ

    • लगा था कि C code भी शामिल होगा, लेकिन सिर्फ .cpp और .S हैं
    • less_slow.cpp में कई C++ features इस्तेमाल हुए हैं. सूची से "C" हटाना चाहिए या इसमें संशोधन होना चाहिए