1. Jay Barra
  2. CS523

Source

CS523 / Firefly / main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpi.h>
//---------------------------------------------------------------------------
#define ROOT 0

#define MESSAGE_TAG 1000
#define WORK_TAG    1001
//---------------------------------------------------------------------------
#define LAMBDA 0.2
//---------------------------------------------------------------------------
struct Position {
    float x;
    float y;
};

struct Firefly {
    struct Position current_pos;
    float current_fitness;

    struct Position best_pos;
    float best_fitness;
};
//---------------------------------------------------------------------------
// Algorithm Functions
void  updateFirefly(struct Firefly f);

float percievedBrightnes(struct Firefly f1, struct Firefly f2);
float fitness(struct Firefly& f);

float distance(struct Position p1, struct Position p2);
//---------------------------------------------------------------------------
// MPI Functions
void getStatus(MPI_Status status, char* pcStatus);
//---------------------------------------------------------------------------
main (int argc, char** argv)
{
  MPI_Init(&argc, &argv);
	
  int iNameLen;
  int iNodes;
  int id;
  
  MPI_Status status;
 
  char* pcName    = (char*)malloc(1024);
  char* pcMessage = (char*)malloc(1024);
  char* pcStatus  = (char*)malloc(1024);
  
  MPI_Comm_rank(MPI_COMM_WORLD, &id);
  MPI_Comm_size(MPI_COMM_WORLD, &iNodes);
  MPI_Get_processor_name(pcName, &iNameLen);
  
  bool bRoot = (id == ROOT);

  if (!bRoot) {
    sprintf(pcMessage, "%i", id, pcName);
    MPI_Send(pcMessage, strlen(pcMessage)+1, MPI_CHAR, ROOT, MESSAGE_TAG, MPI_COMM_WORLD);
  } else {
    printf("Initializing Nodes\n");
    printf("*********************************************************\n");
    printf("Root Node: %s\n", pcName);
    printf("Awaiting worker node responses...\n");
    for (int iSource = 1; iSource < 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", iNodes);
    printf("*********************************************************\n");
  }



  MPI_Finalize();
  
  return 0; 
}
//-------------------------------------------------
void 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");
  }
}
//------------------------------------------------