+#include "xbt/module.h"
+#include "mc/mc.h"
+
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_kernel, surf,
+ "Logging specific to SURF (kernel)");
+
+/* Additional declarations for Windows potability. */
+
+#ifndef MAX_DRIVE
+#define MAX_DRIVE 26
+#endif
+
+#ifdef _WIN32
+#include <windows.h>
+static const char *disk_drives_letter_table[MAX_DRIVE] = {
+ "A:\\",
+ "B:\\",
+ "C:\\",
+ "D:\\",
+ "E:\\",
+ "F:\\",
+ "G:\\",
+ "H:\\",
+ "I:\\",
+ "J:\\",
+ "K:\\",
+ "L:\\",
+ "M:\\",
+ "N:\\",
+ "O:\\",
+ "P:\\",
+ "Q:\\",
+ "R:\\",
+ "S:\\",
+ "T:\\",
+ "U:\\",
+ "V:\\",
+ "W:\\",
+ "X:\\",
+ "Y:\\",
+ "Z:\\"
+};
+#endif /* #ifdef _WIN32 */
+
+/*
+ * Returns the initial path. On Windows the initial path is
+ * the current directory for the current process in the other
+ * case the function returns "./" that represents the current
+ * directory on Unix/Linux platforms.
+ */
+
+const char *__surf_get_initial_path(void)
+{
+
+#ifdef _WIN32
+ unsigned i;
+ char current_directory[MAX_PATH + 1] = { 0 };
+ unsigned int len = GetCurrentDirectory(MAX_PATH + 1, current_directory);
+ char root[4] = { 0 };
+
+ if (!len)
+ return NULL;
+
+ strncpy(root, current_directory, 3);
+
+ for (i = 0; i < MAX_DRIVE; i++) {
+ if (toupper(root[0]) == disk_drives_letter_table[i][0])
+ return disk_drives_letter_table[i];
+ }
+
+ return NULL;
+#else
+ return "./";
+#endif
+}
+
+/* The __surf_is_absolute_file_path() returns 1 if
+ * file_path is a absolute file path, in the other
+ * case the function returns 0.
+ */
+int __surf_is_absolute_file_path(const char *file_path)
+{
+#ifdef _WIN32
+ WIN32_FIND_DATA wfd = { 0 };
+ HANDLE hFile = FindFirstFile(file_path, &wfd);
+
+ if (INVALID_HANDLE_VALUE == hFile)
+ return 0;
+
+ FindClose(hFile);
+ return 1;
+#else
+ return (file_path[0] == '/');
+#endif
+}
+
+double NOW = 0;
+
+xbt_dynar_t model_list = NULL;
+tmgr_history_t history = NULL;
+lmm_system_t maxmin_system = NULL;
+xbt_dynar_t surf_path = NULL;
+
+/* Don't forget to update the option description in smx_config when you change this */
+s_surf_model_description_t surf_network_model_description[] = {
+ {"Constant", "Simplistic network model where all communication take a constant time (one second)", NULL, surf_network_model_init_Constant},
+ {"Vivaldi", "Scalable network model using the Vivaldi coordinate ideas", NULL, surf_network_model_init_Vivaldi},
+ {"CM02", "Realistic network model with lmm_solve and no correction factors", NULL, surf_network_model_init_CM02},
+ {"LV08", "Realistic network model with lmm_solve and these correction factors: latency*=10.4, bandwidth*=.92, S=8775" , NULL, surf_network_model_init_LegrandVelho},
+#ifdef HAVE_GTNETS
+ {"GTNets", "Network Pseudo-model using the GTNets simulator instead of an analytic model", NULL, surf_network_model_init_GTNETS},
+#endif
+ {"Reno", "Model using lagrange_solve instead of lmm_solve (experts only)", NULL, surf_network_model_init_Reno},
+ {"Reno2", "Model using lagrange_solve instead of lmm_solve (experts only)", NULL, surf_network_model_init_Reno2},
+ {"Vegas", "Model using lagrange_solve instead of lmm_solve (experts only)", NULL, surf_network_model_init_Vegas},
+ {NULL, NULL, NULL, NULL} /* this array must be NULL terminated */
+};
+
+s_surf_model_description_t surf_cpu_model_description[] = {
+ {"Cas01_fullupdate", "CPU classical model time=size/power", NULL, surf_cpu_model_init_Cas01},
+ {"Cas01", "Variation of Cas01_fullupdate with partial invalidation optimization of lmm system. Should produce the same values, only faster", NULL, surf_cpu_model_init_Cas01_im},
+ {"CpuTI", "Variation of Cas01 with also trace integration. Should produce the same values, only faster if you use availability traces", NULL, surf_cpu_model_init_ti},
+ {NULL, NULL, NULL, NULL} /* this array must be NULL terminated */
+};
+
+s_surf_model_description_t surf_workstation_model_description[] = {
+ {"CLM03", "Default workstation model, using LV08 and CM02 as network and CPU", NULL, surf_workstation_model_init_CLM03, create_workstations},
+ {"compound", "Workstation model allowing you to use other network and CPU models", NULL, surf_workstation_model_init_compound, create_workstations},
+ {"ptask_L07", "Workstation model with better parallel task modeling", NULL, surf_workstation_model_init_ptask_L07, NULL},
+ {NULL, NULL, NULL, NULL} /* this array must be NULL terminated */
+};
+
+void update_model_description(s_surf_model_description_t * table,
+ const char *name, surf_model_t model)
+{
+ int i = find_model_description(table, name);
+ table[i].model = model;
+}
+
+/** Displays the long description of all registered models, and quit */
+void model_help(const char* category, s_surf_model_description_t * table) {
+ int i;
+ printf("Long description of the %s models accepted by this simulator:\n",category);
+ for (i = 0; table[i].name; i++)
+ printf(" %s: %s\n", table[i].name, table[i].description);
+}
+
+int find_model_description(s_surf_model_description_t * table,
+ const char *name)
+{
+ int i;
+ char *name_list = NULL;
+
+ for (i = 0; table[i].name; i++)
+ if (!strcmp(name, table[i].name)) {
+ return i;
+ }
+ name_list = strdup(table[0].name);
+ for (i = 1; table[i].name; i++) {
+ name_list =
+ xbt_realloc(name_list, strlen(name_list) + strlen(table[i].name) + 2);
+ strcat(name_list, ", ");
+ strcat(name_list, table[i].name);
+ }
+ xbt_assert2(0, "Model '%s' is invalid! Valid models are: %s.", name,
+ name_list);
+}
+
+double generic_maxmin_share_resources(xbt_swag_t running_actions,
+ size_t offset,
+ lmm_system_t sys,
+ void (*solve) (lmm_system_t))
+{
+ surf_action_t action = NULL;
+ double min = -1;
+ double value = -1;
+#define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + (offset))))
+
+ xbt_assert0(solve, "Give me a real solver function!");
+ solve(sys);