1. Jay Barra
  2. CS523

Commits

Jay Barra  committed 3801ffa Merge

Merge branch 'PSO' into mpe

  • Participants
  • Parent commits 0c4e3a4, def1496
  • Branches master

Comments (0)

Files changed (20)

File PSO/.gitignore

View file
  • Ignore whitespace
+# Compiled source #
+###################
+*.com
+*.class
+*.o
+*.so
+
+mpi
+mpi.core

File PSO/README

  • Ignore whitespace
Empty file added.

File PSO/cluster

View file
  • Ignore whitespace
+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
+

File PSO/main.cpp

View file
  • Ignore whitespace
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <mpi.h>
+//---------------------------------------------------------------------------
+#include "mpiNode.h"
+//---------------------------------------------------------------------------
+main (int argc, char** argv)
+{
+	MPI::Init(argc, argv);
+	
+	mpiNode* node = new mpiNode(argc, argv);
+	node->init();
+	node->printParticles();
+	//node->run();
+
+	MPI::Finalize();
+	delete node;
+	return 0;
+}
+//----------------------------------------------------------------------------
+

File PSO/makefile

View file
  • Ignore whitespace
+TARGET=mpi
+CC=mpicxx
+HEADERS=mpiNode.h \
+	particle.h
+
+SOURCES=main.cpp \
+        mpiNode.cpp \
+	particle.cpp
+
+MPI_BASE=/home/undergrad/barraj/mpich2-1.4.1p1
+
+INCLUDEPATH=$(MPI_BASE)/include
+MPI_LIBS=$(MPI_BASE)/lib
+
+LIBS=-lmpich
+
+LDFLAGS= 
+CFLAGS=-c -Wall
+OBJECTS=$(SOURCES:.cpp=.o)
+#----------------------------------------------
+all: $(SOURCES) $(TARGET) 
+
+$(TARGET): $(OBJECTS) 
+	$(CC) -I$(INCLUDEPATH) -L$(MPI_LIBS) $(LIBS) $(LDFLAGS) $(OBJECTS) -o $(TARGET) 
+
+.cpp.o:
+	$(CC) $(CFLAGS) $< -o $@
+
+clean:
+	rm -rf *.core *.o $(TARGET)
+
+

File PSO/mpiNode.cpp

View file
  • Ignore whitespace
+#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;
+	
+	MPI_Status status;		
+	
+	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");
+	}
+	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);
+}
+//------------------------------------------------
+

File PSO/mpiNode.h

View file
  • Ignore whitespace
+#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
+

File PSO/particle.cpp

View file
  • Ignore whitespace
+#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;
+}

File PSO/particle.h

View file
  • Ignore whitespace
+#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
+

File PSO/run_mpi.sh

View file
  • Ignore whitespace
+#!/usr/local/bin/tcsh
+clear
+mpirun -l -machinefile cluster -np 8 $PWD/mpi

File hmwk1/MPI/.gitignore

  • Ignore whitespace
-# Compiled source #
-###################
-*.com
-*.class
-*.o
-*.so
-
-mpi
-mpi.core

File hmwk1/MPI/README

  • Ignore whitespace
-To Run:
-./run_mpi.sh
-
-
-//------------------------------------------------
-mpirun -np 4 -machinefile mymachines mpi

File hmwk1/MPI/cluster.hosts

  • Ignore whitespace
-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
-

File hmwk1/MPI/main.cpp

  • Ignore whitespace
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <mpi.h>
-//---------------------------------------------------------------------------
-#include "mpiNode.h"
-//---------------------------------------------------------------------------
-main (int argc, char** argv)
-{
-	MPI::Init(argc, argv);
-	
-	mpiNode* node = new mpiNode(argc, argv);
-	node->run();
-
-	MPI::Finalize();
-	delete node;
-	return 0;
-}
-//----------------------------------------------------------------------------
-

File hmwk1/MPI/makefile

  • Ignore whitespace
-TARGET=mpi
-CC=mpicxx
-HEADERS=mpiNode.h
-SOURCES=main.cpp \
-        mpiNode.cpp
-
-MPI_BASE=/home/undergrad/barraj/mpich2-1.4.1p1
-
-INCLUDEPATH=$(MPI_BASE)/include
-MPI_LIBS=$(MPI_BASE)/lib
-
-LIBS=-lmpich
-
-LDFLAGS= 
-CFLAGS=-c -Wall
-OBJECTS=$(SOURCES:.cpp=.o)
-#----------------------------------------------
-all: $(SOURCES) $(TARGET) 
-
-$(TARGET): $(OBJECTS) 
-	$(CC) -I$(INCLUDEPATH) -L$(MPI_LIBS) $(LIBS) $(LDFLAGS) $(OBJECTS) -o $(TARGET) 
-
-.cpp.o:
-	$(CC) $(CFLAGS) $< -o $@
-
-clean:
-	rm -rf *.core *.o $(TARGET)
-
-

File hmwk1/MPI/mpiNode.cpp

  • Ignore whitespace
-#include "mpiNode.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.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;
-	
-	MPI_Status status;		
-	
-	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("Initializing 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");
-	}
-	free(pcMessage);
-	free(pcStatus);
-}
-//-------------------------------------------------
-mpiNode::~mpiNode()
-{
- 	free(m_pcName); 
-}
-//-------------------------------------------------
-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 = 3;
-	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);
-}
-//------------------------------------------------
-

File hmwk1/MPI/mpiNode.h

  • Ignore whitespace
-#ifndef MPI_NODE_H
-#define MPI_NODE_H
-//------------------------------------------------------------
-#include <mpi.h>
-//------------------------------------------------------------
-#define ROOT 0
-#define MESSAGE_TAG 1000
-#define WORK_TAG 2000
-//------------------------------------------------------------
-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();
-	
-protected:
-	int m_iNodes;
-	int m_ID;
-	char* m_pcName;
-	bool m_bRoot;
-};
-//------------------------------------------------------------
-#endif
-

File hmwk1/MPI/mymachines.hosts

  • Ignore whitespace
-#******************************************
-#swaic.cs.sunyit.edu
-#fox.cs.sunyit.edu
-#coyote.cs.sunyit.edu
-#ren.cs.sunyit.edu
-#mcgruff.cs.sunyit.edu
-#********************************************
-localhost
-localhost
-localhost
-localhost

File hmwk1/MPI/run_mpi.sh

  • Ignore whitespace
-#!/usr/local/bin/tcsh
-clear
-mpirun -np 4 -machinefile mymachines mpi

File hmwk1/README

  • Ignore whitespace
Empty file removed.