+#ifdef HAVE_GTNETS
+XBT_LOG_EXTERNAL_CATEGORY(surf_network_gtnets);
+#endif
+
+void surf_init(int *argc, char **argv)
+{
+ /* Connect our log channels: that must be done manually under windows */
+ XBT_LOG_CONNECT(surf_cpu, surf);
+ XBT_LOG_CONNECT(surf_kernel, surf);
+ XBT_LOG_CONNECT(surf_lagrange, surf);
+ XBT_LOG_CONNECT(surf_lagrange_dichotomy, surf_lagrange);
+ XBT_LOG_CONNECT(surf_maxmin, surf);
+ XBT_LOG_CONNECT(surf_network, surf);
+ XBT_LOG_CONNECT(surf_trace, surf);
+ XBT_LOG_CONNECT(surf_parse, surf);
+ XBT_LOG_CONNECT(surf_timer, surf);
+ XBT_LOG_CONNECT(surf_workstation, surf);
+ XBT_LOG_CONNECT(surf_config, surf);
+ XBT_LOG_CONNECT(surf_route, surf);
+
+#ifdef HAVE_GTNETS
+ XBT_LOG_CONNECT(surf_network_gtnets, surf);
+#endif
+
+ xbt_init(argc, argv);
+ if (!model_list)
+ model_list = xbt_dynar_new(sizeof(surf_model_private_t), NULL);
+ if (!history)
+ history = tmgr_history_new();
+
+ surf_config_init(argc, argv);
+ if (MC_IS_ENABLED)
+ MC_memory_init();
+}
+
+#ifdef _XBT_WIN32
+# define FILE_DELIM "\\"
+#else
+# define FILE_DELIM "/" /* FIXME: move to better location */
+#endif
+
+FILE *surf_fopen(const char *name, const char *mode)
+{
+ unsigned int cpt;
+ char *path_elm = NULL;
+ char *buff;
+ FILE *file = NULL;
+
+ xbt_assert(name);
+
+ if (__surf_is_absolute_file_path(name)) /* don't mess with absolute file names */
+ return fopen(name, mode);
+
+ /* search relative files in the path */
+ xbt_dynar_foreach(surf_path, cpt, path_elm) {
+ buff = bprintf("%s" FILE_DELIM "%s", path_elm, name);
+ file = fopen(buff, mode);
+ free(buff);
+
+ if (file)
+ return file;
+ }
+ return NULL;
+}
+
+void surf_exit(void)
+{
+ unsigned int iter;
+ surf_model_t model = NULL;
+
+ surf_config_finalize();
+
+ xbt_dynar_foreach(model_list, iter, model)
+ model->model_private->finalize();
+ xbt_dynar_free(&model_list);
+
+ if (maxmin_system) {
+ lmm_system_free(maxmin_system);
+ maxmin_system = NULL;
+ }
+ if (history) {
+ tmgr_history_free(history);
+ history = NULL;
+ }
+
+ if (surf_path)
+ xbt_dynar_free(&surf_path);
+
+ tmgr_finalize();
+ surf_parse_lex_destroy();
+ surf_parse_free_callbacks();
+ NOW = 0; /* Just in case the user plans to restart the simulation afterward */
+}
+
+void surf_presolve(void)
+{
+ double next_event_date = -1.0;
+ tmgr_trace_event_t event = NULL;
+ double value = -1.0;
+ surf_resource_t resource = NULL;
+ surf_model_t model = NULL;
+ unsigned int iter;
+
+ DEBUG0
+ ("First Run! Let's \"purge\" events and put models in the right state");
+ while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
+ if (next_event_date > NOW)
+ break;
+ while ((event =
+ tmgr_history_get_next_event_leq(history, next_event_date,
+ &value,
+ (void **) &resource))) {
+ resource->model->model_private->update_resource_state(resource,
+ event, value,
+ NOW);
+ }
+ }
+ xbt_dynar_foreach(model_list, iter, model)
+ model->model_private->update_actions_state(NOW, 0.0);
+}
+
+double surf_solve(double max_date)
+{
+ double min = -1.0; /* duration */
+ double next_event_date = -1.0;
+ double model_next_action_end = -1.0;
+ double value = -1.0;
+ surf_resource_t resource = NULL;
+ surf_model_t model = NULL;
+ tmgr_trace_event_t event = NULL;
+ unsigned int iter;
+
+ if (max_date != -1.0 && max_date != NOW) {
+ min = max_date - NOW;
+ }
+
+ DEBUG0("Looking for next action end");
+ xbt_dynar_foreach(model_list, iter, model) {
+ DEBUG1("Running for Resource [%s]", model->name);
+ model_next_action_end = model->model_private->share_resources(NOW);
+ DEBUG2("Resource [%s] : next action end = %f",
+ model->name, model_next_action_end);
+ if (((min < 0.0) || (model_next_action_end < min))
+ && (model_next_action_end >= 0.0))
+ min = model_next_action_end;
+ }
+ DEBUG1("Next action end : %f", min);
+
+ DEBUG0("Looking for next event");
+ while ((next_event_date = tmgr_history_next_date(history)) != -1.0) {
+ DEBUG1("Next TRACE event : %f", next_event_date);
+ if ((min != -1.0) && (next_event_date > NOW + min))
+ break;
+ DEBUG0("Updating models");
+ while ((event =
+ tmgr_history_get_next_event_leq(history, next_event_date,
+ &value,
+ (void **) &resource))) {
+ if (resource->model->model_private->resource_used(resource)) {
+ min = next_event_date - NOW;
+ DEBUG1
+ ("This event will modify model state. Next event set to %f",
+ min);
+ }
+ /* update state of model_obj according to new value. Does not touch lmm.
+ It will be modified if needed when updating actions */
+ DEBUG2("Calling update_resource_state for resource %s with min %lf",
+ resource->model->name, min);
+ resource->model->model_private->update_resource_state(resource,
+ event, value,
+ NOW + min);
+ }
+ }
+
+
+ /* FIXME: Moved this test to here to avoid stoping simulation if there are actions running on cpus and all cpus are with availability = 0.
+ * This may cause an infinite loop if one cpu has a trace with periodicity = 0 and the other a trace with periodicity > 0.
+ * The options are: all traces with same periodicity(0 or >0) or we need to change the way how the events are managed */
+ if (min < 0.0)
+ return -1.0;
+
+ DEBUG1("Duration set to %f", min);
+
+ NOW = NOW + min;
+
+ xbt_dynar_foreach(model_list, iter, model)
+ model->model_private->update_actions_state(NOW, min);
+
+ return min;
+}
+
+XBT_INLINE double surf_get_clock(void)
+{
+ return NOW;
+}