[122] | 1 | #include "Graf.h" |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | * the copy operator |
---|
| 5 | */ |
---|
| 6 | ProcesorGraf& ProcesorGraf::operator=(const ProcesorGraf& clone) |
---|
| 7 | { |
---|
| 8 | //printf("!!!\n"); |
---|
| 9 | nr_noduri = clone.nr_noduri; |
---|
| 10 | nr_muchii = clone.nr_muchii; |
---|
| 11 | noduri = (Procesor *)calloc(nr_noduri, sizeof(Procesor)); |
---|
| 12 | muchii = (Muchie *)calloc(nr_muchii, sizeof(Muchie)); |
---|
| 13 | for(int i = 0 ; i < nr_noduri ; i ++){ |
---|
| 14 | noduri[i] = Procesor(clone.noduri[i].id, clone.noduri[i].cost); |
---|
| 15 | noduri[i].addCosts(clone.noduri[i].cost_comm,nr_noduri); |
---|
| 16 | } |
---|
| 17 | for(int i = 0 ; i < nr_muchii ; i ++) |
---|
| 18 | { |
---|
| 19 | int gasit = 0, sursa = -1, destin = -1; |
---|
| 20 | for(int j = 0 ; j < nr_noduri ; j ++) |
---|
| 21 | { |
---|
| 22 | if(gasit == 2) break; |
---|
| 23 | if(noduri[j].id == clone.muchii[i].predecesor->id) { sursa = j; gasit ++;} |
---|
| 24 | if(noduri[j].id == clone.muchii[i].succesor->id) { destin = j; gasit ++;} |
---|
| 25 | } |
---|
| 26 | muchii[i] = Muchie(&noduri[sursa], &noduri[destin], clone.muchii[i].cost); |
---|
| 27 | noduri[destin].addPredecesor(&muchii[i]); |
---|
| 28 | noduri[sursa].addSuccesor(&muchii[i]); |
---|
| 29 | |
---|
| 30 | } |
---|
| 31 | return *this; |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * method initializing a graph of processors to the structure specified in the file |
---|
| 36 | * |
---|
| 37 | * @param filename the input filename containing the configuration |
---|
| 38 | */ |
---|
| 39 | void ProcesorGraf::init(char * filename) |
---|
| 40 | { |
---|
| 41 | FILE *file = fopen(filename, "r"); |
---|
| 42 | int s, d; |
---|
| 43 | double cost; |
---|
| 44 | |
---|
| 45 | if (file == NULL) |
---|
| 46 | { |
---|
| 47 | perror("Error reading file"); |
---|
| 48 | exit(1); |
---|
| 49 | } |
---|
| 50 | else |
---|
| 51 | { |
---|
| 52 | fscanf(file, "%i %i", &nr_noduri, &nr_muchii); |
---|
| 53 | noduri = (Procesor *) calloc(nr_noduri, sizeof(Procesor)); |
---|
| 54 | muchii = (Muchie *) calloc(nr_muchii, sizeof(Muchie)); |
---|
| 55 | |
---|
| 56 | int **mat; |
---|
| 57 | mat = (int **)calloc(nr_noduri,sizeof(int*)); |
---|
| 58 | for (int i=0;i<nr_noduri;i++){ |
---|
| 59 | mat[i] = (int *)calloc(nr_noduri,sizeof(int)); |
---|
| 60 | for (int j=0;j<nr_noduri;j++) |
---|
| 61 | if (i==j) mat[i][j]=0; |
---|
| 62 | else mat[i][j] = 30000; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | for(int i = 0 ; i < nr_noduri ; i ++) |
---|
| 66 | { |
---|
| 67 | fscanf(file, "%lf", &cost); |
---|
| 68 | noduri[i] = Procesor(i, cost); |
---|
| 69 | } |
---|
| 70 | for(int i = 0 ; i < nr_muchii ; i ++) |
---|
| 71 | { |
---|
| 72 | fscanf(file, "%i %i %lf", &s, &d, &cost); |
---|
| 73 | mat[s][d]=cost; |
---|
| 74 | mat[d][s]=cost; |
---|
| 75 | int gasit = 0, sursa = -1, destin = -1; |
---|
| 76 | for(int j = 0 ; j < nr_noduri ; j ++) |
---|
| 77 | { |
---|
| 78 | if(gasit == 2) break; |
---|
| 79 | if(noduri[j].id == s) { sursa = j; gasit ++;} |
---|
| 80 | if(noduri[j].id == d) { destin = j; gasit ++;} |
---|
| 81 | } |
---|
| 82 | muchii[i] = Muchie(&noduri[sursa], &noduri[destin], cost); |
---|
| 83 | noduri[destin].addPredecesor(&muchii[i]); |
---|
| 84 | noduri[sursa].addSuccesor(&muchii[i]); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | for (int k = 0 ; k < nr_noduri ; k++) |
---|
| 88 | for (int i = 0 ; i < nr_noduri ; i++) |
---|
| 89 | for (int j = 0 ; j < nr_noduri ; j++) |
---|
| 90 | if (mat[i][j] > mat[i][k] + mat[k][j]) |
---|
| 91 | mat[i][j] = mat[i][k] + mat[k][j]; |
---|
| 92 | for (int i = 0 ; i < nr_noduri ; i++) |
---|
| 93 | noduri[i].addCosts(mat[i],nr_noduri); |
---|
| 94 | } |
---|
| 95 | fclose (file); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | * the constructor |
---|
| 100 | * |
---|
| 101 | * @param n the number of processors |
---|
| 102 | * @param m the number of communication links |
---|
| 103 | */ |
---|
| 104 | ProcesorGraf::ProcesorGraf(int n, int m) |
---|
| 105 | { |
---|
| 106 | nr_noduri = n; |
---|
| 107 | nr_muchii = m; |
---|
| 108 | noduri = (Procesor *)calloc (n, sizeof(Procesor)); |
---|
| 109 | muchii = (Muchie *)calloc (m, sizeof (Muchie)); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * method that initializes the structure with the default configuration |
---|
| 114 | */ |
---|
| 115 | void ProcesorGraf::init(){ |
---|
| 116 | int i; |
---|
| 117 | for (i = 0 ; i < nr_noduri ; i++) |
---|
| 118 | noduri[i].init(); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | * method that prints the information about the graph of processors |
---|
| 123 | */ |
---|
| 124 | void ProcesorGraf::print() |
---|
| 125 | { |
---|
| 126 | printf("### procesor graf\n"); |
---|
| 127 | printf("nr_noduri = %i, nr_muchii = %i\n", nr_noduri, nr_muchii); |
---|
| 128 | for(int i = 0 ; i < nr_noduri ; i ++) |
---|
| 129 | noduri[i].print(); |
---|
| 130 | for(int i = 0 ; i < nr_muchii ; i ++) |
---|
| 131 | muchii[i].print(); |
---|
| 132 | printf("###\n"); |
---|
| 133 | } |
---|