You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
7.3 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "mandelbrot.h"
  10. #include <iostream>
  11. #include<QtGui/QPainter>
  12. #include<QtGui/QImage>
  13. #include<QtGui/QMouseEvent>
  14. #include<QtCore/QTime>
  15. void MandelbrotWidget::resizeEvent(QResizeEvent *)
  16. {
  17. if(size < width() * height())
  18. {
  19. std::cout << "reallocate buffer" << std::endl;
  20. size = width() * height();
  21. if(buffer) delete[]buffer;
  22. buffer = new unsigned char[4*size];
  23. }
  24. }
  25. template<typename T> struct iters_before_test { enum { ret = 8 }; };
  26. template<> struct iters_before_test<double> { enum { ret = 16 }; };
  27. template<typename Real> void MandelbrotThread::render(int img_width, int img_height)
  28. {
  29. enum { packetSize = Eigen::internal::packet_traits<Real>::size }; // number of reals in a Packet
  30. typedef Eigen::Array<Real, packetSize, 1> Packet; // wrap a Packet as a vector
  31. enum { iters_before_test = iters_before_test<Real>::ret };
  32. max_iter = (max_iter / iters_before_test) * iters_before_test;
  33. const int alignedWidth = (img_width/packetSize)*packetSize;
  34. unsigned char *const buffer = widget->buffer;
  35. const double xradius = widget->xradius;
  36. const double yradius = xradius * img_height / img_width;
  37. const int threadcount = widget->threadcount;
  38. typedef Eigen::Array<Real, 2, 1> Vector2;
  39. Vector2 start(widget->center.x() - widget->xradius, widget->center.y() - yradius);
  40. Vector2 step(2*widget->xradius/img_width, 2*yradius/img_height);
  41. total_iter = 0;
  42. for(int y = id; y < img_height; y += threadcount)
  43. {
  44. int pix = y * img_width;
  45. // for each pixel, we're going to do the iteration z := z^2 + c where z and c are complex numbers,
  46. // starting with z = c = complex coord of the pixel. pzi and pzr denote the real and imaginary parts of z.
  47. // pci and pcr denote the real and imaginary parts of c.
  48. Packet pzi_start, pci_start;
  49. for(int i = 0; i < packetSize; i++) pzi_start[i] = pci_start[i] = start.y() + y * step.y();
  50. for(int x = 0; x < alignedWidth; x += packetSize, pix += packetSize)
  51. {
  52. Packet pcr, pci = pci_start, pzr, pzi = pzi_start, pzr_buf;
  53. for(int i = 0; i < packetSize; i++) pzr[i] = pcr[i] = start.x() + (x+i) * step.x();
  54. // do the iterations. Every iters_before_test iterations we check for divergence,
  55. // in which case we can stop iterating.
  56. int j = 0;
  57. typedef Eigen::Matrix<int, packetSize, 1> Packeti;
  58. Packeti pix_iter = Packeti::Zero(), // number of iteration per pixel in the packet
  59. pix_dont_diverge; // whether or not each pixel has already diverged
  60. do
  61. {
  62. for(int i = 0; i < iters_before_test/4; i++) // peel the inner loop by 4
  63. {
  64. # define ITERATE \
  65. pzr_buf = pzr; \
  66. pzr = pzr.square(); \
  67. pzr -= pzi.square(); \
  68. pzr += pcr; \
  69. pzi = (2*pzr_buf)*pzi; \
  70. pzi += pci;
  71. ITERATE ITERATE ITERATE ITERATE
  72. }
  73. pix_dont_diverge = ((pzr.square() + pzi.square())
  74. .eval() // temporary fix as what follows is not yet vectorized by Eigen
  75. <= Packet::Constant(4))
  76. // the 4 here is not a magic value, it's a math fact that if
  77. // the square modulus is >4 then divergence is inevitable.
  78. .template cast<int>();
  79. pix_iter += iters_before_test * pix_dont_diverge;
  80. j++;
  81. total_iter += iters_before_test * packetSize;
  82. }
  83. while(j < max_iter/iters_before_test && pix_dont_diverge.any()); // any() is not yet vectorized by Eigen
  84. // compute pixel colors
  85. for(int i = 0; i < packetSize; i++)
  86. {
  87. buffer[4*(pix+i)] = 255*pix_iter[i]/max_iter;
  88. buffer[4*(pix+i)+1] = 0;
  89. buffer[4*(pix+i)+2] = 0;
  90. }
  91. }
  92. // if the width is not a multiple of packetSize, fill the remainder in black
  93. for(int x = alignedWidth; x < img_width; x++, pix++)
  94. buffer[4*pix] = buffer[4*pix+1] = buffer[4*pix+2] = 0;
  95. }
  96. return;
  97. }
  98. void MandelbrotThread::run()
  99. {
  100. setTerminationEnabled(true);
  101. double resolution = widget->xradius*2/widget->width();
  102. max_iter = 128;
  103. if(resolution < 1e-4f) max_iter += 128 * ( - 4 - std::log10(resolution));
  104. int img_width = widget->width()/widget->draft;
  105. int img_height = widget->height()/widget->draft;
  106. single_precision = resolution > 1e-7f;
  107. if(single_precision)
  108. render<float>(img_width, img_height);
  109. else
  110. render<double>(img_width, img_height);
  111. }
  112. void MandelbrotWidget::paintEvent(QPaintEvent *)
  113. {
  114. static float max_speed = 0;
  115. long long total_iter = 0;
  116. QTime time;
  117. time.start();
  118. for(int th = 0; th < threadcount; th++)
  119. threads[th]->start(QThread::LowPriority);
  120. for(int th = 0; th < threadcount; th++)
  121. {
  122. threads[th]->wait();
  123. total_iter += threads[th]->total_iter;
  124. }
  125. int elapsed = time.elapsed();
  126. if(draft == 1)
  127. {
  128. float speed = elapsed ? float(total_iter)*1000/elapsed : 0;
  129. max_speed = std::max(max_speed, speed);
  130. std::cout << threadcount << " threads, "
  131. << elapsed << " ms, "
  132. << speed << " iters/s (max " << max_speed << ")" << std::endl;
  133. int packetSize = threads[0]->single_precision
  134. ? int(Eigen::internal::packet_traits<float>::size)
  135. : int(Eigen::internal::packet_traits<double>::size);
  136. setWindowTitle(QString("resolution ")+QString::number(xradius*2/width(), 'e', 2)
  137. +QString(", %1 iterations per pixel, ").arg(threads[0]->max_iter)
  138. +(threads[0]->single_precision ? QString("single ") : QString("double "))
  139. +QString("precision, ")
  140. +(packetSize==1 ? QString("no vectorization")
  141. : QString("vectorized (%1 per packet)").arg(packetSize)));
  142. }
  143. QImage image(buffer, width()/draft, height()/draft, QImage::Format_RGB32);
  144. QPainter painter(this);
  145. painter.drawImage(QPoint(0, 0), image.scaled(width(), height()));
  146. if(draft>1)
  147. {
  148. draft /= 2;
  149. setWindowTitle(QString("recomputing at 1/%1 resolution...").arg(draft));
  150. update();
  151. }
  152. }
  153. void MandelbrotWidget::mousePressEvent(QMouseEvent *event)
  154. {
  155. if( event->buttons() & Qt::LeftButton )
  156. {
  157. lastpos = event->pos();
  158. double yradius = xradius * height() / width();
  159. center = Eigen::Vector2d(center.x() + (event->pos().x() - width()/2) * xradius * 2 / width(),
  160. center.y() + (event->pos().y() - height()/2) * yradius * 2 / height());
  161. draft = 16;
  162. for(int th = 0; th < threadcount; th++)
  163. threads[th]->terminate();
  164. update();
  165. }
  166. }
  167. void MandelbrotWidget::mouseMoveEvent(QMouseEvent *event)
  168. {
  169. QPoint delta = event->pos() - lastpos;
  170. lastpos = event->pos();
  171. if( event->buttons() & Qt::LeftButton )
  172. {
  173. double t = 1 + 5 * double(delta.y()) / height();
  174. if(t < 0.5) t = 0.5;
  175. if(t > 2) t = 2;
  176. xradius *= t;
  177. draft = 16;
  178. for(int th = 0; th < threadcount; th++)
  179. threads[th]->terminate();
  180. update();
  181. }
  182. }
  183. int main(int argc, char *argv[])
  184. {
  185. QApplication app(argc, argv);
  186. MandelbrotWidget w;
  187. w.show();
  188. return app.exec();
  189. }
  190. #include "mandelbrot.moc"