Parallelism and Concurrency 2

Posted 2008. 3. 28. 03:10
얼마 전에 Parallelism and Concurrency라는 글을 통해 동시성(concurrency)와 병렬(parallelism)의 정확한 정의에 대해서 의문을 제기했었는데 오늘 Simon Peyton Jones가 최근에 정리해서 내놓은 Tackling the Awkward Squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell을 읽다가 보다 명확한 정의를 발견했습니다.

28 페이지 Concurrency에 대한 항목을 보시면 두 용어의 정의가 나옵니다.

A parallel functional program uses multiple processors to gain performance. For example, it may be faster to evaluate e1 + e2 by evaluating e1 and e2 in parallel, and then add the results. Parallelism has no semantic impact at all: the meaning of a program is unchanged whether it is executed sequentially or in parallel. Furthermore, the results are deterministic; there is no possibility that a parallel program will give one result in one run and a different result in a different run.

In contrast, a concurrent program has concurrency as part of its specification. The program must run concurrent threads, each of which can independently perform input/output. The program may be run on many processors, or on one — that is an implementation choice. The behaviour of the program is, necessarily and by design, non-deterministic. Hence, unlike parallelism, concurrency has a substantial semantic impact.


병렬 프로그래밍은 성능 향상을 목적으로 명시적으로 멀티 프로세서를 사용함을 의미합니다. 반대로 동시성 프로그래밍은 동시성 자체가 명세에 포함되는 개념입니다. 프로그램이 여러 개의 동시적인 쓰레드로 실행이 되고 따라서 결과가 비결정적이다라는 사실까지 말입니다. 병렬 프로그래밍과 마찬가지로 멀티 프로세서를 사용할 수도 있지만 이는 필수적인 것이 아니라 구현 세부사항일 뿐입니다.

여기까지는 지난 번에 보여드렸던 설명과 동일합니다. 위 정의에는 여기에 덧붙여 더 중요한 정의가 나오는데, 바로 시멘틱에 관한 부분입니다. 병렬 프로그래밍은 프로그램의 시멘틱에 전혀 영향을 미치지 않습니다. 병렬 프로그래밍에서는 e1 + e2를 계산할 때 e1과 e2를 각각의 프로세서에서 실행을 하던 하나의 프로세서에서 실행을 하던 겉으로 보이는 결과는 반드시 같아야 합니다.

하지만 동시성 프로그래밍에서는 멀티쓰레드 사용 자체가 시멘틱에 포함되어 있기 때문에 쓰레드가 어떻게 수행되느냐에 따라서 결과가 달리나올 수 있음을 내포하고 있습니다. 즉, 시멘틱이 달라질 수 있다는 사실이 깔려있습니다.

앞으로는 이런 사실을 염두에 두고 동시성 프로그래밍과 병렬 프로그래밍 용어를 명확히 구분해서 사용하려고 합니다.

비고: Tackling the Awkaward Squad는 굉장히 읽을 만한 글입니다. 헤스켈에서 가장 어려운 부분이지만, 쓸만한 프로그램을 만들기 위해서는 반드시 알아야 하는 입출력(IO), 동시성(concurrency), 예외 처리(exception), 외부 함수 호출(foreign-language call)을 다루고 있습니다. 특히, IO monad의 operational semantics를 설명하는 부분에 감동했습니다 +_+