Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Less error prone unit conversion
[simgrid.git] / src / surf / surfxml_parse.c
index e367d9e..02fd37b 100644 (file)
@@ -49,6 +49,7 @@ double surf_parse_get_double(const char *string) {
   int ret = sscanf(string, "%lg", &res);
   if (ret != 1)
     surf_parse_error("%s is not a double", string);
+  //printf("Parsed double [%lg] %s\n", res, string);  
   return res;
 }
 
@@ -60,6 +61,103 @@ int surf_parse_get_int(const char *string) {
   return res;
 }
 
+double surf_parse_get_time(const char *string) {
+  char* ptr;
+  double res = strtod(string, &ptr);
+  if (ptr == string)
+    surf_parse_error("This is not a time: %s", string);
+  else if (strcmp(ptr, "ps") == 0)
+    res *= 1E-12;
+  else if (strcmp(ptr, "ns") == 0)
+    res *= 1E-9;
+  else if (strcmp(ptr, "us") == 0)
+    res *= 1E-6;
+  else if (strcmp(ptr, "ms") == 0)
+    res *= 1E-3;
+  else if (strcmp(ptr, "s") == 0)
+    res *= 1;
+  else if (strcmp(ptr, "m") == 0)
+    res *= 60;
+  else if (strcmp(ptr, "h") == 0)
+    res *= 3600;
+  else if (strcmp(ptr, "d") == 0)
+    res *= 86400;
+  else if (strcmp(ptr, "w") == 0)
+    res *= 604800;
+  return res;
+}
+
+double surf_parse_get_bandwidth(const char *string) {
+  char* ptr;
+  double res = strtod(string, &ptr);
+  if (ptr == string)
+    surf_parse_error("This is not a bandwidth: %s", string);
+  else if (strcmp(ptr, "KBps") == 0)
+    res *= 1E3;
+  else if (strcmp(ptr, "MBps") == 0) 
+    res *= 1E6;
+  else if (strcmp(ptr, "GBps") == 0)
+    res *= 1E9;
+  else if (strcmp(ptr, "TBps") == 0)
+    res *= 1E12;
+  else if (strcmp(ptr, "Bps") == 0)
+    res *= 1;
+  else if (strcmp(ptr, "kbps") == 0)
+    res *= 0.125 * 1E3;
+  else if (strcmp(ptr, "mbps") == 0)
+    res *= 0.125 * 1E6;
+  else if (strcmp(ptr, "gbps") == 0)
+    res *= 0.125 * 1E9;
+  else if (strcmp(ptr, "tbps") == 0)
+    res *= 0.125 * 1E12;
+  else if (strcmp(ptr, "bps") == 0)
+    res *= 0.125;
+  return res;
+}
+
+double surf_parse_get_power(const char *string) {
+  char* ptr;
+  double res = strtod(string, &ptr);
+  if (ptr == string)
+    surf_parse_error("This is not a power: %s", string);
+  else if (strcmp(ptr, "kiloflops") == 0)
+    res *= 1E3;
+  else if (strcmp(ptr, "megaflops") == 0)
+    res *= 1E6;
+  else if (strcmp(ptr, "gigaflops") == 0)
+    res *= 1E9;
+  else if (strcmp(ptr, "teraflops") == 0)
+    res *= 1E12;
+  else if (strcmp(ptr, "petaflops") == 0)
+    res *= 1E15;
+  else if (strcmp(ptr, "exaflops") == 0)
+    res *= 1E18;
+  else if (strcmp(ptr, "zettaflops") == 0)
+    res *= 1E21;
+  else if (strcmp(ptr, "yottaflops") == 0)
+    res *= 1E24;
+  else if (strcmp(ptr, "flops") == 0)
+    res *= 1;
+  else if (strcmp(ptr, "kf") == 0)
+    res *= 1E3;
+  else if (strcmp(ptr, "mf") == 0)
+    res *= 1E6;
+  else if (strcmp(ptr, "gf") == 0)
+    res *= 1E9;
+  else if (strcmp(ptr, "tf") == 0)
+    res *= 1E12;
+  else if (strcmp(ptr, "pf") == 0)
+    res *= 1E15;
+  else if (strcmp(ptr, "ef") == 0)
+    res *= 1E18;
+  else if (strcmp(ptr, "zf") == 0)
+    res *= 1E21;
+  else if (strcmp(ptr, "yf") == 0)
+    res *= 1E24;
+  else if (strcmp(ptr, "f") == 0)
+    res *= 1;
+  return res;
+}
 
 /*
  * All the callback lists that can be overridden anywhere.
@@ -340,14 +438,20 @@ void STag_surfxml_cluster(void){
   cluster.prefix = A_surfxml_cluster_prefix;
   cluster.suffix = A_surfxml_cluster_suffix;
   cluster.radical = A_surfxml_cluster_radical;
-  cluster.power= surf_parse_get_double(A_surfxml_cluster_power);
+  cluster.power = surf_parse_get_power(A_surfxml_cluster_power);
   cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
-  cluster.bw =   surf_parse_get_double(A_surfxml_cluster_bw);
-  cluster.lat =  surf_parse_get_double(A_surfxml_cluster_lat);
+  cluster.bw =   surf_parse_get_bandwidth(A_surfxml_cluster_bw);
+  cluster.lat =  surf_parse_get_time(A_surfxml_cluster_lat);
   if(strcmp(A_surfxml_cluster_bb___bw,""))
-    cluster.bb_bw = surf_parse_get_double(A_surfxml_cluster_bb___bw);
+    cluster.bb_bw = surf_parse_get_bandwidth(A_surfxml_cluster_bb___bw);
   if(strcmp(A_surfxml_cluster_bb___lat,""))
-    cluster.bb_lat = surf_parse_get_double(A_surfxml_cluster_bb___lat);
+    cluster.bb_lat = surf_parse_get_time(A_surfxml_cluster_bb___lat);
+  if(strcmp(A_surfxml_cluster_limiter___link,""))
+    cluster.limiter_link = surf_parse_get_double(A_surfxml_cluster_limiter___link);
+  if(strcmp(A_surfxml_cluster_loopback___bw,""))
+    cluster.loopback_bw = surf_parse_get_bandwidth(A_surfxml_cluster_loopback___bw);
+  if(strcmp(A_surfxml_cluster_loopback___lat,""))
+    cluster.loopback_lat = surf_parse_get_time(A_surfxml_cluster_loopback___lat);
   cluster.router_id = A_surfxml_cluster_router___id;
 
   switch (AX_surfxml_cluster_sharing___policy) {
@@ -389,9 +493,9 @@ void STag_surfxml_cabinet(void){
   cabinet.id = A_surfxml_cabinet_id;
   cabinet.prefix = A_surfxml_cabinet_prefix;
   cabinet.suffix = A_surfxml_cabinet_suffix;
-  cabinet.power = surf_parse_get_double(A_surfxml_cabinet_power);
-  cabinet.bw = surf_parse_get_double(A_surfxml_cabinet_bw);
-  cabinet.lat = surf_parse_get_double(A_surfxml_cabinet_lat);
+  cabinet.power = surf_parse_get_power(A_surfxml_cabinet_power);
+  cabinet.bw = surf_parse_get_bandwidth(A_surfxml_cabinet_bw);
+  cabinet.lat = surf_parse_get_time(A_surfxml_cabinet_lat);
   cabinet.radical = A_surfxml_cabinet_radical;
 
   sg_platf_new_cabinet(&cabinet);
@@ -401,10 +505,10 @@ void STag_surfxml_peer(void){
   s_sg_platf_peer_cbarg_t peer;
   memset(&peer,0,sizeof(peer));
   peer.id = A_surfxml_peer_id;
-  peer.power = surf_parse_get_double(A_surfxml_peer_power);
-  peer.bw_in = surf_parse_get_double(A_surfxml_peer_bw___in);
-  peer.bw_out = surf_parse_get_double(A_surfxml_peer_bw___out);
-  peer.lat = surf_parse_get_double(A_surfxml_peer_lat);
+  peer.power = surf_parse_get_power(A_surfxml_peer_power);
+  peer.bw_in = surf_parse_get_bandwidth(A_surfxml_peer_bw___in);
+  peer.bw_out = surf_parse_get_bandwidth(A_surfxml_peer_bw___out);
+  peer.lat = surf_parse_get_time(A_surfxml_peer_lat);
   peer.coord = A_surfxml_peer_coordinates;
   peer.availability_trace = tmgr_trace_new_from_file(A_surfxml_peer_availability___file);
   peer.state_trace = tmgr_trace_new_from_file(A_surfxml_peer_state___file);
@@ -424,9 +528,11 @@ void ETag_surfxml_link(void){
   link.properties = current_property_set;
 
   link.id = A_surfxml_link_id;
-  link.bandwidth = surf_parse_get_double(A_surfxml_link_bandwidth);
+  link.bandwidth = surf_parse_get_bandwidth(A_surfxml_link_bandwidth);
+  //printf("Link bandwidth [%lg]\n", link.bandwidth);  
   link.bandwidth_trace = tmgr_trace_new_from_file(A_surfxml_link_bandwidth___file);
-  link.latency = surf_parse_get_double(A_surfxml_link_latency);
+  link.latency = surf_parse_get_time(A_surfxml_link_latency);
+  //printf("Link latency [%lg]\n", link.latency);  
   link.latency_trace = tmgr_trace_new_from_file(A_surfxml_link_latency___file);
 
   switch (A_surfxml_link_state) {
@@ -483,6 +589,22 @@ void STag_surfxml_link___ctn(void){
   xbt_dynar_push(parsed_link_list, &link_id);
 }
 
+void ETag_surfxml_backbone(void){
+  s_sg_platf_link_cbarg_t link;
+  memset(&link,0,sizeof(link));
+
+  link.properties = NULL;
+
+  link.id = A_surfxml_backbone_id;
+  link.bandwidth = surf_parse_get_bandwidth(A_surfxml_backbone_bandwidth);
+  link.latency = surf_parse_get_time(A_surfxml_backbone_latency);
+  link.state = SURF_RESOURCE_ON;
+  link.policy = SURF_LINK_SHARED;
+
+  sg_platf_new_link(&link);
+  routing_cluster_add_backbone(xbt_lib_get_or_null(link_lib, A_surfxml_backbone_id, SURF_LINK_LEVEL));
+}
+
 void STag_surfxml_route(void){
   xbt_assert(strlen(A_surfxml_route_src) > 0 || strlen(A_surfxml_route_dst) > 0,
       "Missing end-points while defining route \"%s\"->\"%s\"",
@@ -783,6 +905,7 @@ void ETag_surfxml_host___link(void){}
 void ETag_surfxml_cluster(void){}
 void ETag_surfxml_cabinet(void){}
 void ETag_surfxml_peer(void){}
+void STag_surfxml_backbone(void){}
 void ETag_surfxml_link___ctn(void){}
 void ETag_surfxml_argument(void){}
 
@@ -824,6 +947,7 @@ void surf_parse_close(void)
   xbt_dynar_free(&surf_parsed_filename_stack);
 
   free(surf_parsed_filename);
+  surf_parsed_filename = NULL;
 
   if (surf_file_to_parse) {
     surf_parse__delete_buffer(surf_input_buffer);
@@ -866,7 +990,7 @@ double get_cpu_power(const char *power)
       power_scale = random_generate(random);
     }
   } else {
-    power_scale = surf_parse_get_double(power);
+    power_scale = surf_parse_get_power(power);
   }
   return power_scale;
 }