C++ Mandelbrot集的多线程计算
我创建了一个程序,它创建了一个C++ Mandelbrot集的多线程计算,c++,multithreading,fractals,mandelbrot,C++,Multithreading,Fractals,Mandelbrot,我创建了一个程序,它创建了一个Mandelbrotset。现在,我正在尝试使它成为多线程的 // mandelbrot.cpp // compile with: g++ -std=c++11 mandelbrot.cpp -o mandelbrot // view output with: eog mandelbrot.ppm #include <fstream> #include <complex> // if you make use of complex numb
Mandelbrot
set。现在,我正在尝试使它成为多线程的
// mandelbrot.cpp
// compile with: g++ -std=c++11 mandelbrot.cpp -o mandelbrot
// view output with: eog mandelbrot.ppm
#include <fstream>
#include <complex> // if you make use of complex number facilities in C++
#include <iostream>
#include <cstdlib>
#include <thread>
#include <mutex>
#include <vector>
using namespace std;
template <class T> struct RGB { T r, g, b; };
template <class T>
class Matrix {
public:
Matrix(const size_t rows, const size_t cols) : _rows(rows), _cols(cols) {
_matrix = new T*[rows];
for (size_t i = 0; i < rows; ++i) {
_matrix[i] = new T[cols];
}
}
Matrix(const Matrix &m) : _rows(m._rows), _cols(m._cols) {
_matrix = new T*[m._rows];
for (size_t i = 0; i < m._rows; ++i) {
_matrix[i] = new T[m._cols];
for (size_t j = 0; j < m._cols; ++j) {
_matrix[i][j] = m._matrix[i][j];
}
}
}
~Matrix() {
for (size_t i = 0; i < _rows; ++i) {
delete [] _matrix[i];
}
delete [] _matrix;
}
T *operator[] (const size_t nIndex)
{
return _matrix[nIndex];
}
size_t width() const { return _cols; }
size_t height() const { return _rows; }
protected:
size_t _rows, _cols;
T **_matrix;
};
// Portable PixMap image
class PPMImage : public Matrix<RGB<unsigned char> >
{
public:
unsigned int size;
PPMImage(const size_t height, const size_t width) : Matrix(height, width) { }
void save(const std::string &filename)
{
std::ofstream out(filename, std::ios_base::binary);
out <<"P6" << std::endl << _cols << " " << _rows << std::endl << 255 << std::endl;
for (size_t y=0; y<_rows; y++)
for (size_t x=0; x<_cols; x++)
out << _matrix[y][x].r << _matrix[y][x].g << _matrix[y][x].b;
}
};
/*Draw mandelbrot according to the provided parameters*/
void draw_Mandelbrot(PPMImage & image, const unsigned width, const unsigned height, double cxmin, double cxmax, double cymin, double cymax,unsigned int max_iterations)
{
for (std::size_t ix = 0; ix < width; ++ix)
for (std::size_t iy = 0; iy < height; ++iy)
{
std::complex<double> c(cxmin + ix / (width - 1.0)*(cxmax - cxmin), cymin + iy / (height - 1.0)*(cymax - cymin));
std::complex<double> z = 0;
unsigned int iterations;
for (iterations = 0; iterations < max_iterations && std::abs(z) < 2.0; ++iterations)
z = z*z + c;
image[iy][ix].r = image[iy][ix].g = image[iy][ix].b = iterations;
}
}
int main()
{
const unsigned width = 1600;
const unsigned height = 1600;
PPMImage image(height, width);
int parts = 8;
std::vector<int>bnd (parts, image.size);
std::thread *tt = new std::thread[parts - 1];
time_t start, end;
time(&start);
//Lauch parts-1 threads
for (int i = 0; i < parts - 1; ++i) {
tt[i] = std::thread(draw_Mandelbrot,ref(image), width, height, -2.0, 0.5, -1.0, 1.0, 10);
}
//Use the main thread to do part of the work !!!
for (int i = parts - 1; i < parts; ++i) {
draw_Mandelbrot(ref(image), width, height, -2.0, 0.5, -1.0, 1.0, 10);
}
//Join parts-1 threads
for (int i = 0; i < parts - 1; ++i)
tt[i].join();
time(&end);
std::cout << difftime(end, start) << " seconds" << std::endl;
image.save("mandelbrot.ppm");
delete[] tt;
return 0;
}
//mandelbrot.cpp
//使用:g++-std=c++11 mandelbrot.cpp-o mandelbrot编译
//使用:eog mandelbrot.ppm查看输出
#包括
包含/ /如果你在C++中使用复数设施
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
模板结构RGB{tr,g,b;};
模板
类矩阵{
公众:
矩阵(常量大小行、常量大小列):\行(行)、列(列){
_矩阵=新的T*[行];
对于(大小i=0;i 这种方法的一个大问题是,不同的地区需要不同的时间来计算
一个更普遍的方法是
- 启动1个源线程
- 启动N个工作线程
- 启动1个水槽线程
- 创建2个线程安全队列(称为源队列和接收器队列)
- 将图像分成M个(远远多于N个)部分
- 源线程将片段推送到源队列中
- 工作人员从源队列中提取piecse,将片段转换为结果片段,并将这些片段推送到接收队列中
- sink线程从sink队列中获取片段,并将它们组合到最终映像中
通过这种方式分配工作,所有工作线程将一直处于繁忙状态。您使这项工作(相当多)变得比需要的更困难。这是OpenMP几乎完全适合的任务类型。对于此任务,它几乎可以以最小的工作量实现完美的扩展
我修改了您的draw\u mandelbrot
,在for
循环的外部之前插入了一个pragma:
#pragma omp parallel for
for (int ix = 0; ix < width; ++ix)
for (int iy = 0; iy < height; ++iy)
在我的(相当慢的)机器上,您的原始代码运行时间为4.73秒。我修改后的代码运行时间为1.38秒。这是代码的3.4倍,与普通的单线程版本几乎没有区别
为了让它有价值,我做了更多的修改来得到这个:
// mandelbrot.cpp
// compile with: g++ -std=c++11 mandelbrot.cpp -o mandelbrot
// view output with: eog mandelbrot.ppm
#include <fstream>
#include <complex> // if you make use of complex number facilities in C++
#include <iostream>
#include <cstdlib>
#include <thread>
#include <mutex>
#include <vector>
using namespace std;
template <class T> struct RGB { T r, g, b; };
template <class T>
struct Matrix
{
std::vector<T> data;
size_t rows;
size_t cols;
class proxy {
Matrix &m;
size_t index_1;
public:
proxy(Matrix &m, size_t index_1) : m(m), index_1(index_1) { }
T &operator[](size_t index) { return m.data[index * m.rows + index_1]; }
};
class const_proxy {
Matrix const &m;
size_t index_1;
public:
const_proxy(Matrix const &m, size_t index_1) : m(m), index_1(index_1) { }
T const &operator[](size_t index) const { return m.data[index * m.rows + index_1]; }
};
public:
Matrix(size_t rows, size_t cols) : data(rows * cols), rows(rows), cols(cols) { }
proxy operator[](size_t index) { return proxy(*this, index); }
const_proxy operator[](size_t index) const { return const_proxy(*this, index); }
};
template <class T>
std::ostream &operator<<(std::ostream &out, Matrix<T> const &m) {
out << "P6" << std::endl << m.cols << " " << m.rows << std::endl << 255 << std::endl;
for (size_t y = 0; y < m.rows; y++)
for (size_t x = 0; x < m.cols; x++) {
T pixel = m[y][x];
out << pixel.r << pixel.g << pixel.b;
}
return out;
}
/*Draw Mandelbrot according to the provided parameters*/
template <class T>
void draw_Mandelbrot(T & image, const unsigned width, const unsigned height, double cxmin, double cxmax, double cymin, double cymax, unsigned int max_iterations) {
#pragma omp parallel for
for (int ix = 0; ix < width; ++ix)
for (int iy = 0; iy < height; ++iy)
{
std::complex<double> c(cxmin + ix / (width - 1.0)*(cxmax - cxmin), cymin + iy / (height - 1.0)*(cymax - cymin));
std::complex<double> z = 0;
unsigned int iterations;
for (iterations = 0; iterations < max_iterations && std::abs(z) < 2.0; ++iterations)
z = z*z + c;
image[iy][ix].r = image[iy][ix].g = image[iy][ix].b = iterations;
}
}
int main() {
const unsigned width = 1600;
const unsigned height = 1600;
Matrix<RGB<unsigned char>> image(height, width);
clock_t start = clock();
draw_Mandelbrot(image, width, height, -2.0, 0.5, -1.0, 1.0, 255);
clock_t stop = clock();
std::cout << (double(stop - start) / CLOCKS_PER_SEC) << " seconds\n";
std::ofstream out("mandelbrot.ppm", std::ios::binary);
out << image;
return 0;
}
显然,将正确的数据传递给计算线程需要做一些工作(每个线程都应该传递一个对结果图片切片的引用),但这显然是一个划分任务的相当干净的地方。特别是,它将任务划分为足够多的任务,您可以半自动地获得相当好的负载平衡(即,您可以让所有核心保持忙碌)但是足够大,这样就不会在线程之间的通信和同步上浪费大量时间
对于结果,将迭代次数设置为255,我得到以下结果(比例为25%):
…这与我的预期非常接近。您可以通过将分形的开始和结束与屏幕尺寸分开来将分形分为几部分:
$this->stepsRe = (double)((($this->startRe * -1) + ($this->endeRe)) / ($this->size_x-1));
$this->stepsIm = (double)((($this->startIm * -1) + ($this->endeIm)) / ($this->size_y-1));
相关(Java):基本算法;计算出每个部分的边界,将这些边界以及相应的x和y边界传递给线程。您需要将其中一个角点的坐标添加到线程函数中。我建议您首先以单线程方式执行(通过从main
调用函数)所以你不必同时解决数学问题和线程问题。@ MulbDNILO你有一个例子吗?@赛布伦数学的一个例子?“MalbDNILO不,一个例子,你解释的类似的事情是用C++完成的。这是一个巨大的改进,我将研究OpenMP,但是现在的主要问题是把图像分割成碎片和P。用多个线程处理这些片段。如何做?@Sybren:通过如上所述插入pragma(即,这正是OpenMP所做的)。按照您的说明插入pragma并用简化的main替换main,但GIMP
现在显示此错误:PNM图像插件无法打开图像。
(我使用GIMP
打开图像)@Sybren:我不知道——我做了一个逐位比较,发现我得到的图像与之前生成的图像完全相同。我看到你做了更多的重写,然后添加了pragma并简化了main。你不使用PPMImage
类,原因是什么?我更喜欢让程序尽可能与我发布的代码保持一致。如何将图像分割成小块?我找不到任何例子。@Sybren很难相信你编写了代码来绘制mandelbrot分形,但却不知道如何将矩形区域分割成小块。如果你有一个宽度w
和高度h
的矩形,那么你可以简单地将其分割成两个宽度的矩形E> W/2 和Health H< /C>,其中一个X源偏移为<代码> W/2 原始。我将留下一个练习作为一个练习。世界上什么是代码> $-> >代码>?你是尝试写PHP而不是C++吗?是的。它是PHP。但是它不是关于语言的!这个问题是标记C++的。而M的基本概念是。ath并不局限于任何一种语言,他仍然相当明确地要求使用一种语言,而且似乎没有任何收获
for (int iy = 0; iy < height; ++iy)
{
std::complex<double> c(cxmin + ix / (width - 1.0)*(cxmax - cxmin), cymin + iy / (height - 1.0)*(cymax - cymin));
std::complex<double> z = 0;
unsigned int iterations;
for (iterations = 0; iterations < max_iterations && std::abs(z) < 2.0; ++iterations)
z = z*z + c;
image[iy][ix].r = image[iy][ix].g = image[iy][ix].b = iterations;
}
$this->stepsRe = (double)((($this->startRe * -1) + ($this->endeRe)) / ($this->size_x-1));
$this->stepsIm = (double)((($this->startIm * -1) + ($this->endeIm)) / ($this->size_y-1));