Commits

Jay Barra committed 6c9dcb4

other changes requried for build

Comments (0)

Files changed (10)

 #define WORK_TAG    1001
 //---------------------------------------------------------------------------
 #define GAMMA 0.2
-#define GENERATIONS 1000
+#define GENERATIONS 16 
 //---------------------------------------------------------------------------
 struct Position {
     float x;
       } else {
         beta = 0;
       }
-       vFireflies[id].current_pos.x += beta * exp(-1 * GAMMA * dx * dx) + ((float)(rand() % 1000)/ 1000.0);
-       vFireflies[id].current_pos.y += beta * exp(-1 * GAMMA * dy * dy) + ((float)(rand() % 1000)/ 1000.0);
+      
+      vFireflies[id].current_pos.x += beta * exp(-1 * GAMMA * dx * dx) + ((float)(rand() % 1000)/ 1000.0);
+      vFireflies[id].current_pos.y += beta * exp(-1 * GAMMA * dy * dy) + ((float)(rand() % 1000)/ 1000.0);
+      if (bRoot) {
+        printf("calc:%f dx: %f  dy:%f  dist:%f  newx:%f  newy:%f\n", beta * exp(-1 * GAMMA * dx * dx), dx, dy, dist, vFireflies[id].current_pos.x, vFireflies[id].current_pos.y);
+      }
 
       fitness(vFireflies[id]);
-      //printf("[%i (%f, %f) -> %f\n", id, vFireflies[id].current_pos.x, vFireflies[id].current_pos.y, vFireflies[id].current_fitness);
     
     }
