多核编程/ Mandelbrot Set / c++
programming for multiple cores / Mandelbrot Set / c++
我有一个关于"ppl.h"头的Concurrency::parallel_for算法的问题。这个例子来自Ivor Horton的书——"Beginning Visual c++ 2010"。
链接到完整的。cpp文件:http://media.wiley.com/product_ancillary/83/04705008/DOWNLOAD/500880ch13.zip"Ch13/Ex13_03/Ex13_03.cpp "
在这个特殊的例子中,他展示了如何使用并行计算来构建Mandelbrot Set。
处理它的函数是:
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
函数中计算的。
水平行像素以随机顺序呈现。我的问题是- Concurrency::parallel_for
算法究竟如何决定窗口的哪个区域(即"y"像素的水平行集)由哪个核心渲染。
注。工作示例如下:http://hotfile.com/dl/137661392/d63280a/MANDELBROT.rar.html
感谢您的宝贵时间!从表面上看,parallel_for对0到imageHeight之间的每个值调用一次lambda函数。有效:
Concurrency::parallel_for(0, imageHeight, [&](int y) {
等于:
for(int y=0; y<imageHeight; ++y) {
因此,对于图像中的每个y调用一次lambda函数,可能会在多个工作线程中拆分调用,以允许它们并行运行。
因为parallel_for是一个库函数,你真的不应该担心它是如何在内部工作的。只要接受它对每个y调用一次lambda。严格地说,没有定义的顺序,因为多个调用可能同时发生(例如在不同的处理器内核上)。
这是由编译器处理时,每个代码的线程生成的。编译器负责将指令分发给内核。
更多:http://www.multicoreinfo.com/research/papers/whitepapers/intel-opti-mc.pdf
http://arco.e.ac.upc.edu/wiki/images/b/b4/Madriles_isca09.pdf- 为什么我无法更改"set<set>"循环中的值<int>
- 对于set上的循环-获取next元素迭代器
- 在声明中合并两个常量"std::set"(不是在运行时)
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将 std::set 与基于键的比较器一起使用
- 如何使用set实现无序数据结构?
- 使用运算符调用 void 函数时出错<set>
- 修改"std::set"中用户定义类型的值
- 生成提升::hana::set 的常量表达式问题
- 如何在构造函数参数中初始化"std::set"?
- 如何使用 lower_bound/upper_bound 从 std::set 获取索引号?
- 如何在 C++ 中转发声明 std::set?
- 重构使用动态强制转换的 std::set 的比较运算符
- set::find 查找不存在的元素
- 为什么 std::set.erase(first, last) 会影响从中获取 (first, last) 的容器?
- 将 std:set<int32_t> 复制到 std::set <uint32_t>的好方法
- 错误 C2676:std::set::const_iterator 没有运算符 + 函数?
- std::set 是否将对象连续存储在内存中?
- 是否有一个 std::set 函数来确定不超过数字 x 的最大元素?
- google test PrintTo for std::set<std::string>