我有一个关于并发::“ppl.h”头的parallel_for时算法的问题。这个例子来自Ivor Horton的书 - “Visual C++ 2010开始”。编程多核/ Mandelbrot集/ C++
链接完成.cpp文件: http://media.wiley.com/product_ancillary/83/04705008/DOWNLOAD/500880ch13.zip “CH13/Ex13_03/Ex13_03.cpp”
在这个特殊的例子,他展示了如何构建使用并行计算Mandelbrot集。
处理它的功能是:
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);
}
基本上这个功能呈现Mandelbrot集,正被在IteratePoint
函数计算。
水平行像素以随机顺序呈现。我的问题是 - 算法如何确定窗口的哪个区域(即像素的“y”水平行集合)是由哪个内核渲染的。
p.s.该工作例子是在这里:http://hotfile.com/dl/137661392/d63280a/MANDELBROT.rar.html
感谢您的时间!
非常感谢你为清除该对我) – 2011-12-16 15:05:22