Skip to content

Commit

Permalink
final stage
Browse files Browse the repository at this point in the history
  • Loading branch information
CanisLupus committed Sep 3, 2013
1 parent 6789f19 commit 220310b
Show file tree
Hide file tree
Showing 70 changed files with 9,977 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.o
*.d
*.sh
Debug
Release
.settings
/.cproject
/.project

87 changes: 87 additions & 0 deletions Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "igcl/igcl.hpp"

#include <iostream>
#include <sstream>
#include <string>

#define PROBLEM 4 // EVOLUTIONARY:0, MATRIX_MULT:1, RAYTRACER:2, SORT:3, TSP:4

#if (PROBLEM == 0)
#include "MainIslandModel.hpp"
#elif (PROBLEM == 15)
#include "MainMatrixMultiplication_Buffered.hpp"
#elif (PROBLEM == 18)
#include "MainMatrixMultiplication_VsMPI.hpp"
#elif (PROBLEM == 19)
#include "MainMatrixMultiplication_Threads.hpp"
#elif (PROBLEM == 2)
#include "MainRaytracing.hpp"
#elif (PROBLEM == 25)
#include "MainRaytracing_Threads.hpp"
#elif (PROBLEM == 28)
#include "MainRaytracing_Small.hpp"
#elif (PROBLEM == 3)
#include "MainSort.hpp"
#elif (PROBLEM == 4)
#include "MainParallelTSP.hpp"
#endif


int main(int argc, char ** argv)
{
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);

int isCoordinator = 1;
char * coordinatorIp = NULL;
int ownPort = -1, coordinatorPort = -1;

bool correctArgs = false;

if (argc >= 3) {
std::stringstream ss;

isCoordinator = stoi(argv[1]);
ownPort = stoi(argv[2]);
ss << isCoordinator << ' ' << ownPort;

if (!isCoordinator and argc >= 5) {
coordinatorIp = argv[3];
coordinatorPort = stoi(argv[4]);
ss << ' ' << coordinatorIp << ' ' << coordinatorPort;
}

if (isCoordinator or (!isCoordinator and argc >= 5)) {
correctArgs = true;
dbg("command line args:", ss.str());
}
}

if (!correctArgs) {
std::cout << argc << std::endl;
std::cout << "Usage ( coordinator ): executable 1 ownPort" << std::endl;
std::cout << "Usage (non-coordinator): executable 0 ownPort coordinatorIp coordinatorPort" << std::endl;
return 0;
}

#ifdef TEST_READY
if (isCoordinator and argc > 3) setSize (stoi(argv[3]));
if (isCoordinator and argc > 4) setNTests(stoi(argv[4]));
if (isCoordinator and argc > 5) setNNodes(stoi(argv[5]));
if (!isCoordinator and argc > 5) setSize (stoi(argv[5]));
if (!isCoordinator and argc > 6) setNTests(stoi(argv[6]));
if (!isCoordinator and argc > 7) setNNodes(stoi(argv[7]));
#endif

if (isCoordinator) {
auto node = new igcl::Coordinator(ownPort);
runCoordinator(node);
delete node;
} else {
auto node = new igcl::Peer(ownPort, coordinatorIp, coordinatorPort);
runPeer(node);
delete node;
}

return 0;
}
133 changes: 133 additions & 0 deletions MainIslandModel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include "igcl/igcl.hpp"

#include "tsp/Operators.hpp"
#include "tsp/GeneticAlgorithm.hpp"
#include "tsp/TSP.hpp"

#include <iomanip>

using namespace std;


TSPInstance * loadProblemInstance()
{
TSPInstance * instance = new TSPInstance();

bool valid = false;
valid = instance->initializeFromFile("tsp/tsp_datasets/berlin52.tsp");
if (!valid)
return NULL;

instance->precalculateDistances();

cout << "Dataset size: " << instance->points.size() << endl;

return instance;
}


TSP * loadAlgorithm(TSPInstance * instance)
{
TSP * tsp = new TSP(*instance);
tsp->sortingFunctor = new TSPIndividualSortingFunctor();
tsp->populationSize = 100;
tsp->mutationChance = 0.3;
tsp->crossoverChance = 0.8;
tsp->unionStrategy = new HalfAndHalfUnion(tsp->sortingFunctor);
return tsp;
}


