noc_functions.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "my_rtrm.h"
  5. #include "idag_defs.h"
  6. #include "libfunctions.h"
  7. #include "variables.h"
  8. /* FIXME num_idags_y to be removed from cluster info*/
  9. /*
  10. const int scc2grid_mapping[48] = {0, 6, 1, 7, 2, 8,
  11. 3, 9, 4, 10, 5, 11,
  12. 12, 18, 13, 19, 14, 20,
  13. 15, 21, 16, 22, 17, 23,
  14. 24, 30, 25, 31, 26, 32,
  15. 27, 33, 28, 34, 29, 35,
  16. 36, 42, 37, 43, 38, 44,
  17. 39, 45, 40, 46, 41, 47};
  18. const int grid2scc_mapping[48] = {0, 2, 4, 6, 8, 10,
  19. 1, 3, 5, 7, 9, 11,
  20. 12, 14, 16, 18, 20, 22,
  21. 13, 15, 17, 19, 21, 23,
  22. 24, 26, 28, 30, 32, 34,
  23. 25, 27, 19, 31, 33, 35,
  24. 36, 38, 40, 42, 44, 46,
  25. 37, 39, 41, 43, 45, 47};
  26. */
  27. static int *scc2grid_mapping, *grid2scc_mapping;
  28. void create_scc2grid_mapping (char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_NUM_SIZE]) {
  29. int i, j;
  30. char scc2grid_path[MAX_STR_NAME_SIZE];
  31. FILE *scc2grid_file;
  32. scc2grid_mapping = (int *) malloc(X_max * Y_max * sizeof(int));
  33. #ifdef PLAT_SCC
  34. strcpy(scc2grid_path, "/shared/herc/");
  35. #else
  36. strcpy(scc2grid_path, "../");
  37. #endif
  38. strcat(scc2grid_path, scen_directory);
  39. strcat(scc2grid_path, "/");
  40. strcat(scc2grid_path, scen_num);
  41. strcat(scc2grid_path, "/grids/scc2grid_");
  42. strcat(scc2grid_path, itoa(X_max));
  43. strcat(scc2grid_path, "_");
  44. strcat(scc2grid_path, itoa(Y_max));
  45. if (node_id == 0){
  46. printf("scc2grid_path = %s\n",scc2grid_path);
  47. fflush(stdout);
  48. }
  49. if ((scc2grid_file = fopen(scc2grid_path, "r")) == NULL){
  50. printf("Cannot open input file with file path = %s ",scc2grid_path);
  51. perror("open scc2grid_path");
  52. }
  53. for (i=0; i<Y_max; i++)
  54. for (j=0; j<X_max; j++)
  55. fscanf(scc2grid_file, "%d", &scc2grid_mapping[i*X_max + j]);
  56. }
  57. void create_grid2scc_mapping (char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_NUM_SIZE]) {
  58. int i, j;
  59. char grid2scc_path[MAX_STR_NAME_SIZE];
  60. FILE *grid2scc_file;
  61. grid2scc_mapping = (int *) malloc(X_max * Y_max * sizeof(int));
  62. #ifdef PLAT_SCC
  63. strcpy(grid2scc_path, "/shared/herc/");
  64. #else
  65. strcpy(grid2scc_path, "../");
  66. #endif
  67. strcat(grid2scc_path, scen_directory);
  68. strcat(grid2scc_path, "/");
  69. strcat(grid2scc_path, scen_num);
  70. strcat(grid2scc_path, "/grids/grid2scc_");
  71. strcat(grid2scc_path, itoa(X_max));
  72. strcat(grid2scc_path, "_");
  73. strcat(grid2scc_path, itoa(Y_max));
  74. if (node_id == 0){
  75. printf("grid2scc_path = %s\n",grid2scc_path);
  76. fflush(stdout);
  77. }
  78. if ((grid2scc_file = fopen(grid2scc_path, "r")) == NULL){
  79. printf("Cannot open input file with file path = %s ",grid2scc_path);
  80. perror("open grid2scc_path");
  81. }
  82. for (i=0; i<Y_max; i++)
  83. for (j=0; j<X_max; j++)
  84. fscanf(grid2scc_file, "%d", &grid2scc_mapping[i*X_max + j]);
  85. }
  86. int distance(int core_start, int core_fin) {
  87. int x1,x2,y1,y2;
  88. x1 = scc2grid_mapping[core_start] % X_max;
  89. y1 = scc2grid_mapping[core_start] / X_max;
  90. //printf("x1 = %d, y1 = %d\n",x1,y1);
  91. x2 = scc2grid_mapping[core_fin] % X_max;
  92. y2 = scc2grid_mapping[core_fin] / X_max;
  93. //printf("x2 = %d, y2 = %d\n",x2,y2);
  94. return abs(x1 - x2) + abs(y1 - y2);
  95. }
  96. extern FILE *log_file;
  97. void get_reg_idags(region cur_reg, int *region_idags){
  98. int y,l,count=0,center_node,cur_line,i,j;
  99. for (i=0; i<num_idags; i++)
  100. region_idags[i]=0;
  101. region_idags[i] = -1; //the one reserved for count is set to -1 for safety
  102. l = cur_reg.r;
  103. //printf("x = %d, y = %d l=%d\n",x,y,l);
  104. /* Lower half of area + center*/
  105. /* Transpose to regural grid coordinates*/
  106. center_node = scc2grid_mapping[cur_reg.C];
  107. cur_line = center_node / X_max;
  108. fprintf(log_file,"\t\tSearching for idags... num_idags = %d\n",num_idags);
  109. while (cur_line >= 0 && l>=0){
  110. cur_line = center_node / X_max;
  111. for (i=0; i<=l; i++){
  112. y = (center_node + i) / X_max;
  113. if (y == cur_line){
  114. for (j=0; j<num_idags; j++)
  115. if (idag_id_arr[j] == idag_mask[grid2scc_mapping[center_node + i]]) break;
  116. if (region_idags[j] == 0) {
  117. region_idags[j] = 1;
  118. count++;
  119. }
  120. }
  121. y = (center_node - i) / X_max;
  122. if (y == cur_line && (center_node - i)>=0){
  123. for (j=0; j<num_idags; j++)
  124. if (idag_id_arr[j] == idag_mask[grid2scc_mapping[center_node - i]]) break;
  125. if (region_idags[j] == 0) {
  126. region_idags[j] = 1;
  127. count++;
  128. }
  129. }
  130. }
  131. center_node -= X_max;
  132. cur_line--;
  133. l--;
  134. }
  135. /* Upper half of area */
  136. if (cur_reg.C + X_max < X_max*Y_max) {
  137. center_node = scc2grid_mapping[cur_reg.C + X_max];
  138. l = cur_reg.r-1;
  139. cur_line = center_node / X_max;
  140. while (cur_line < Y_max && l>=0) {
  141. cur_line = center_node / X_max;
  142. for (i=0; i<=l; i++) {
  143. y = (center_node + i) / X_max;
  144. if (y == cur_line) {
  145. for (j=0; j<num_idags; j++)
  146. if (idag_id_arr[j] == idag_mask[grid2scc_mapping[center_node + i]]) break;
  147. if (region_idags[j] == 0) {
  148. region_idags[j] = 1;
  149. count++;
  150. }
  151. }
  152. y = (center_node - i) / X_max;
  153. if (y == cur_line && (center_node - i)>=0) {
  154. for (j=0; j<num_idags; j++)
  155. if (idag_id_arr[j] == idag_mask[grid2scc_mapping[center_node - i]]) break;
  156. if (region_idags[j] == 0) {
  157. region_idags[j] = 1;
  158. count++;
  159. }
  160. }
  161. }
  162. center_node += X_max;
  163. cur_line++;
  164. l--;
  165. }
  166. }
  167. region_idags[num_idags] = count;
  168. }
  169. int region_count(region cur_reg){
  170. int x,y,l,count=0;
  171. x = scc2grid_mapping[cur_reg.C] % X_max;
  172. y = scc2grid_mapping[cur_reg.C] / X_max;
  173. l = cur_reg.r;
  174. //printf("x = %d, y = %d l=%d\n",x,y,l);
  175. while (y >= 0 && l>=0){
  176. count++;////1 for center
  177. if (x + l < X_max) count += l;
  178. else count += X_max - x -1;
  179. if (x - l >= 0) count += l;
  180. else count += x;
  181. y--;
  182. l--;
  183. }
  184. y = (scc2grid_mapping[cur_reg.C] / X_max) + 1;
  185. l = cur_reg.r - 1;
  186. while (y < Y_max && l>=0){
  187. count++;
  188. if (x + l < X_max) count += l;
  189. else count += X_max - x -1;
  190. if (x - l >= 0) count += l;
  191. else count += x;
  192. y++;
  193. l--;
  194. }
  195. return count;
  196. }
  197. //returns on function name the idag_id and in call by name, cluster sizes
  198. int get_cluster_info(int idag_num, int *Cl_x_max, int *Cl_y_max){
  199. int x_coord,y_coord, diff, tmp_cl_x, tmp_cl_y;
  200. int num_idags_y = num_idags / num_idags_x;
  201. tmp_cl_x = X_max / num_idags_x;
  202. tmp_cl_y = Y_max / num_idags_y;
  203. x_coord = (idag_num % num_idags_x) * tmp_cl_x;
  204. y_coord = (idag_num / num_idags_x) * tmp_cl_y;
  205. diff = X_max % num_idags_x;
  206. if ((diff > 0) && (idag_num % num_idags_x < diff)){
  207. x_coord += idag_num % num_idags_x;
  208. tmp_cl_x++;
  209. } else x_coord += diff;
  210. diff = Y_max % num_idags_y;
  211. if ((diff > 0) && (idag_num / num_idags_x < diff)){
  212. y_coord += idag_num / num_idags_x; //se poia grammh - ypsos tou y einai
  213. tmp_cl_y++;
  214. } else y_coord += diff;
  215. //printf("idag_id = %d, x_coord = %d, y_coord = %d\n",idag_num,x_coord,y_coord);
  216. *Cl_x_max = tmp_cl_x;
  217. *Cl_y_max = tmp_cl_y;
  218. return (y_coord * X_max) + x_coord;
  219. }