Esempio n. 1
0
int main(int argc, char** argv)
{
    StackDescription_t stkd ;
    Analysis_t         analysis ;
    Output_t           output ;
    ThermalData_t      tdata ;

    // Checks if there are the all the arguments
    ////////////////////////////////////////////////////////////////////////////

    if (argc != 3)
    {
        fprintf(stderr, "Usage: \"%s file.stk smfile.txt\"\n", argv[0]) ;
        return EXIT_FAILURE ;
    }

    // Init StackDescription and parse the input file
    ////////////////////////////////////////////////////////////////////////////

    stack_description_init (&stkd) ;
    analysis_init          (&analysis) ;
    output_init            (&output) ;

    if (parse_stack_description_file (argv[1], &stkd, &analysis, &output) != 0)

        return EXIT_FAILURE ;

    // Init thermal data and fill it using the StackDescription
    ////////////////////////////////////////////////////////////////////////////

    thermal_data_init (&tdata) ;

    Error_t result = thermal_data_build

        (&tdata, &stkd.StackElements, stkd.Dimensions, &analysis) ;

    if (result != TDICE_SUCCESS)
    {
        stack_description_destroy (&stkd) ;
        output_destroy            (&output) ;

        return EXIT_FAILURE ;
    }

    // Run the simulation and print the output
    ////////////////////////////////////////////////////////////////////////////

    system_matrix_print (tdata.SM_A, argv[2]) ;


    // free all data
    ////////////////////////////////////////////////////////////////////////////

    thermal_data_destroy      (&tdata) ;
    stack_description_destroy (&stkd) ;
    output_destroy            (&output) ;

    return EXIT_SUCCESS ;
}
Esempio n. 2
0
int main (int argc, char** argv)
{
  StackDescription_t stkd ;
  Analysis_t         analysis ;
  Output_t           output ;
  ThermalData_t      tdata ;

  Error_t error ;

  struct timespec t1,t2;
  struct timespec res;
  res.tv_sec=0;
  res.tv_nsec=0;
  Quantity_t server_port ;

  Socket_t server_socket, client_socket ;

  NetworkMessage_t request, reply , tmapReply;

  bool headers = false ;

    /* Checks if all arguments are there **************************************/

  if (argc != 3)
  {
    fprintf (stderr, "Usage: \"%s file.stk server_port\n", argv[0]) ;

    return EXIT_FAILURE ;
  }

  server_port = atoi (argv[2]) ;

  /* Parses stack file (fills stack descrition and analysis) ****************/

  fprintf (stdout, "Preparing stk data ... ") ; fflush (stdout) ;

  stack_description_init (&stkd) ;
  analysis_init          (&analysis) ;
  output_init            (&output) ;
  
    error = parse_stack_description_file (argv[1], &stkd, &analysis, &output) ;

  /* Initialise the compression related data ********************************/

  Quantity_t nflpel_ = get_total_number_of_floorplan_elements (&stkd) ;

  float hist_table_d[nflpel_][HIST_TABLE_SIZE];
  
  int tail_d[nflpel_];
  
  for (unsigned int i = 0; i < nflpel_; i++)
  {
    tail_d[i] = 0;
  
    for (int j = 0; j < HIST_TABLE_SIZE; j++)
      hist_table_d[i][j] = -1.0;
  }

  for(int i=0;i<MAXRES;i++)
    for(int j=0;j<MAXROWS;j++)
      for(int k=0;k<MAXCOLS;k++)
        for(int l=0;l<HIST_TABLE_SIZE;l++)
          histTableMap[i][j][k][l] = -1.0;
  int compressionUsed;



  if (error != TDICE_SUCCESS)    return EXIT_FAILURE ;

  if (analysis.AnalysisType != TDICE_ANALYSIS_TYPE_TRANSIENT)
  {
    fprintf (stderr, "only transient analysis!\n") ;

    goto wrong_analysis_error ;
  }

  fprintf (stdout, "done !\n") ;

  /* Prepares thermal data **************************************************/

  fprintf (stdout, "Preparing thermal data ... ") ; fflush (stdout) ;

  thermal_data_init (&tdata) ;

  error = thermal_data_build

    (&tdata, &stkd.StackElements, stkd.Dimensions, &analysis) ;


  if (error != TDICE_SUCCESS)    goto ftd_error ;

  fprintf (stdout, "done !\n") ;

  /* Creates socket *********************************************************/

  fprintf (stdout, "Creating socket ... ") ; fflush (stdout) ;

  socket_init (&server_socket) ;

  error = open_server_socket (&server_socket, server_port) ;



  if (error != TDICE_SUCCESS)    goto socket_error ;

  fprintf (stdout, "done !\n") ;

  /* Waits for a client to connect ******************************************/

  fprintf (stdout, "Waiting for client ... ") ; fflush (stdout) ;

  socket_init (&client_socket) ;

  error = wait_for_client (&server_socket, &client_socket) ;

  if (error != TDICE_SUCCESS)    goto wait_error ;

  fprintf (stdout, "done !\n") ;

  //float timeForTmap = 0.0;
  /* Runs the simlation *****************************************************/

  do
  {
    network_message_init (&request) ;

    receive_message_from_socket (&client_socket, &request) ;

    switch (*request.Type)
    {

      /**********************************************************************/
      case TDICE_COMPRESSION_USED :
        {
          extract_message_word (&request, &compressionUsed,0);
          break;
        }
      case TDICE_EXIT_SIMULATION :
        {
          network_message_destroy (&request) ;

          goto quit ;
        }

        /**********************************************************************/

      case TDICE_RESET_THERMAL_STATE :
        {
          reset_thermal_state (&tdata, &analysis) ;

          break ;
        }

        /**********************************************************************/

      case TDICE_TOTAL_NUMBER_OF_FLOORPLAN_ELEMENTS :
        {
          network_message_init (&reply) ;

          build_message_head   (&reply, TDICE_TOTAL_NUMBER_OF_FLOORPLAN_ELEMENTS) ;

          Quantity_t nflpel = get_total_number_of_floorplan_elements (&stkd) ;

          insert_message_word (&reply, &nflpel) ;

          send_message_to_socket (&client_socket, &reply) ;

          network_message_destroy (&reply) ;

          break ;
        }

        /**********************************************************************/

      case TDICE_INSERT_POWERS_AND_SIMULATE_SLOT :
        {
          Quantity_t nflpel, index ;

          PowersQueue_t queue ;

          powers_queue_init (&queue) ;

          extract_message_word (&request, &nflpel, 0) ;

          powers_queue_build (&queue, nflpel) ;
          if(compressionUsed)
          {
            unsigned int tmp[3];
            int ret=-1;
            unsigned int compressedWord;
            float power=0.0;
            int readIndex=1;
            index=1;
            initTmp(tmp);
            extract_message_word (&request, &compressedWord,readIndex);
            while(index<=nflpel)
            {
              ret=getNextDecompressedWord(compressedWord,tmp,0);
              switch(ret)
              {
                case WI_DC:
                case WC_DC: 
                  switch(tmp[0])
                  {
                    case 0:
                      power=(int)hist_table_d[index-1][tmp[1]]+(hist_table_d[index-1][tmp[2]]-(int)hist_table_d[index-1][tmp[2]]);
                      break; 
                    case 1:
                      power=tmp[1]+(hist_table_d[index-1][tmp[2]]-(int)hist_table_d[index-1][tmp[2]]);
                      hist_table_d[index-1][tail_d[index-1]]=power;
                      tail_d[index-1]=(tail_d[index-1]+1)%HIST_TABLE_SIZE;
                      break;
                    case 2:
                      power=(int)hist_table_d[index-1][tmp[1]]+((float)tmp[2])/(pow(10,DECIMAL_DIGITS));
                      hist_table_d[index-1][tail_d[index-1]]=power;
                      tail_d[index-1]=(tail_d[index-1]+1)%HIST_TABLE_SIZE;
                      break;
                    case 3:
                      power=tmp[1]+((float)tmp[2])/(pow(10,DECIMAL_DIGITS));
                      hist_table_d[index-1][tail_d[index-1]]=power;
                      tail_d[index-1]=(tail_d[index-1]+1)%HIST_TABLE_SIZE;
                      break;
                  }
                  put_into_powers_queue(&queue,power);
                  index++;
                  initTmp(tmp);
                  if(ret==WC_DC)
                  {
                    readIndex++;
                    extract_message_word (&request, &compressedWord,readIndex);
                  }
                  break;
                case WC_DI:
                  readIndex++;
                  extract_message_word (&request, &compressedWord,readIndex);
                  break;
                default:
                  break;
              }
            }
            ret=getNextDecompressedWord(compressedWord,tmp,1);
          }
          /* NO COMPRESSION *************************************/
          else 
          {
            for (index = 1, nflpel++ ; index != nflpel ; index++)
            {
              float power_value ;

              extract_message_word (&request, &power_value, index) ;

              put_into_powers_queue (&queue, power_value) ;
            }

          }
          error = insert_power_values (&tdata.PowerGrid, &queue) ;

          if (error != TDICE_SUCCESS)
          {
            fprintf (stderr, "error: insert power values\n") ;

            powers_queue_destroy (&queue) ;

            goto sim_error ;
          }

          powers_queue_destroy (&queue) ;

          network_message_init (&reply) ;
          build_message_head   (&reply, TDICE_INSERT_POWERS_AND_SIMULATE_SLOT) ;

          SimResult_t result = emulate_slot

            (&tdata, stkd.Dimensions, &analysis) ;

          insert_message_word (&reply, &result) ;

          send_message_to_socket (&client_socket, &reply) ;

          network_message_destroy (&reply) ;

          if (result != TDICE_SLOT_DONE)
          {
            fprintf (stderr, "error %d: emulate slot\n", result) ;

            goto sim_error ;
          }

          break ;
        }

        /**********************************************************************/

      case TDICE_SEND_OUTPUT :
        {
          OutputInstant_t  instant ;
          OutputType_t     type ;
          OutputQuantity_t quantity ;
          //clock_t Time;          

          extract_message_word (&request, &instant,  0) ;
          extract_message_word (&request, &type,     1) ;
          extract_message_word (&request, &quantity, 2) ;

          network_message_init (&reply) ;
          build_message_head   (&reply, TDICE_SEND_OUTPUT) ;

          float   time = get_simulated_time (&analysis) ;
          Quantity_t n = get_number_of_inspection_points (&output, instant, type, quantity) ;

          insert_message_word (&reply, &time) ;
          insert_message_word (&reply, &n) ;

          if (n > 0)
          {
            error = fill_output_message

              (&output, stkd.Dimensions,
               tdata.Temperatures, tdata.PowerGrid.Sources,
               instant, type, quantity, &reply) ;

            if (error != TDICE_SUCCESS)
            {
              fprintf (stderr, "error: generate message content\n") ;

              network_message_destroy (&reply) ;

              goto sim_error ;
            }
          }
          if(type == TDICE_OUTPUT_TYPE_TMAP && compressionUsed)
          {
            //init histTableMap tailMap for compression
            //New packet tmapReply is being initialised /
            //clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &t1);
            network_message_init (&tmapReply);
            build_message_head (&tmapReply, TDICE_SEND_OUTPUT);

            insert_message_word (&tmapReply, &time);
            insert_message_word (&tmapReply, &n);

            CellIndex_t nrows, ncols;

            int readIndex = 4;

            //get the no of rows and cols from reply packet
            extract_message_word (&reply, &nrows, 2);
            extract_message_word (&reply, &ncols, 3);

            //insert nrows and ncols into my packet
            insert_message_word (&tmapReply, &nrows);
            insert_message_word (&tmapReply, &ncols);

            int ret=-1, f1, f2;
            unsigned int compressedWord, i, j, k, l;
            unsigned int flag=0, intPart, decPart;

            unsigned int tmp[3];
            float temper;

            for(i=0; i<n; i++)
              for(j=0; j<nrows; j++)
                for(k=0; k<ncols; k++)
                {
                  f1 = -1;f2 = -1;

                  extract_message_word (&reply, &temper, readIndex);
                  readIndex++;
                  unsigned int intPartTemper, decPartTemper;
                  getParts (temper, &intPartTemper, &decPartTemper);
                  for (l=0;l<HIST_TABLE_SIZE;l++)
                  {
                    if(histTableMap[i][j][k][l] != -1.0)
                    {
                      getParts (histTableMap[i][j][k][l], &intPart, &decPart);
                      if(intPart == intPartTemper)f1 = l;
                      float val=((float)decPart-decPartTemper);
                      val=val/(float)pow(10,DECIMAL_DIGITS);
                      val=abs (val);
                      if(val <= TOLERANCE)
                        f2=l;
                    }
                  }
                  flag = getFlag (f1,f2);

                  switch (flag)
                  {
                    case 1:
                      histTableMap[i][j][k][tailMap[i][j][k]] = intPartTemper + 
                        (histTableMap[i][j][k][f2] - (int)histTableMap[i][j][k][f2]);
                      tailMap[i][j][k] = (tailMap[i][j][k] + 1)%HIST_TABLE_SIZE;
                      break;

                    case 2:
                      histTableMap[i][j][k][tailMap[i][j][k]] = (int)histTableMap[i][j][k][f1] + 
                        ((float)decPartTemper)/(pow(10,DECIMAL_DIGITS));
                      tailMap[i][j][k] = (tailMap[i][j][k] + 1)%HIST_TABLE_SIZE;
                      break;

                    case 3:
                      histTableMap[i][j][k][tailMap[i][j][k]] = intPartTemper + 
                        ((float)decPartTemper)/(pow(10,DECIMAL_DIGITS));
                      tailMap[i][j][k] = (tailMap[i][j][k] + 1)%HIST_TABLE_SIZE;
                      break;

                    default:
                      break;
                  }
                  getArgsInArray (f1, f2, flag, intPartTemper, decPartTemper, tmp);
                  ret = getNextCompressedWord (tmp[1], tmp[2], flag, &compressedWord, !WRAP_UP);
                  if(ret != WORD_INCOMPLETE)
                    insert_message_word (&tmapReply, &compressedWord);
                }
            switch(ret)
            {
              case WORD_INCOMPLETE:
              case WORD_IN_TEMP:
                getNextCompressedWord (tmp[1], tmp[2], flag, &compressedWord, WRAP_UP);
                insert_message_word (&tmapReply, &compressedWord);
                break;
              default:
                break;
            }
            clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &t1);
            send_message_to_socket (&client_socket, &tmapReply);
            clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &t2);
            diff(t1,t2,&res); 
            network_message_destroy (&reply);
            network_message_destroy (&tmapReply);
          }
          else
          {
            if(type == TDICE_OUTPUT_TYPE_TMAP)
            {
            //Time = clock();            
            clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &t1);
            send_message_to_socket (&client_socket, &reply);
            //timeForTmap +=  ((double)clock() - Time) / CLOCKS_PER_SEC  ;
            clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &t2);
            diff(t1,t2,&res); 
            }
            else
            send_message_to_socket (&client_socket, &reply) ;

            network_message_destroy (&reply) ;
          }
          break ;
        }

        /**********************************************************************/

      case TDICE_PRINT_OUTPUT :
        {
          OutputInstant_t  instant ;

          extract_message_word (&request, &instant,  0) ;

          if (headers == false)
          {
            Error_t error = generate_output_headers

              (&output, stkd.Dimensions, (String_t)"% ") ;

            if (error != TDICE_SUCCESS)
            {
              fprintf (stderr, "error in initializing output files \n ");

              goto sim_error ;
            }

            headers = true ;
          }

          generate_output

            (&output, stkd.Dimensions,
             tdata.Temperatures, tdata.PowerGrid.Sources,
             get_simulated_time (&analysis), instant) ;

          break ;
        }

        /**********************************************************************/

      case TDICE_SIMULATE_SLOT :
        {
          network_message_init (&reply) ;
          build_message_head   (&reply, TDICE_SIMULATE_SLOT) ;

          SimResult_t result = emulate_slot (&tdata, stkd.Dimensions, &analysis) ;

          insert_message_word (&reply, &result) ;

          send_message_to_socket (&client_socket, &reply) ;

          network_message_destroy (&reply) ;

          if (result == TDICE_END_OF_SIMULATION)
          {
            network_message_destroy (&request) ;

            goto quit ;
          }
          else if (result != TDICE_SLOT_DONE)
          {
            fprintf (stderr, "error %d: emulate slot\n", result) ;

            goto sim_error ;
          }

          break ;
        }

        /**********************************************************************/

      case TDICE_SIMULATE_STEP :
        {
          network_message_init (&reply) ;
          build_message_head   (&reply, TDICE_SIMULATE_STEP) ;

          SimResult_t result = emulate_step (&tdata, stkd.Dimensions, &analysis) ;

          insert_message_word (&reply, &result) ;

          send_message_to_socket (&client_socket, &reply) ;

          network_message_destroy (&reply) ;

          if (result == TDICE_END_OF_SIMULATION)
          {
            network_message_destroy (&request) ;

            goto quit ;
          }
          else if (result != TDICE_STEP_DONE && result != TDICE_SLOT_DONE)
          {
            fprintf (stderr, "error %d: emulate step\n", result) ;

            goto sim_error ;
          }

          break ;
        }

        /**********************************************************************/

      default :

        fprintf (stderr, "ERROR :: received unknown message type") ;
    }

    network_message_destroy (&request) ;

  } while (1) ;

  /**************************************************************************/

quit :

  socket_close              (&client_socket) ;
  socket_close              (&server_socket) ;
  thermal_data_destroy      (&tdata) ;
  stack_description_destroy (&stkd) ;
  output_destroy            (&output) ;

  printf("Time taken %d sec %d nsec \n",(int)res.tv_sec,(int)res.tv_nsec);
  return EXIT_SUCCESS ;

sim_error :
  network_message_destroy   (&request) ;
  socket_close              (&client_socket) ;
wait_error :
  socket_close              (&server_socket) ;
socket_error :
  thermal_data_destroy      (&tdata) ;
ftd_error :
wrong_analysis_error :
  stack_description_destroy (&stkd) ;
  output_destroy            (&output) ;

  return EXIT_FAILURE ;
}