• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

멀티쓰레딩 예제 실행시간 재질문

20.08.21 22:48 작성 조회수 103

0

말씀하신 코드는 다음과 같습니다

#include<iostream>

#include<chrono>

#include<mutex>

#include<random>

#include<thread>

#include<utility>

#include<vector>

#include<atomic>

#include<future>

#include<numeric> //std::inner_product

#include<execution> // parallel_execution

using namespace std;

mutex mtx;

void dotProducNaive(const vector<int>& v0, const vector<int>& v1,

const unsigned i_start, const unsigned i_end, unsigned long long& sum)

{

for (unsigned i = i_start; i < i_end ; i++)

{

sum += (v0[i] * v1[i]);

//내적 구현 한 것

}

}

void dotProductLock(const vector<int>& v0, const vector<int>& v1,

const unsigned i_start, const unsigned i_end, unsigned long long& sum)

{

//cout<<"Thread start"<<endl;

for (unsigned i = i_start; i < i_end; i++)

{

std::scoped_lock lock(mtx); //c++17

sum += v0[i] * v1[i];

}

}

void dotProducAtomic(const vector<int>& v0, const vector<int>& v1,

const unsigned i_start, const unsigned i_end, atomic<unsigned long long>& sum)

{

for (unsigned i = i_start; i < i_end ; i++)

{

sum += (v0[i] * v1[i]);

//내적 구현 한 것

}

}

int main()

{

const long long n_data = 100'000'000;

const unsigned n_threads = 8;

//initialize vectors

std::vector<int> v0, v1;

v0.reserve(n_data);

v1.reserve(n_data);

random_device seed;

mt19937 engine(seed());

uniform_int_distribution <> uniformDist(1, 10);

for (long long i = 0; i < n_data; i++)

{

v0.push_back(uniformDist(engine));

v1.push_back(uniformDist(engine));

}

cout << "std::inner_product" << endl;

{

const auto sta = chrono::steady_clock::now(); //시간 재는중

const auto sum = std::inner_product(v0.begin(), v0.end(), v1.begin(), 0ull);

const chrono::duration<double> dur = chrono::steady_clock::now() - sta; //시간 재는 중

//계산한 시간 출력

cout << dur.count() << endl;

cout << sum << endl;

cout << endl;

}

cout << "Native" << endl;

{

const auto sta = chrono::steady_clock::now();

unsigned long long sum = 0;

vector<thread> threads;

threads.resize(n_threads);

//쓰레드 마다 맡길 일의 양을 결정

const unsigned n_per_thread = n_data / n_threads; //assumes remainder=0

//쓰레드 할당 중 + 쓰레드에 값을 넣어주는 중

for (unsigned t = 0; t < n_threads; t++)

{

threads[t] = std::thread(dotProducNaive,std::ref(v0),std::ref(v1),

t* n_per_thread, (t + 1)* n_per_thread, std::ref(sum));

}

//join으로 기다리는 코드

for (unsigned t = 0; t < n_threads; t++)

{

threads[t].join();

}

const chrono::duration<double> dur = chrono::steady_clock::now() - sta;

cout << dur.count() << endl;

cout << sum << endl;

cout << endl;

}

cout << "Lock guard" << endl;

{

const auto sta = chrono::steady_clock::now();

unsigned long long sum = 0;

vector<thread> threads;

threads.resize(n_threads);

//쓰레드 마다 맡길 일의 양을 결정

const unsigned n_per_thread = n_data / n_threads; //assumes remainder=0

//쓰레드 할당 중 + 쓰레드에 값을 넣어주는 중

for (unsigned t = 0; t < n_threads; t++)

{

threads[t] = std::thread(dotProductLock, std::ref(v0), std::ref(v1),

t * n_per_thread, (t + 1) * n_per_thread, std::ref(sum));

}

//join으로 기다리는 코드

for (unsigned t = 0; t < n_threads; t++)

{

threads[t].join();

}

const chrono::duration<double> dur = chrono::steady_clock::now() - sta;

cout << dur.count() << endl;

cout << sum << endl;

cout << endl;

}

cout << "atomic" << endl;

{

const auto sta = chrono::steady_clock::now();

atomic<unsigned long long> sum = 0;

vector<thread> threads;

threads.resize(n_threads);

//쓰레드 마다 맡길 일의 양을 결정

const unsigned n_per_thread = n_data / n_threads; //assumes remainder=0

//쓰레드 할당 중 + 쓰레드에 값을 넣어주는 중

for (unsigned t = 0; t < n_threads; t++)

{

threads[t] = std::thread(dotProducAtomic, std::ref(v0), std::ref(v1),

t * n_per_thread, (t + 1) * n_per_thread, std::ref(sum));

}

//join으로 기다리는 코드

for (unsigned t = 0; t < n_threads; t++)

{

threads[t].join();

}

const chrono::duration<double> dur = chrono::steady_clock::now() - sta;

cout << dur.count() << endl;

cout << sum << endl;

cout << endl;

}

return 0;

}

답변 3

·

답변을 작성해보세요.

1

감사합니다 궁금한 점은 재질문 했습니다

1

Hello Yeo님의 프로필

Hello Yeo

2020.08.22

다시 올려주셨군요. 감사합니다.
확인해보니 코드 내에서 아래의 부분에서 상당한 시간을 소요하네요. 이 부분에서 따로 시간 측정을 해보시면 이해될 수 있는 부분이라 생각됩니다.
덧붙이면 다중 프로세서 컴파일은 여러 개의 프로세서로 컴파일을 하는 것이라 프로그램의 실행 도중에는 영향을 끼치지 못하며, 실행 결과에는 크게 이상이 없는 것 같습니다.

 for (long long i = 0; i < n_data; i++)

 {

 v0.push_back(uniformDist(engine));

 v1.push_back(uniformDist(engine));

 }

0

강의 11:08초에 예제를 실행시킬 때 교수님께서는

0.08초 이런식으로 1초가 걸리지 않는 시간에 내적 결과값이 나옵니다

하지만 제 컴퓨터의 경우에는(8코어 16쓰레드 입니다)  결과 값이 도출되기까지 4분이 소요됩니다.

const unsigned n_threads = 4; 의 숫자를 키워봐도 시간이 빨라지지 않습니다.

ctrl+F5로 프로그램을 실행 시킨 후

CPU 이용률을 살펴 봐도 평상시처럼 5%대라고 나와 있습니다. CPU가 벡터 예제를 전혀 연산하지 않는 것 같습니다.

또한

비주얼 스튜디오 속도 빠르게 하기로 구글링 하여  프로젝트 속성-> C/C++-> 일반에서 다중 프로세서 컴파일에 예라고 설정을 변경 한 후 프로젝트를 빌드하면 CPU 이용률이 100%까지 치솟으며 빌드는 1초도 안되서 끝납니다.

빌드는 1초도 안되어서 실행파일을 형성 시키는데 정작 실행파일 자체는 3~4분이 걸리는 이 현상,

뭐가 문제인가요?

교수님은 락가드를 걸어야 2.15 초가 나오지만 저는 기본이 4초 7초 이렇게 나옵니다. 그 결과가 찍히기 까지는 4분이상 기다려야 하구요.

3~4분 있으면 CPU 이용률이 30~40프로로 바뀌면서 연산을 시작하는(?) 느낌을 받습니다. 그런데 그마저도 시간을 측정하는 것에

따라 출력된 값이

이런식입니다. 가 질문의 원내용이었습니다.