parallel:thread
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
— | parallel:thread [2020-07-26 18:40] (aktuell) – angelegt - Externe Bearbeitung 127.0.0.1 | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | ====== Apfelmännchen mit C++11 std::thread ====== | ||
+ | Die Berechnung der Bildzeilen wurde einzelnen Threads in einem [[https:// | ||
+ | <code cpp> | ||
+ | //: mandelbrot.cpp : Fraktalbild - R.Richter 2014-05-02 | ||
+ | /////////////////////////////////////////////////////// | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | #include " | ||
+ | |||
+ | typedef std:: | ||
+ | |||
+ | int compute(complex c, int maxIterations) | ||
+ | { | ||
+ | int count = 0; | ||
+ | complex z; | ||
+ | |||
+ | while (abs(z) <= 2.0 && count < maxIterations) | ||
+ | { | ||
+ | z = z * z + c; | ||
+ | ++ count; | ||
+ | } | ||
+ | return count; | ||
+ | } | ||
+ | |||
+ | inline | ||
+ | Color color(int height, int max) | ||
+ | { | ||
+ | // color scheme from: | ||
+ | // http:// | ||
+ | if (height >= max) return Color:: | ||
+ | double h = 255 * log(double(height)) / log(double(max)); | ||
+ | return Color(0.9 * h, 0.8 * h, 0.6 * h); | ||
+ | } | ||
+ | |||
+ | inline | ||
+ | double scale(int pos, int length, double low, double high) | ||
+ | { | ||
+ | return low + pos * (high-low) / (length-1); | ||
+ | } | ||
+ | |||
+ | Image mandelbrot(int width, int height, | ||
+ | int maxIterations, | ||
+ | | ||
+ | { | ||
+ | int const numThreads = 16; | ||
+ | ThreadPool pool(numThreads); | ||
+ | std:: | ||
+ | |||
+ | for (int y = 0; y < height; ++y) | ||
+ | { | ||
+ | lines.push_back( | ||
+ | pool.enqueue( | ||
+ | [y, width, height, maxIterations, | ||
+ | { | ||
+ | std:: | ||
+ | for (int x = 0; x < width; ++x) | ||
+ | { | ||
+ | complex c(scale(x, width, | ||
+ | scale(y, height, imag(left_bottom), | ||
+ | |||
+ | int iterations = compute(c, maxIterations); | ||
+ | line[x] = color(iterations, | ||
+ | } | ||
+ | // std::cout << ("line " + std:: | ||
+ | return line; | ||
+ | } | ||
+ | ) | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | Image image(width, | ||
+ | for (int y = 0; y < height; ++y) | ||
+ | { | ||
+ | auto line = lines[y].get(); | ||
+ | for (int x = 0; x < width; ++x) | ||
+ | { | ||
+ | image.pixel(x, | ||
+ | } | ||
+ | } | ||
+ | return image; | ||
+ | } | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int width = 1000; | ||
+ | int height = 1000; | ||
+ | int maxIterations = 10000; | ||
+ | complex left_bottom(-2.0, | ||
+ | complex right_top | ||
+ | |||
+ | saveBMP(" | ||
+ | mandelbrot(width, | ||
+ | |||
+ | std::cout << clock() / double(CLOCKS_PER_SEC) << " seconds CPU time\n"; | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Übersetzung, | ||
+ | Das Programm wurde mit g++ 4.9.0, Option '' | ||
+ | Die Zahl der gleichzeitig ausführenden Threads ('' | ||
+ | |||
+ | Zur Ausführung kann das Programm auf [[Udoo|Udoo Quad]], einem Dual-Core-Rechner unter Windows 7 und einem Quad-Core-Rechner mit Hyperthreading unter Windows 7. | ||
+ | |||
+ | ===== Zeitmessung ===== | ||
+ | {{: | ||
+ | |||
+ | Die Ausführungszeit sinkt auf einen Minimalwert, | ||
+ | Unter Linux bleibt die Berechnungszeit auch dann bei diesem Minimum, wenn die Zahl der im Programmm angegebenen Threads höher ist als die tatsächliche Anzahl vorhandener Prozessoren. | ||
+ | Unter Windows ist die Angabe einer zu hohen Threadanzahl schädlich. Dieser Effekt scheint an der von [[http:// | ||
+ | Bei Übersetzung und Messungen unter Microsoft Visual Studio 2013 Express verschwand dieserr |