diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba deleted file mode 120000 index 5cd551cf2693e4b4f65d7954ec621454c2b20326..0000000000000000000000000000000000000000 --- a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba +++ /dev/null @@ -1 +0,0 @@ -../src \ No newline at end of file diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/Makefile b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..b6d64fba4ba48d6a207029d215eedf5df8a69730 --- /dev/null +++ b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/Makefile @@ -0,0 +1,14 @@ +include ../environment + +OBJS = States.o netstream.o + +all: $(OBJS) + +States.o: States.cc States.hh + $(CXX) $(CPPFLAGS) States.cc -c + +netstream.o: netstream.cc netstream.hh + $(CXX) $(CPPFLAGS) netstream.cc -c + +clean: + rm -f *.o *~ *% diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/Matrix.hh b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/Matrix.hh new file mode 100644 index 0000000000000000000000000000000000000000..cc30ae7e23189594a197d99982205dac483fd37b --- /dev/null +++ b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/Matrix.hh @@ -0,0 +1,337 @@ +/***************************************************************************** +*** *** +*** Este fichero decribe el template Matrix, para el manejo de matrices. *** +*** Tambien permite el manejo de vectores, los que trata como matrices cuya*** +*** primera dimension es 1. *** +*** *** +*****************************************************************************/ +#ifndef _MATRIX +#define _MATRIX + +#include "Messages.h" +#include <assert.h> + +template <class T> class Matrix +{ + private: + T *_matrix; + T nulo,neutro,inverso; + int _dimX,_dimY; + + public: + Matrix() + { + _dimX = _dimY = 0; + _matrix = NULL; + } + + Matrix(const int x,const int y,const T nulo = 0.0,const T neutro = 1.0,const T inverso = -1.0) + { + assert(x >= 1); + assert(y >= 1); + + int k = 0; + + _dimX = x; + _dimY = y; + + this->nulo = nulo; + this->neutro = neutro; + this->inverso = inverso; + + _matrix = new T [_dimX*_dimY]; + if(!_matrix) show_message(7); + + for(int i = 0; i < x; i++) + { + for(int j = 0; j < y ; j++) + { + _matrix[k] = (i!=j?nulo:neutro); + k++; + } + + } + } + + Matrix(const int y,const T nulo = 0.0,const T neutro = 1.0,const T inverso = -1.0) + { + assert(y >= 1); + + _dimX = 1; + _dimY = y; + + this->nulo = nulo; + this->neutro = neutro; + this->inverso = inverso; + + _matrix = new T [_dimY]; + if(!_matrix) show_message(7); + + _matrix[0] = neutro; + for(int j = 1; j < y ; j++) + _matrix[j] = nulo; + } + + Matrix(const Matrix<T> &m) + { + + _dimX = m.dimX(); + _dimY = m.dimY(); + + nulo = m.nulo; + neutro = m.neutro; + inverso = m.inverso; + + _matrix = new T [_dimX*_dimY]; + if(!_matrix) show_message(7); + + for(int i = 0; i < (_dimX*_dimY); i++) + { + _matrix[i] = m[i]; + } + } + + + ~Matrix() + { + remove(); + } + + T &operator()(const int x,const int y) const + { + if((x >= _dimX) || (y >= _dimY)) + show_message(14); + + return (T&)(*(_matrix + (x*_dimX) + y)); + } + + T &operator[](const int y) const + { + if(y >= (_dimX*_dimY)) + show_message(14); + + return (T&)(*(_matrix + y)); + } + + T &operator()(const int y) const + { + if(y >= (_dimX*_dimY)) + show_message(14); + + return (T&)(*(_matrix + y)); + } + + Matrix<T> &operator=(const Matrix<T> &m) + { + remove(); + + _dimX = m.dimX(); + _dimY = m.dimY(); + + _matrix = new T [_dimX*_dimY]; + if(!_matrix) show_message(7); + + for(int i = 0; i < (_dimX*_dimY); i++) + { + _matrix[i] = m[i]; + } + + return (*this); + } + + bool operator==(const Matrix<T> &m) const + { + if((_dimX != m.dimX()) || (_dimY != m.dimY())) + return false; + + for(int i = 0; i < (_dimX*_dimY); i++) + if(_matrix[i] != m[i]) return false; + + return true; + } + + bool operator!=(const Matrix<T> &m) const + { + return !((*this) == m); + } + + Matrix<T> operator*(const Matrix<T> &m) + { + int x = (m.dimX()!=_dimY?0:_dimX); + int y = (x!=0?m.dimY():0); + T acum = nulo; + + Matrix<T> res(x,y); + + for(int i = 0; i < _dimX; i++) + for(int j = 0; j < m.dimY(); j++) + { + acum = nulo; + for( int k = 0; k < _dimY; k++) + acum += (*this)(i,k)* m(k,j); + res(i,j) = acum; + } + + return Matrix<T>(res); + + } + + Matrix<T> &operator*=(const Matrix<T> &m) + { + int x = (m.dimX()!=_dimY?0:_dimX); + int y = (x!=0?0:m.dimY()); + T acum = nulo; + + Matrix<T> res(x,y); + + for(int i = 0; i < _dimX; i++) + for(int j = 0; j < m.dimY(); j++) + { + acum = nulo; + for( int k = 0; k < _dimY; k++) + acum += (*this)(i,k)*m(k,j); + res(i,j) = acum; + } + + (*this) = res; + + return (*this); + + } + + Matrix<T> operator*(const T &elem) + { + Matrix<T> res(_dimX,_dimY); + + for(int i = 0; i < (_dimX*_dimY); i++) + res[i] = _matrix[i] * elem; + + return Matrix(res); + } + + Matrix<T> &operator*=(const T &elem) + { + for(int i = 0; i < (_dimX*_dimY); i++) + _matrix[i] *= elem; + + return (*this); + } + + Matrix<T> operator+(const Matrix<T> &m) + { + int x = (m.dimX()!=_dimX?0:_dimX); + int y = (m.dimY()!=_dimY?0:_dimY); + + Matrix<T> res(x,y); + + for(int i = 0; i < (x*y); i++) + res[i] = _matrix[i] + m[i]; + + return Matrix<T>(res); + } + + Matrix<T> &operator+=(const Matrix<T> &m) + { + int x = (m.dimX()!=_dimX?0:_dimX); + int y = (m.dimY()!=_dimY?0:_dimY); + + for(int i = 0; i < (x*y); i++) + _matrix[i] += m[i]; + + return (*this); + } + + Matrix<T> operator-(const Matrix<T> &m) + { + Matrix<T> res(); + + res = m * inverso; + return (*this) + res; + } + + + Matrix<T> &operator-=(const Matrix<T> &m) + { + Matrix<T> res(); + + res = m * inverso; + (*this) += res; + + return (*this); + } + + Matrix<T> Traspuesta() + { + Matrix<T> res(_dimY,_dimX); + + for(int i = 0; i < _dimX; i++) + for(int j = 0; j < _dimY; j++) + res(j,i) = (*this)(i,j); + + return Matrix<T>(res); + } + + Matrix<T> &nula() + { + for(int i = 0; i < (_dimX*dimY); i++) + _matrix[i] = nulo; + + return (*this); + } + + Matrix<T> &identity() + { + register int k = 0; + + for(int i = 0; i < _dimX; i++) + for(int j = 0; j < _dimY; j++) + { + _matrix[k] = (i!=j?nulo:neutro); + k++; + } + + return (*this); + } + + unsigned int size() const + { + return (sizeof(T)*_dimX*_dimY); + } + + char *to_string() const + { + return (char *) _matrix; + } + + Matrix<T> &to_Matrix(char *_cadena) + { + T *ptr = (T *)_cadena; + + for(int i = 0; i < (_dimX*_dimY) ; i++) + { + _matrix[i] = *ptr; + ptr++; + } + + return (*this); + } + + + int dimX() const + { + return _dimX; + } + + int dimY() const + { + return _dimY; + } + + void remove() + { + if(_matrix != NULL) + delete [] _matrix; + } +}; + +#endif diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/Messages.h b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/Messages.h new file mode 100644 index 0000000000000000000000000000000000000000..e46b1d823977e76b1c942dda51104206102cc62f --- /dev/null +++ b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/Messages.h @@ -0,0 +1,112 @@ +/*****************************************************************************/ +/*** ***/ +/*** Modificado por G.J.L.P. ***/ +/*** Añadidos nuevos mensajes que indican falta de algún ***/ +/*** Fichero de Configuración (No específico para ningún ***/ +/*** problema) o nuevos errores. ***/ +/*** ***/ +/*****************************************************************************/ + +#ifndef RLFAP_MESSAGES +#define RLFAP_MESSAGES + +#ifndef MAX_BUFFER +#define MAX_BUFFER 200 +#endif + +#include <iostream> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +using namespace std; + +inline void show_message(int value) +{ + switch (value) + { + case 1: cout << endl << "Error: number of arguments in the execution call is incorrect !!" + << endl; break; + case 2: cout << endl << "Error: It's imposible find Configuration file !!" << endl; + break; + /* Específicos de RLFAP */ + case 3: cout << endl << "Error: It is imposible find the Celar problem definition file (cst.txt) !!" + << endl; break; + case 4: cout << endl << "Error: It is imposible find the Celar domains file (dom.txt) !!" + << endl; break; + case 5: cout << endl << "Error: It is imposible find the Celar links file (var.txt) !!" + << endl; break; + case 6: cout << endl << "Error: It is imposible find the Celar constraints file (ctr.txt) !!" + << endl; break; + /* Fallos de Memoria */ + case 7: cout << endl << "Error: No avalible memory for \"malloc\" operation !!" << endl; + break; + case 8: cout << endl << "Error: in \"free\" operation !!" << endl; + break; + /* Específicos del MaxCut */ + case 9: cout << endl << "Error: It is imposible find the Maxcut file (Maxcut.txt) !!" + << endl; break; + /* Genéricos de Falta de ficheros de configuracion adicionales al mensaje 2 */ + case 10: cout << endl << "Error: It's imposible find Configuration file (Config.cfg) !!" + << endl; break; + case 11: cout << endl << "Error: It's imposible find Skeleton Configuration File (Ske.cfg) !!" + << endl; break; + case 12: cout << endl << "Error: It's imposible find Instance Problem File !!" << endl; + break; + case 13: cout << endl << "Error: It's imposible find Resultate File !!" << endl; + break; + case 14: cout << endl << "Error: Index out of Range !!" << endl; + break; + default: cout << endl << "Unkown Error !!" << endl; + } + + cout << endl << " " << endl; + exit(-1); +} + +inline void continue_question() +{ + fflush(stdout); + cout << endl << "Press any key to continue..." << endl; + fflush(stdin); + getc(stdin); +} + +inline void get_path(const char *source,char *target) +{ + int last = 0; + + for(int i = 0; i < strlen(source); i++) + { + target[i] = source[i]; + if(target[i] == '/') + last = i; + } + target[last+1] = '\0'; +} + +inline unsigned count_lines(char *file_name) // returns the number of lines of a file +{ + char line[MAX_BUFFER]; + FILE *file; + int count=0; + + if ((file=fopen(file_name,"r"))==NULL) + { + fflush(stdout); + printf("File not found !"); + } + + while (!feof(file)) + { + if (fgets(line,MAX_BUFFER,file)) count++; + else + { + fclose(file); + break; + } + } + return count; +} + +#endif diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/Rarray.h b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/Rarray.h new file mode 100644 index 0000000000000000000000000000000000000000..5d1a1c24919e00e84e1c1bf889021e2c04054dba --- /dev/null +++ b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/Rarray.h @@ -0,0 +1,145 @@ +/****************************************************************************** +*** *** +*** Template para el manejo dinámico de arrays *** +*** Añadido métodos para invertir todo o parte del array. *** +*** *** +******************************************************************************/ + +#ifndef ARRAY_INC +#define ARRAY_INC 1 +#include <iostream> +#include <assert.h> + +using namespace std; + +template<class E1> class Rarray +{ + private: + E1 *first; + int count; + + public: + Rarray() + { + first = NULL; + count = 0; + } + + ~Rarray() + { + remove(); + } + + E1* get_first() const + { + return first; + } + + void message_a(int cod) const + { + switch (cod) + { + case 1: cout << endl << "The size of array must be upper that 0 !!!" ; + } + } + + Rarray(const int size_a) + { + if (size_a<0) message_a(1); + first = new E1[size_a]; + count=size_a; + } + + void remove() + { + if (count!=0) + delete [] first; + } + + int size() const + { + return count; + } + + E1& operator[](int pos) const + { + return (E1&)(*(first + pos)); + } + + friend ostream& operator<< (ostream& os,const Rarray<E1>& a) + { + for (int i=0;i<a.size();i++) + os << endl << a[i]; + return os; + } + + Rarray<E1>& operator=(const Rarray<E1>& source) + { + remove(); + count = source.size(); + first = new E1[count]; + + for (int i=0;i<count;i++) + (*this)[i] = source[i]; + + return (*this); + } + + + Rarray<E1>& invert() + { + return invert(0,count-1); + } + + Rarray<E1>& invert(const int pos1, const int pos2) + { + int max,min,half,i,j; + E1 aux; + + if(pos1 > pos2) + { + max = pos1; + min = pos2; + } + else + { + max = pos2; + min = pos1; + } + + assert((min > 0) || (min < count-1)); + half = ((max-min)/2) + 1; + + for(i = min,j=max; i< half; i++,j--) + { + aux = first[min]; + first[min] = first[max]; + first[max] = aux; + } + + return (*this); + } + + Rarray<E1>& sort(int (*comp)(const E1 &,const E1 &)) + { + E1 aux; + int j; + + for (int i=1; i < count ; i++) + { + aux = first[i]; + j = i - 1; + while ( (comp(aux,first[j])) && (j >= 0) ) + { + first[j+1]= first[j]; + j--; + } + first[j+1] = aux; + } + + return (*this); + } + +}; // end of class + +#endif diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/Rlist.h b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/Rlist.h new file mode 100644 index 0000000000000000000000000000000000000000..f561d0bcd49bbffc7fe449f74252df988343f2ff --- /dev/null +++ b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/Rlist.h @@ -0,0 +1,415 @@ +/****************************************************************************** +*** *** +*** Este template sirve para el manejo de listas din�micas *** +*** *** +******************************************************************************/ + +#ifndef LIST_INC +#define LIST_INC 1 +#include <iostream> +#include <stdlib.h> +#include <Messages.h> + +using namespace std; + +template<class E> class Rlist_item +{ + public: + E *useful_data; // pointer to the structure stored in the list + Rlist_item<E> *next; + Rlist_item<E> *previous; + + Rlist_item(E& new_data) + { + useful_data=&new_data; + next=NULL; + previous=NULL; + } + + Rlist_item(E *new_data) + { + useful_data=new_data; + next=NULL; + previous=NULL; + } + + ~Rlist_item() + { + } + + Rlist_item<E>& next_item() + { + return *(next); + } + + Rlist_item<E>& previous_item() + { + return *(previous); + } + + bool is_last() + { + return (next==NULL); + } + + bool is_first() + { + return (previous==NULL); + } + + E& data() + { + return *(useful_data); + } +}; + +template<class E> class Rlist +{ + private: + Rlist_item<E> *first; // first item in the list + Rlist_item<E> *last; // last item un the list + int count; // number of items in the list + + public: + // constructor + Rlist() + { + first=NULL; + last=NULL; + count=0; + } + + // destructor + ~Rlist() + { + remove(); + } + + // Return the size of the list + int size() const + { return count; } + + // Go back to the initial state of the list + void reset() + { + first=NULL; + last=NULL; + count=0; + } + + // Add a item at the final of the list + Rlist<E>& append(E& new_item) + { + Rlist_item<E> *new_Rlist_item=new Rlist_item<E>(new_item); + if (first==NULL) + { + first=new_Rlist_item; + new_Rlist_item->next=NULL; + new_Rlist_item->previous=NULL; + } + else + { + last->next=new_Rlist_item; + new_Rlist_item->previous=last; + } + last=new_Rlist_item; + count++; + return *this; + } + + Rlist<E>& append(E *new_item) + { + append(*new_item); + return (*this); + } + + // Add a item in a position ( pos ) of the list + Rlist<E>& add_pos(E& new_item, const int pos) + { + if (pos==size()-1) + return append(new_item); + + if (pos==-1) + return add(new_item,NULL); + else + return add(new_item,get_at(pos).useful_data); + } + + // Add a item in the list in the position next to "previous item" + Rlist<E>& add(E& new_item,E *previous_item) + { + if (first==NULL) + return append(new_item); + + Rlist_item<E> *new_Rlist_item=new Rlist_item<E>(new_item); + + if (previous_item==NULL) // Add the item like the first of the list + { + new_Rlist_item->next=first; + new_Rlist_item->previous=NULL; + first->previous=new_Rlist_item; + first=new_Rlist_item; + } + else + { + int previous_position=get_position(*previous_item); + if (previous_position==-1) return(*this); + Rlist_item<E> *previous_Rlist_item = &( get_at(previous_position)); + new_Rlist_item->next=previous_Rlist_item->next; + new_Rlist_item->previous=previous_Rlist_item; + if (previous_Rlist_item->next!=NULL) + (previous_Rlist_item->next)->previous=new_Rlist_item; + else last=new_Rlist_item; + previous_Rlist_item->next=new_Rlist_item; + } + count++; + return *this; + } + + // Return a pointer to the first item of the list + Rlist_item<E> *get_first() const + { return first; } + + // Assign a item like the first item in the list + void set_first(Rlist_item<E> *new_first) + { first=new_first; } + + // Return a pointer to the last item of the list + Rlist_item<E> *get_last() const + { return last; } + + // Assign a item like the last item in the list + void set_last(Rlist_item<E> *new_last) + { last=new_last; } + + // Return the item at position "pos" + E& operator[](int pos) const + { + return *(get_at(pos).useful_data); + } + + // Return the Rlist_item at position "pos" + Rlist_item<E>& get_at(int pos) const + { + Rlist_item<E> *present=first; + for (int k=0;k<size();k++) + if (k==pos) return *present; + else present=present->next; + } + + // Return the item position in the list + int get_position(const E& item) const // probado + { + Rlist_item<E> *present=first; + int i=0; + + while(present!=NULL) + { + if (present->useful_data==&item) return i; + i++; + present=present->next; + } + return -1; // the object has not been found + } + + // Delete a item of the list + Rlist<E>& delete_item(E& item) + { + int position = get_position(item); + + if (position==-1) return *this; + Rlist_item<E> *present=&(get_at(position)); + + if (&item==first->useful_data) // is the first + { + if (&item==last->useful_data) + { + delete(first->useful_data); + delete(first); + first=NULL; + last=NULL; + count=0; + } + else + { + first=first->next; + first->previous=NULL; + delete(present->useful_data); + delete(present); + count--; + } + } + else + { + if (&item==last->useful_data) + { + last=present->previous; + last->next=NULL; + delete(present->useful_data); + delete(present); + count--; + } + else + { + (present->next)->previous=present->previous; + (present->previous)->next=present->next; + delete(present->useful_data); + delete(present); + count--; + } + } + return *this; + } + + // Delete a item of the list without free the useful_data + Rlist<E>& delete_item_1(E& item) + { + int position = get_position(item); + + if (position==-1) return *this; + Rlist_item<E> *present=&(get_at(position)); + + if (&item==first->useful_data) // is the first + { + if (&item==last->useful_data) + { + delete(first); + first=NULL; + last=NULL; + count=0; + } + else + { + first=first->next; + first->previous=NULL; + delete(present); + count--; + } + } + else + { + if (&item==last->useful_data) + { + last=present->previous; + last->next=NULL; + delete(present); + count--; + } + else + { + (present->next)->previous=present->previous; + (present->previous)->next=present->next; + delete(present); + count--; + } + } + return *this; + } + + // Delete item at position "pos" + Rlist<E>& delete_item_by_position(const int pos) + { return delete_item(*(get_at(pos).useful_data)); } + + // Delete the last item in the list + Rlist<E>& delete_last() + { return delete_item(*(last->useful_data)); } + + // delete all items in the list + Rlist<E>& remove() + { + Rlist_item<E> *next,*present=first; + while (present!=NULL) + { + next=present->next; + delete(present->useful_data); + delete(present); + present=next; + } + first=NULL; + last=NULL; + count=0; + return *this; + } + + // Join a new list to this list + Rlist<E>& join(Rlist<E>& new_list) + { + if (new_list.size()==0) + return *this; + + if (first==NULL) + { + first=new_list.get_first(); + last=new_list.get_last(); + } + else + { + last->next=new_list.get_first(); + (new_list.get_first())->previous=last; + last = new_list.get_last(); + } + count += new_list.size(); + new_list.reset(); + return *this; + } + + // Show items of the list + friend ostream& operator<<(ostream& os, const Rlist<E>& list) + { + Rlist_item<E> *present=list.get_first(); + if (list.get_first()==NULL) os << endl << "THE LIST IS EMPTY !!"; + while (present!=NULL) + { + os << endl << (*(present->useful_data)); + // Falta el operador para stat. + // (habra que ver) + present=present->next; + } + return os; + } + + // Copy the list passed + Rlist<E>& operator=(const Rlist<E>& source) + { + E *new_item; + remove(); + if (source.first==NULL && source.last==NULL) + { + first=NULL; + last=NULL; + count=0; + } + else + { + for (int i=0;i<source.size();i++) + { + if ((new_item=(E *)malloc(sizeof(E)))==NULL) + show_message(7); + (*new_item)=*(source.get_at(i).useful_data); + append(*new_item); + } + } + return *this; + } + + // Invert the order of items in the list + Rlist<E>& invert() + { + Rlist_item<E> *present,*interchange; + + present=first; + + for (int i=0;i<size();i++) + { + interchange=present->next; + present->next=present->previous; + present->previous=interchange; + present=interchange; + } + interchange=first; + first=last; + last=interchange; + return (*this); + } +}; // end of class +#endif diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/States.cc b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/States.cc new file mode 100644 index 0000000000000000000000000000000000000000..f36d1979373c40068088fb6951f13a126bc033fe --- /dev/null +++ b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/States.cc @@ -0,0 +1,228 @@ +/****************************************************************************** + Modified by Carlos Cotta Porras + April 2001 + + Modified by G.J.L.P +******************************************************************************/ + + +#include <string.h> +#include "States.hh" + +// Methods of class State_Vble + + // constructors + State_Vble ::State_Vble () + { + name=NULL; + nitems=0; + length=0; + content=NULL; + } + + State_Vble ::State_Vble (const char *st_name) + { + name=strdup(st_name); + nitems=0; + length=0; + content=NULL; + } + + State_Vble::State_Vble (const char *st_name,StateCenter& sc) + { + name=strdup(st_name); + nitems=0; + length=0; + content=NULL; + sc.add(*this); + } + + State_Vble ::State_Vble (const char *st_name,const char *new_contents, unsigned long new_nitems, unsigned long new_length) + { + name=strdup(st_name); + nitems=0; + length=0; + content=NULL; + set_contents(new_contents,new_nitems,new_length); + } + + State_Vble ::State_Vble (const char *st_name,const char *new_contents, unsigned long new_nitems, unsigned long new_length,StateCenter& sc) + { + name=strdup(st_name); + nitems=0; + length=0; + content=NULL; + set_contents(new_contents,new_nitems,new_length); + sc.add(*this); + } + + + // Set the name of a state vble. + void State_Vble ::set_name (const char* st_name) // Set the name of a state vble. + { + if (name!=NULL) + free(name); + name=strdup(st_name); + } + + // Get the name of a state vble. + char* State_Vble ::get_name () const // Get the name of a state vble. + { + return name; + } + + // Number of basic items + unsigned long State_Vble ::get_nitems() const // Number of basic items + { + return nitems; + } + + // Get the total number of bytes + unsigned long State_Vble ::get_length () const // Get the total number of bytes + { + return length; + } + + // Fill up a state vble. + void State_Vble ::set_contents (const char *new_contents, unsigned long new_nitems, unsigned long new_length) + { + if (content!=NULL) + free(content); + content=(char *)malloc(new_nitems * new_length); + memcpy(content,new_contents,(new_nitems*new_length)); + nitems=new_nitems; + length=new_length; + } + + // Obtain the contents of a state vble. + void *State_Vble ::get_contents (char *read_contents, unsigned long& read_nitems, unsigned long& read_length) const + { + memcpy(read_contents,content,nitems * length); + read_nitems=nitems; + read_length=length; + return NULL; + } + + ostream& operator<< (ostream& os,const State_Vble& st) + { + os << endl << st.name + << endl << st.nitems + << endl << st.length + << endl << st.content; + + return os; + } + + State_Vble ::~State_Vble () + { + free(content); + free(name); + } + +// Methods of class StateCenter + + StateCenter::StateCenter():state_variables() + {} + + // searchs a state variable + State_Vble *StateCenter::find(const char *st_name) const + { + Rlist_item<State_Vble> *current_state=state_variables.get_first(); + + for (int i=0;i<state_variables.size();i++) + { + if (!(strcmp(current_state->data().get_name(),st_name))) + return &(current_state->data()); + current_state=¤t_state->next_item(); + } + return NULL; + } + + // Add one state variable + void StateCenter::add(State_Vble& st) + { + State_Vble *found_state=find(st.get_name()); + if (found_state==NULL) + state_variables.append(st); + else + cout << endl << "You are trying to introduce a state variable that is yet used in the skeleton !!" << st.get_name(); + } + + void StateCenter::add(State_Vble *st) + { + State_Vble *found_state=find(st->get_name()); + if (found_state==NULL) + state_variables.append(*st); + else + cout << endl << "You are trying to introduce a state variable that is yet used in the skeleton !!" << st->get_name(); + } + + // Remove one state variable + void StateCenter::remove(const char* st_name) + { + State_Vble *found_state=find(st_name); + if (found_state!=NULL) + state_variables.delete_item_1(*found_state); + } + + // Update the contents of one vble. + void StateCenter::update(const char* st_name, const State_Vble& st) const // Update the contents of one vble. + { + State_Vble *found_state=find(st_name); + if (found_state!=NULL) + { + char *a=(char *)malloc(found_state->get_nitems() * found_state->get_length()); + unsigned long nitems,length; + st.get_contents(a,nitems,length); + found_state->set_contents(a,nitems,length); + free(a); + } + } + + // Get a vble. with a given name + State_Vble& StateCenter::get(const char* st_name) const // Get a vble. with a given name + { + State_Vble *found_state=find(st_name); + return *found_state; + } + + // Allows an easy iterated extraction + State_Vble* StateCenter::get_next(const State_Vble& st) const + { + State_Vble *found_state=find(st.get_name()); + if (found_state==NULL) return NULL; + if ( state_variables.get_at(state_variables.get_position(*found_state)).is_last()) return NULL; + else return &(state_variables.get_at(state_variables.get_position(*found_state)).next_item().data()); + } + + // returns the number of state variables + unsigned int StateCenter::size() const + { + return state_variables.size(); + } + + // Obtain the contents of a state vble. of name st_name + void StateCenter::get_contents_state_variable(const char *st_name,char *read_contents, unsigned long& read_nitems, unsigned long& read_length) const + { + get(st_name).get_contents(read_contents,read_nitems,read_length); + } + + // Fill up a state vble.of name st_name + void StateCenter::set_contents_state_variable(const char *st_name,const char *new_contents, unsigned long new_nitems, unsigned long new_length) const + { + get(st_name).set_contents(new_contents,new_nitems,new_length); + } + + void StateCenter::removeAll() + { + while(state_variables.get_first()) + { + Rlist_item<State_Vble>* v = state_variables.get_first(); + remove(v->useful_data->get_name()); + } + } + + StateCenter::~StateCenter() + { + removeAll(); + } diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/States.hh b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/States.hh new file mode 100644 index 0000000000000000000000000000000000000000..e3a215f5066608ec161882d5ae0c1273944ceaa1 --- /dev/null +++ b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/States.hh @@ -0,0 +1,64 @@ +/******************************************************************************************************** +*** *** +*** Class StateCenter: Pool of state variables for skeletons *** +*** *** +*** Class State_Vble: State Variable of a skeleton *** +*** *** +*********************************************************************************************************/ + +#ifndef STATE_CENTER +#define STATE_CENTER 1 + +#include "Rlist.h" +#include <iostream> + +class StateCenter; + +class State_Vble +{ + private: + char *name; + unsigned long nitems; + unsigned long length; + char *content; + + public: + // constructors + State_Vble (); + State_Vble (const char *st_name); + State_Vble (const char *st_name,StateCenter& sc); + State_Vble (const char *st_name,const char *new_contents, unsigned long new_nitems, unsigned long new_length); + State_Vble (const char *st_name,const char *new_contents, unsigned long new_nitems, unsigned long new_length,StateCenter& sc); + + void set_name (const char* st_name); // Set the name of a state vble. + char* get_name () const; // Get the name of a state vble. + unsigned long get_nitems() const; + unsigned long get_length () const; // Get the total number of bytes + void set_contents (const char *new_contents, unsigned long new_nitems, unsigned long new_length); // Fill up a state vble. + void *get_contents (char *read_contents, unsigned long& read_nitems, unsigned long& read_length) const; // Obtain the contents of a state vble. + friend ostream& operator<< (ostream& os,const State_Vble& st); + ~State_Vble (); +}; + +class StateCenter +{ + private: + Rlist<State_Vble> state_variables; + + public: + StateCenter(); + State_Vble *find(const char *st_name) const; // search a state variable + void add(State_Vble& st); // Add one state variable + void add(State_Vble *st); // Add one state variable + void remove(const char* st_name); // Remove one state variable + void removeAll(); // Removes all variables + void update(const char* st_name, const State_Vble& st) const; // Update the contents of one vble. + State_Vble& get(const char* st_name) const; // Get a vble. with a given name + State_Vble* get_next(const State_Vble& st) const; // Allows an easy iterated extraction + unsigned int size() const; // returns the number of state variables + void get_contents_state_variable(const char *st_name,char *read_contents, unsigned long& read_nitems, unsigned long& read_length) const; // Obtain the contents of a state vble. of name st_name + void set_contents_state_variable(const char *st_name,const char *new_contents, unsigned long new_nitems, unsigned long new_length) const; // Fill up a state vble.of name st_name + ~StateCenter(); +}; + +#endif diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/States.o b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/States.o new file mode 100644 index 0000000000000000000000000000000000000000..f5e95c8928fb22e26363c12820d0e56fd98709d2 Binary files /dev/null and b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/States.o differ diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/mallba.hh b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/mallba.hh new file mode 100644 index 0000000000000000000000000000000000000000..e4a35c07363b0c19d91cf9d5f03f352d0550fd83 --- /dev/null +++ b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/mallba.hh @@ -0,0 +1,26 @@ +/***************************************************************************** +*** *** +*** Este fichero hace algunas declaraciones y declara algunos tipos comu- *** +*** nes a todos los algoritmos implementados. *** +*** *** +*****************************************************************************/ + +#ifndef INC_mallba_hh +#define INC_mallba_hh + +#define skeleton namespace +#define requires +#define provides +#define hybridizes(x) +#define inherits typedef +#define as + +enum Direction { minimize=-1,maximize=1}; + +extern const double plus_infinity; +extern const double minus_infinity; + +#define false 0 +#define true 1 + +#endif diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/netstream.cc b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/netstream.cc new file mode 100644 index 0000000000000000000000000000000000000000..932e89e56ab5eff64306137bd0ae97b1380a05e2 --- /dev/null +++ b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/netstream.cc @@ -0,0 +1,380 @@ +/*************************************************************************** + *** netstream.cc *** + *** v1.6 - July 2001 *** + ** *** + *** v1.5 - March 2001 *** + *** v1.0 - November 2000 *** + *** *** + *** v1.5 extends v1.0: *** + *** .- Changes metods init() and finalize() to be static *** + *** .- Incorporates process group management *** + *** .- Do not consider LEDA anymore *** + *** .- Contains a method "int my_pid()" for easy invokations *** + *** .- Adds "unsigned" and "long double" input/output *** + *** *** + *** v1.6 extends v1.5: *** + ** .- Internal in/out buffers for packed separated *** + *** *** + *** Communication services for LAN/WAN use following the message *** + *** passing paradigm. *** + *** STREAM C++ VERSION *** + *** MPI implementation *** + *** Developed by Erique Alba *** + ***************************************************************************/ +#include "netstream.hh" +// Default constructor +NetStream::NetStream() +{ + this->reset(); // Reset to default values member variables + packin_buffer = new char[MAX_PACK_BUFFER_SIZE]; + packout_buffer = new char[MAX_PACK_BUFFER_SIZE]; +} +// Init the underlying communication library (MPI) +NetStream::NetStream (int argc, char ** argv) +{ + init(argc,argv); + this->reset(); + packin_buffer = new char[MAX_PACK_BUFFER_SIZE]; + packout_buffer = new char[MAX_PACK_BUFFER_SIZE]; +} +// Default destructor +NetStream::~NetStream() +{ + delete [] packin_buffer; delete [] packout_buffer; + this->reset(); +} +// Give default values to member variables +void NetStream::reset(void) +{ + default_target = default_source = 0; + pack_in_progress = false; + packin_index = packout_index = 0; + pending_input_packet = false; + pack_in = pack_out = false; + broadcast = false; + my_communicator = MPI_COMM_WORLD; +} +// Init the communication system. Invoke it only ONCE +void NetStream::init(int argc, char** argv) +{ + static bool system_up = false; // Is MPI already running? + if (!system_up) + { MPI_Init(&argc,&argv); + system_up = true; + } +} +// Shutdown the communication system. Invoke it ONCE +void NetStream::finalize(void) +{ + MPI_Finalize(); // Unconditional Finalization +} +// BASIC INPUT/OUTPUT SERVICES +// =================================================================================== + +NetStream& NetStream::operator>> (bool& d) +{ rcv(&d,1,NET_BOOL,default_source); return(*this); } + +NetStream& NetStream::operator<< (bool d) +{ send(&d,1,NET_BOOL,default_target); return(*this); } +NetStream& NetStream::operator>> (char& d) +{ rcv(&d,1,NET_CHAR,default_source); return(*this); } +NetStream& NetStream::operator<< (char d) +{ send(&d,1,NET_CHAR,default_target); return(*this); } +NetStream& NetStream::operator>> (short& d) +{ rcv(&d,1,NET_SHORT,default_source); return(*this); } +NetStream& NetStream::operator<< (short d) +{ send(&d,1,NET_SHORT,default_target); return(*this); } +NetStream& NetStream::operator>> (int& d) +{ rcv(&d,1,NET_INT,default_source); return(*this); } +NetStream& NetStream::operator<< (int d) +{ send(&d,1,NET_INT,default_target); return(*this); } +NetStream& NetStream::operator>> (long& d) +{ rcv(&d,1,NET_LONG,default_source); return(*this); } +NetStream& NetStream::operator<< (long d) +{ send(&d,1,NET_LONG,default_target); return(*this); } +NetStream& NetStream::operator>> (float& d) +{ rcv(&d,1,NET_FLOAT,default_source); return(*this); } +NetStream& NetStream::operator<< (float d) +{ send(&d,1,NET_FLOAT,default_target); return(*this); } +NetStream& NetStream::operator>> (double& d) +{ rcv(&d,1,NET_DOUBLE,default_source); return(*this); } +NetStream& NetStream::operator<< (double d) +{ send(&d,1,NET_DOUBLE,default_target); return(*this); } +NetStream& NetStream::operator>> (char* d) +{ rcv(d,MAX_MSG_LENGTH,NET_CHAR,default_source); return(*this); } +NetStream& NetStream::operator<< (char* d) +{ send(d,strlen(d)+1,NET_CHAR,default_target); return(*this); } +NetStream& NetStream::operator>> (void* d) +{ rcv(d,MAX_MSG_LENGTH,NET_CHAR,default_source); return(*this); } +NetStream& NetStream::operator<< (void* d) +{ send(d,strlen((char*)d)+1,NET_CHAR,default_target); return(*this); } +// Extended data types from version 1.5 on +NetStream& NetStream::operator>> (unsigned char& d) +{ rcv(&d,MAX_MSG_LENGTH,NET_UNSIGNED_CHAR,default_source);return(*this); } + +NetStream& NetStream::operator<< (unsigned char d) +{ send(&d,1,NET_UNSIGNED_CHAR,default_target); return(*this); } +NetStream& NetStream::operator>> (unsigned short int& d) +{ rcv(&d,MAX_MSG_LENGTH,NET_UNSIGNED_SHORT,default_source);return(*this); } +NetStream& NetStream::operator<< (unsigned short int d) +{ send(&d,1,NET_UNSIGNED_SHORT,default_target); return(*this); } +NetStream& NetStream::operator>> (unsigned int& d) +{ rcv(&d,MAX_MSG_LENGTH,NET_UNSIGNED,default_source); return(*this); } + +NetStream& NetStream::operator<< (unsigned int d) +{ send(&d,1,NET_UNSIGNED,default_target); return(*this); } +NetStream& NetStream::operator>> (unsigned long int& d) +{ rcv(&d,MAX_MSG_LENGTH,NET_UNSIGNED_LONG,default_source);return(*this); } + +NetStream& NetStream::operator<< (unsigned long int d) +{ send(&d,1,NET_UNSIGNED_LONG,default_target); return(*this); } +NetStream& NetStream::operator>> (long double& d) +{ rcv(&d,MAX_MSG_LENGTH,NET_LONG_DOUBLE,default_source); return(*this); } + +NetStream& NetStream::operator<< (long double d) +{ send(&d,1,NET_LONG_DOUBLE,default_target); return(*this); } +// SET-GET TARGET AND SOURCE PROCESSES +NetStream& __set_target(NetStream& n, const int p) { return n._set_target(p); } +NetStream& NetStream::_set_target(const int p) +{ assert(p>=0); default_target = p; return (*this); } +NetStream& __get_target(NetStream& n, int* p) { return n._get_target(p); } +NetStream& NetStream::_get_target(int* p) +{ *p = default_target; return (*this); } +NetStream& __set_source(NetStream& n, const int p) { return n._set_source(p); } +NetStream& NetStream::_set_source(const int p) +{ /*assert(p>=0);*/ default_source = p; return (*this); } +NetStream& __get_source(NetStream& n, int* p) { return n._get_source(p); } +NetStream& NetStream::_get_source(int* p) +{ *p = default_source; return (*this); } +// Get the number of processes involved in the communications +int NetStream::pnumber(void) +{ int numprocs, rvalue; + rvalue = MPI_Comm_size(my_communicator,&numprocs); + assert(rvalue==MPI_SUCCESS); + return numprocs; +} +// MANIPULATORS: SYNCHRONIZATION AND PACKING SERVICES +// =================================================================================== +// Get the process ID [0, 1, 2, ...] fro the calling process +NetStream& __my_pid(NetStream& n, int* pid) +{ + return n._my_pid(pid); +} +NetStream& NetStream::_my_pid(int* pid) +{ + MPI_Comm_rank(my_communicator,pid); + return (*this); +} +// EASY access to rank - Returns the process ID of the calling process +int NetStream::my_pid(void) +{ int pid; + this->_my_pid(&pid); + return pid; +} +// Sit and wait until all processes are in the same barrier +// Can be used as a MANIPULATOR +NetStream& barrier(NetStream& n) +{ + return n._barrier(); +} +NetStream& NetStream::_barrier(void) +{ int status; + status = MPI_Barrier(my_communicator); + assert(status==MPI_SUCCESS); + return (*this); +} +// Wait for an incoming message in any input stream +NetStream& NetStream::_wait(const int stream_type) // class +{ + int rvalue; + MPI_Status status; + assert(stream_type==regular||stream_type==packed||stream_type==any); + if( ((stream_type==packed) || (stream_type==any) )&& (pending_input_packet) ) + //if( (stream_type==packed) && (pending_input_packet) ) + return (*this); // wait befor packet_begin when already received + rvalue = MPI_Probe(default_source,stream_type,my_communicator,&status); + assert(rvalue==MPI_SUCCESS); + return (*this); +} + +NetStream& NetStream::_wait2(const int stream_type, int& tipo) // class +{ + int rvalue; + MPI_Status status; + assert(stream_type==regular||stream_type==packed||stream_type==any); + if( ((stream_type==packed) || (stream_type==any) )&& (pending_input_packet) ) + //if( (stream_type==packed) && (pending_input_packet) ) + return (*this); // wait befor packet_begin when already received + rvalue = MPI_Probe(default_source,stream_type,my_communicator,&status); + assert(rvalue==MPI_SUCCESS); + if (status.MPI_SOURCE == 0){ + tipo = 1; + } + return (*this); +} +NetStream& __wait(NetStream& n, const int stream_type) // helper +{ + return n._wait(stream_type); +} +// Marks the beginning of a packed information +NetStream& pack_begin(NetStream& n) +{ + return n._pack_begin(); +} +NetStream& NetStream::_pack_begin(void) +{ + int rvalue=MPI_SUCCESS; + MPI_Status status; + if(!pack_in_progress) + { pack_in_progress = true; + packin_index = packout_index = 0; + pack_in = false; + pack_out = false; + if (!pending_input_packet) + { _probe(packed,pending_input_packet); + if(pending_input_packet) + rvalue = MPI_Recv(packin_buffer, MAX_PACK_BUFFER_SIZE, NET_PACKED, + default_source, PACKED_STREAM_TAG, my_communicator, &status); + } + } + return (*this); +} +// Marks the end of a packed and flush it to the net +NetStream& pack_end(NetStream& n) +{ + return n._pack_end(); +} +NetStream& NetStream::_pack_end(void) +{ + int rvalue, mypid; + if (pack_in_progress) + { + if(pack_out) + { if(broadcast) // Packet broadcast + { broadcast = false; + _my_pid(&mypid); + rvalue = MPI_Bcast(packout_buffer,packout_index,NET_PACKED, + mypid,my_communicator); + assert(rvalue==MPI_SUCCESS); + } + else + { rvalue = MPI_Send(packout_buffer, packout_index, NET_PACKED, + default_target,PACKED_STREAM_TAG,my_communicator); + assert(rvalue==MPI_SUCCESS); + } + } + pack_in_progress = false; + pack_in = pack_out = false; + packin_index = packout_index = 0 ; + } + return (*this); +} +// Check whether there are awaiting data +NetStream& probe(NetStream& n, const int stream_type, int& pending) +{ + return n._probe(stream_type, pending); +} +NetStream& NetStream::_probe(const int stream_type, int& pending) +{ + MPI_Status status; + int rvalue; + assert(stream_type==regular||stream_type==packed||stream_type==any); + rvalue = MPI_Iprobe(default_source,stream_type,my_communicator,&pending,&status); + assert(rvalue==MPI_SUCCESS); + return (*this); +} +// Broadcast a message to all the processes +NetStream& broadcast(NetStream& n) +{ + return n._broadcast(); +} +NetStream& NetStream::_broadcast(void) +{ + broadcast = true; + return (*this); +} +// PRIVATE SERVICES +// =================================================================================== +// Usually, the length is the number of bytes for every net type +// When packing we must use in the pack calls the numer of items (length divided by type size) +// Any char is encoded with a leading field of length +void NetStream::send(void* d, const int len, const NET_TYPE type, const int target) +{ + int rvalue = MPI_SUCCESS, length = len; + // PACKING SERVICE + if(pack_in_progress) + { + pack_out = true; + assert(pack_out!=pack_in); // Error condition + if(type==NET_CHAR) + send(&length,sizeof(NET_INT),NET_INT,target); // Recursive call to store string length + else + length = 1; + rvalue = MPI_Pack(d,length,type,packout_buffer,MAX_PACK_BUFFER_SIZE,&packout_index,my_communicator); + assert(rvalue==MPI_SUCCESS); + return; + } + if(broadcast) // Regular broadcast, packed broadcast managed in _pack_end() + { int mypid; + + broadcast = false; + _my_pid(&mypid); + rvalue = MPI_Bcast(d,len,type,mypid,my_communicator); + assert(rvalue==MPI_SUCCESS); + return; + } + rvalue = MPI_Send(d,len,type,target,REGULAR_STREAM_TAG,my_communicator); + assert(rvalue==MPI_SUCCESS); +} +void NetStream::rcv (void* d, const int len, const NET_TYPE type, const int source) +{ MPI_Status status; + int rvalue = MPI_SUCCESS, length=len; + if(pack_in_progress) + { +// if(!pack_in && !pending_input_packet) +// rvalue = MPI_Recv(packin_buffer, MAX_PACK_BUFFER_SIZE, NET_PACKED, +// default_source, PACKED_STREAM_TAG, my_communicator, &status); + pack_in = true; + pending_input_packet = false; + assert(pack_out!=pack_in); + if(type==NET_CHAR) + rcv(&length,sizeof(NET_INT),NET_INT,source); // Gets the string length + else + length = 1; + rvalue = MPI_Unpack(packin_buffer, MAX_PACK_BUFFER_SIZE, &packin_index, d, + length, type, my_communicator); + assert(rvalue==MPI_SUCCESS); + return; + } + + rvalue=MPI_Recv(d,len,type,source,REGULAR_STREAM_TAG,my_communicator,&status); + assert(status.MPI_ERROR==MPI_SUCCESS); + assert(rvalue==MPI_SUCCESS); +} +/////////////////////////////////////// GROUP MANAGEMENT //////////////////////////////// +// Set the netstream to a new communicator +void NetStream::set_communicator(NET_Comm comm) +{ + my_communicator = comm; +} +// Get the present communicator in this netstream +NET_Comm NetStream::get_communicator(void) +{ + return my_communicator; +} +// Create a new group inside the present communicator +NET_Comm NetStream::create_group(NET_Comm comm, int color, int key) +{ int rvalue; + NET_Comm newcomm; + rvalue=MPI_Comm_split(comm,color,key,&newcomm); + assert(rvalue==MPI_SUCCESS); + return newcomm; +} +// Create a bridge between local and remote MATCHING call +NET_Comm NetStream::create_inter_group(NET_Comm lcomm, int lrank, NET_Comm bcomm, int rrank, int strtype) +{ int rvalue; + NET_Comm newcomm; + rvalue=MPI_Intercomm_create(lcomm,lrank,bcomm,rrank,strtype,&newcomm); + assert(rvalue==MPI_SUCCESS); + return newcomm; +} diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/netstream.hh b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/netstream.hh new file mode 100644 index 0000000000000000000000000000000000000000..5c24a3dc072e2d1dbed9ebecef2b7d6190e51683 --- /dev/null +++ b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/netstream.hh @@ -0,0 +1,161 @@ +/*************************************************************************** + *** netstream.cc *** + *** v1.6 - July 2001 *** + ** *** + *** v1.5 - March 2001 *** + *** v1.0 - November 2000 *** + *** *** + *** v1.5 extends v1.0: *** + *** .- Changes metods init() and finalize() to be static *** + *** .- Incorporates process group management *** + *** .- Do not consider LEDA anymore *** + *** .- Contains a method "int my_pid()" for easy invokations *** + *** .- Adds "unsigned" and "long double" input/output *** + *** *** + *** v1.6 extends v1.5: *** + ** .- Internal in/out buffers for packed separated *** + *** *** + *** Communication services for LAN/WAN use following the message *** + *** passing paradigm. *** + *** STREAM C++ VERSION *** + *** MPI implementation *** + *** Developed by Erique Alba *** + ***************************************************************************/ +#ifndef INC_netstream +#define INC_netstream +#include "mpi.h" +#include <assert.h> +#include <string.h> +// Class NetStream allows to define and use network streams trhough LAN and WAN +#define REGULAR_STREAM_TAG 0 // Used for tagging MPI regular messages +#define PACKED_STREAM_TAG 1 // Used for tagging MPI packet messages +#define NET_TYPE MPI_Datatype // Network allowable data types +#define NET_BOOL MPI_CHAR // Bools like chars +#define NET_CHAR MPI_CHAR +#define NET_SHORT MPI_SHORT +#define NET_INT MPI_INT +#define NET_LONG MPI_LONG +#define NET_UNSIGNED_CHAR MPI_UNSIGNED_CHAR +#define NET_UNSIGNED_SHORT MPI_UNSIGNED_SHORT +#define NET_UNSIGNED MPI_UNSIGNED +#define NET_UNSIGNED_LONG MPI_UNSIGNED_LONG +#define NET_FLOAT MPI_FLOAT +#define NET_DOUBLE MPI_DOUBLE +#define NET_LONG_DOUBLE MPI_LONG_DOUBLE +#define NET_BYTE MPI_BYTE +#define NET_PACKED MPI_PACKED +#define NET_Comm MPI_Comm +#define MAX_MSG_LENGTH 204800 // Max length of a message +#define MAX_PACK_BUFFER_SIZE 204800 // Max length of a packed message +// Help structure for manipulators having one int& argument +class NetStream; +struct smanip1c // "const int" +{ NetStream& (*f)(NetStream&, const int); // The ONE argument function + int i; // The argument + smanip1c( NetStream&(*ff)(NetStream&,const int), int ii) : f(ff), i(ii) {} // Constuctor +}; +struct smanip1 // "int*" note: references do not work! "int&" +{ NetStream& (*f)(NetStream&, int*); // The ONE argument function + int* i; // The argument + smanip1( NetStream&(*ff)(NetStream&, int*), int* ii) : f(ff), i(ii) {} // Constuctor +}; +// Tags for the available streams +const int any = MPI_ANY_TAG; // Tag value valid for any stream +const int regular = REGULAR_STREAM_TAG; // Tag value for regular stream of data +const int packed = PACKED_STREAM_TAG; // Tag value for packed stream of data +// Tags for sources +const int any_source = MPI_ANY_SOURCE; // Tag value valid for any source +class NetStream +{ + public: + NetStream (); // Default constructor + // Constructor with source integer left unchanged + NetStream (int, char **); // Init the communications + ~NetStream (); // Default destructor + static void init(int,char**); // Init the communication system. Invoke it only ONCE + static void finalize(void); // Shutdown the communication system. Invoke it ONCE + // GROUP management + void set_communicator(NET_Comm comm); // Set the netstream to a new communicator + NET_Comm get_communicator(void); // Get the present communicator in this netstream + static NET_Comm create_group(NET_Comm comm, int color, int key); // Create a new group inside the present communicator + // Create a bridge between local and remote MATCHING call + static NET_Comm create_inter_group(NET_Comm lcomm, int lrank, NET_Comm bcomm, int rrank, int strtrype); + +// BASIC INPUT SERVICES <comments> BASIC OUTPUT SERVICES +// ============================================================================================================ + NetStream& operator>> (bool& d); NetStream& operator<< (bool d); + NetStream& operator>> (char& d); NetStream& operator<< (char d); + NetStream& operator>> (short& d); NetStream& operator<< (short d); + NetStream& operator>> (int& d); NetStream& operator<< (int d); + NetStream& operator>> (long& d); NetStream& operator<< (long d); + NetStream& operator>> (float& d); NetStream& operator<< (float d); + NetStream& operator>> (double& d); NetStream& operator<< (double d); + NetStream& operator>> (char* d); /*NULL terminated*/ NetStream& operator<< (char* d); + NetStream& operator>> (void* d); /*NULL terminated*/ NetStream& operator<< (void* d); + // Extended data types from version 1.5 on + NetStream& operator>> (unsigned char& d); NetStream& operator<< (unsigned char d); + NetStream& operator>> (unsigned short int& d); NetStream& operator<< (unsigned short int d); + NetStream& operator>> (unsigned int& d); NetStream& operator<< (unsigned int d); + NetStream& operator>> (unsigned long int& d); NetStream& operator<< (unsigned long int d); + NetStream& operator>> (long double& d); NetStream& operator<< (long double d); + int pnumber(void); // Returns the number of processes + bool broadcast; // Determines whether the next sent message is for broadcasting + // Input MANIPULATORS for modifying the behavior of the channel on the fly + // NO ARGUMENTS + NetStream& operator<< (NetStream& (*f)(NetStream& n)) { return f(*this); } // NO arguments + NetStream& _barrier(void); // Sit and wait until all processes are in barrier + NetStream& _pack_begin(void); // Marks the beginning of a packed information + NetStream& _pack_end(void); // Marks the end of a packed and flush it to the net + NetStream& _probe(const int stream_type, int& pending); // Check whether there are awaiting data + NetStream& _broadcast(void); // Broadcast a message to all the processes + // ONE ARGUMENT + // "const int" + NetStream& operator<< (smanip1c m) { return m.f((*this),m.i); }// ONE int& argument constant + // "int*" + NetStream& operator<< (smanip1 m) { return m.f((*this),m.i); }// ONE int& argument + // BASIC CLASS METHODS FOR MANIPULATORS + NetStream& _my_pid(int* pid); // Returns the process ID of the calling process + NetStream& _wait(const int stream_type); // Wait for an incoming message in the specified stream + NetStream& _wait2(const int stream_type, int& tipo); + NetStream& _set_target(const int p); // Stablish "p" as the default receiver + NetStream& _get_target(int* p); // Get into "p" the default receiver + NetStream& _set_source(const int p); // Stablish "p" as the default transmitter + NetStream& _get_source(int* p); // Get into "p" the default transmitter + // AUXILIAR PUBLIC METHODS FOR ALLOWING EASY MANAGEMENTS OF NETSTREAMS + int my_pid(void); // Returns the process ID of the calling process + private: + int default_target, default_source; // Default process IDs to send-recv data to-from + bool pack_in_progress; // Defines whether a packet is being defined with "pack_begin-pack_end" + int packin_index; // Index to be used for extracting from a IN packed message - v1.6 + int packout_index; // Index to be used for adding to an OUT packed message - v1.6 + int pending_input_packet;//Is there a pending packet already read into the IN buffer? - v1.6 + char* packin_buffer; // Buffer to temporary storage of the IN packed being defined - v1.6 + char* packout_buffer; // Buffer to temporary storage of the OUT packed being defined - v1.6 + bool pack_in, pack_out; // Define whether input-output packed message is being used + void reset(void); // Reset member variables of this class + NET_Comm my_communicator; // Communicator of this netstream + void send(void* d, const int len, const NET_TYPE type, const int target); + void rcv (void* d, const int len, const NET_TYPE type, const int source); +}; // class NetStream + // MANIPULATORS (must be static or non-member methods in C++ -mpiCC only allows non-member!-) + // NO ARGUMENTS + NetStream& barrier(NetStream& n); // Sit and wait until all processes are in barrier + NetStream& broadcast(NetStream& n); // Broadcast a message to all the processes + NetStream& pack_begin(NetStream& n); // Marks the beginning of a packed information + NetStream& pack_end(NetStream& n); // Marks the end of a packed and flush it to the net + // ONE ARGUMENT + NetStream& __my_pid(NetStream& n, int* pid); // Returns the process ID of the calling process + inline smanip1 my_pid(int* pid){ return smanip1(__my_pid,pid); } // manipulator + NetStream& __wait(NetStream& n, const int stream_type);// Wait for an incoming message - helper + inline smanip1c wait(const int stream_type){ return smanip1c(__wait,stream_type); } // manipulator + NetStream& __set_target(NetStream& n, const int p); // Stablish "p" as the default receiver + inline smanip1c set_target(const int p){ return smanip1c(__set_target,p); } // manipulator + NetStream& __get_target(NetStream& n, int* p); // Get into "p" the default receiver + inline smanip1 get_target(int* p){ return smanip1(__get_target,p); } // manipulator + NetStream& __set_source(NetStream& n, const int p); // Stablish "p" as the default transmitter + inline smanip1c set_source(const int p){ return smanip1c(__set_source,p); } // manipulator + NetStream& __get_source(NetStream& n, int* p); // Get into "p" the default transmitter + inline smanip1 get_source(int* p){ return smanip1(__get_source,p); } // manipulator + // TWO ARGUMENTS - not used yet + NetStream& probe(NetStream& n, const int stream_type, int& pending); // Check whether there are awaiting data +#endif diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/netstream.o b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/netstream.o new file mode 100644 index 0000000000000000000000000000000000000000..fb40ece360fb739d83d40beffbf1b01c031a15aa Binary files /dev/null and b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/netstream.o differ diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/random.hh b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/random.hh new file mode 100644 index 0000000000000000000000000000000000000000..ae6bde6ae8230c733aeb341be926e21fc5648d94 --- /dev/null +++ b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/random.hh @@ -0,0 +1,50 @@ +/****************************************************************************** +*** *** +*** Este fichero contiene funciones (en general inlines) para generar núme- *** +*** ros aleatorios y otras funciones relacionadas con ello *** +*** *** +*******************************************************************************/ + +#ifndef INC_random_hh +#define INC_random_hh + +#include <stdlib.h> + + +inline double undefined () +{ + double zero = 0; + return zero/zero; +} + +// Returns a valeu greater than any other +inline double infinity () +{ + double one=1.0; + double zero=0.0; + return one/zero; +} + +// Returns a random number in [0,1]. +inline double rand01 () +{ + return drand48(); +} + +// Returns a random number +inline int rand_int (float min,float max) +{ + int value=rand(); + int range= (int)(max - min); + int order = value % (range+1); + return ((int)min + order); +} + +// selects a seed +inline void random_seed(long int seed) +{ + srand48(seed); + srand(seed); +} + +#endif diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/time.hh b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/time.hh new file mode 100644 index 0000000000000000000000000000000000000000..088f4d5ea51f4a597ec1f0609ae5e175eae0a055 --- /dev/null +++ b/ProyectoFinal/AlgoritmoGenetico/malva/inc/Mallba/time.hh @@ -0,0 +1,43 @@ +/****************************************************************************** +*** *** +*** Este fichero incluye algunas funciones útiles para la medicion de tiem- *** +*** pos. *** +*** *** +******************************************************************************/ + +#ifndef INC_time_hh +#define INC_time_hh + +#define MAXTIME 4294 + +#include <time.h> +#include <sys/time.h> +#include <unistd.h> + +// return time in microseconds +inline float _used_time() +{ + struct timeval tv ; + static long tiempo = -1; + float u_clock; + + gettimeofday(&tv,NULL); + + if(tiempo < 0) tiempo = tv.tv_sec; + + u_clock = ((float)(tv.tv_sec - tiempo)*1000000.0) + ((float)tv.tv_usec); + return u_clock; + +} + + +inline float _used_time(float time) +{ + float dif=_used_time() - time; + if (dif<0) dif = 0; + return dif; +} + + + +#endif diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainLan b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainLan index dab1dc79bf353d0f899df5ff03cb425d66216531..ab6538e4f63e54e48c3ad994081e40e4ac249c95 100755 Binary files a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainLan and b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainLan differ diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainSeq b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainSeq index 55e5b86acc0295e756cbd36ef7de88c13a26e95d..83e93383c8484d0538e85437637a30b8e1cc4bb0 100755 Binary files a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainSeq and b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainSeq differ diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/datos_columnas b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/datos_columnas index 03e3fe4955750de9c9a6e4208e3a5bd2838b4f34..ca3e2b162b6ccea26fc8b4bc0c636566f34298de 100644 --- a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/datos_columnas +++ b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/datos_columnas @@ -1,11 +1,11 @@ 10 -1 3 -2 1 1 1 -1 1 1 -1 1 3 -3 1 1 +1 4 +2 1 +3 6 1 1 1 1 -1 2 -2 1 1 -1 2 2 -4 1 1 +2 1 1 1 +1 3 +2 1 +1 2 1 +1 3 1 +1 4 diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/datos_filas b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/datos_filas index 36f7a6c6d9c240da96dff43f6afb636214a76fe4..b005d6fa8406a6886713579cc56f188b50c5cbda 100644 --- a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/datos_filas +++ b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/datos_filas @@ -1,11 +1,11 @@ 10 -5 1 -1 1 1 -1 3 +2 3 +1 +3 1 +1 3 1 +2 3 2 1 3 -1 1 1 +1 2 1 1 +1 1 3 1 +3 1 1 1 3 1 -1 1 -1 1 1 1 -5 3 -1 1 1 diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.cfg b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.cfg index c7caf7a288f728c5dade1cf41a5313771d977886..681f85af314cc8bf32a74b0d48f7cb021d86c3b5 100644 --- a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.cfg +++ b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.cfg @@ -1,12 +1,12 @@ 10 // number of independent runs -100 // number of generations -60 // number of individuals +1000 // number of generations +100 // number of individuals 100 // size of offsprings in each generation 1 // if replaces parents for offsprings, or only offsprings may be new parents 1 // display state ? Selections // selections to apply 1 3 // selection of parents -2 0 // selection of offsprings +2 4 // selection of offsprings Intra-Operators // operators to apply in the population 0 0.6 // crossover & its probability 1 1.0 0.01 // mutation & its probability diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.cc b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.cc index 87caff6b03724ae44b78cdcdcbe666d90859887c..ccb9c22dd843e603e9e69f96682db151807ed0a5 100644 --- a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.cc +++ b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.cc @@ -476,7 +476,7 @@ skeleton newGA double Solution::fitness () { - //cout << "ESTOY ENTRANDO A FITNESS" << endl; + //cout << "ESTOY ENTRANDO A FITNESS" << endl; double fitness = 0.0; std::vector<std::vector<int>> datosDeFilas = _pbm.datosFilas(); std::vector<int> cantGruposDeFila = _pbm.cantGruposFila(); @@ -487,6 +487,7 @@ skeleton newGA int cantGrupos= cantGruposDeFila[i]; int iteradorGrupos=0; int resultado=0; //Resultado de la diferencia entre el bloque que tiene que haber y el bloque en el juego + int cantUnos=0; bool terminoBloque = true; while (columna < _pbm.cantColumnas()) { @@ -494,6 +495,7 @@ skeleton newGA { cantSeguidos++; terminoBloque=false; + cantUnos++; } else { @@ -516,12 +518,38 @@ skeleton newGA } columna++; //Si se termino la columna y faltan bloques - if((columna == _pbm.cantColumnas()) && (iteradorGrupos < cantGrupos)) - { - int restar= cantGrupos - iteradorGrupos; - fitness= fitness + 2100*restar; - } + } + if (cantSeguidos > 0) + { + if (iteradorGrupos < cantGruposDeFila[i]) + { + resultado= abs(datosDeFilas[i][iteradorGrupos] - cantSeguidos); //valor absoluto de la resta + } + else + { + resultado = cantSeguidos; + } + fitness= fitness + 1000 * resultado; + terminoBloque=true; + iteradorGrupos++; + resultado=0; + } + if(iteradorGrupos != cantGrupos) + { + int restar= abs(cantGrupos - iteradorGrupos); + fitness= fitness + 1100*restar; + } + int totalUnos = 0; + for(int j = 0; j< cantGrupos; j++) + { + totalUnos += datosDeFilas[i][j]; + } + if(cantUnos != totalUnos) + { + int restar= abs(totalUnos - cantUnos); + fitness= fitness + 300*restar; + } } extern int currentBestFitness; if (fitness < currentBestFitness) @@ -540,7 +568,7 @@ skeleton newGA if (_var[col][fil] != 0 && _var[col][fil] != 1) { cout << "fitness end" << endl; - exit(EXIT_FAILURE); + //exit(EXIT_FAILURE); } } //cout << endl; @@ -552,7 +580,6 @@ skeleton newGA cout << "found sol lmao" << endl; } return fitness; - } char *Solution::to_String() const diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.o b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.o index 5870685a5daf0898db697bf8fad0813ac1df73ec..efc6ca1890255eb78b74611e83076191427cb970 100644 Binary files a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.o and b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.o differ diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/res/sol.txt b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/res/sol.txt index f5ffab34a78455b600466253486c4c2a238a1603..9b721636576a19cd39eca3dacc220076fe4468e8 100644 --- a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/res/sol.txt +++ b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/res/sol.txt @@ -3,14 +3,14 @@ STATISTICS OF TRIALS ------------------------------------------------------------------ -1 14000 51100 1960 19 11244 56560 -2 14200 56400 1460 14 7515 52727 -3 9000 55200 2460 24 10842 44523 -4 12200 56400 2060 20 8742 43456 -5 11000 52200 9660 96 48456 50138 -6 15100 54300 3960 39 17263 47436 -7 14400 52200 1760 17 12878 56495 -8 18100 63500 1960 19 8947 50044 -9 10100 51200 1260 12 5809 44501 -10 14300 58300 7560 75 34227 44742 +1 9200 59800 2600 25 27064 589222 +2 2600 63700 4300 42 21797 493408 +3 5200 58000 11400 113 52831 481602 +4 5900 59300 13900 138 83146 531894 +5 7300 52800 49400 493 287669 550838 +6 2600 54500 21500 214 115742 534893 +7 5900 58100 11000 109 60535 527605 +8 7800 58100 1900 18 10128 489493 +9 2600 58500 4400 43 27452 515218 +10 12400 61600 32900 328 184002 544424 ------------------------------------------------------------------ diff --git a/malva/inc/Mallba b/malva/inc/Mallba deleted file mode 120000 index 5cd551cf2693e4b4f65d7954ec621454c2b20326..0000000000000000000000000000000000000000 --- a/malva/inc/Mallba +++ /dev/null @@ -1 +0,0 @@ -../src \ No newline at end of file diff --git a/malva/inc/Mallba/Makefile b/malva/inc/Mallba/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..b6d64fba4ba48d6a207029d215eedf5df8a69730 --- /dev/null +++ b/malva/inc/Mallba/Makefile @@ -0,0 +1,14 @@ +include ../environment + +OBJS = States.o netstream.o + +all: $(OBJS) + +States.o: States.cc States.hh + $(CXX) $(CPPFLAGS) States.cc -c + +netstream.o: netstream.cc netstream.hh + $(CXX) $(CPPFLAGS) netstream.cc -c + +clean: + rm -f *.o *~ *% diff --git a/malva/inc/Mallba/Matrix.hh b/malva/inc/Mallba/Matrix.hh new file mode 100644 index 0000000000000000000000000000000000000000..cc30ae7e23189594a197d99982205dac483fd37b --- /dev/null +++ b/malva/inc/Mallba/Matrix.hh @@ -0,0 +1,337 @@ +/***************************************************************************** +*** *** +*** Este fichero decribe el template Matrix, para el manejo de matrices. *** +*** Tambien permite el manejo de vectores, los que trata como matrices cuya*** +*** primera dimension es 1. *** +*** *** +*****************************************************************************/ +#ifndef _MATRIX +#define _MATRIX + +#include "Messages.h" +#include <assert.h> + +template <class T> class Matrix +{ + private: + T *_matrix; + T nulo,neutro,inverso; + int _dimX,_dimY; + + public: + Matrix() + { + _dimX = _dimY = 0; + _matrix = NULL; + } + + Matrix(const int x,const int y,const T nulo = 0.0,const T neutro = 1.0,const T inverso = -1.0) + { + assert(x >= 1); + assert(y >= 1); + + int k = 0; + + _dimX = x; + _dimY = y; + + this->nulo = nulo; + this->neutro = neutro; + this->inverso = inverso; + + _matrix = new T [_dimX*_dimY]; + if(!_matrix) show_message(7); + + for(int i = 0; i < x; i++) + { + for(int j = 0; j < y ; j++) + { + _matrix[k] = (i!=j?nulo:neutro); + k++; + } + + } + } + + Matrix(const int y,const T nulo = 0.0,const T neutro = 1.0,const T inverso = -1.0) + { + assert(y >= 1); + + _dimX = 1; + _dimY = y; + + this->nulo = nulo; + this->neutro = neutro; + this->inverso = inverso; + + _matrix = new T [_dimY]; + if(!_matrix) show_message(7); + + _matrix[0] = neutro; + for(int j = 1; j < y ; j++) + _matrix[j] = nulo; + } + + Matrix(const Matrix<T> &m) + { + + _dimX = m.dimX(); + _dimY = m.dimY(); + + nulo = m.nulo; + neutro = m.neutro; + inverso = m.inverso; + + _matrix = new T [_dimX*_dimY]; + if(!_matrix) show_message(7); + + for(int i = 0; i < (_dimX*_dimY); i++) + { + _matrix[i] = m[i]; + } + } + + + ~Matrix() + { + remove(); + } + + T &operator()(const int x,const int y) const + { + if((x >= _dimX) || (y >= _dimY)) + show_message(14); + + return (T&)(*(_matrix + (x*_dimX) + y)); + } + + T &operator[](const int y) const + { + if(y >= (_dimX*_dimY)) + show_message(14); + + return (T&)(*(_matrix + y)); + } + + T &operator()(const int y) const + { + if(y >= (_dimX*_dimY)) + show_message(14); + + return (T&)(*(_matrix + y)); + } + + Matrix<T> &operator=(const Matrix<T> &m) + { + remove(); + + _dimX = m.dimX(); + _dimY = m.dimY(); + + _matrix = new T [_dimX*_dimY]; + if(!_matrix) show_message(7); + + for(int i = 0; i < (_dimX*_dimY); i++) + { + _matrix[i] = m[i]; + } + + return (*this); + } + + bool operator==(const Matrix<T> &m) const + { + if((_dimX != m.dimX()) || (_dimY != m.dimY())) + return false; + + for(int i = 0; i < (_dimX*_dimY); i++) + if(_matrix[i] != m[i]) return false; + + return true; + } + + bool operator!=(const Matrix<T> &m) const + { + return !((*this) == m); + } + + Matrix<T> operator*(const Matrix<T> &m) + { + int x = (m.dimX()!=_dimY?0:_dimX); + int y = (x!=0?m.dimY():0); + T acum = nulo; + + Matrix<T> res(x,y); + + for(int i = 0; i < _dimX; i++) + for(int j = 0; j < m.dimY(); j++) + { + acum = nulo; + for( int k = 0; k < _dimY; k++) + acum += (*this)(i,k)* m(k,j); + res(i,j) = acum; + } + + return Matrix<T>(res); + + } + + Matrix<T> &operator*=(const Matrix<T> &m) + { + int x = (m.dimX()!=_dimY?0:_dimX); + int y = (x!=0?0:m.dimY()); + T acum = nulo; + + Matrix<T> res(x,y); + + for(int i = 0; i < _dimX; i++) + for(int j = 0; j < m.dimY(); j++) + { + acum = nulo; + for( int k = 0; k < _dimY; k++) + acum += (*this)(i,k)*m(k,j); + res(i,j) = acum; + } + + (*this) = res; + + return (*this); + + } + + Matrix<T> operator*(const T &elem) + { + Matrix<T> res(_dimX,_dimY); + + for(int i = 0; i < (_dimX*_dimY); i++) + res[i] = _matrix[i] * elem; + + return Matrix(res); + } + + Matrix<T> &operator*=(const T &elem) + { + for(int i = 0; i < (_dimX*_dimY); i++) + _matrix[i] *= elem; + + return (*this); + } + + Matrix<T> operator+(const Matrix<T> &m) + { + int x = (m.dimX()!=_dimX?0:_dimX); + int y = (m.dimY()!=_dimY?0:_dimY); + + Matrix<T> res(x,y); + + for(int i = 0; i < (x*y); i++) + res[i] = _matrix[i] + m[i]; + + return Matrix<T>(res); + } + + Matrix<T> &operator+=(const Matrix<T> &m) + { + int x = (m.dimX()!=_dimX?0:_dimX); + int y = (m.dimY()!=_dimY?0:_dimY); + + for(int i = 0; i < (x*y); i++) + _matrix[i] += m[i]; + + return (*this); + } + + Matrix<T> operator-(const Matrix<T> &m) + { + Matrix<T> res(); + + res = m * inverso; + return (*this) + res; + } + + + Matrix<T> &operator-=(const Matrix<T> &m) + { + Matrix<T> res(); + + res = m * inverso; + (*this) += res; + + return (*this); + } + + Matrix<T> Traspuesta() + { + Matrix<T> res(_dimY,_dimX); + + for(int i = 0; i < _dimX; i++) + for(int j = 0; j < _dimY; j++) + res(j,i) = (*this)(i,j); + + return Matrix<T>(res); + } + + Matrix<T> &nula() + { + for(int i = 0; i < (_dimX*dimY); i++) + _matrix[i] = nulo; + + return (*this); + } + + Matrix<T> &identity() + { + register int k = 0; + + for(int i = 0; i < _dimX; i++) + for(int j = 0; j < _dimY; j++) + { + _matrix[k] = (i!=j?nulo:neutro); + k++; + } + + return (*this); + } + + unsigned int size() const + { + return (sizeof(T)*_dimX*_dimY); + } + + char *to_string() const + { + return (char *) _matrix; + } + + Matrix<T> &to_Matrix(char *_cadena) + { + T *ptr = (T *)_cadena; + + for(int i = 0; i < (_dimX*_dimY) ; i++) + { + _matrix[i] = *ptr; + ptr++; + } + + return (*this); + } + + + int dimX() const + { + return _dimX; + } + + int dimY() const + { + return _dimY; + } + + void remove() + { + if(_matrix != NULL) + delete [] _matrix; + } +}; + +#endif diff --git a/malva/inc/Mallba/Messages.h b/malva/inc/Mallba/Messages.h new file mode 100644 index 0000000000000000000000000000000000000000..e46b1d823977e76b1c942dda51104206102cc62f --- /dev/null +++ b/malva/inc/Mallba/Messages.h @@ -0,0 +1,112 @@ +/*****************************************************************************/ +/*** ***/ +/*** Modificado por G.J.L.P. ***/ +/*** Añadidos nuevos mensajes que indican falta de algún ***/ +/*** Fichero de Configuración (No específico para ningún ***/ +/*** problema) o nuevos errores. ***/ +/*** ***/ +/*****************************************************************************/ + +#ifndef RLFAP_MESSAGES +#define RLFAP_MESSAGES + +#ifndef MAX_BUFFER +#define MAX_BUFFER 200 +#endif + +#include <iostream> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +using namespace std; + +inline void show_message(int value) +{ + switch (value) + { + case 1: cout << endl << "Error: number of arguments in the execution call is incorrect !!" + << endl; break; + case 2: cout << endl << "Error: It's imposible find Configuration file !!" << endl; + break; + /* Específicos de RLFAP */ + case 3: cout << endl << "Error: It is imposible find the Celar problem definition file (cst.txt) !!" + << endl; break; + case 4: cout << endl << "Error: It is imposible find the Celar domains file (dom.txt) !!" + << endl; break; + case 5: cout << endl << "Error: It is imposible find the Celar links file (var.txt) !!" + << endl; break; + case 6: cout << endl << "Error: It is imposible find the Celar constraints file (ctr.txt) !!" + << endl; break; + /* Fallos de Memoria */ + case 7: cout << endl << "Error: No avalible memory for \"malloc\" operation !!" << endl; + break; + case 8: cout << endl << "Error: in \"free\" operation !!" << endl; + break; + /* Específicos del MaxCut */ + case 9: cout << endl << "Error: It is imposible find the Maxcut file (Maxcut.txt) !!" + << endl; break; + /* Genéricos de Falta de ficheros de configuracion adicionales al mensaje 2 */ + case 10: cout << endl << "Error: It's imposible find Configuration file (Config.cfg) !!" + << endl; break; + case 11: cout << endl << "Error: It's imposible find Skeleton Configuration File (Ske.cfg) !!" + << endl; break; + case 12: cout << endl << "Error: It's imposible find Instance Problem File !!" << endl; + break; + case 13: cout << endl << "Error: It's imposible find Resultate File !!" << endl; + break; + case 14: cout << endl << "Error: Index out of Range !!" << endl; + break; + default: cout << endl << "Unkown Error !!" << endl; + } + + cout << endl << " " << endl; + exit(-1); +} + +inline void continue_question() +{ + fflush(stdout); + cout << endl << "Press any key to continue..." << endl; + fflush(stdin); + getc(stdin); +} + +inline void get_path(const char *source,char *target) +{ + int last = 0; + + for(int i = 0; i < strlen(source); i++) + { + target[i] = source[i]; + if(target[i] == '/') + last = i; + } + target[last+1] = '\0'; +} + +inline unsigned count_lines(char *file_name) // returns the number of lines of a file +{ + char line[MAX_BUFFER]; + FILE *file; + int count=0; + + if ((file=fopen(file_name,"r"))==NULL) + { + fflush(stdout); + printf("File not found !"); + } + + while (!feof(file)) + { + if (fgets(line,MAX_BUFFER,file)) count++; + else + { + fclose(file); + break; + } + } + return count; +} + +#endif diff --git a/malva/inc/Mallba/Rarray.h b/malva/inc/Mallba/Rarray.h new file mode 100644 index 0000000000000000000000000000000000000000..5d1a1c24919e00e84e1c1bf889021e2c04054dba --- /dev/null +++ b/malva/inc/Mallba/Rarray.h @@ -0,0 +1,145 @@ +/****************************************************************************** +*** *** +*** Template para el manejo dinámico de arrays *** +*** Añadido métodos para invertir todo o parte del array. *** +*** *** +******************************************************************************/ + +#ifndef ARRAY_INC +#define ARRAY_INC 1 +#include <iostream> +#include <assert.h> + +using namespace std; + +template<class E1> class Rarray +{ + private: + E1 *first; + int count; + + public: + Rarray() + { + first = NULL; + count = 0; + } + + ~Rarray() + { + remove(); + } + + E1* get_first() const + { + return first; + } + + void message_a(int cod) const + { + switch (cod) + { + case 1: cout << endl << "The size of array must be upper that 0 !!!" ; + } + } + + Rarray(const int size_a) + { + if (size_a<0) message_a(1); + first = new E1[size_a]; + count=size_a; + } + + void remove() + { + if (count!=0) + delete [] first; + } + + int size() const + { + return count; + } + + E1& operator[](int pos) const + { + return (E1&)(*(first + pos)); + } + + friend ostream& operator<< (ostream& os,const Rarray<E1>& a) + { + for (int i=0;i<a.size();i++) + os << endl << a[i]; + return os; + } + + Rarray<E1>& operator=(const Rarray<E1>& source) + { + remove(); + count = source.size(); + first = new E1[count]; + + for (int i=0;i<count;i++) + (*this)[i] = source[i]; + + return (*this); + } + + + Rarray<E1>& invert() + { + return invert(0,count-1); + } + + Rarray<E1>& invert(const int pos1, const int pos2) + { + int max,min,half,i,j; + E1 aux; + + if(pos1 > pos2) + { + max = pos1; + min = pos2; + } + else + { + max = pos2; + min = pos1; + } + + assert((min > 0) || (min < count-1)); + half = ((max-min)/2) + 1; + + for(i = min,j=max; i< half; i++,j--) + { + aux = first[min]; + first[min] = first[max]; + first[max] = aux; + } + + return (*this); + } + + Rarray<E1>& sort(int (*comp)(const E1 &,const E1 &)) + { + E1 aux; + int j; + + for (int i=1; i < count ; i++) + { + aux = first[i]; + j = i - 1; + while ( (comp(aux,first[j])) && (j >= 0) ) + { + first[j+1]= first[j]; + j--; + } + first[j+1] = aux; + } + + return (*this); + } + +}; // end of class + +#endif diff --git a/malva/inc/Mallba/Rlist.h b/malva/inc/Mallba/Rlist.h new file mode 100644 index 0000000000000000000000000000000000000000..f561d0bcd49bbffc7fe449f74252df988343f2ff --- /dev/null +++ b/malva/inc/Mallba/Rlist.h @@ -0,0 +1,415 @@ +/****************************************************************************** +*** *** +*** Este template sirve para el manejo de listas din�micas *** +*** *** +******************************************************************************/ + +#ifndef LIST_INC +#define LIST_INC 1 +#include <iostream> +#include <stdlib.h> +#include <Messages.h> + +using namespace std; + +template<class E> class Rlist_item +{ + public: + E *useful_data; // pointer to the structure stored in the list + Rlist_item<E> *next; + Rlist_item<E> *previous; + + Rlist_item(E& new_data) + { + useful_data=&new_data; + next=NULL; + previous=NULL; + } + + Rlist_item(E *new_data) + { + useful_data=new_data; + next=NULL; + previous=NULL; + } + + ~Rlist_item() + { + } + + Rlist_item<E>& next_item() + { + return *(next); + } + + Rlist_item<E>& previous_item() + { + return *(previous); + } + + bool is_last() + { + return (next==NULL); + } + + bool is_first() + { + return (previous==NULL); + } + + E& data() + { + return *(useful_data); + } +}; + +template<class E> class Rlist +{ + private: + Rlist_item<E> *first; // first item in the list + Rlist_item<E> *last; // last item un the list + int count; // number of items in the list + + public: + // constructor + Rlist() + { + first=NULL; + last=NULL; + count=0; + } + + // destructor + ~Rlist() + { + remove(); + } + + // Return the size of the list + int size() const + { return count; } + + // Go back to the initial state of the list + void reset() + { + first=NULL; + last=NULL; + count=0; + } + + // Add a item at the final of the list + Rlist<E>& append(E& new_item) + { + Rlist_item<E> *new_Rlist_item=new Rlist_item<E>(new_item); + if (first==NULL) + { + first=new_Rlist_item; + new_Rlist_item->next=NULL; + new_Rlist_item->previous=NULL; + } + else + { + last->next=new_Rlist_item; + new_Rlist_item->previous=last; + } + last=new_Rlist_item; + count++; + return *this; + } + + Rlist<E>& append(E *new_item) + { + append(*new_item); + return (*this); + } + + // Add a item in a position ( pos ) of the list + Rlist<E>& add_pos(E& new_item, const int pos) + { + if (pos==size()-1) + return append(new_item); + + if (pos==-1) + return add(new_item,NULL); + else + return add(new_item,get_at(pos).useful_data); + } + + // Add a item in the list in the position next to "previous item" + Rlist<E>& add(E& new_item,E *previous_item) + { + if (first==NULL) + return append(new_item); + + Rlist_item<E> *new_Rlist_item=new Rlist_item<E>(new_item); + + if (previous_item==NULL) // Add the item like the first of the list + { + new_Rlist_item->next=first; + new_Rlist_item->previous=NULL; + first->previous=new_Rlist_item; + first=new_Rlist_item; + } + else + { + int previous_position=get_position(*previous_item); + if (previous_position==-1) return(*this); + Rlist_item<E> *previous_Rlist_item = &( get_at(previous_position)); + new_Rlist_item->next=previous_Rlist_item->next; + new_Rlist_item->previous=previous_Rlist_item; + if (previous_Rlist_item->next!=NULL) + (previous_Rlist_item->next)->previous=new_Rlist_item; + else last=new_Rlist_item; + previous_Rlist_item->next=new_Rlist_item; + } + count++; + return *this; + } + + // Return a pointer to the first item of the list + Rlist_item<E> *get_first() const + { return first; } + + // Assign a item like the first item in the list + void set_first(Rlist_item<E> *new_first) + { first=new_first; } + + // Return a pointer to the last item of the list + Rlist_item<E> *get_last() const + { return last; } + + // Assign a item like the last item in the list + void set_last(Rlist_item<E> *new_last) + { last=new_last; } + + // Return the item at position "pos" + E& operator[](int pos) const + { + return *(get_at(pos).useful_data); + } + + // Return the Rlist_item at position "pos" + Rlist_item<E>& get_at(int pos) const + { + Rlist_item<E> *present=first; + for (int k=0;k<size();k++) + if (k==pos) return *present; + else present=present->next; + } + + // Return the item position in the list + int get_position(const E& item) const // probado + { + Rlist_item<E> *present=first; + int i=0; + + while(present!=NULL) + { + if (present->useful_data==&item) return i; + i++; + present=present->next; + } + return -1; // the object has not been found + } + + // Delete a item of the list + Rlist<E>& delete_item(E& item) + { + int position = get_position(item); + + if (position==-1) return *this; + Rlist_item<E> *present=&(get_at(position)); + + if (&item==first->useful_data) // is the first + { + if (&item==last->useful_data) + { + delete(first->useful_data); + delete(first); + first=NULL; + last=NULL; + count=0; + } + else + { + first=first->next; + first->previous=NULL; + delete(present->useful_data); + delete(present); + count--; + } + } + else + { + if (&item==last->useful_data) + { + last=present->previous; + last->next=NULL; + delete(present->useful_data); + delete(present); + count--; + } + else + { + (present->next)->previous=present->previous; + (present->previous)->next=present->next; + delete(present->useful_data); + delete(present); + count--; + } + } + return *this; + } + + // Delete a item of the list without free the useful_data + Rlist<E>& delete_item_1(E& item) + { + int position = get_position(item); + + if (position==-1) return *this; + Rlist_item<E> *present=&(get_at(position)); + + if (&item==first->useful_data) // is the first + { + if (&item==last->useful_data) + { + delete(first); + first=NULL; + last=NULL; + count=0; + } + else + { + first=first->next; + first->previous=NULL; + delete(present); + count--; + } + } + else + { + if (&item==last->useful_data) + { + last=present->previous; + last->next=NULL; + delete(present); + count--; + } + else + { + (present->next)->previous=present->previous; + (present->previous)->next=present->next; + delete(present); + count--; + } + } + return *this; + } + + // Delete item at position "pos" + Rlist<E>& delete_item_by_position(const int pos) + { return delete_item(*(get_at(pos).useful_data)); } + + // Delete the last item in the list + Rlist<E>& delete_last() + { return delete_item(*(last->useful_data)); } + + // delete all items in the list + Rlist<E>& remove() + { + Rlist_item<E> *next,*present=first; + while (present!=NULL) + { + next=present->next; + delete(present->useful_data); + delete(present); + present=next; + } + first=NULL; + last=NULL; + count=0; + return *this; + } + + // Join a new list to this list + Rlist<E>& join(Rlist<E>& new_list) + { + if (new_list.size()==0) + return *this; + + if (first==NULL) + { + first=new_list.get_first(); + last=new_list.get_last(); + } + else + { + last->next=new_list.get_first(); + (new_list.get_first())->previous=last; + last = new_list.get_last(); + } + count += new_list.size(); + new_list.reset(); + return *this; + } + + // Show items of the list + friend ostream& operator<<(ostream& os, const Rlist<E>& list) + { + Rlist_item<E> *present=list.get_first(); + if (list.get_first()==NULL) os << endl << "THE LIST IS EMPTY !!"; + while (present!=NULL) + { + os << endl << (*(present->useful_data)); + // Falta el operador para stat. + // (habra que ver) + present=present->next; + } + return os; + } + + // Copy the list passed + Rlist<E>& operator=(const Rlist<E>& source) + { + E *new_item; + remove(); + if (source.first==NULL && source.last==NULL) + { + first=NULL; + last=NULL; + count=0; + } + else + { + for (int i=0;i<source.size();i++) + { + if ((new_item=(E *)malloc(sizeof(E)))==NULL) + show_message(7); + (*new_item)=*(source.get_at(i).useful_data); + append(*new_item); + } + } + return *this; + } + + // Invert the order of items in the list + Rlist<E>& invert() + { + Rlist_item<E> *present,*interchange; + + present=first; + + for (int i=0;i<size();i++) + { + interchange=present->next; + present->next=present->previous; + present->previous=interchange; + present=interchange; + } + interchange=first; + first=last; + last=interchange; + return (*this); + } +}; // end of class +#endif diff --git a/malva/inc/Mallba/States.cc b/malva/inc/Mallba/States.cc new file mode 100644 index 0000000000000000000000000000000000000000..f36d1979373c40068088fb6951f13a126bc033fe --- /dev/null +++ b/malva/inc/Mallba/States.cc @@ -0,0 +1,228 @@ +/****************************************************************************** + Modified by Carlos Cotta Porras + April 2001 + + Modified by G.J.L.P +******************************************************************************/ + + +#include <string.h> +#include "States.hh" + +// Methods of class State_Vble + + // constructors + State_Vble ::State_Vble () + { + name=NULL; + nitems=0; + length=0; + content=NULL; + } + + State_Vble ::State_Vble (const char *st_name) + { + name=strdup(st_name); + nitems=0; + length=0; + content=NULL; + } + + State_Vble::State_Vble (const char *st_name,StateCenter& sc) + { + name=strdup(st_name); + nitems=0; + length=0; + content=NULL; + sc.add(*this); + } + + State_Vble ::State_Vble (const char *st_name,const char *new_contents, unsigned long new_nitems, unsigned long new_length) + { + name=strdup(st_name); + nitems=0; + length=0; + content=NULL; + set_contents(new_contents,new_nitems,new_length); + } + + State_Vble ::State_Vble (const char *st_name,const char *new_contents, unsigned long new_nitems, unsigned long new_length,StateCenter& sc) + { + name=strdup(st_name); + nitems=0; + length=0; + content=NULL; + set_contents(new_contents,new_nitems,new_length); + sc.add(*this); + } + + + // Set the name of a state vble. + void State_Vble ::set_name (const char* st_name) // Set the name of a state vble. + { + if (name!=NULL) + free(name); + name=strdup(st_name); + } + + // Get the name of a state vble. + char* State_Vble ::get_name () const // Get the name of a state vble. + { + return name; + } + + // Number of basic items + unsigned long State_Vble ::get_nitems() const // Number of basic items + { + return nitems; + } + + // Get the total number of bytes + unsigned long State_Vble ::get_length () const // Get the total number of bytes + { + return length; + } + + // Fill up a state vble. + void State_Vble ::set_contents (const char *new_contents, unsigned long new_nitems, unsigned long new_length) + { + if (content!=NULL) + free(content); + content=(char *)malloc(new_nitems * new_length); + memcpy(content,new_contents,(new_nitems*new_length)); + nitems=new_nitems; + length=new_length; + } + + // Obtain the contents of a state vble. + void *State_Vble ::get_contents (char *read_contents, unsigned long& read_nitems, unsigned long& read_length) const + { + memcpy(read_contents,content,nitems * length); + read_nitems=nitems; + read_length=length; + return NULL; + } + + ostream& operator<< (ostream& os,const State_Vble& st) + { + os << endl << st.name + << endl << st.nitems + << endl << st.length + << endl << st.content; + + return os; + } + + State_Vble ::~State_Vble () + { + free(content); + free(name); + } + +// Methods of class StateCenter + + StateCenter::StateCenter():state_variables() + {} + + // searchs a state variable + State_Vble *StateCenter::find(const char *st_name) const + { + Rlist_item<State_Vble> *current_state=state_variables.get_first(); + + for (int i=0;i<state_variables.size();i++) + { + if (!(strcmp(current_state->data().get_name(),st_name))) + return &(current_state->data()); + current_state=¤t_state->next_item(); + } + return NULL; + } + + // Add one state variable + void StateCenter::add(State_Vble& st) + { + State_Vble *found_state=find(st.get_name()); + if (found_state==NULL) + state_variables.append(st); + else + cout << endl << "You are trying to introduce a state variable that is yet used in the skeleton !!" << st.get_name(); + } + + void StateCenter::add(State_Vble *st) + { + State_Vble *found_state=find(st->get_name()); + if (found_state==NULL) + state_variables.append(*st); + else + cout << endl << "You are trying to introduce a state variable that is yet used in the skeleton !!" << st->get_name(); + } + + // Remove one state variable + void StateCenter::remove(const char* st_name) + { + State_Vble *found_state=find(st_name); + if (found_state!=NULL) + state_variables.delete_item_1(*found_state); + } + + // Update the contents of one vble. + void StateCenter::update(const char* st_name, const State_Vble& st) const // Update the contents of one vble. + { + State_Vble *found_state=find(st_name); + if (found_state!=NULL) + { + char *a=(char *)malloc(found_state->get_nitems() * found_state->get_length()); + unsigned long nitems,length; + st.get_contents(a,nitems,length); + found_state->set_contents(a,nitems,length); + free(a); + } + } + + // Get a vble. with a given name + State_Vble& StateCenter::get(const char* st_name) const // Get a vble. with a given name + { + State_Vble *found_state=find(st_name); + return *found_state; + } + + // Allows an easy iterated extraction + State_Vble* StateCenter::get_next(const State_Vble& st) const + { + State_Vble *found_state=find(st.get_name()); + if (found_state==NULL) return NULL; + if ( state_variables.get_at(state_variables.get_position(*found_state)).is_last()) return NULL; + else return &(state_variables.get_at(state_variables.get_position(*found_state)).next_item().data()); + } + + // returns the number of state variables + unsigned int StateCenter::size() const + { + return state_variables.size(); + } + + // Obtain the contents of a state vble. of name st_name + void StateCenter::get_contents_state_variable(const char *st_name,char *read_contents, unsigned long& read_nitems, unsigned long& read_length) const + { + get(st_name).get_contents(read_contents,read_nitems,read_length); + } + + // Fill up a state vble.of name st_name + void StateCenter::set_contents_state_variable(const char *st_name,const char *new_contents, unsigned long new_nitems, unsigned long new_length) const + { + get(st_name).set_contents(new_contents,new_nitems,new_length); + } + + void StateCenter::removeAll() + { + while(state_variables.get_first()) + { + Rlist_item<State_Vble>* v = state_variables.get_first(); + remove(v->useful_data->get_name()); + } + } + + StateCenter::~StateCenter() + { + removeAll(); + } diff --git a/malva/inc/Mallba/States.hh b/malva/inc/Mallba/States.hh new file mode 100644 index 0000000000000000000000000000000000000000..e3a215f5066608ec161882d5ae0c1273944ceaa1 --- /dev/null +++ b/malva/inc/Mallba/States.hh @@ -0,0 +1,64 @@ +/******************************************************************************************************** +*** *** +*** Class StateCenter: Pool of state variables for skeletons *** +*** *** +*** Class State_Vble: State Variable of a skeleton *** +*** *** +*********************************************************************************************************/ + +#ifndef STATE_CENTER +#define STATE_CENTER 1 + +#include "Rlist.h" +#include <iostream> + +class StateCenter; + +class State_Vble +{ + private: + char *name; + unsigned long nitems; + unsigned long length; + char *content; + + public: + // constructors + State_Vble (); + State_Vble (const char *st_name); + State_Vble (const char *st_name,StateCenter& sc); + State_Vble (const char *st_name,const char *new_contents, unsigned long new_nitems, unsigned long new_length); + State_Vble (const char *st_name,const char *new_contents, unsigned long new_nitems, unsigned long new_length,StateCenter& sc); + + void set_name (const char* st_name); // Set the name of a state vble. + char* get_name () const; // Get the name of a state vble. + unsigned long get_nitems() const; + unsigned long get_length () const; // Get the total number of bytes + void set_contents (const char *new_contents, unsigned long new_nitems, unsigned long new_length); // Fill up a state vble. + void *get_contents (char *read_contents, unsigned long& read_nitems, unsigned long& read_length) const; // Obtain the contents of a state vble. + friend ostream& operator<< (ostream& os,const State_Vble& st); + ~State_Vble (); +}; + +class StateCenter +{ + private: + Rlist<State_Vble> state_variables; + + public: + StateCenter(); + State_Vble *find(const char *st_name) const; // search a state variable + void add(State_Vble& st); // Add one state variable + void add(State_Vble *st); // Add one state variable + void remove(const char* st_name); // Remove one state variable + void removeAll(); // Removes all variables + void update(const char* st_name, const State_Vble& st) const; // Update the contents of one vble. + State_Vble& get(const char* st_name) const; // Get a vble. with a given name + State_Vble* get_next(const State_Vble& st) const; // Allows an easy iterated extraction + unsigned int size() const; // returns the number of state variables + void get_contents_state_variable(const char *st_name,char *read_contents, unsigned long& read_nitems, unsigned long& read_length) const; // Obtain the contents of a state vble. of name st_name + void set_contents_state_variable(const char *st_name,const char *new_contents, unsigned long new_nitems, unsigned long new_length) const; // Fill up a state vble.of name st_name + ~StateCenter(); +}; + +#endif diff --git a/malva/inc/Mallba/States.o b/malva/inc/Mallba/States.o new file mode 100644 index 0000000000000000000000000000000000000000..f5e95c8928fb22e26363c12820d0e56fd98709d2 Binary files /dev/null and b/malva/inc/Mallba/States.o differ diff --git a/malva/inc/Mallba/mallba.hh b/malva/inc/Mallba/mallba.hh new file mode 100644 index 0000000000000000000000000000000000000000..e022ff848068593d7f2acc836d0741eb8dd70fb0 --- /dev/null +++ b/malva/inc/Mallba/mallba.hh @@ -0,0 +1,26 @@ +/***************************************************************************** +*** *** +*** Este fichero hace algunas declaraciones y declara algunos tipos comu- *** +*** nes a todos los algoritmos implementados. *** +*** *** +*****************************************************************************/ + +#ifndef INC_mallba_hh +#define INC_mallba_hh + +#define skeleton namespace +#define requires +#define provides +#define hybridizes(x) +#define inherits typedef +#define as + +enum Direction { minimize=-1,maximize=1}; + +extern const double plus_infinity; +extern const double minus_infinity; + +#define false 0 +#define true 1 + +#endif diff --git a/malva/inc/Mallba/netstream.cc b/malva/inc/Mallba/netstream.cc new file mode 100644 index 0000000000000000000000000000000000000000..932e89e56ab5eff64306137bd0ae97b1380a05e2 --- /dev/null +++ b/malva/inc/Mallba/netstream.cc @@ -0,0 +1,380 @@ +/*************************************************************************** + *** netstream.cc *** + *** v1.6 - July 2001 *** + ** *** + *** v1.5 - March 2001 *** + *** v1.0 - November 2000 *** + *** *** + *** v1.5 extends v1.0: *** + *** .- Changes metods init() and finalize() to be static *** + *** .- Incorporates process group management *** + *** .- Do not consider LEDA anymore *** + *** .- Contains a method "int my_pid()" for easy invokations *** + *** .- Adds "unsigned" and "long double" input/output *** + *** *** + *** v1.6 extends v1.5: *** + ** .- Internal in/out buffers for packed separated *** + *** *** + *** Communication services for LAN/WAN use following the message *** + *** passing paradigm. *** + *** STREAM C++ VERSION *** + *** MPI implementation *** + *** Developed by Erique Alba *** + ***************************************************************************/ +#include "netstream.hh" +// Default constructor +NetStream::NetStream() +{ + this->reset(); // Reset to default values member variables + packin_buffer = new char[MAX_PACK_BUFFER_SIZE]; + packout_buffer = new char[MAX_PACK_BUFFER_SIZE]; +} +// Init the underlying communication library (MPI) +NetStream::NetStream (int argc, char ** argv) +{ + init(argc,argv); + this->reset(); + packin_buffer = new char[MAX_PACK_BUFFER_SIZE]; + packout_buffer = new char[MAX_PACK_BUFFER_SIZE]; +} +// Default destructor +NetStream::~NetStream() +{ + delete [] packin_buffer; delete [] packout_buffer; + this->reset(); +} +// Give default values to member variables +void NetStream::reset(void) +{ + default_target = default_source = 0; + pack_in_progress = false; + packin_index = packout_index = 0; + pending_input_packet = false; + pack_in = pack_out = false; + broadcast = false; + my_communicator = MPI_COMM_WORLD; +} +// Init the communication system. Invoke it only ONCE +void NetStream::init(int argc, char** argv) +{ + static bool system_up = false; // Is MPI already running? + if (!system_up) + { MPI_Init(&argc,&argv); + system_up = true; + } +} +// Shutdown the communication system. Invoke it ONCE +void NetStream::finalize(void) +{ + MPI_Finalize(); // Unconditional Finalization +} +// BASIC INPUT/OUTPUT SERVICES +// =================================================================================== + +NetStream& NetStream::operator>> (bool& d) +{ rcv(&d,1,NET_BOOL,default_source); return(*this); } + +NetStream& NetStream::operator<< (bool d) +{ send(&d,1,NET_BOOL,default_target); return(*this); } +NetStream& NetStream::operator>> (char& d) +{ rcv(&d,1,NET_CHAR,default_source); return(*this); } +NetStream& NetStream::operator<< (char d) +{ send(&d,1,NET_CHAR,default_target); return(*this); } +NetStream& NetStream::operator>> (short& d) +{ rcv(&d,1,NET_SHORT,default_source); return(*this); } +NetStream& NetStream::operator<< (short d) +{ send(&d,1,NET_SHORT,default_target); return(*this); } +NetStream& NetStream::operator>> (int& d) +{ rcv(&d,1,NET_INT,default_source); return(*this); } +NetStream& NetStream::operator<< (int d) +{ send(&d,1,NET_INT,default_target); return(*this); } +NetStream& NetStream::operator>> (long& d) +{ rcv(&d,1,NET_LONG,default_source); return(*this); } +NetStream& NetStream::operator<< (long d) +{ send(&d,1,NET_LONG,default_target); return(*this); } +NetStream& NetStream::operator>> (float& d) +{ rcv(&d,1,NET_FLOAT,default_source); return(*this); } +NetStream& NetStream::operator<< (float d) +{ send(&d,1,NET_FLOAT,default_target); return(*this); } +NetStream& NetStream::operator>> (double& d) +{ rcv(&d,1,NET_DOUBLE,default_source); return(*this); } +NetStream& NetStream::operator<< (double d) +{ send(&d,1,NET_DOUBLE,default_target); return(*this); } +NetStream& NetStream::operator>> (char* d) +{ rcv(d,MAX_MSG_LENGTH,NET_CHAR,default_source); return(*this); } +NetStream& NetStream::operator<< (char* d) +{ send(d,strlen(d)+1,NET_CHAR,default_target); return(*this); } +NetStream& NetStream::operator>> (void* d) +{ rcv(d,MAX_MSG_LENGTH,NET_CHAR,default_source); return(*this); } +NetStream& NetStream::operator<< (void* d) +{ send(d,strlen((char*)d)+1,NET_CHAR,default_target); return(*this); } +// Extended data types from version 1.5 on +NetStream& NetStream::operator>> (unsigned char& d) +{ rcv(&d,MAX_MSG_LENGTH,NET_UNSIGNED_CHAR,default_source);return(*this); } + +NetStream& NetStream::operator<< (unsigned char d) +{ send(&d,1,NET_UNSIGNED_CHAR,default_target); return(*this); } +NetStream& NetStream::operator>> (unsigned short int& d) +{ rcv(&d,MAX_MSG_LENGTH,NET_UNSIGNED_SHORT,default_source);return(*this); } +NetStream& NetStream::operator<< (unsigned short int d) +{ send(&d,1,NET_UNSIGNED_SHORT,default_target); return(*this); } +NetStream& NetStream::operator>> (unsigned int& d) +{ rcv(&d,MAX_MSG_LENGTH,NET_UNSIGNED,default_source); return(*this); } + +NetStream& NetStream::operator<< (unsigned int d) +{ send(&d,1,NET_UNSIGNED,default_target); return(*this); } +NetStream& NetStream::operator>> (unsigned long int& d) +{ rcv(&d,MAX_MSG_LENGTH,NET_UNSIGNED_LONG,default_source);return(*this); } + +NetStream& NetStream::operator<< (unsigned long int d) +{ send(&d,1,NET_UNSIGNED_LONG,default_target); return(*this); } +NetStream& NetStream::operator>> (long double& d) +{ rcv(&d,MAX_MSG_LENGTH,NET_LONG_DOUBLE,default_source); return(*this); } + +NetStream& NetStream::operator<< (long double d) +{ send(&d,1,NET_LONG_DOUBLE,default_target); return(*this); } +// SET-GET TARGET AND SOURCE PROCESSES +NetStream& __set_target(NetStream& n, const int p) { return n._set_target(p); } +NetStream& NetStream::_set_target(const int p) +{ assert(p>=0); default_target = p; return (*this); } +NetStream& __get_target(NetStream& n, int* p) { return n._get_target(p); } +NetStream& NetStream::_get_target(int* p) +{ *p = default_target; return (*this); } +NetStream& __set_source(NetStream& n, const int p) { return n._set_source(p); } +NetStream& NetStream::_set_source(const int p) +{ /*assert(p>=0);*/ default_source = p; return (*this); } +NetStream& __get_source(NetStream& n, int* p) { return n._get_source(p); } +NetStream& NetStream::_get_source(int* p) +{ *p = default_source; return (*this); } +// Get the number of processes involved in the communications +int NetStream::pnumber(void) +{ int numprocs, rvalue; + rvalue = MPI_Comm_size(my_communicator,&numprocs); + assert(rvalue==MPI_SUCCESS); + return numprocs; +} +// MANIPULATORS: SYNCHRONIZATION AND PACKING SERVICES +// =================================================================================== +// Get the process ID [0, 1, 2, ...] fro the calling process +NetStream& __my_pid(NetStream& n, int* pid) +{ + return n._my_pid(pid); +} +NetStream& NetStream::_my_pid(int* pid) +{ + MPI_Comm_rank(my_communicator,pid); + return (*this); +} +// EASY access to rank - Returns the process ID of the calling process +int NetStream::my_pid(void) +{ int pid; + this->_my_pid(&pid); + return pid; +} +// Sit and wait until all processes are in the same barrier +// Can be used as a MANIPULATOR +NetStream& barrier(NetStream& n) +{ + return n._barrier(); +} +NetStream& NetStream::_barrier(void) +{ int status; + status = MPI_Barrier(my_communicator); + assert(status==MPI_SUCCESS); + return (*this); +} +// Wait for an incoming message in any input stream +NetStream& NetStream::_wait(const int stream_type) // class +{ + int rvalue; + MPI_Status status; + assert(stream_type==regular||stream_type==packed||stream_type==any); + if( ((stream_type==packed) || (stream_type==any) )&& (pending_input_packet) ) + //if( (stream_type==packed) && (pending_input_packet) ) + return (*this); // wait befor packet_begin when already received + rvalue = MPI_Probe(default_source,stream_type,my_communicator,&status); + assert(rvalue==MPI_SUCCESS); + return (*this); +} + +NetStream& NetStream::_wait2(const int stream_type, int& tipo) // class +{ + int rvalue; + MPI_Status status; + assert(stream_type==regular||stream_type==packed||stream_type==any); + if( ((stream_type==packed) || (stream_type==any) )&& (pending_input_packet) ) + //if( (stream_type==packed) && (pending_input_packet) ) + return (*this); // wait befor packet_begin when already received + rvalue = MPI_Probe(default_source,stream_type,my_communicator,&status); + assert(rvalue==MPI_SUCCESS); + if (status.MPI_SOURCE == 0){ + tipo = 1; + } + return (*this); +} +NetStream& __wait(NetStream& n, const int stream_type) // helper +{ + return n._wait(stream_type); +} +// Marks the beginning of a packed information +NetStream& pack_begin(NetStream& n) +{ + return n._pack_begin(); +} +NetStream& NetStream::_pack_begin(void) +{ + int rvalue=MPI_SUCCESS; + MPI_Status status; + if(!pack_in_progress) + { pack_in_progress = true; + packin_index = packout_index = 0; + pack_in = false; + pack_out = false; + if (!pending_input_packet) + { _probe(packed,pending_input_packet); + if(pending_input_packet) + rvalue = MPI_Recv(packin_buffer, MAX_PACK_BUFFER_SIZE, NET_PACKED, + default_source, PACKED_STREAM_TAG, my_communicator, &status); + } + } + return (*this); +} +// Marks the end of a packed and flush it to the net +NetStream& pack_end(NetStream& n) +{ + return n._pack_end(); +} +NetStream& NetStream::_pack_end(void) +{ + int rvalue, mypid; + if (pack_in_progress) + { + if(pack_out) + { if(broadcast) // Packet broadcast + { broadcast = false; + _my_pid(&mypid); + rvalue = MPI_Bcast(packout_buffer,packout_index,NET_PACKED, + mypid,my_communicator); + assert(rvalue==MPI_SUCCESS); + } + else + { rvalue = MPI_Send(packout_buffer, packout_index, NET_PACKED, + default_target,PACKED_STREAM_TAG,my_communicator); + assert(rvalue==MPI_SUCCESS); + } + } + pack_in_progress = false; + pack_in = pack_out = false; + packin_index = packout_index = 0 ; + } + return (*this); +} +// Check whether there are awaiting data +NetStream& probe(NetStream& n, const int stream_type, int& pending) +{ + return n._probe(stream_type, pending); +} +NetStream& NetStream::_probe(const int stream_type, int& pending) +{ + MPI_Status status; + int rvalue; + assert(stream_type==regular||stream_type==packed||stream_type==any); + rvalue = MPI_Iprobe(default_source,stream_type,my_communicator,&pending,&status); + assert(rvalue==MPI_SUCCESS); + return (*this); +} +// Broadcast a message to all the processes +NetStream& broadcast(NetStream& n) +{ + return n._broadcast(); +} +NetStream& NetStream::_broadcast(void) +{ + broadcast = true; + return (*this); +} +// PRIVATE SERVICES +// =================================================================================== +// Usually, the length is the number of bytes for every net type +// When packing we must use in the pack calls the numer of items (length divided by type size) +// Any char is encoded with a leading field of length +void NetStream::send(void* d, const int len, const NET_TYPE type, const int target) +{ + int rvalue = MPI_SUCCESS, length = len; + // PACKING SERVICE + if(pack_in_progress) + { + pack_out = true; + assert(pack_out!=pack_in); // Error condition + if(type==NET_CHAR) + send(&length,sizeof(NET_INT),NET_INT,target); // Recursive call to store string length + else + length = 1; + rvalue = MPI_Pack(d,length,type,packout_buffer,MAX_PACK_BUFFER_SIZE,&packout_index,my_communicator); + assert(rvalue==MPI_SUCCESS); + return; + } + if(broadcast) // Regular broadcast, packed broadcast managed in _pack_end() + { int mypid; + + broadcast = false; + _my_pid(&mypid); + rvalue = MPI_Bcast(d,len,type,mypid,my_communicator); + assert(rvalue==MPI_SUCCESS); + return; + } + rvalue = MPI_Send(d,len,type,target,REGULAR_STREAM_TAG,my_communicator); + assert(rvalue==MPI_SUCCESS); +} +void NetStream::rcv (void* d, const int len, const NET_TYPE type, const int source) +{ MPI_Status status; + int rvalue = MPI_SUCCESS, length=len; + if(pack_in_progress) + { +// if(!pack_in && !pending_input_packet) +// rvalue = MPI_Recv(packin_buffer, MAX_PACK_BUFFER_SIZE, NET_PACKED, +// default_source, PACKED_STREAM_TAG, my_communicator, &status); + pack_in = true; + pending_input_packet = false; + assert(pack_out!=pack_in); + if(type==NET_CHAR) + rcv(&length,sizeof(NET_INT),NET_INT,source); // Gets the string length + else + length = 1; + rvalue = MPI_Unpack(packin_buffer, MAX_PACK_BUFFER_SIZE, &packin_index, d, + length, type, my_communicator); + assert(rvalue==MPI_SUCCESS); + return; + } + + rvalue=MPI_Recv(d,len,type,source,REGULAR_STREAM_TAG,my_communicator,&status); + assert(status.MPI_ERROR==MPI_SUCCESS); + assert(rvalue==MPI_SUCCESS); +} +/////////////////////////////////////// GROUP MANAGEMENT //////////////////////////////// +// Set the netstream to a new communicator +void NetStream::set_communicator(NET_Comm comm) +{ + my_communicator = comm; +} +// Get the present communicator in this netstream +NET_Comm NetStream::get_communicator(void) +{ + return my_communicator; +} +// Create a new group inside the present communicator +NET_Comm NetStream::create_group(NET_Comm comm, int color, int key) +{ int rvalue; + NET_Comm newcomm; + rvalue=MPI_Comm_split(comm,color,key,&newcomm); + assert(rvalue==MPI_SUCCESS); + return newcomm; +} +// Create a bridge between local and remote MATCHING call +NET_Comm NetStream::create_inter_group(NET_Comm lcomm, int lrank, NET_Comm bcomm, int rrank, int strtype) +{ int rvalue; + NET_Comm newcomm; + rvalue=MPI_Intercomm_create(lcomm,lrank,bcomm,rrank,strtype,&newcomm); + assert(rvalue==MPI_SUCCESS); + return newcomm; +} diff --git a/malva/inc/Mallba/netstream.hh b/malva/inc/Mallba/netstream.hh new file mode 100644 index 0000000000000000000000000000000000000000..5c24a3dc072e2d1dbed9ebecef2b7d6190e51683 --- /dev/null +++ b/malva/inc/Mallba/netstream.hh @@ -0,0 +1,161 @@ +/*************************************************************************** + *** netstream.cc *** + *** v1.6 - July 2001 *** + ** *** + *** v1.5 - March 2001 *** + *** v1.0 - November 2000 *** + *** *** + *** v1.5 extends v1.0: *** + *** .- Changes metods init() and finalize() to be static *** + *** .- Incorporates process group management *** + *** .- Do not consider LEDA anymore *** + *** .- Contains a method "int my_pid()" for easy invokations *** + *** .- Adds "unsigned" and "long double" input/output *** + *** *** + *** v1.6 extends v1.5: *** + ** .- Internal in/out buffers for packed separated *** + *** *** + *** Communication services for LAN/WAN use following the message *** + *** passing paradigm. *** + *** STREAM C++ VERSION *** + *** MPI implementation *** + *** Developed by Erique Alba *** + ***************************************************************************/ +#ifndef INC_netstream +#define INC_netstream +#include "mpi.h" +#include <assert.h> +#include <string.h> +// Class NetStream allows to define and use network streams trhough LAN and WAN +#define REGULAR_STREAM_TAG 0 // Used for tagging MPI regular messages +#define PACKED_STREAM_TAG 1 // Used for tagging MPI packet messages +#define NET_TYPE MPI_Datatype // Network allowable data types +#define NET_BOOL MPI_CHAR // Bools like chars +#define NET_CHAR MPI_CHAR +#define NET_SHORT MPI_SHORT +#define NET_INT MPI_INT +#define NET_LONG MPI_LONG +#define NET_UNSIGNED_CHAR MPI_UNSIGNED_CHAR +#define NET_UNSIGNED_SHORT MPI_UNSIGNED_SHORT +#define NET_UNSIGNED MPI_UNSIGNED +#define NET_UNSIGNED_LONG MPI_UNSIGNED_LONG +#define NET_FLOAT MPI_FLOAT +#define NET_DOUBLE MPI_DOUBLE +#define NET_LONG_DOUBLE MPI_LONG_DOUBLE +#define NET_BYTE MPI_BYTE +#define NET_PACKED MPI_PACKED +#define NET_Comm MPI_Comm +#define MAX_MSG_LENGTH 204800 // Max length of a message +#define MAX_PACK_BUFFER_SIZE 204800 // Max length of a packed message +// Help structure for manipulators having one int& argument +class NetStream; +struct smanip1c // "const int" +{ NetStream& (*f)(NetStream&, const int); // The ONE argument function + int i; // The argument + smanip1c( NetStream&(*ff)(NetStream&,const int), int ii) : f(ff), i(ii) {} // Constuctor +}; +struct smanip1 // "int*" note: references do not work! "int&" +{ NetStream& (*f)(NetStream&, int*); // The ONE argument function + int* i; // The argument + smanip1( NetStream&(*ff)(NetStream&, int*), int* ii) : f(ff), i(ii) {} // Constuctor +}; +// Tags for the available streams +const int any = MPI_ANY_TAG; // Tag value valid for any stream +const int regular = REGULAR_STREAM_TAG; // Tag value for regular stream of data +const int packed = PACKED_STREAM_TAG; // Tag value for packed stream of data +// Tags for sources +const int any_source = MPI_ANY_SOURCE; // Tag value valid for any source +class NetStream +{ + public: + NetStream (); // Default constructor + // Constructor with source integer left unchanged + NetStream (int, char **); // Init the communications + ~NetStream (); // Default destructor + static void init(int,char**); // Init the communication system. Invoke it only ONCE + static void finalize(void); // Shutdown the communication system. Invoke it ONCE + // GROUP management + void set_communicator(NET_Comm comm); // Set the netstream to a new communicator + NET_Comm get_communicator(void); // Get the present communicator in this netstream + static NET_Comm create_group(NET_Comm comm, int color, int key); // Create a new group inside the present communicator + // Create a bridge between local and remote MATCHING call + static NET_Comm create_inter_group(NET_Comm lcomm, int lrank, NET_Comm bcomm, int rrank, int strtrype); + +// BASIC INPUT SERVICES <comments> BASIC OUTPUT SERVICES +// ============================================================================================================ + NetStream& operator>> (bool& d); NetStream& operator<< (bool d); + NetStream& operator>> (char& d); NetStream& operator<< (char d); + NetStream& operator>> (short& d); NetStream& operator<< (short d); + NetStream& operator>> (int& d); NetStream& operator<< (int d); + NetStream& operator>> (long& d); NetStream& operator<< (long d); + NetStream& operator>> (float& d); NetStream& operator<< (float d); + NetStream& operator>> (double& d); NetStream& operator<< (double d); + NetStream& operator>> (char* d); /*NULL terminated*/ NetStream& operator<< (char* d); + NetStream& operator>> (void* d); /*NULL terminated*/ NetStream& operator<< (void* d); + // Extended data types from version 1.5 on + NetStream& operator>> (unsigned char& d); NetStream& operator<< (unsigned char d); + NetStream& operator>> (unsigned short int& d); NetStream& operator<< (unsigned short int d); + NetStream& operator>> (unsigned int& d); NetStream& operator<< (unsigned int d); + NetStream& operator>> (unsigned long int& d); NetStream& operator<< (unsigned long int d); + NetStream& operator>> (long double& d); NetStream& operator<< (long double d); + int pnumber(void); // Returns the number of processes + bool broadcast; // Determines whether the next sent message is for broadcasting + // Input MANIPULATORS for modifying the behavior of the channel on the fly + // NO ARGUMENTS + NetStream& operator<< (NetStream& (*f)(NetStream& n)) { return f(*this); } // NO arguments + NetStream& _barrier(void); // Sit and wait until all processes are in barrier + NetStream& _pack_begin(void); // Marks the beginning of a packed information + NetStream& _pack_end(void); // Marks the end of a packed and flush it to the net + NetStream& _probe(const int stream_type, int& pending); // Check whether there are awaiting data + NetStream& _broadcast(void); // Broadcast a message to all the processes + // ONE ARGUMENT + // "const int" + NetStream& operator<< (smanip1c m) { return m.f((*this),m.i); }// ONE int& argument constant + // "int*" + NetStream& operator<< (smanip1 m) { return m.f((*this),m.i); }// ONE int& argument + // BASIC CLASS METHODS FOR MANIPULATORS + NetStream& _my_pid(int* pid); // Returns the process ID of the calling process + NetStream& _wait(const int stream_type); // Wait for an incoming message in the specified stream + NetStream& _wait2(const int stream_type, int& tipo); + NetStream& _set_target(const int p); // Stablish "p" as the default receiver + NetStream& _get_target(int* p); // Get into "p" the default receiver + NetStream& _set_source(const int p); // Stablish "p" as the default transmitter + NetStream& _get_source(int* p); // Get into "p" the default transmitter + // AUXILIAR PUBLIC METHODS FOR ALLOWING EASY MANAGEMENTS OF NETSTREAMS + int my_pid(void); // Returns the process ID of the calling process + private: + int default_target, default_source; // Default process IDs to send-recv data to-from + bool pack_in_progress; // Defines whether a packet is being defined with "pack_begin-pack_end" + int packin_index; // Index to be used for extracting from a IN packed message - v1.6 + int packout_index; // Index to be used for adding to an OUT packed message - v1.6 + int pending_input_packet;//Is there a pending packet already read into the IN buffer? - v1.6 + char* packin_buffer; // Buffer to temporary storage of the IN packed being defined - v1.6 + char* packout_buffer; // Buffer to temporary storage of the OUT packed being defined - v1.6 + bool pack_in, pack_out; // Define whether input-output packed message is being used + void reset(void); // Reset member variables of this class + NET_Comm my_communicator; // Communicator of this netstream + void send(void* d, const int len, const NET_TYPE type, const int target); + void rcv (void* d, const int len, const NET_TYPE type, const int source); +}; // class NetStream + // MANIPULATORS (must be static or non-member methods in C++ -mpiCC only allows non-member!-) + // NO ARGUMENTS + NetStream& barrier(NetStream& n); // Sit and wait until all processes are in barrier + NetStream& broadcast(NetStream& n); // Broadcast a message to all the processes + NetStream& pack_begin(NetStream& n); // Marks the beginning of a packed information + NetStream& pack_end(NetStream& n); // Marks the end of a packed and flush it to the net + // ONE ARGUMENT + NetStream& __my_pid(NetStream& n, int* pid); // Returns the process ID of the calling process + inline smanip1 my_pid(int* pid){ return smanip1(__my_pid,pid); } // manipulator + NetStream& __wait(NetStream& n, const int stream_type);// Wait for an incoming message - helper + inline smanip1c wait(const int stream_type){ return smanip1c(__wait,stream_type); } // manipulator + NetStream& __set_target(NetStream& n, const int p); // Stablish "p" as the default receiver + inline smanip1c set_target(const int p){ return smanip1c(__set_target,p); } // manipulator + NetStream& __get_target(NetStream& n, int* p); // Get into "p" the default receiver + inline smanip1 get_target(int* p){ return smanip1(__get_target,p); } // manipulator + NetStream& __set_source(NetStream& n, const int p); // Stablish "p" as the default transmitter + inline smanip1c set_source(const int p){ return smanip1c(__set_source,p); } // manipulator + NetStream& __get_source(NetStream& n, int* p); // Get into "p" the default transmitter + inline smanip1 get_source(int* p){ return smanip1(__get_source,p); } // manipulator + // TWO ARGUMENTS - not used yet + NetStream& probe(NetStream& n, const int stream_type, int& pending); // Check whether there are awaiting data +#endif diff --git a/malva/inc/Mallba/netstream.o b/malva/inc/Mallba/netstream.o new file mode 100644 index 0000000000000000000000000000000000000000..fb40ece360fb739d83d40beffbf1b01c031a15aa Binary files /dev/null and b/malva/inc/Mallba/netstream.o differ diff --git a/malva/inc/Mallba/random.hh b/malva/inc/Mallba/random.hh new file mode 100644 index 0000000000000000000000000000000000000000..ae6bde6ae8230c733aeb341be926e21fc5648d94 --- /dev/null +++ b/malva/inc/Mallba/random.hh @@ -0,0 +1,50 @@ +/****************************************************************************** +*** *** +*** Este fichero contiene funciones (en general inlines) para generar núme- *** +*** ros aleatorios y otras funciones relacionadas con ello *** +*** *** +*******************************************************************************/ + +#ifndef INC_random_hh +#define INC_random_hh + +#include <stdlib.h> + + +inline double undefined () +{ + double zero = 0; + return zero/zero; +} + +// Returns a valeu greater than any other +inline double infinity () +{ + double one=1.0; + double zero=0.0; + return one/zero; +} + +// Returns a random number in [0,1]. +inline double rand01 () +{ + return drand48(); +} + +// Returns a random number +inline int rand_int (float min,float max) +{ + int value=rand(); + int range= (int)(max - min); + int order = value % (range+1); + return ((int)min + order); +} + +// selects a seed +inline void random_seed(long int seed) +{ + srand48(seed); + srand(seed); +} + +#endif diff --git a/malva/inc/Mallba/time.hh b/malva/inc/Mallba/time.hh new file mode 100644 index 0000000000000000000000000000000000000000..2dcb9a778e5055c74d4b59121dcb143ec7bb0276 --- /dev/null +++ b/malva/inc/Mallba/time.hh @@ -0,0 +1,43 @@ +/****************************************************************************** +*** *** +*** Este fichero incluye algunas funciones útiles para la medicion de tiem- *** +*** pos. *** +*** *** +******************************************************************************/ + +#ifndef INC_time_hh +#define INC_time_hh + +#define MAXTIME 4294 + +#include <time.h> +#include <sys/time.h> +#include <unistd.h> + +// return time in microseconds +inline float _used_time() +{ + struct timeval tv ; + static long tiempo = -1; + float u_clock; + + gettimeofday(&tv,NULL); + + if(tiempo < 0) tiempo = tv.tv_sec; + + u_clock = ((float)(tv.tv_sec - tiempo)*1000000.0) + ((float)tv.tv_usec); + return u_clock; + +} + + +inline float _used_time(float time) +{ + float dif=_used_time() - time; + if (dif<0) dif = 0; + return dif; +} + + + +#endif