auto fakeExpensiveOperation = [](int i) -> float
{
Sleep(10);
return sqrt(i);
};
// Performance test
int size = 4000;
float* out = new float[size];
#define THREAD_RUNNING 0
#define THREAD_FINISHED (1 << 0)
#define THREAD_EXIT (1 << 1)
#define THREAD_STALL (1 << 2)
const int coreCount = 8;
thread cores[coreCount];
atomic<unsigned int> msgArray[coreCount]; for (auto& msg : msgArray) msg.store(THREAD_STALL);
auto kernel = [out, &fakeExpensiveOperation](int idx){ out[idx] = fakeExpensiveOperation(idx); };
for (int i = 0; i < coreCount; i++)
{
cores[i] = thread([&msgArray, i, kernel]()
{
while (true)
{
unsigned int msg = msgArray[i].load();
if((msg & THREAD_STALL) == THREAD_STALL)
continue;
if ((msg & THREAD_EXIT) == THREAD_EXIT)
break;
if ((msg & THREAD_RUNNING) == THREAD_RUNNING)
{
int idx = (msg >> 3) + i;
// Do the function
kernel(idx);
msgArray[i].store(THREAD_FINISHED);
}
}
});
}
auto t2 = time_call([&]()
{
for (int i = 0; i < size; i += coreCount)
{
for (int n = 0; n < coreCount; n++)
{
if((msgArray[n].load() & THREAD_RUNNING) == THREAD_RUNNING) continue; // The core is still working
unsigned int msg = THREAD_RUNNING;
msg |= (i << 3);
msgArray[n].store(msg);
}
}
});
for (int n = 0; n < coreCount; n++) msgArray[n].store(THREAD_EXIT);
cout << "sqrt 0 : " << out[0] << endl;
cout << "sqrt 1 : " << out[1] << endl;
cout << "sqrt 2 : " << out[2] << endl;
cout << "sqrt 4 : " << out[4] << endl;
cout << "sqrt 16 : " << out[16] << endl;
cout << "Parallel : " << t2 << endl;
system("pause");
delete[] out;
return 0;
我真的没有想法。任何人都可以指出这里有什么问题吗?
编辑:我做了我提到的更改,但仍然得到错误的值。我更改了标志的值,并在创建它们后分离了线程。
答案 0 :(得分:0)
我可能是错的,(我对C ++ 11线程的东西并不是很熟悉),但看起来你正在运行一个线程cores[i] = thread([&msgArray, i, kernel]()
然后等待该线程完成。然后创建下一个线程。基本上使它成为单线程。
我也喜欢用C ++ 11进行计时
std::chrono::time_point<std::chrono::system_clock> start, end;
start = std::chrono::system_clock::now();
// Do Stuff
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds_d1 = end-start;
std::time_t end_time_d1 = std::chrono::system_clock::to_time_t(end);
std::cout << "elapsed time: " << elapsed_seconds_d1.count() << "s\n";