C.W.K.
Stream
Lesson 01 of 05 · published

Metal Shading Language이 뭔데

~12 min · metal, msl, apple-silicon, kernel, intro

Level 0Beginner
0 XP0/38 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

C++11에 attribute 괄호 추가 — 그리고 architectural twist 하나

Metal Shading Language (MSL)이 .metal 파일 안에 쓰는 거야. CUDA 개발자가 보면 즉시 친숙해 — function qualifier로 GPU entry 표시, attribute 괄호로 SIMT index 노출, address-space 키워드로 memory region 명시. 그래픽스 개발자가 보면 GLSL의 더 lean하고 typed한 사촌처럼 보여.

가장 중요한 확장 셋:

  • kernel function qualifier — CPU가 launch하는 GPU entry. CUDA의 __global__ 쌍둥이.
  • Attribute 괄호[[thread_position_in_grid]], [[threadgroup_position_in_grid]], [[thread_index_in_threadgroup]]. Metal의 threadIdx + blockIdx 답.
  • Address-space 키워드device, threadgroup, constant, thread — 포인터가 어느 메모리 region에 사는지 명시. CUDA는 __shared__/__constant__로 추론, Metal은 type의 일부로 만들어.

나머지는 표준 C++11 + Apple 확장이야: packed vector (float4, half3), threadgroup barrier, math built-in. 전체 MSL spec이 PDF 한 권 — 오후에 다 읽혀.

눈에 띄지 않게 숨어있는 architectural twist: Apple Silicon에선 device 메모리랑 CPU-visible 메모리가 같은 물리 RAM이야. 다음 레슨 통째로 이거 다룰 거.

Code

Metal kernel — CUDA랑 같은 모양, 다른 어휘·metal
#include <metal_stdlib>
using namespace metal;

// 'kernel' = CUDA의 __global__
// device   = GPU-visible 메모리 포인터
// constant = dispatch당 한 번 전달되는 read-only uniform
// [[thread_position_in_grid]] = threadIdx + blockIdx 합친 거
kernel void vector_add(
    const device float *A     [[buffer(0)]],
    const device float *B     [[buffer(1)]],
    device       float *C     [[buffer(2)]],
    constant     uint  &N     [[buffer(3)]],
    uint gid                  [[thread_position_in_grid]])
{
    if (gid < N) C[gid] = A[gid] + B[gid];
}
외워둘 만한 흔한 MSL built-in·metal
// thread별 정체성
uint gid     [[thread_position_in_grid]];        // global thread index
uint tid     [[thread_index_in_threadgroup]];    // threadgroup 안 local index
uint tg_idx  [[threadgroup_position_in_grid]];   // 어느 threadgroup인가
uint tg_size [[threads_per_threadgroup]];        // 내 threadgroup 크기

// SIMD-group (Apple의 warp, 32 thread)
uint simd_lane [[thread_index_in_simdgroup]];
uint simd_id   [[simdgroup_index_in_threadgroup]];

External links

Exercise

Metal Shading Language Specification PDF (위 링크) 첫 30 페이지 훑어. 외우지 말고 — 목차가 'C++11에 이런 추가 박힘' 식으로 읽힌다는 것만 인지해. C++ 알면 MSL이 가장 onboard 쉬운 GPU 언어 중 하나라는 게 이 때문.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.