void work(igcl::Peer * peer)
{
TSPInstance * instance = loadProblemInstance();
TSP * tsp = loadAlgorithm(instance);

timeval globalIniTime, ini, end;
cout << "START" << endl;
gettimeofday(&globalIniTime, NULL);

tsp->start();

TSPIndividual * bestIndiv = (TSPIndividual *) tsp->population[0];
cout << "Generation " << tsp->doneGenerations << ": best fitness " << bestIndiv->fitness << endl;
cout << *bestIndiv << endl;

double realTime = 0;
uint individualSize = instance->points.size();

while (1)
{
gettimeofday(&ini, NULL);
tsp->loop();
gettimeofday(&end, NULL);
realTime += timeDiff(ini, end) / 1000.0;

if (tsp->doneGenerations % ((int) (100 * 400.0/individualSize)) == 0)
{
gettimeofday(&end, NULL);

bestIndiv = (TSPIndividual *) tsp->population[0];
cout << "\nGeneration " << tsp->doneGenerations << ": best fitness " << bestIndiv->fitness << endl;
cout << *bestIndiv << endl;

double diff = timeDiff(globalIniTime, end) / 1000.0;
cout << "time elapsed: " << diff << " sec" << endl;
cout << "useful time: " << realTime << " sec" << endl;

cout << "doneGenerations: " << setw(9) << tsp->doneGenerations << " (" << setw(8) << tsp->doneGenerations/diff << " p/sec)" << endl;
cout << "doneMutations: " << setw(9) << tsp->doneMutations << " (" << setw(8) << tsp->doneMutations/diff << " p/sec)" << endl;
cout << "doneRecombinations: " << setw(9) << tsp->doneRecombinations << " (" << setw(8) << tsp->doneRecombinations/diff << " p/sec)" << endl;
cout << "doneEvaluations: " << setw(9) << tsp->doneEvaluations << " (" << setw(8) << tsp->doneEvaluations/diff << " p/sec)" << endl;

int count = 0;
int * received = NULL;
uint matSize = 0;
igcl::peer_id id;

while (peer->tryRecvNewFromAny(id, received, matSize) == igcl::SUCCESS)
{
std::cout << "received stuff from: " << id << std::endl;
vector<order_type> v(received, received+matSize);
free(received);
TSPIndividual * indiv = new TSPIndividual(v);
std::cout << "received indiv: " << *indiv << std::endl;

indiv->fitness = tsp->evaluateExternalIndividual(indiv);

uint index = tsp->population.size()-1-count;
delete tsp->population[index];
tsp->population[index] = indiv;

++count;
}

int indiv_arr[individualSize];
copy(bestIndiv->pointOrder.begin(), bestIndiv->pointOrder.end(), indiv_arr+0);
if (peer->sendToAll(indiv_arr+0, individualSize) == igcl::FAILURE) {
//if (peer->sendToAllPeersThroughCoordinator(indiv_arr+0, individualSize) == igcl::FAILURE) {
std::cout << "FAILURE" << std::endl;
return;
}

//peer->barrier();
}
}
}


void runCoordinator(igcl::Coordinator * coord)
{
coord->setLayout(GroupLayout::getFreeAllToAllLayout());
coord->start();
/* do nothing special */
coord->hang();
}


void runPeer(igcl::Peer* peer)
{
peer->start();
work(peer);
peer->terminate();
}
120 changes: 120 additions & 0 deletions MainMatrixMultiplicationAux.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#ifndef MAINMATRIXMULTIPLICATIONAUX_HPP_
#define MAINMATRIXMULTIPLICATIONAUX_HPP_

#include <fstream>
#include <sstream>
#include <ostream>
#include <string>
#include <iomanip>

const char DATA_ROW = 1;
const char DATA_COL = 2;
const char DATA = 3;

const std::string filepath1 = "./matrices/mat128_1.txt";
const std::string filepath2 = "./matrices/mat128_2.txt";

bool readMatrix(const std::string & filepath, float * & matrix, uint & size)
{
std::ifstream file;
file.open(filepath);
if (file.fail())
return false;

std::string line;
getline(file, line);
size = stoi(line);

matrix = (float *) malloc(size*size * sizeof(float));

for (uint i = 0; i < size; i++) {
getline(file, line);
std::stringstream ss(line);
for (uint j = 0; j < size; j++)
ss >> matrix[i*size+j];
}
file.close();
return true;
}

template <typename T>
void printMatrix(const T * matrix, const int size)
{
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
std::cout << std::setw(5) << matrix[i * size + j] << ' ';
}
std::cout << std::endl;
}
std::cout << std::endl;
}

template <typename T>
void transposeMatrix(const T * matrix, T * transposed, int size)
{
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
transposed[j*size + i] = matrix[i*size + j];
}
}
}

template <typename T>
void transposeMatrix(T * matrix, int size)
{
for (int i = 0; i < size-1; i++) {
for (int j = i+1; j < size; j++) {
std::swap(matrix[i*size+j], matrix[j*size+i]);
}
}
}

template <typename T>
T * multiplyMatrices(const T * mat1, const T * mat2, int size, T * res)
{
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
T sum = 0;
for (int k = 0; k < size; k++)
sum += mat1[i*size+k] * mat2[k*size+j];
res[i*size+j] = sum;
}
}
return res;
}

template <typename T>
T * multiplyMatricesT(const T * mat1, const T * mat2, int size, T * res)
{
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
T sum = 0;
for (int k = 0; k < size; k++)
sum += mat1[i*size+k] * mat2[j*size+k];
res[i*size+j] = sum;
}
}
return res;
}

template <typename T>
T multiplyRows(const T * row1, const T * row2, int size)
{
T res = 0;
for (int i = 0; i < size; i++) {
res += row1[i] * row2[i];
}
return res;
}

template <typename T>
void multiplyRows(const T * row1, const T * row2, int size, T & res)
{
res = 0;
for (int i = 0; i < size; i++) {
res += row1[i] * row2[i];
}
}


#endif /* MAINMATRIXMULTIPLICATIONAUX_HPP_ */
Loading

0 comments on commit 220310b

Please sign in to comment.