-      MPI_Bcast(&vFireflies[id].current_pos.x,   1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
-      MPI_Bcast(&vFireflies[id].current_pos.y,   1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
-      MPI_Bcast(&vFireflies[id].current_fitness, 1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
+    for (int i = 1; i < iNodes; i++) {
+      if (bRoot) {
+        MPI_Recv(&vFireflies[i].current_pos.x,   1, MPI_FLOAT, i, MESSAGE_TAG, MPI_COMM_WORLD, &status);
+        MPI_Recv(&vFireflies[i].current_pos.y,   1, MPI_FLOAT, i, MESSAGE_TAG, MPI_COMM_WORLD, &status);
+        MPI_Recv(&vFireflies[i].current_fitness, 1, MPI_FLOAT, i, MESSAGE_TAG, MPI_COMM_WORLD, &status);
+      } else {
+        if (i != id) continue;
+        MPI_Send(&vFireflies[id].current_pos.x,   1, MPI_FLOAT, ROOT, MESSAGE_TAG, MPI_COMM_WORLD);
+        MPI_Send(&vFireflies[id].current_pos.y,   1, MPI_FLOAT, ROOT, MESSAGE_TAG, MPI_COMM_WORLD);
+        MPI_Send(&vFireflies[id].current_fitness, 1, MPI_FLOAT, ROOT, MESSAGE_TAG, MPI_COMM_WORLD);
+      }
+ 
+      for(int i = 0; i < iNodes; i++) {
+        MPI_Bcast(&vFireflies[i].current_pos.x,   1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
+        MPI_Bcast(&vFireflies[i].current_pos.y,   1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
+        MPI_Bcast(&vFireflies[i].current_fitness, 1, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
+        if(bRoot) {
+          //printf("%i (%f, %f) -> %f\n", i, vFireflies[i].current_pos.x, vFireflies[i].current_pos.y, vFireflies[i].current_fitness);
+        }
+      }
+      if (bRoot) { printf("\n"); }
+      
+
+    }
   //MPI_Barrier(MPI_COMM_WORLD); 
   }
 
     }
   }
   if (bRoot) {
-    printf("And the winner is: %i (%f, %f) -> %f\n", iBest, vFireflies[iBest].best_pos.x, vFireflies[iBest].best_pos.y, vFireflies[iBest].best_fitness);
-  }
+    printf("And the winner is: %i (%f, %f) -> %f\n", iBest, vFireflies[iBest].best_pos.x, vFireflies[iBest].best_pos.y, vFireflies[iBest].best_fitness); }
  
   MPI_Finalize();
   

PSO/.gitignore

File contents unchanged.
File contents unchanged.

PSO/cluster

-node1.cs.sunyit.edu
-node2.cs.sunyit.edu
-node3.cs.sunyit.edu
-node4.cs.sunyit.edu
-node5.cs.sunyit.edu
-node6.cs.sunyit.edu
-node7.cs.sunyit.edu
-node8.cs.sunyit.edu
-
 TARGET=mpi
 CC=mpicxx
-HEADERS=mpiNode.h \
-	particle.h
+HEADERS=
+SOURCES=main.cpp 
 
-SOURCES=main.cpp \
-        mpiNode.cpp \
-	particle.cpp
-
-MPI_BASE=/home/undergrad/barraj/mpich2-1.4.1p1
+MPI_BASE=/home/f/csci/scott/local/mpich2-1.4.1p1_BUILD/src/mpe2
 
 INCLUDEPATH=$(MPI_BASE)/include
 MPI_LIBS=$(MPI_BASE)/lib

PSO/mpiNode.cpp

-#include "mpiNode.h"
-//-------------------------------------------------
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <limits>
-#include <time.h>
-//-------------------------------------------------
-mpiNode::mpiNode(int argc, char** argv)
-{
-	int iNameLen;
-	char* m_pcName  = (char*)malloc(1024);
-	char* pcMessage = (char*)malloc(1024);
-	char* pcStatus  = (char*)malloc(1024);
-	int iSource;
-	int event1a, event1b;
-	int event2a, event2b;
-	
-	MPI_Status status;
-	
-	myStatus = MPE_Log_get_state_eventIDs(&event1a, &event1b);
-	
-	m_ID     = MPI::COMM_WORLD.Get_rank();
-	m_bRoot  = (m_ID == 0);
-	m_iNodes = MPI::COMM_WORLD.Get_size() - 1; // subtract 1 to not count the root node
-
-	MPI_Get_processor_name(m_pcName, &iNameLen);
-	if (!m_bRoot) {
-		sprintf(pcMessage, "%i", m_ID, m_pcName);
-		send(pcMessage, ROOT);
-	} else {
-		printf("\nInitializing Nodes\n");
-		printf("*********************************************************\n");
-		printf("Root Node: %s\n", m_pcName);
-		printf("Awaiting worker node responses...\n");
-		for (iSource = 1; iSource <= m_iNodes; iSource++) {
-			MPI_Recv(pcMessage, 1024, MPI_CHAR, iSource, MESSAGE_TAG, MPI_COMM_WORLD, &status);
-			getStatus(status, pcStatus);
-			printf("AgentNode: %s\t| Status: %s\n", pcMessage, pcStatus);
-		}
-		printf("Node initialization complete...\n");
-		printf("%i Nodes Registered for work...\n", m_iNodes);
-		printf("*********************************************************\n");
-
-		MPE_Describe_state(event1a, event1b, "Root", "red");
-		MPE_Describe_state(event2a, event2b, "worker", "green");
-	}
-	free(pcMessage);
-	free(pcStatus);
-	MPI_Barrier(MPI_COMM_WORLD);
-}
-//-------------------------------------------------
-mpiNode::~mpiNode()
-{
-}
-//-------------------------------------------------
-void mpiNode::init()
-{
-	srand(time(NULL));
-	if (m_bRoot) {
-		printf("initializing swarm...\n");
-	}
-	for (int i = 0; i < 1; i++) {
-		particle* p = new particle();
-		p->setPos(rand() / (float)std::numeric_limits<int>::max());
-		p->setVel(rand() / (float)std::numeric_limits<int>::max());
-		m_vParticles.push_back(p);
-		printf("particle: %f %f\n", p->position(), p->velocity());
-	}
-	MPI_Barrier(MPI_COMM_WORLD);
-}
-//-------------------------------------------------
-void mpiNode::printParticles()
-{
-	if (m_bRoot) {	
-		printf("*********************************************************\n");
-	}
-	std::list<particle*>::iterator it;
-	for (it = m_vParticles.begin(); it != m_vParticles.end(); it++) {
-	
-		printf("%f ", (*it)->position());	
-	}
-}
-//-------------------------------------------------
-int mpiNode::id()
-{
-	return m_ID;
-}
-//-------------------------------------------------
-char* mpiNode::name()
-{
-	return m_pcName;
-}
-//-------------------------------------------------
-int mpiNode::send(char* pcMessage, int iDest)
-{
-	int rc;
-	rc = MPI_Send(pcMessage, strlen(pcMessage)+1, MPI_CHAR, iDest, MESSAGE_TAG, MPI_COMM_WORLD);
-	return rc;
-}
-//------------------------------------------------
-void mpiNode::getStatus(MPI_Status status, char* pcStatus)
-{
-	switch (status.MPI_ERROR) {
-		case MPI_SUCCESS:
-			sprintf(pcStatus, "Success");
-			break;
-		case MPI_ERR_BUFFER:
-			sprintf(pcStatus, "Buffer Error");
-			break;
-		case MPI_ERR_COUNT:
-			sprintf(pcStatus, "Count Error");
-			break;
-		case MPI_ERR_TYPE:
-			sprintf(pcStatus, "Type Error");
-			break;
-		case MPI_ERR_TAG:
-			sprintf(pcStatus, "Tag Error");
-			break;
-		case MPI_ERR_COMM:
-			sprintf(pcStatus, "COMM Error");
-			break;
-		case MPI_ERR_RANK:
-			sprintf(pcStatus, "Rank Error");
-			break;
-		case MPI_ERR_REQUEST:
-			sprintf(pcStatus, "Request Error");
-			break;
-		case MPI_ERR_ROOT:
-			sprintf(pcStatus, "Root Error");
-			break;
-		case MPI_ERR_GROUP:
-			sprintf(pcStatus, "Group Error");
-			break;	
-		case MPI_ERR_OP:
-			sprintf(pcStatus, "OP Error");
-			break;
-		case MPI_ERR_TOPOLOGY:
-			sprintf(pcStatus, "Topology Error");
-			break;
-		case MPI_ERR_DIMS:
-			sprintf(pcStatus, "DIMS Error");
-			break;
-		case MPI_ERR_ARG:
-			sprintf(pcStatus, "Args Error");
-			break;
-		case MPI_ERR_UNKNOWN:
-			sprintf(pcStatus, "Unknown Error");
-			break;
-		case MPI_ERR_TRUNCATE:
-			sprintf(pcStatus, "Truncate Error");
-			break;	
-		case MPI_ERR_OTHER:
-			sprintf(pcStatus, "Other Error");
-			break;	
-		case MPI_ERR_INTERN:
-			sprintf(pcStatus, "Internal Error");
-			break;
-		case MPI_ERR_IN_STATUS:
-			sprintf(pcStatus, "In Status Error");
-			break;
-		case MPI_ERR_PENDING:
-			sprintf(pcStatus, "Pending Error");
-			break;
-		default:
-			sprintf(pcStatus, "Unknown Error");
-	}
-}
-//------------------------------------------------
-void mpiNode::run()
-{
-	int iSize = 8;
-	double** pdSend = (double**)malloc(iSize * iSize * sizeof(double)); 
-	double** pdRecv = (double**)malloc(iSize * iSize * sizeof(double));
-	
-	for (int i = 0; i < iSize; i++) {
-		pdSend[i] = (double*)malloc(iSize * sizeof(double));
-		pdRecv[i] = (double*)malloc(iSize * sizeof(double));
-	}
-
-	MPI_Scatter(*pdSend, iSize, MPI_DOUBLE, *pdRecv, iSize, MPI_DOUBLE, ROOT, MPI_COMM_WORLD);
-	
-	if (m_ID != ROOT) {
-	for (int i = 0; i < iSize; i++) {
-		for (int m = 0; m < iSize; m++) {
-			pdRecv[i][m] = (double)(100 * i) / iSize + m_ID;
-		}
-	} 
-	printf("\n\n");
-	for (int i = 0; i < iSize; i++) {
-		printf("( ");
-		for (int m = 0; m < iSize; m++) {	
-			printf("%f ", pdRecv[i][m]);
-		}
-		printf(") -> %i\n", m_ID);
-	}
-	}
-	
-	MPI_Gather(pdRecv, iSize, MPI_DOUBLE, pdSend, iSize * iSize, MPI_DOUBLE, ROOT, MPI_COMM_WORLD);
-
-
-	if (m_ID == ROOT) {
-		for (int k = 0; k < iSize; k++) {
-			printf("[ ");	
-			for (int l = 0; l < iSize; l++) {
-				printf("%f ", pdSend[k][l]);
-			}
-			printf("]\n");
-		}	
-	}
-	
-	free(*pdSend);
-	free(pdRecv);
-}
-//------------------------------------------------
-

PSO/mpiNode.h

-#ifndef MPI_NODE_H
-#define MPI_NODE_H
-//------------------------------------------------------------
-#include <mpi.h>
-#include <stdlib.h>
-#include <cstdlib>
-#include <list>
-//------------------------------------------------------------
-#include "particle.h"
-//------------------------------------------------------------
-#define ROOT 0
-#define MESSAGE_TAG 1000
-#define WORK_TAG 2000
-//------------------------------------------------------------
-class particle;
-//------------------------------------------------------------
-class mpiNode
-{
-public:
-	mpiNode(int argc, char** argv);
-	~mpiNode();
-
-	int id();
-	char* name();
-
-	int send(char* pcMessage, int iDest);
-
-	void getStatus(MPI_Status status, char* pcStatus); 
-	void run();
-	void init();
-
-	void printParticles();
-	
-protected:
-	int m_iNodes;
-	int m_ID;
-	char* m_pcName;
-	bool m_bRoot;
-
-	std::list<particle*> m_vParticles;
-};
-//------------------------------------------------------------
-#endif
-

PSO/particle.cpp

-#include "particle.h"
-//------------------------------------------------
-#include <stdlib.h>
-#include <stdio.h>
-//------------------------------------------------
-particle::particle()
-{
-	m_fPos = 0.0f;
-	m_fVel = 0.0f;
-}
-//-----------------------------------------------
-particle::~particle()
-{
-}
-//-----------------------------------------------
-void particle::setPos(float fPos)
-{
-	m_fPos = fPos;
-}
-//-----------------------------------------------
-void particle::setVel(float fVel)
-{
-	m_fVel = fVel;
-}
-//----------------------------------------------
-float particle::position()
-{
-	return m_fPos;
-}
-//----------------------------------------------
-float particle::velocity()
-{
-	return m_fVel;
-}

PSO/particle.h

-#ifndef PARTICLE_H
-#define PARTICLE_H
-//-----------------------------------------
-//-----------------------------------------
-class particle
-{
-public:
-	particle();
-	~particle();
-
-	void setPos(float fPos);
-	void setVel(float fVel);
-
-	float position();
-	float velocity(); 
-
-private:
-	float m_fPos;
-	float m_fVel;
-};
-//-----------------------------------------
-#endif
-
 #!/usr/local/bin/tcsh
 clear
-mpirun -l -machinefile cluster -np 8 $PWD/mpi
+mpirun -np 8 ~/cs523/Firefly/mpi -machinefile cluster.hosts