C++/C++ 기타
-
파이프라인과 멀티스레드C++/C++ 기타 2022. 10. 25. 22:25
오늘은 파이프라인의 개념에 대해서 간단히 알아보겠습니다. 컴퓨터구조에서 파이프라인 개념의 핵심은 코드의 실행순서가 임의로 바뀔 수 있다는 것입니다. CPU에서는 Fetch Decode Excute Write-Back 의 순서로 명령이 실행 됩니다. 그런데, 실행순서가 바뀌어도 서로 연관이 없는 명령들이라면 순서를 바꿈으로써 속도가 더 빨라질 수 있다면 CPU는 임의로 실행순서를 바꾸기도 합니다. 예를 들어서 int x=5; y=y+1; 과같은 코드는 두 순서를 바꾸어도 아무런 문제가 없습니다. 그래서 필요하다면 순서가 바뀌기도 하는거구요. 이게 싱글스레드 상황에서는 아무런 문제가 발생하지 않습니다. 그런데 멀티스레드 상황이라면 이야기가 달라집니다. int x = 0; int y = 0; int r1 = ..
-
캐시(Cache)(feat. Hit Rate, 메모리 계층구조, Locality)C++/C++ 기타 2022. 10. 24. 18:36
안녕하세요. 오늘은 컴퓨터의 메모리 중 캐시에 대해서 알아보겠습니다. 이 캐시는 아니구요. 시작해보겠습니다. 컴퓨터의 메모리계층구조 컴퓨터에는 다양한 저장장치가 있습니다. SSD, HDD, RAM(일반적으로 메모리라고 하면 RAM을 뜻합니다.), 캐시, 레지스터 등등 이렇게 저장장치들이 다양하게 나뉘게 된 이유는 이러한 저장장치들의 특징이 각기 다르기 때문입니다. 컴퓨터는 기본적으로 CPU가 연산을 수행합니다. CPU는 연산에 필요한 데이터를 저장장치로부터 가지고 와서 작업을 하는데요. 이때 필요한 데이터가 있는 저장장치가 CPU와 가까울수록 효율이 높아집니다. 가깝다는 말은, CPU가 작업에 필요한 데이터를 꺼내오는데에 걸리는 시간이 적다는 뜻입니다. CPU는 컴퓨터에서 가장 빠른 부품입니다. 그에 반..
-
C++, volatileC++/C++ 기타 2022. 10. 9. 14:25
c++에서 volatile의 의미는 컴파일러에게 최적화를 하지 말라는 의미이다. visual studio에서 실행모드를 release모드로 바꾸고 다음과 같은 코드를 실행해보자. 출력하는 곳에 중단점을 찍고 디버그를 해서, 디스어셈블리해보면, 이렇게 나온다. a에 값을 할당하는 일련의 코드들이 실행되지않는것이다. 이는, 컴파일러가 "어차피 출력하는 건 a에 3이 들어간것이고, 그 이전의 값은 사용되지도 않는데, 뭐하러 하나??" 라고 생각하여 최적화한 결과이다. 이 때 volatile 키워드를 사용하면, 다음과같이 변경된다. a의 값에 1, 2, 3을 차례로 할당해주고 있다. 그러니까 volatile은 아니 비효율적인거 아는데, 일부러 한거니까 최적화 하지말고 냅둬! 라는 것이다. 잘 사용하진 않지만 대..