CPU versus GPU workloads and memory transfer: Worked Example — Foundations of Accelerated Data Science (NVIDIA-Certified Associate: Accelerated Data Science)
Understanding CPU versus GPU Workloads and Memory Transfer: A Worked Example In accelerated data science, efficiently leveraging GPUs requires a...
Understanding CPU versus GPU Workloads and Memory Transfer: A Worked Example
In accelerated data science, efficiently leveraging GPUs requires a clear understanding of how workloads differ between CPUs and GPUs, as well as the implications of memory transfer between host (CPU) and device (GPU). This worked example demonstrates these concepts applied to a realistic data processing task.
Scenario
Suppose you have a large dataset of 10 million numerical records that need to be normalized (scaling values between 0 and 1) before training a machine learning model. You want to compare the performance and workflow differences between performing this normalization on a CPU versus a GPU, including the impact of memory transfer.
Step 1: Define the Workload
- Task: Normalize an array of 10 million floating-point numbers.
- CPU Approach: Use NumPy on the CPU to compute min, max, and scale values.
- GPU Approach: Use CuPy (a GPU-accelerated NumPy-compatible library) to perform the same operations on the GPU.
Step 2: Data Location and Memory Transfer
Initially, the dataset resides in CPU memory (RAM). For GPU processing, the data must be transferred from CPU memory to GPU memory (device memory). After processing, results may need to be transferred back to CPU memory if further CPU-based tasks or storage are required.
- CPU workload: No memory transfer needed; data remains in CPU RAM.
- GPU workload: Requires explicit memory transfer:
- Host to device (H2D): Transfer data array to GPU memory.
- Device to host (D2H): Transfer normalized results back to CPU memory if needed.
Step 3: Execution and Timing
Measure the time taken for each step to understand the impact of memory transfer and computation:
- CPU: Time to normalize data using NumPy.
- GPU: Time to transfer data H2D + time to normalize using CuPy + time to transfer results D2H.
Step 4: Code Outline
CPU (NumPy):
import numpy as np import time
data = np.random.rand(10_000_000).astype(np.float32) start = time.time() normalized = (data - data.min()) / (data.max() - data.min()) cpu_time = time.time() - start print(f"CPU normalization time: {cpu_time:.4f} seconds")
GPU (CuPy):
import cupy as cp import time
data = np.random.rand(10_000_000).astype(np.float32) start = time.time() data_gpu = cp.asarray(data) # H2D transfer normalized_gpu = (data_gpu - data_gpu.min()) / (data_gpu.max() - data_gpu.min()) result = cp.asnumpy(normalized_gpu) # D2H transfer gpu_time = time.time() - start print(f"GPU normalization (including memory transfer) time: {gpu_time:.4f} seconds")
Step 5: Analysis of Results
Key observations:
- Computation speed: GPU normalization is typically faster due to parallel processing capabilities.
- Memory transfer overhead: The H2D and D2H transfers add latency that can reduce or negate GPU speedup for smaller datasets.
- Overall performance: For very large datasets, GPU acceleration outweighs transfer costs; for smaller datasets, CPU processing may be more efficient.
Step 6: Summary of Concepts
- CPU workloads operate directly on data in main memory without transfer overhead.
- GPU workloads require explicit memory transfer between host and device, which introduces latency.
- Efficient GPU acceleration depends on minimizing transfer frequency and maximizing computation per transfer.
- End-to-end workflow must consider both computation and data movement to optimize performance.
Conclusion
This example highlights the critical balance between computation speed and memory transfer overhead in accelerated data science workflows. Understanding when and how to leverage GPU acceleration, while managing data movement efficiently, is foundational knowledge for the NVIDIA-Certified Associate: Accelerated Data Science certification.
More in this topic
Ready to test your knowledge?
Put what you've learned into practice with a quick quiz and track your progress.
Test your knowledge →