Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
document last changes
[simgrid.git] / src / nws_portability / Forecast / forecast_api.c
1 /* $Id$ */
2
3 #include <stdlib.h>        /* free() malloc() */
4 #include <string.h>        /* strdup() */
5 #include "forc.h"          /* forecaster functions */
6 #include "diagnostic.h"    /* FAIL() */
7 #include "forecast_api.h"
8
9
10 struct FORECASTAPI_ForecastStateStruct {
11   char *Forecaster_state;
12 };
13
14
15 void
16 FORECASTAPI_FreeForecastState(FORECASTAPI_ForecastState **state) {
17   FreeForcl((*state)->Forecaster_state);
18   free(*state);
19   *state = NULL;
20 }
21
22 const char *
23 FORECASTAPI_MethodName(unsigned int methodIndex) {
24
25   int i;
26   static int methodCount = 0;
27   static char *methodNames[MAX_FORC];
28   char *stateForNames;
29
30   if(methodCount == 0) {
31     stateForNames = InitForcl(MAX_FORC, MAX_DATA_ENTRIES);
32     GetForcNames(stateForNames, methodNames, MAX_FORC, &methodCount);
33     /*
34     ** GetForNames() gives us pointers which will be freed by FreeForcl(), so
35     ** we have to strdup() them.
36     */
37     for(i = 0; i < methodCount; i++) {
38       methodNames[i] = strdup(methodNames[i]);
39     }
40     FreeForcl(stateForNames);
41   }
42
43   return((methodIndex < methodCount) ? methodNames[methodIndex] : NULL);
44
45 }
46   
47
48 FORECASTAPI_ForecastState *
49 FORECASTAPI_NewForecastState(void) {
50   FORECASTAPI_ForecastState *returnValue;
51   /*
52    * init the forecaster state
53    */
54   returnValue = 
55     (FORECASTAPI_ForecastState *)malloc(sizeof(FORECASTAPI_ForecastState)); 
56   if(returnValue == NULL)
57     FAIL("NewForecastState: out of memory\n");
58   
59   returnValue->Forecaster_state = InitForcl(MAX_FORC,MAX_DATA_ENTRIES);
60   if(returnValue->Forecaster_state == NULL) {
61     free(returnValue);
62     FAIL("NewForecastState: out of memory\n");
63   }
64   return returnValue;
65 }
66
67
68 void
69 FORECASTAPI_UpdateForecastState(FORECASTAPI_ForecastState *state,
70                                 const FORECASTAPI_Measurement *measurements,
71                                 size_t howManyMeasurements,
72                                 FORECASTAPI_ForecastCollection *forecasts,
73                                 size_t howManyForecasts) {
74         int i;
75
76         /* sanity check */
77         if (state == NULL) {
78                 return;
79         }
80
81         /* check if we want forecast */
82         if (forecasts == NULL) {
83                 howManyForecasts = 0;
84         }
85
86         for(i = howManyMeasurements - 1; i >= 0; i--) {
87                 UpdateForecasts(state->Forecaster_state, (double)measurements[i].timeStamp, (double)measurements[i].measurement);
88                 if(i < howManyForecasts) {
89                         FORECASTAPI_ComputeForecast(state, &forecasts[i]);
90                         forecasts[i].measurement = measurements[i];
91                 }
92         }
93 }
94
95 int
96 FORECASTAPI_ComputeForecast(        FORECASTAPI_ForecastState *state,
97                                 FORECASTAPI_ForecastCollection *forecast) {
98
99         /* sanity check */
100         if (state == NULL || forecast == NULL) {
101                 return 1;
102         }
103
104         forecast->forecasts[FORECASTAPI_MAE_FORECAST].forecast = MAEForecast(state->Forecaster_state);
105         forecast->forecasts[FORECASTAPI_MAE_FORECAST].error = MAEError(state->Forecaster_state);
106         forecast->forecasts[FORECASTAPI_MAE_FORECAST].methodUsed = MAEMethod(state->Forecaster_state);
107         forecast->forecasts[FORECASTAPI_MSE_FORECAST].forecast = MSEForecast(state->Forecaster_state);
108         forecast->forecasts[FORECASTAPI_MSE_FORECAST].error = MSEError(state->Forecaster_state);
109         forecast->forecasts[FORECASTAPI_MSE_FORECAST].methodUsed = MSEMethod(state->Forecaster_state);
110
111         return 0;
112 }
113