From: Sam Moore Date: Thu, 6 Sep 2012 09:46:40 +0000 (+0800) Subject: Parallel Programming - Make single threaded version X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=1612d2449b7c9e4fa202adac00cda23700df44c7;p=matches%2Fhonours.git Parallel Programming - Make single threaded version Trying to modify the template program so that it is a) actually organised b) Not as over engineered as my own single threaded version (assignment0) Anyway, it doesn't compile but at least I started Sam (who else?) --- diff --git a/course/semester2/pprog/assignment1/Makefile b/course/semester2/pprog/assignment1/Makefile index cd6f0fee..b1a9b132 100644 --- a/course/semester2/pprog/assignment1/Makefile +++ b/course/semester2/pprog/assignment1/Makefile @@ -1,45 +1,20 @@ -INCLUDE_PATH = -I/usr/include -LIBRARY_PATH = -L/usr/lib64 -CCFLAGS = -std=c99 #-framework GLUT -framework OpenGL -framework Cocoa -LDFLAGS = -lglut -lGL -lGLU -lpthread -lm +#Makefile for nbody program -# the directories containing the OpenGL libraries, f90gl libraries, GLUT -# libraries, and f90gl GLUT libraries -OGLLIBDIR = -L/usr/X11/lib +CXX = gcc --std=c99 -Wall -pedantic -g -lm -lGL -lglut -lGLU -lpthread -fopenmp +LINK_OBJ = main.o nbody.o graphics.o -# the fortran 90 libraries for OpenGL, including GLUT, GLU and OpenGL -F90GLUTLIB = -lf90glut -lf90GLU -lf90GL +BIN = nbody -# the X11 libraries -X11LIB = -framework GLUT -framework OpenGL -framework Cocoa +$(BIN) : $(OBJ) + $(CXX) -o $(BIN) $(OBJ) -# the f90 compiler flag for specifying the location of MOD files -MODS = -I/usr/X11/include/GL +%.o : %.c + $(CXX) -c $< -# the directory containing the X11 libraries -X11LIBDIR = - -# fortran 90 compiler and compiler flags -F95 = g95 -F90 = /usr/local/gfortran/bin/gfortran -F90FLAGS = -O -F90FLAGS2 = -fopenmp -O -GCC = gcc - -APP = nbody - -all: $(APP) - @echo Make done - -#%: %.c -# gcc $^ -o $@ $(INCLUDE_PATH) $(CCFLAGS) $(LIBRARY_PATH) $(LDFLAGS) - -clean: - @rm -f *.o $(APP) - -nbodyf: nbodyf.f90 - $(F90) nbodyf.f90 -o nbodyf $(F90FLAGS) $(MODS) $(OGLLIBDIR) $(F90GLUTLIB) $(X11LIBDIR) $(X11LIB) - -nbody: nbody.c - $(GCC) -o nbody nbody.c $(INCLUDE_PATH) $(CCFLAGS) $(LIBRARY_PATH) $(LDFLAGS) +clean : + $(RM) $(BIN) $(OBJ) $(LINKOBJ) +clean_full: #cleans up all backup files + $(RM) $(BIN) $(OBJ) $(LINKOBJ) + $(RM) *.*~ + $(RM) *~ diff --git a/course/semester2/pprog/assignment1/description.pdf b/course/semester2/pprog/assignment1/description.pdf new file mode 100644 index 00000000..8d86c96d Binary files /dev/null and b/course/semester2/pprog/assignment1/description.pdf differ diff --git a/course/semester2/pprog/assignment1/graphics.c b/course/semester2/pprog/assignment1/graphics.c new file mode 100644 index 00000000..62711be6 --- /dev/null +++ b/course/semester2/pprog/assignment1/graphics.c @@ -0,0 +1,70 @@ +/** + * @file graphics.c + * @author Sam Moore (20503628) 2012 - adapted from template program provided by UWA + * @pur + +/* + * Initialization of graphics + */ +void Init(void) { + + glClearColor(1.0,1.0,1.0,0.0); + glColor3f(0.0f, 0.0f, 0.0f); + glPointSize(POINT_SIZE); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + /*init lighting */ + + GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; + GLfloat mat_shininess[] = { 50.0 }; + GLfloat light_position[] = { 1.0, 1.0, 0.0, 0.0 }; + glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); + glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); + glLightfv(GL_LIGHT0, GL_POSITION, light_position); + + glColorMaterial(GL_FRONT,GL_DIFFUSE); // Set Color Capability + + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glEnable(GL_DEPTH_TEST); + + glEnable(GL_COLOR_MATERIAL); // Enable color + + double displayRatio = 1.0 * WIDTH / HEIGHT; + windowWidth = WIDTH; + windowHeight = HEIGHT; + previousTime = clock(); + eyeTheta = 0; + eyePhi = 0.5 * M_PI; + eyeRho = RHO; + upY = 1; + look[0] = 0; + look[1] = 0; + look[2] = 0; + gluPerspective(VIEW_ANGLE, displayRatio, WORLD_NEAR, WORLD_FAR); +} + +/* + * This function redraws the screen after the positions of particles + * have been updated + */ +void Display(void) { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + gluLookAt(eyeRho * sin(eyePhi) * sin(eyeTheta), eyeRho * cos(eyePhi), + eyeRho * sin(eyePhi) * cos(eyeTheta), + look[0], look[1], look[2], 0, upY, 0); + + for (int i = 0; i < N; i++) { + //glClearColor(1.0,1.0,1.0,0.0); + glColor3f(0.0f, body[i].mass/1e11*100, 0.0f); + //glColor3f(1.0f, 0.0f, 0.0f); + glPushMatrix(); // to save the current matrix + glTranslated(SCALE*body[i].X, SCALE*body[i].Y, SCALE*body[i].Z); + glutSolidSphere (BALL_SIZE, 10, 10); + glPopMatrix(); // restore the previous matrix + } + glFlush(); +} diff --git a/course/semester2/pprog/assignment1/graphics.h b/course/semester2/pprog/assignment1/graphics.h new file mode 100644 index 00000000..01ca839a --- /dev/null +++ b/course/semester2/pprog/assignment1/graphics.h @@ -0,0 +1,40 @@ +#ifndef _GRAPHICS_H +#define _GRAPHICS_H + +/** + * @file graphics.h + * @author Sam Moore (20503628) 2012 - adapted from template program provided by UWA + * @purpose N-Body simulator - declarations of all graphics related functions + * NOTE: I prefer to keep graphics entirely seperate from the simulation as much as possible, hence seperate files + */ + +#include +#include + +#include "nbody.h" + +#define SCREEN_WIDTH 800 +#define SCREEN_HEIGHT 800 +#define POINT_SIZE 1 +#define POSITION_X 112 +#define POSITION_Y 20 +#define WORLD_LEFT -10000 +#define WORLD_RIGHT 10000 +#define WORLD_BOTTOM -10000 +#define WORLD_TOP 10000 +#define VIEW_ANGLE 45 +#define RHO 100 +#define WORLD_NEAR 0.1 +#define WORLD_FAR 1000000 +#define BALL_SIZE 0.5 +#define REFRESH_RATE 0.001 +#define LINE_SIZE 1000 + +void Graphics_Init(void); +void Graphics_Display(System * s); +void Graphics_Keyboard(unsigned char key, int mouse_x, int mouse_y); +void Graphics_Reshape(int width, int height); + +#endif //_GRAPHICS_H + +//EOF diff --git a/course/semester2/pprog/assignment1/main.c b/course/semester2/pprog/assignment1/main.c new file mode 100644 index 00000000..28e6409b --- /dev/null +++ b/course/semester2/pprog/assignment1/main.c @@ -0,0 +1,6 @@ +#include +#include +#include "nbody.h" +#include "graphics.h" + + diff --git a/course/semester2/pprog/assignment1/nbody.c b/course/semester2/pprog/assignment1/nbody.c index 5891d141..07761607 100644 --- a/course/semester2/pprog/assignment1/nbody.c +++ b/course/semester2/pprog/assignment1/nbody.c @@ -1,7 +1,8 @@ -// nbody.cpp : Template program for N-body -// Copyright UWA, 2012 - -#define _ANSI_SOURCE +/** + * @file nbody.c + * @author Sam Moore (20503628) 2012 + * @purpose N-Body simulator - Definition of simulation functions; single threaded version + */ #include #include @@ -12,42 +13,6 @@ #include #include -#define M_PI 3.14159265358979323846264338327950288 /* pi */ -#define WIDTH 800 -#define HEIGHT 800 -#define POINT_SIZE 1 -#define POSITION_X 112 -#define POSITION_Y 20 -#define WORLD_LEFT -10000 -#define WORLD_RIGHT 10000 -#define WORLD_BOTTOM -10000 -#define WORLD_TOP 10000 -#define VIEW_ANGLE 45 -#define RHO 100 -#define WORLD_NEAR 0.1 -#define WORLD_FAR 1000000 -#define BALL_SIZE 0.5 -#define REFRESH_RATE 0.001 -#define LINE_SIZE 1000 -#define G 6.67428E-11 -#define DELTA_T 0.05 - -#define square(x) ((x)*(x)) - -typedef struct { - double mass; - double X; - double Y; - double Z; - double Vx; - double Vy; - double Vz; - double Fx; - double Fy; - double Fz; - int color; -} Particle; - double previousTime, eyeTheta, eyePhi, eyeRho; float look[3]; int windowWidth, windowHeight, upY; @@ -58,71 +23,7 @@ Particle *body; int N; int numberOfProcessors=1; -/* - * Initialization of graphics - */ -void Init(void) { - - glClearColor(1.0,1.0,1.0,0.0); - glColor3f(0.0f, 0.0f, 0.0f); - glPointSize(POINT_SIZE); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - - /*init lighting */ - - GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; - GLfloat mat_shininess[] = { 50.0 }; - GLfloat light_position[] = { 1.0, 1.0, 0.0, 0.0 }; - glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); - glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); - glLightfv(GL_LIGHT0, GL_POSITION, light_position); - glColorMaterial(GL_FRONT,GL_DIFFUSE); // Set Color Capability - - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - glEnable(GL_DEPTH_TEST); - - glEnable(GL_COLOR_MATERIAL); // Enable color - - double displayRatio = 1.0 * WIDTH / HEIGHT; - windowWidth = WIDTH; - windowHeight = HEIGHT; - previousTime = clock(); - eyeTheta = 0; - eyePhi = 0.5 * M_PI; - eyeRho = RHO; - upY = 1; - look[0] = 0; - look[1] = 0; - look[2] = 0; - gluPerspective(VIEW_ANGLE, displayRatio, WORLD_NEAR, WORLD_FAR); -} - -/* - * This function redraws the screen after the positions of particles - * have been updated - */ -void Display(void) { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - gluLookAt(eyeRho * sin(eyePhi) * sin(eyeTheta), eyeRho * cos(eyePhi), - eyeRho * sin(eyePhi) * cos(eyeTheta), - look[0], look[1], look[2], 0, upY, 0); - - for (int i = 0; i < N; i++) { - //glClearColor(1.0,1.0,1.0,0.0); - glColor3f(0.0f, body[i].mass/1e11*100, 0.0f); - //glColor3f(1.0f, 0.0f, 0.0f); - glPushMatrix(); // to save the current matrix - glTranslated(SCALE*body[i].X, SCALE*body[i].Y, SCALE*body[i].Z); - glutSolidSphere (BALL_SIZE, 10, 10); - glPopMatrix(); // restore the previous matrix - } - glFlush(); -} /* * Prints the body on screen diff --git a/course/semester2/pprog/assignment1/nbody.h b/course/semester2/pprog/assignment1/nbody.h new file mode 100644 index 00000000..37232051 --- /dev/null +++ b/course/semester2/pprog/assignment1/nbody.h @@ -0,0 +1,58 @@ +#ifndef _NBODY_H +#define _NBODY_H + +/** + * @file nbody.h + * @author Sam Moore (205030628) + * @purpose N-Body simulator: declarations of simulation related parameters + */ + +#define M_PI 3.14159265358979323846264338327950288 /* pi */ +#define G 6.67428E-11 +#define DELTA_T 0.05 +#define DIMENSIONS 3 +#define square(x) ((x)*(x)) + + +/** + * Structure to represent a single Body + * @param mass - Mass of the body + * @param x - Position vector (array) + * @param v - Velocity vector (array) + * @param F - Net force vector (array) + */ +typedef struct +{ + + double mass; + double x[DIMENSIONS]; + double v[DIMENSIONS]; + double F[DIMENSIONS]; + int colour; + +} Body; + +/** + * Structure to store an array of bodies, along with the size of the array + * @param N - Size of the array + * @param body - The array of bodies + */ +typedef struct +{ + unsigned N; + Body * body; + +} System; + +void Body_Print(Body * a); //Print body a +void Body_Force(Body * a, System * s); //Compute force on body a due to system of bodies s +void Body_Velocity(Body * a); //Compute velocity of body a +void Body_Position(Body * a); //Compute position of body a + +System * System_Init(char * fileName); //Initialise System (array of bodies) from a text file + +void System_Step(System * system); //Perform a single computation step for a System + + + +#endif //_NBODY_H