converted computations to use the C++ complex<double> class

git-svn-id: svn://anubis/gvsu@325 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-12-07 23:23:24 +00:00
parent fc4cf4b420
commit 22f840d3d5
3 changed files with 20 additions and 9 deletions

View File

@ -1,7 +1,8 @@
#include <math.h> /* sqrt() */ #include <math.h> /* sqrt() */
#include <complex.h> /* complex, I, creal(), cimag() */ #include <complex>
#include "FatouComputation.h" #include "FatouComputation.h"
using namespace std;
#define LEEWAY 0.001 #define LEEWAY 0.001
#define ITERATIONS 100 #define ITERATIONS 100
@ -12,13 +13,13 @@ static unsigned int colors[] = {0xFF0000, 0x00FF00, 0x0000FF};
unsigned int FatouComputation::compute(double x, double y) unsigned int FatouComputation::compute(double x, double y)
{ {
double complex z = 0.1; complex<double> z(0.1);
double complex p = x + I * y; complex<double> p(x, y);
for (int iter = 0; iter < ITERATIONS; iter++) for (int iter = 0; iter < ITERATIONS; iter++)
{ {
z = p * (1 - z) * z; z = p * (complex<double>(1) - z) * z;
if (cabs(z) > 2.0) if (abs(z) > 2.0)
{ {
return colors[iter % (sizeof(colors)/sizeof(colors[0]))]; return colors[iter % (sizeof(colors)/sizeof(colors[0]))];
} }

View File

@ -1,12 +1,13 @@
#include <math.h> /* sqrt() */ #include <math.h> /* sqrt() */
#include <complex.h> /* complex, I, creal(), cimag() */ #include <complex>
#include "NewtonComputation.h" #include "NewtonComputation.h"
using namespace std;
#define LEEWAY 0.001 #define LEEWAY 0.001
#define ITERATIONS 32 #define ITERATIONS 32
#define CLOSE_ENOUGH(z, x, y) \ #define CLOSE_ENOUGH(z, x, y) \
(fabs(creal(z) - (x)) < LEEWAY && fabs(cimag(z) - (y)) < LEEWAY) (fabs(real(z) - (x)) < LEEWAY && fabs(imag(z) - (y)) < LEEWAY)
/* /*
* This Computation class generates a design in the complex plane * This Computation class generates a design in the complex plane
@ -15,7 +16,7 @@
*/ */
unsigned int NewtonComputation::compute(double x, double y) unsigned int NewtonComputation::compute(double x, double y)
{ {
double complex rootGuess = x + I * y; complex<double> rootGuess(x, y);
static double halfRoot3 = sqrt(3) / 2.0; static double halfRoot3 = sqrt(3) / 2.0;
for (int iter = 0; iter < ITERATIONS; iter++) for (int iter = 0; iter < ITERATIONS; iter++)
@ -61,6 +62,6 @@ unsigned int NewtonComputation::compute(double x, double y)
* More information can be found at: * More information can be found at:
* http://www.willamette.edu/~sekino/fractal/fractal.htm * http://www.willamette.edu/~sekino/fractal/fractal.htm
*/ */
rootGuess -= (cpow(rootGuess, 6.0) - 1.0) / (6.0 * cpow(rootGuess, 5.0)); rootGuess -= (pow(rootGuess, 6.0) - 1.0) / (6.0 * pow(rootGuess, 5.0));
} }
} }

View File

@ -147,6 +147,8 @@ int main(int argc, char * argv[])
/* slave loop */ /* slave loop */
for (;;) for (;;)
{ {
// DEBUG:
cout << "MPI node " << my_rank << " waiting for command." << endl;
/* wait for a redraw or quit command */ /* wait for a redraw or quit command */
MPI_Recv(&window_vals[0], 4, MPI_DOUBLE, MPI_Recv(&window_vals[0], 4, MPI_DOUBLE,
MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, NULL); MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, NULL);
@ -155,6 +157,9 @@ int main(int argc, char * argv[])
x_center = window_vals[1]; x_center = window_vals[1];
y_center = window_vals[2]; y_center = window_vals[2];
zoom = window_vals[3]; zoom = window_vals[3];
// DEBUG:
cout << "MPI node " << my_rank << " received ("
<< x_center << ", " << y_center << "), zoom " << zoom << endl;
draw(my_rank, world_size, nprocs, width, height, draw(my_rank, world_size, nprocs, width, height,
NULL, taskVals, computation); NULL, taskVals, computation);
} }
@ -226,6 +231,8 @@ void getSizes(int * rank, int * size, int * nprocs)
void draw(int rank, int world_size, int nprocs, int width, int height, void draw(int rank, int world_size, int nprocs, int width, int height,
Uint32 * pixels, Uint32 * taskVals, Computation * computation) Uint32 * pixels, Uint32 * taskVals, Computation * computation)
{ {
// DEBUG:
cout << "In draw() with rank " << rank << endl;
MPI_Status mpi_status; MPI_Status mpi_status;
if (world_size == 1) if (world_size == 1)
{ {
@ -302,6 +309,8 @@ void draw(int rank, int world_size, int nprocs, int width, int height,
void sendWindowVals(double * winVals, int world_size) void sendWindowVals(double * winVals, int world_size)
{ {
// DEBUG:
cout << "Master sending out new window values" << endl;
for (int to_proc = 1; to_proc < world_size; to_proc++) for (int to_proc = 1; to_proc < world_size; to_proc++)
{ {
MPI_Send(winVals, 4, MPI_DOUBLE, to_proc, MPI_Send(winVals, 4, MPI_DOUBLE, to_proc,