CUDA(Compute Unified Device Architecture)는 NVIDIA가 개발한 GPU 병렬 컴퓨팅 플랫폼이다. GPU의 수천 개 코어를 활용해 딥러닝 훈련, 과학 시뮬레이션, 그래픽스를 가속화한다.
CPU vs GPU
CPU:
- 코어 수: 4~64개
- 각 코어: 복잡한 순차 작업에 최적화
- 클럭: 3~5GHz
GPU (NVIDIA A100):
- 코어 수: 6912개 CUDA 코어
- 각 코어: 단순 병렬 연산에 최적화
- 행렬 연산: CPU 대비 100배+ 빠름
딥러닝 행렬 곱셈 → GPU의 병렬성 완벽하게 활용
python
import torch
# GPU 사용 가능 확인
print(torch.cuda.is_available()) # True
print(torch.cuda.get_device_name(0)) # NVIDIA A100
# 데이터를 GPU로 이동
device = torch.device('cuda')
X = torch.randn(1000, 1000).to(device)
y = torch.randn(1000, 1).to(device)
model = MyModel().to(device)
# GPU에서 연산
output = model(X) # 자동으로 GPU에서 실행
python
# 메모리 해제
torch.cuda.empty_cache()
# 메모리 사용량 확인
print(f"할당됨: {torch.cuda.memory_allocated()/1e9:.2f}GB")
# 혼합 정밀도 훈련 (FP16, 메모리 절반)
from torch.cuda.amp import autocast
with autocast():
output = model(X)
관련 개념
- •딥러닝 — CUDA의 주요 활용 분야
- •PyTorch — CUDA와 긴밀하게 통합
- •TensorFlow — CUDA 위에서 동작
참고문헌
- •NVIDIA CUDA 공식 문서: docs.nvidia.com/cuda