code_junkie

Benchmark problems for testing concurency

Nov 12th, 2011
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. #include <cstdlib> //for atoi
  2. #include <iostream>
  3. #include <iomanip> //for setw and setfill
  4. #include <vector>
  5.  
  6.  
  7. int DoThread(const double x, const double y, int maxiter) {
  8. double curX,curY,xSquare,ySquare;
  9. int i;
  10.  
  11. curX = x + x*x - y*y;
  12. curY = y + x*y + x*y;
  13. ySquare = curY*curY;
  14. xSquare = curX*curX;
  15.  
  16. for (i=0; i<maxiter && ySquare + xSquare < 4;i++) {
  17. ySquare = curY*curY;
  18. xSquare = curX*curX;
  19. curY = y + curX*curY + curX*curY;
  20. curX = x - ySquare + xSquare;
  21. }
  22. return i;
  23. }
  24.  
  25. void SingleThreaded(int horizPixels, int vertPixels, int maxiter, std::vector<std::vector<int> >& result) {
  26. for(int x = horizPixels; x > 0; x--) {
  27. for(int y = vertPixels; y > 0; y--) {
  28. //3.0 -> so we always have -1.5 -> 1.5 as the window; (x - (horizPixels / 2) will go from -horizPixels/2 to +horizPixels/2
  29. result[x-1][y-1] = DoThread((3.0 / horizPixels) * (x - (horizPixels / 2)),(3.0 / vertPixels) * (y - (vertPixels / 2)),maxiter);
  30. }
  31. }
  32. }
  33.  
  34. int main(int argc, char* argv[]) {
  35. //first arg = length along horizontal axis
  36. int horizPixels = atoi(argv[1]);
  37.  
  38. //second arg = length along vertical axis
  39. int vertPixels = atoi(argv[2]);
  40.  
  41. //third arg = iterations
  42. int maxiter = atoi(argv[3]);
  43.  
  44. //fourth arg = threads
  45. int threadCount = atoi(argv[4]);
  46.  
  47. std::vector<std::vector<int> > result(horizPixels, std::vector<int>(vertPixels,0)); //create and init 2-dimensional vector
  48. SingleThreaded(horizPixels, vertPixels, maxiter, result);
  49.  
  50. //TODO: remove these lines
  51. for(int y = 0; y < vertPixels; y++) {
  52. for(int x = 0; x < horizPixels; x++) {
  53. std::cout << std::setw(2) << std::setfill('0') << std::hex << result[x][y] << " ";
  54. }
  55. std::cout << std::endl;
  56. }
  57. }
  58.  
  59. #include <cstdlib> //for atoi
  60. #include <iostream>
  61. #include <iomanip> //for setw and setfill
  62. #include <vector>
  63.  
  64. #include "bitmap_Image.h" //for saving the mandelbrot as a bmp
  65.  
  66. #include <pthread.h>
  67.  
  68. pthread_mutex_t mutexCounter;
  69. int sharedCounter(0);
  70. int percent(0);
  71.  
  72. int horizPixels(0);
  73. int vertPixels(0);
  74. int maxiter(0);
  75.  
  76. //doesn't need to be locked
  77. std::vector<std::vector<int> > result; //create 2 dimensional vector
  78.  
  79. void *DoThread(void *null) {
  80. double curX,curY,xSquare,ySquare,x,y;
  81. int i, intx, inty, counter;
  82. counter = 0;
  83.  
  84. do {
  85. counter++;
  86. pthread_mutex_lock (&mutexCounter); //lock
  87. intx = int((sharedCounter / vertPixels) + 0.5);
  88. inty = sharedCounter % vertPixels;
  89. sharedCounter++;
  90. pthread_mutex_unlock (&mutexCounter); //unlock
  91.  
  92. //exit thread when finished
  93. if (intx >= horizPixels) {
  94. std::cout << "exited thread - I did " << counter << " calculations" << std::endl;
  95. pthread_exit((void*) 0);
  96. }
  97.  
  98. //set x and y to the correct value now -> in the range like singlethread
  99. x = (3.0 / horizPixels) * (intx - (horizPixels / 1.5));
  100. y = (3.0 / vertPixels) * (inty - (vertPixels / 2));
  101.  
  102. curX = x + x*x - y*y;
  103. curY = y + x*y + x*y;
  104. ySquare = curY*curY;
  105. xSquare = curX*curX;
  106.  
  107. for (i=0; i<maxiter && ySquare + xSquare < 4;i++){
  108. ySquare = curY*curY;
  109. xSquare = curX*curX;
  110. curY = y + curX*curY + curX*curY;
  111. curX = x - ySquare + xSquare;
  112. }
  113. result[intx][inty] = i;
  114. } while (true);
  115. }
  116.  
  117. int DoSingleThread(const double x, const double y) {
  118. double curX,curY,xSquare,ySquare;
  119. int i;
  120.  
  121. curX = x + x*x - y*y;
  122. curY = y + x*y + x*y;
  123. ySquare = curY*curY;
  124. xSquare = curX*curX;
  125.  
  126. for (i=0; i<maxiter && ySquare + xSquare < 4;i++){
  127. ySquare = curY*curY;
  128. xSquare = curX*curX;
  129. curY = y + curX*curY + curX*curY;
  130. curX = x - ySquare + xSquare;
  131. }
  132. return i;
  133.  
  134. }
  135.  
  136. void SingleThreaded(std::vector<std::vector<int> >& result) {
  137. for(int x = horizPixels - 1; x != -1; x--) {
  138. for(int y = vertPixels - 1; y != -1; y--) {
  139. //3.0 -> so we always have -1.5 -> 1.5 as the window; (x - (horizPixels / 2) will go from -horizPixels/2 to +horizPixels/2
  140. result[x][y] = DoSingleThread((3.0 / horizPixels) * (x - (horizPixels / 1.5)),(3.0 / vertPixels) * (y - (vertPixels / 2)));
  141. }
  142. }
  143. }
  144.  
  145. void MultiThreaded(int threadCount, std::vector<std::vector<int> >& result) {
  146. /* Initialize and set thread detached attribute */
  147. pthread_t thread[threadCount];
  148. pthread_attr_t attr;
  149. pthread_attr_init(&attr);
  150. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  151.  
  152.  
  153. for (int i = 0; i < threadCount - 1; i++) {
  154. pthread_create(&thread[i], &attr, DoThread, NULL);
  155. }
  156. std::cout << "all threads created" << std::endl;
  157.  
  158. for(int i = 0; i < threadCount - 1; i++) {
  159. pthread_join(thread[i], NULL);
  160. }
  161. std::cout << "all threads joined" << std::endl;
  162. }
  163.  
  164. int main(int argc, char* argv[]) {
  165. //first arg = length along horizontal axis
  166. horizPixels = atoi(argv[1]);
  167.  
  168. //second arg = length along vertical axis
  169. vertPixels = atoi(argv[2]);
  170.  
  171. //third arg = iterations
  172. maxiter = atoi(argv[3]);
  173.  
  174. //fourth arg = threads
  175. int threadCount = atoi(argv[4]);
  176.  
  177. result = std::vector<std::vector<int> >(horizPixels, std::vector<int>(vertPixels,21)); // init 2-dimensional vector
  178. if (threadCount <= 1) {
  179. SingleThreaded(result);
  180. } else {
  181. MultiThreaded(threadCount, result);
  182. }
  183.  
  184.  
  185. //TODO: remove these lines
  186. bitmapImage image(horizPixels, vertPixels);
  187. for(int y = 0; y < vertPixels; y++) {
  188. for(int x = 0; x < horizPixels; x++) {
  189. image.setPixelRGB(x,y,16777216*result[x][y]/maxiter % 256, 65536*result[x][y]/maxiter % 256, 256*result[x][y]/maxiter % 256);
  190. //std::cout << std::setw(2) << std::setfill('0') << std::hex << result[x][y] << " ";
  191. }
  192. std::cout << std::endl;
  193. }
  194.  
  195. image.saveToBitmapFile("~/Desktop/test.bmp",32);
  196. }
Add Comment
Please, Sign In to add comment