私は、Windowsマシン(MSVS2015を使用してコンパイルされたもの)とSolaris 10を実行するサーバー(GCC 4.9.3を使用してコンパイルされたもの)の両方で簡単なスレッドテストプログラムを実行しています。 Windowsでは、スレッドを1から利用可能なコアの量に増やすことでパフォーマンスが大幅に向上します。しかし、Solaris 10ではまったく同じコードではパフォーマンスの向上は見られません。WindowsとSolaris 10でのstd :: asyncパフォーマンス
Windowsマシンには4つのコア(8論理)があり、UNIXマシンには8コア(16論理)があります。
この原因は何ですか? -pthread
でコンパイルしています。はです。最初の "F"の前にすべての "S"が表示されるため、スレッドを作成しています。私はSolarisマシン上でrootアクセス権を持っておらず、プロセスの親和性を見るために使用できるインストールされたツールはありません。
例コード:原則として
#include <iostream>
#include <vector>
#include <future>
#include <random>
#include <chrono>
std::default_random_engine gen(std::chrono::system_clock::now().time_since_epoch().count());
std::normal_distribution<double> randn(0.0, 1.0);
double generate_randn(uint64_t iterations)
{
// Print "S" when a thread starts
std::cout << "S";
std::cout.flush();
double rvalue = 0;
for (int i = 0; i < iterations; i++)
{
rvalue += randn(gen);
}
// Print "F" when a thread finishes
std::cout << "F";
std::cout.flush();
return rvalue/iterations;
}
int main(int argc, char *argv[])
{
if (argc < 2)
return 0;
uint64_t count = 100000000;
uint32_t threads = std::atoi(argv[1]);
double total = 0;
std::vector<std::future<double>> futures;
std::chrono::high_resolution_clock::time_point t1;
std::chrono::high_resolution_clock::time_point t2;
// Start timing
t1 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < threads; i++)
{
// Start async tasks
futures.push_back(std::async(std::launch::async, generate_randn, count/threads));
}
for (auto &future : futures)
{
// Wait for tasks to finish
future.wait();
total += future.get();
}
// End timing
t2 = std::chrono::high_resolution_clock::now();
// Take the average of the threads' results
total /= threads;
std::cout << std::endl;
std::cout << total << std::endl;
std::cout << "Finished in " << std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count() << " ms" << std::endl;
}
'gen'と' randn'の宣言を 'generate_randn'の中で動かしてみましょう。一つの共有RNGの代わりにスレッドごとに一つのRNGインスタンスがあるようにしてください。私はまた、スレッドごとのランタイムの完全な分布を見ることに興味があります。 – zwol
genとrandnをgenerate_randnの中に移動した後、正常に動作しました!それを答えとして加えて、それをマークします。 :) –