Commits

Jay Barra committed 1844b36 Merge

Merge branch 'Firefly'

  • Participants
  • Parent commits f6a34de, b0ad621

Comments (0)

Files changed (8)

Firefly/.gitignore

+# Compiled source #
+###################
+*.com
+*.class
+*.o
+*.so
+
+mpi
+mpi.core

Firefly/README

Empty file added.

Firefly/cluster.hosts

+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
+
+#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;
+}
+//----------------------------------------------------------------------------
+
+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)
+
+

Firefly/mpiNode.cpp

+#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);
+}
+//------------------------------------------------
+

Firefly/mpiNode.h

+#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
+

Firefly/run_mpi.sh

+#!/usr/local/bin/tcsh
+clear
+mpirun -np 4 -machinefile mymachines mpi