c multi-threading 질문드립니다.

익명 사용자의 이미지

안녕하세요.

multi-thread에 관심이 생겨서 이것저것 테스트 해보던 중 궁금한 것이 생겨서 글올립니다.

multi-thread로 processing을 해도 thread 간 공유하는 데이터가 없다면 single-thread로 1개함수를 돌렸을 때와 multi-thread로 3개 함수를 돌릴 때 걸리는 시간이 거의 유사해야 하는게 맞지 않나요?

제가 테스트한 함수는 2차원 배열을 생성하고 배열 내 데이터들의 표준편차를 구하는 함수입니다.
하나의 thread만 돌릴때, 3개의 thread를 돌릴 때 시간 차이가 발생하네요.

cpu는 12개 core라서 core가 부족할 일은 없을 것 같은데, 원인이 무엇일까요?

void CalExample();
double getAvg(double** dDAta, int nSize);
double getStd(double** dData, double dAvg, int nSize);
void CMFCApplication3Dlg::OnBnClickedButton1()
{
std::thread thread1[3];

for(int i = 0 ; i < 3; i++)
thread1[i] = std::thread(CalExample);

if (thread1[0].joinable())
thread1[0].join();
if (thread1[1].joinable())
thread1[1].join();
if (thread1[2].joinable())
thread1[2].join();

}
void CalExample()
{
int nDataSize = 10000;
double** dData = new double*[nDataSize];
for (int i = 0; i < nDataSize; i++)
dData[i] = new double[nDataSize];

for (int i = 0; i < nDataSize; i++)
for (int j = 0; j < nDataSize; j++)
dData[i][j] = i + j;

clock_t start = clock();
double dAVg = getAvg(dData, nDataSize);
double dStd = getStd(dData, dAVg, nDataSize);
clock_t end = clock();
CString str;
str.Format(L"It took %lf sec\n%f", (end - start) / (float)CLOCKS_PER_SEC,dStd);
AfxMessageBox(str);
for (int i = 0; i < nDataSize; i++)
delete dData[i];

delete dData;
}
double getAvg(double** dDAta, int nSize)
{
double dTempVal = 0.0;
for (int i = 0; i < nSize; i++)
for (int j = 0; j < nSize; j++)
dTempVal += dDAta[i][j];

return dTempVal / (double)nSize;
}
double getStd(double**dData, double dAvg, int nSize)
{
double dTempStd = 0.0;
for (int i = 0; i < nSize; i++)
for (int j = 0; j < nSize; j++)
dTempStd += (dData[i][j] - dAvg)*(dData[i][j] - dAvg);

dTempStd /= nSize;

return dTempStd;
}

작성자의 이미지

void CalExample();
double getAvg(double** dDAta, int nSize);
double getStd(double** dData, double dAvg, int nSize);
void CMFCApplication3Dlg::OnBnClickedButton1()
{
	std::thread thread1[3];
 
	for(int i = 0 ; i < 3; i++)
		thread1[i] = std::thread(CalExample);
 
	if (thread1[0].joinable())
		thread1[0].join();
	if (thread1[1].joinable())
		thread1[1].join();
	if (thread1[2].joinable())
		thread1[2].join();
 
}
void CalExample()
{
	int nDataSize = 10000;
	double** dData = new double*[nDataSize];
	for (int i = 0; i < nDataSize; i++)
		dData[i] = new double[nDataSize];
 
	for (int i = 0; i < nDataSize; i++)
		for (int j = 0; j < nDataSize; j++)
			dData[i][j] = i + j;
 
	clock_t start = clock();
	double dAVg = getAvg(dData, nDataSize);
	double dStd = getStd(dData, dAVg, nDataSize);
	clock_t end = clock();
	CString str;
	str.Format(L"It took %lf sec\n%f", (end - start) / (float)CLOCKS_PER_SEC,dStd);
	AfxMessageBox(str);
	for (int i = 0; i < nDataSize; i++)
		delete dData[i];
 
	delete dData;
}
double getAvg(double** dDAta, int nSize)
{
	double dTempVal = 0.0;
	for (int i = 0; i < nSize; i++)
		for (int j = 0; j < nSize; j++)
			dTempVal += dDAta[i][j];
 
	return dTempVal / (double)nSize;
}
double getStd(double**dData, double dAvg, int nSize)
{
	double dTempStd = 0.0;
	for (int i = 0; i < nSize; i++)
		for (int j = 0; j < nSize; j++)
			dTempStd += (dData[i][j] - dAvg)*(dData[i][j] - dAvg);
 
	dTempStd /= nSize;
 
	return dTempStd;
}
joone의 이미지

전문가는 아니지만, CPU 두개 달았다고 성능이 꼭 두개가 되는거는 아닙니다. 어차피 같은 리소스를(저장장치, 메모리, 버스 등)를 공유하므로 core수 만큼 성능 체감이 높지는 않을겁니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.