多核编程/Mandelbrot集/C ++ [英] programming for multiple cores / Mandelbrot Set / c++

查看:85
本文介绍了多核编程/Mandelbrot集/C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对"ppl.h"标头的Concurrency :: parallel_for算法有疑问.此示例来自Ivor Horton的书-"Visual C ++ 2010入门".

I have a question concerning the Concurrency::parallel_for algorithm of "ppl.h" header. This example is from Ivor Horton's book - "Beginning Visual C++ 2010".

链接到完整的.cpp文件: http://media.wiley.com/product_ancillary/83/04705008/DOWNLOAD /500880ch13.zip "Ch13/Ex13_03/Ex13_03.cpp"

Link to complete .cpp file: http://media.wiley.com/product_ancillary/83/04705008/DOWNLOAD/500880ch13.zip "Ch13/Ex13_03/Ex13_03.cpp"

在这个特定的示例中,他展示了如何使用并行计算来构建Mandelbrot集.

In this particular example he shows how you can build the Mandelbrot Set using parallel calculations.

处理它的函数是:

void DrawSetParallelFor(HWND hWnd)
{

// setting interface here
HDC hdc(GetDC(hWnd));
RECT rect;
GetClientRect(hWnd, & rect);

// getting width and height of our window
int imageHeight(rect.bottom);
int imageWidth(rect.right);

// defining variables and constants
const double realMin(-2.1); // Minimum real value
double imaginaryMin(-1.3); // Minimum imaginary value
double imaginaryMax(+1.3); // Maximum imaginary value
double realMax(realMin+(imaginaryMax-imaginaryMin)*imageWidth/imageHeight);
double realScale((realMax-realMin)/(imageWidth-1));
double imaginaryScale((imaginaryMax-imaginaryMin)/(imageHeight-1));

// defining critical section
Concurrency::critical_section cs; // Mutex for BitBlt() operation

// starting parallel loop
Concurrency::parallel_for(0, imageHeight, [&](int y)
{
   // locking code
   cs.lock();
      HDC memDC = CreateCompatibleDC(hdc);
      HBITMAP bmp = CreateCompatibleBitmap(hdc, imageWidth, 1);
   cs.unlock();

   HGDIOBJ oldBmp = SelectObject(memDC, bmp);

   double cReal(0.0), cImaginary(0.0);
   double zReal(0.0), zImaginary(0.0);

   zImaginary = cImaginary = imaginaryMax - y*imaginaryScale;

   // filling horizontal rows with colored pixels
   for(int x = 0; x < imageWidth; ++x)
   {
      zReal = cReal = realMin + x*realScale;
      SetPixel(memDC, x, 0, Color(IteratePoint(zReal, zImaginary, cReal, cImaginary)));
   }

   // locking again 
   cs.lock();
      BitBlt(hdc, 0, y, imageWidth, 1, memDC, 0, 0, SRCCOPY);
   cs.unlock();

   // deleting objects
   SelectObject(memDC, oldBmp);
   DeleteObject(bmp);
   DeleteDC(memDC);
});

   ReleaseDC(hWnd, hdc);
}

基本上,此函数呈现在IteratePoint函数中计算的Mandelbrot集.

basically this function renders the Mandelbrot Set, that is being calculated in the IteratePoint function.

水平像素行以随机顺序呈现.我的问题是-Concurrency::parallel_for算法如何精确地确定哪个核心渲染窗口的哪个区域(即一组"y"个水平像素行).

horizontal rows of pixels are rendered in random order. my question is - how exactly Concurrency::parallel_for algorithm decides which region of the window (i.e. set of "y" horizontal rows of pixels) is rendered by which core.

p.s.工作示例如下: http://hotfile.com/dl/137661392/d63280a /MANDELBROT.rar.html

p.s. the working example is here: http://hotfile.com/dl/137661392/d63280a/MANDELBROT.rar.html

谢谢您的时间!

推荐答案

从外观上看,parallel_for使用0和imageHeight之间的每个值一次调用lambda函数.有效地:

By the look of things, parallel_for calls the lambda function once with every value between 0 and imageHeight. Effectively:

Concurrency::parallel_for(0, imageHeight, [&](int y) {

与以下相同:

for(int y=0; y<imageHeight; ++y) {

因此,lambda函数针对图像中的每个y调用一次,可能会将调用拆分为多个工作线程,以允许它们并行运行.

Thus, the lambda function is called once for each y in the image, possibly splitting the calls among multiple worker threads to allow them to run in parallel.

由于parallel_for是一种库函数,因此您实际上不必担心它在内部如何工作.只需接受它为每个y调用一次lamda即可.严格来说,没有定义的顺序,因为可能会同时发生多个调用(例如,在不同的处理器内核上).

Since parallel_for is a library function, you really shouldn't worry about how it works internally. Just accept that it calls the lamda once for every y. Strictly speaking there is no defined order, since multiple calls may occur simultaneously (e.g. on different processor cores).

这篇关于多核编程/Mandelbrot集/C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