Created
May 8, 2013 16:43
-
-
Save anonymous/5541760 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <iostream> | |
#include <time.h> | |
#include <pthread.h> | |
using namespace std; | |
int imageWidth = 800; | |
int imageHeight = 800; | |
double realMin = -2.0; | |
double realMax = 0.3; | |
double imagMin = -1.0; | |
double imagMax = 1.3; | |
int numThreads = 1; | |
// the mandelbrot values will be stored in mandelPlot before the image is generated | |
int mandelPlot[800][800] = {0}; | |
// takes a complex number and returns -1 if the point is within the set, | |
// otherwise it returns how many iterations it took for the point to escape | |
int isInMandelbrot(double imaginaryPart, double realPart) | |
{ | |
double real = realPart; | |
double imag = imaginaryPart; | |
double zReal = 0; | |
double zImag = 0; | |
double newZReal; | |
double newZImag; | |
int iterations = 0; | |
while((zReal*zReal + zImag*zImag < 4) && iterations < 300) | |
{ | |
newZReal = zReal*zReal - zImag*zImag + real; | |
newZImag = 2*zReal*zImag + imag; | |
zReal = newZReal; | |
zImag = newZImag; | |
iterations++; | |
} | |
if(iterations == 300) | |
iterations = -1; | |
return iterations; | |
} | |
// takes a column number and converts it into a real value | |
double colToRealValue(int col) | |
{ | |
double result = realMin + ((realMax - realMin + 0.5)*col)/imageWidth; | |
return result; | |
} | |
// takes a row number and converts it into an imaginary value | |
double rowToImagValue(int row) | |
{ | |
double result = imagMax - ((imagMax - imagMin + 0.5)*row)/imageHeight; | |
return result; | |
} | |
// calculates all the mandelValues for a row and stores them in mandelPlot | |
void calculateRow(int row) | |
{ | |
for (int col = 0; col < imageWidth; col++) | |
{ | |
mandelPlot[row][col] = isInMandelbrot(rowToImagValue(row), colToRealValue(col)); | |
} | |
} | |
// launched by each pthread, calculates the initial row, | |
// followed by initial row + numThreads, followed by initial row + 2*numThreads, etc. | |
void *calculateRows(void *initialRowNum) | |
{ | |
int* row; | |
row = (int*)initialRowNum; | |
for (*row; *row < imageHeight; *row += numThreads) | |
{ | |
calculateRow(*row); | |
} | |
} | |
int main() | |
{ | |
// Start timing | |
clock_t t1,t2; | |
t1 = clock(); | |
// Start the threads | |
int rc; | |
pthread_t threads[numThreads]; | |
pthread_attr_t attr; | |
void *status; | |
pthread_attr_init(&attr); | |
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); | |
printf("\nPoint 1\n"); | |
for (int i = 0; i < numThreads; i++) | |
{ | |
rc = pthread_create(&threads[i], NULL, calculateRows, (void *)i); | |
if (rc){ | |
cout << "Error: unable to create thread," << rc << endl; | |
//exit(-1); | |
} | |
} | |
printf("\nPoint 2\n"); | |
//join the threads | |
pthread_attr_destroy(&attr); | |
for (int i = 0; i < numThreads; i++) | |
{ | |
printf("\nPoint 3\n"); | |
rc = pthread_join(threads[i], NULL); | |
printf("\nPoint 4\n"); | |
} | |
// Stop timing | |
t2=clock(); | |
// Calculate the time to fill mandelPlot with mandelValues | |
float diff ((float)t2-(float)t1); | |
printf( "\nMandelbrot Time: %f seconds\n", diff / CLOCKS_PER_SEC); | |
printf( "\nNumber of Threads: %i \n\n", numThreads); | |
pthread_exit(NULL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment