Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib> //for atoi
- #include <iostream>
- #include <iomanip> //for setw and setfill
- #include <vector>
- int DoThread(const double x, const double y, int maxiter) {
- double curX,curY,xSquare,ySquare;
- int i;
- curX = x + x*x - y*y;
- curY = y + x*y + x*y;
- ySquare = curY*curY;
- xSquare = curX*curX;
- for (i=0; i<maxiter && ySquare + xSquare < 4;i++) {
- ySquare = curY*curY;
- xSquare = curX*curX;
- curY = y + curX*curY + curX*curY;
- curX = x - ySquare + xSquare;
- }
- return i;
- }
- void SingleThreaded(int horizPixels, int vertPixels, int maxiter, std::vector<std::vector<int> >& result) {
- for(int x = horizPixels; x > 0; x--) {
- for(int y = vertPixels; y > 0; y--) {
- //3.0 -> so we always have -1.5 -> 1.5 as the window; (x - (horizPixels / 2) will go from -horizPixels/2 to +horizPixels/2
- result[x-1][y-1] = DoThread((3.0 / horizPixels) * (x - (horizPixels / 2)),(3.0 / vertPixels) * (y - (vertPixels / 2)),maxiter);
- }
- }
- }
- int main(int argc, char* argv[]) {
- //first arg = length along horizontal axis
- int horizPixels = atoi(argv[1]);
- //second arg = length along vertical axis
- int vertPixels = atoi(argv[2]);
- //third arg = iterations
- int maxiter = atoi(argv[3]);
- //fourth arg = threads
- int threadCount = atoi(argv[4]);
- std::vector<std::vector<int> > result(horizPixels, std::vector<int>(vertPixels,0)); //create and init 2-dimensional vector
- SingleThreaded(horizPixels, vertPixels, maxiter, result);
- //TODO: remove these lines
- for(int y = 0; y < vertPixels; y++) {
- for(int x = 0; x < horizPixels; x++) {
- std::cout << std::setw(2) << std::setfill('0') << std::hex << result[x][y] << " ";
- }
- std::cout << std::endl;
- }
- }
- #include <cstdlib> //for atoi
- #include <iostream>
- #include <iomanip> //for setw and setfill
- #include <vector>
- #include "bitmap_Image.h" //for saving the mandelbrot as a bmp
- #include <pthread.h>
- pthread_mutex_t mutexCounter;
- int sharedCounter(0);
- int percent(0);
- int horizPixels(0);
- int vertPixels(0);
- int maxiter(0);
- //doesn't need to be locked
- std::vector<std::vector<int> > result; //create 2 dimensional vector
- void *DoThread(void *null) {
- double curX,curY,xSquare,ySquare,x,y;
- int i, intx, inty, counter;
- counter = 0;
- do {
- counter++;
- pthread_mutex_lock (&mutexCounter); //lock
- intx = int((sharedCounter / vertPixels) + 0.5);
- inty = sharedCounter % vertPixels;
- sharedCounter++;
- pthread_mutex_unlock (&mutexCounter); //unlock
- //exit thread when finished
- if (intx >= horizPixels) {
- std::cout << "exited thread - I did " << counter << " calculations" << std::endl;
- pthread_exit((void*) 0);
- }
- //set x and y to the correct value now -> in the range like singlethread
- x = (3.0 / horizPixels) * (intx - (horizPixels / 1.5));
- y = (3.0 / vertPixels) * (inty - (vertPixels / 2));
- curX = x + x*x - y*y;
- curY = y + x*y + x*y;
- ySquare = curY*curY;
- xSquare = curX*curX;
- for (i=0; i<maxiter && ySquare + xSquare < 4;i++){
- ySquare = curY*curY;
- xSquare = curX*curX;
- curY = y + curX*curY + curX*curY;
- curX = x - ySquare + xSquare;
- }
- result[intx][inty] = i;
- } while (true);
- }
- int DoSingleThread(const double x, const double y) {
- double curX,curY,xSquare,ySquare;
- int i;
- curX = x + x*x - y*y;
- curY = y + x*y + x*y;
- ySquare = curY*curY;
- xSquare = curX*curX;
- for (i=0; i<maxiter && ySquare + xSquare < 4;i++){
- ySquare = curY*curY;
- xSquare = curX*curX;
- curY = y + curX*curY + curX*curY;
- curX = x - ySquare + xSquare;
- }
- return i;
- }
- void SingleThreaded(std::vector<std::vector<int> >& result) {
- for(int x = horizPixels - 1; x != -1; x--) {
- for(int y = vertPixels - 1; y != -1; y--) {
- //3.0 -> so we always have -1.5 -> 1.5 as the window; (x - (horizPixels / 2) will go from -horizPixels/2 to +horizPixels/2
- result[x][y] = DoSingleThread((3.0 / horizPixels) * (x - (horizPixels / 1.5)),(3.0 / vertPixels) * (y - (vertPixels / 2)));
- }
- }
- }
- void MultiThreaded(int threadCount, std::vector<std::vector<int> >& result) {
- /* Initialize and set thread detached attribute */
- pthread_t thread[threadCount];
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
- for (int i = 0; i < threadCount - 1; i++) {
- pthread_create(&thread[i], &attr, DoThread, NULL);
- }
- std::cout << "all threads created" << std::endl;
- for(int i = 0; i < threadCount - 1; i++) {
- pthread_join(thread[i], NULL);
- }
- std::cout << "all threads joined" << std::endl;
- }
- int main(int argc, char* argv[]) {
- //first arg = length along horizontal axis
- horizPixels = atoi(argv[1]);
- //second arg = length along vertical axis
- vertPixels = atoi(argv[2]);
- //third arg = iterations
- maxiter = atoi(argv[3]);
- //fourth arg = threads
- int threadCount = atoi(argv[4]);
- result = std::vector<std::vector<int> >(horizPixels, std::vector<int>(vertPixels,21)); // init 2-dimensional vector
- if (threadCount <= 1) {
- SingleThreaded(result);
- } else {
- MultiThreaded(threadCount, result);
- }
- //TODO: remove these lines
- bitmapImage image(horizPixels, vertPixels);
- for(int y = 0; y < vertPixels; y++) {
- for(int x = 0; x < horizPixels; x++) {
- image.setPixelRGB(x,y,16777216*result[x][y]/maxiter % 256, 65536*result[x][y]/maxiter % 256, 256*result[x][y]/maxiter % 256);
- //std::cout << std::setw(2) << std::setfill('0') << std::hex << result[x][y] << " ";
- }
- std::cout << std::endl;
- }
- image.saveToBitmapFile("~/Desktop/test.bmp",32);
- }
Add Comment
Please, Sign In to add comment