Browse Source

move some helper functions to starpu_helper.h

Nathalie Furmento 5 years ago
parent
commit
56fbd1e4f5
3 changed files with 180 additions and 160 deletions
  1. 18 2
      include/starpu_helper.h
  2. 118 0
      src/common/utils.c
  3. 44 158
      src/util/openmp_runtime_support_environment.c

+ 18 - 2
include/starpu_helper.h

@@ -50,6 +50,20 @@ extern int _starpu_silent;
 char *starpu_getenv(const char *str);
 
 /**
+   If the environment variable \c str is defined and its value is contained in the array \c strings, return the array position.
+   Raise an error if the environment variable \c str is defined with a value not in \c strings
+   Return \c defvalue if the environment variable \c str is not defined.
+ */
+int starpu_get_env_string_var_default(const char *str, const char *strings[], int defvalue);
+
+/**
+   If the environment variable \c str is defined with a well-defined size value, return the value as a size in bytes. Expected size qualifiers are b, B, k, K, m, M, g, G. The default qualifier is K.
+   If the environment variable \c str is not defined or is empty, return \c defval
+   Raise an error if the value of the environment variable \c str is not well-defined.
+ */
+int starpu_get_env_size_default(const char *str, int defval);
+
+/**
    Return the integer value of the environment variable named \p str.
    Return 0 otherwise (the variable does not exist or has a
    non-integer value).
@@ -66,7 +80,8 @@ static __starpu_inline int starpu_get_env_number(const char *str)
 		char *pcheck;
 
 		val = strtol(strval, &pcheck, 10);
-		if (*pcheck) {
+		if (*pcheck)
+		{
 			fprintf(stderr,"The %s environment variable must contain an integer\n", str);
 			STARPU_ABORT();
 		}
@@ -103,7 +118,8 @@ static __starpu_inline float starpu_get_env_float_default(const char *str, float
 		char *pcheck;
 
 		val = strtof(strval, &pcheck);
-		if (*pcheck) {
+		if (*pcheck)
+		{
 			fprintf(stderr,"The %s environment variable must contain a float\n", str);
 			STARPU_ABORT();
 		}

+ 118 - 0
src/common/utils.c

@@ -24,6 +24,7 @@
 #include <unistd.h>
 #endif
 #include <fcntl.h>
+#include <ctype.h>
 
 #if defined(_WIN32) && !defined(__CYGWIN__)
 #include <io.h>
@@ -621,3 +622,120 @@ char *starpu_getenv(const char *str)
 #endif
 	return getenv(str);
 }
+
+int _strings_ncmp(const char *strings[], const char *str)
+{
+	int pos = 0;
+	while (strings[pos])
+	{
+		if ((strlen(str) == strlen(strings[pos]) && strncasecmp(str, strings[pos], strlen(strings[pos])) == 0))
+			break;
+		pos++;
+	}
+	if (strings[pos] == NULL)
+		return -1;
+	return pos;
+}
+
+int starpu_get_env_string_var_default(const char *str, const char *strings[], int defvalue)
+{
+	int val;
+	char *strval;
+
+	strval = starpu_getenv(str);
+	if (!strval)
+	{
+		val = defvalue;
+	}
+	else
+	{
+		val = _strings_ncmp(strings, strval);
+		if (val < 0)
+		{
+			_STARPU_MSG("\n");
+			_STARPU_MSG("Invalid value '%s' for environment variable '%s'\n", strval, str);
+			_STARPU_MSG("Valid values are:\n");
+			for(int i=0;strings[i]!=NULL;i++) _STARPU_MSG("\t%s\n",strings[i]);
+			_STARPU_MSG("\n");
+			STARPU_ABORT();
+		}
+	}
+	return val;
+}
+
+static void remove_spaces(char *str)
+{
+	int i = 0;
+	int j = 0;
+
+	while (str[j] != '\0')
+	{
+		if (isspace(str[j]))
+		{
+			j++;
+			continue;
+		}
+		if (j > i)
+		{
+			str[i] = str[j];
+		}
+		i++;
+		j++;
+	}
+	if (j > i)
+	{
+		str[i] = str[j];
+	}
+}
+
+int starpu_get_env_size_default(const char *str, int defval)
+{
+	int val;
+	char *strval;
+
+	strval = starpu_getenv(str);
+	if (!strval)
+	{
+		val = defval;
+	}
+	else
+	{
+		char *value = strdup(strval);
+		if (value == NULL)
+			_STARPU_ERROR("memory allocation failed\n");
+		remove_spaces(value);
+		if (value[0] == '\0')
+		{
+			free(value);
+			val = defval;
+		}
+		else
+		{
+			char *endptr = NULL;
+			int mult = 1024;
+			errno = 0;
+			int v = (int)strtol(value, &endptr, 10);
+			if (errno != 0)
+				_STARPU_ERROR("could not parse environment variable '%s' with value '%s', strtol failed with error %s\n", str, value, strerror(errno));
+			if (*endptr != '\0')
+			{
+				switch (*endptr)
+				{
+				case 'b':
+				case 'B': mult = 1; break;
+				case 'k':
+				case 'K': mult = 1024; break;
+				case 'm':
+				case 'M': mult = 1024*1024; break;
+				case 'g':
+				case 'G': mult = 1024*1024*1024; break;
+				default:
+					_STARPU_ERROR("could not parse environment variable '%s' with value '%s' size suffix invalid\n", str, value);
+				}
+			}
+			val = v*mult;
+			free(value);
+		}
+	}
+	return val;
+}

+ 44 - 158
src/util/openmp_runtime_support_environment.c

@@ -50,7 +50,6 @@ static struct starpu_omp_initial_icv_values _initial_icv_values =
 
 struct starpu_omp_initial_icv_values *_starpu_omp_initial_icv_values = NULL;
 
-/* TODO: move to utils */
 static void remove_spaces(char *str)
 {
 	int i = 0;
@@ -75,21 +74,7 @@ static void remove_spaces(char *str)
 		str[i] = str[j];
 	}
 }
-/* TODO: move to utils */
-static int strings_cmp(const char *strings[], const char *str)
-{
-	int mode = 0;
-	while (strings[mode])
-	{
-		if (strncasecmp(str, strings[mode], strlen(strings[mode])) == 0)
-			break;
-		mode++;
-	}
-	if (strings[mode] == NULL)
-		return -1;
-	return mode;
-}
-/* TODO: move to utils */
+
 static int stringsn_cmp(const char *strings[], const char *str, size_t n)
 {
 	int mode = 0;
@@ -104,23 +89,6 @@ static int stringsn_cmp(const char *strings[], const char *str, size_t n)
 	return mode;
 }
 
-/* TODO: move to utils */
-static int read_string_var(const char *str, const char *strings[], int *dst)
-{
-	int val;
-
-	if (!str)
-		return 0;
-
-	val = strings_cmp(strings, str);
-	if (val < 0)
-		return 0;
-
-	*dst = val;
-	return 1;
-}
-
-/* TODO: move to utils */
 static int read_int_var(const char *str, int *dst)
 {
 	char *endptr;
@@ -150,45 +118,18 @@ static int read_int_var(const char *str, int *dst)
 	return 1;
 }
 
-static void read_size_var(const char *var, int *dest)
+int _strings_cmp(const char *strings[], const char *str)
 {
-	const char *env = starpu_getenv(var);
-	if (env)
+	int mode = 0;
+	while (strings[mode])
 	{
-		char *str = strdup(env);
-		if (str == NULL)
-			_STARPU_ERROR("memory allocation failed\n");
-		remove_spaces(str);
-		if (str[0] == '\0')
-		{
-			free(str);
-			return;
-		}
-		char *endptr = NULL;
-		int mult = 1024;
-		errno = 0;
-		int v = (int)strtol(str, &endptr, 10);
-		if (errno != 0)
-			_STARPU_ERROR("could not parse environment variable %s, strtol failed with error %s\n", var, strerror(errno));
-		if (*endptr != '\0')
-		{
-			switch (*endptr)
-			{
-				case 'b':
-				case 'B': mult = 1; break;
-				case 'k':
-				case 'K': mult = 1024; break;
-				case 'm':
-				case 'M': mult = 1024*1024; break;
-				case 'g':
-				case 'G': mult = 1024*1024*1024; break;
-			default:
-				_STARPU_ERROR("could not parse environment variable %s size suffix invalid\n", var);
-			}
-		}
-		*dest = v*mult;
-		free(str);
+		if (strncasecmp(str, strings[mode], strlen(strings[mode])) == 0)
+			break;
+		mode++;
 	}
+	if (strings[mode] == NULL)
+		return -1;
+	return mode;
 }
 
 static void read_sched_var(const char *var, int *dest, unsigned long long *dest_chunk)
@@ -206,7 +147,7 @@ static void read_sched_var(const char *var, int *dest, unsigned long long *dest_
 			return;
 		}
 		static const char *strings[] = { "undefined", "static", "dynamic", "guided", "auto", NULL };
-		int mode = strings_cmp(strings, str);
+		int mode = _strings_cmp(strings, str);
 		if (mode < 0)
 			_STARPU_ERROR("parse error in variable %s\n", var);
 		*dest = mode;
@@ -231,47 +172,6 @@ static void read_sched_var(const char *var, int *dest, unsigned long long *dest_
 	}
 }
 
-static void read_wait_policy_var()
-{
-	const char *strings[] = { "passive", "active", NULL };
-	int ret, value;
-	char *env;
-
-	env = starpu_getenv("OMP_WAIT_POLICY");
-	if (!env)
-		return;
-
-	ret = read_string_var(env, strings, &value);
-	if (!ret)
-	{
-		_STARPU_MSG("StarPU: Invalid value for environment variable OMP_WAIT_POLICY\n");
-		return;
-	}
-	_initial_icv_values.wait_policy_var = value;
-
-}
-
-static void read_display_env_var(int *dest)
-{
-	const char *strings[] = { "false", "true", "verbose", NULL };
-	int ret, value;
-	char *env;
-
-	env = starpu_getenv("OMP_DISPLAY_ENV");
-	if (!env)
-		return;
-
-	ret = read_string_var(env, strings, &value);
-	if (!ret)
-	{
-		_STARPU_MSG("StarPU: Invalid value for environment variable OMP_DISPLAY_ENV\n");
-		return;
-	}
-
-	*dest = value;
-
-}
-
 static int convert_place_name(const char *str, size_t n)
 {
 	static const char *strings[] = { "threads", "cores", "sockets", NULL };
@@ -547,6 +447,21 @@ static void free_places(struct starpu_omp_place *places)
 	}
 }
 
+static int _get_env_string_var(const char *str, const char *strings[], int *dst)
+{
+	int val;
+
+	if (!str)
+		return 0;
+
+	val = _strings_cmp(strings, str);
+	if (val < 0)
+		return 0;
+
+	*dst = val;
+	return 1;
+}
+
 static void read_proc_bind_var()
 {
 	const int max_levels = _initial_icv_values.max_active_levels_var + 1;
@@ -567,7 +482,7 @@ static void read_proc_bind_var()
 		{
 			int value;
 
-			if (!read_string_var(token, strings, &value))
+			if (!_get_env_string_var(token, strings, &value))
 			{
 				_STARPU_MSG("StarPU: Invalid value for environment variable OMP_PROC_BIND\n");
 				break;
@@ -611,55 +526,25 @@ static void read_num_threads_var()
 	_initial_icv_values.nthreads_var = num_threads_list;
 }
 
-static void read_omp_int_var(const char *name, int *icv)
+static void read_omp_environment(void)
 {
-	int ret, value;
-	char *env;
-
-	env = starpu_getenv(name);
-	if (!env)
-		return;
-
-	ret = read_int_var(env, &value);
-	if (!ret || value < 0)
-	{
-		_STARPU_MSG("StarPU: Invalid value for environment variable %s\n", name);
-		return;
-	}
-	*icv = value;
-}
+	const char *boolean_strings[] = { "false", "true", NULL };
 
-static void read_omp_boolean_var(const char *name, int *icv)
-{
-	const char *strings[] = { "false", "true", NULL };
-	int ret, value;
-	char *env;
+	_initial_icv_values.dyn_var = starpu_get_env_string_var_default("OMP_DYNAMIC", boolean_strings, _initial_icv_values.dyn_var);
+	_initial_icv_values.nest_var = starpu_get_env_string_var_default("OMP_NESTED", boolean_strings, _initial_icv_values.nest_var);
 
-	env = starpu_getenv(name);
-	if (!env)
-		return;
+	read_sched_var("OMP_SCHEDULE", &_initial_icv_values.run_sched_var, &_initial_icv_values.run_sched_chunk_var);
+	_initial_icv_values.stacksize_var = starpu_get_env_size_default("OMP_STACKSIZE", _initial_icv_values.stacksize_var);
 
-	ret = read_string_var(env, strings, &value);
-	if (!ret)
 	{
-		_STARPU_MSG("StarPU: Invalid value for environment variable %s\n", name);
-		return;
+		const char *strings[] = { "passive", "active", NULL };
+		_initial_icv_values.wait_policy_var = starpu_get_env_string_var_default("OMP_WAIT_POLICY", strings, _initial_icv_values.wait_policy_var);
 	}
-	*icv = value;
-}
-
-static void read_omp_environment(void)
-{
-	read_omp_boolean_var("OMP_DYNAMIC", &_initial_icv_values.dyn_var);
-	read_omp_boolean_var("OMP_NESTED", &_initial_icv_values.nest_var);
-	read_sched_var("OMP_SCHEDULE", &_initial_icv_values.run_sched_var, &_initial_icv_values.run_sched_chunk_var);
-	read_size_var("OMP_STACKSIZE", &_initial_icv_values.stacksize_var);
-	read_wait_policy_var();
-	read_omp_int_var("OMP_THREAD_LIMIT", &_initial_icv_values.thread_limit_var);
-	read_omp_int_var("OMP_MAX_ACTIVE_LEVELS", &_initial_icv_values.max_active_levels_var);
-	read_omp_boolean_var("OMP_CANCELLATION", &_initial_icv_values.cancel_var);
-	read_omp_int_var("OMP_DEFAULT_DEVICE", &_initial_icv_values.default_device_var);
-	read_omp_int_var("OMP_MAX_TASK_PRIORITY", &_initial_icv_values.max_task_priority_var);
+	_initial_icv_values.thread_limit_var = starpu_get_env_number_default("OMP_THREAD_LIMIT", _initial_icv_values.thread_limit_var);
+	_initial_icv_values.max_active_levels_var = starpu_get_env_number_default("OMP_MAX_ACTIVE_LEVELS", _initial_icv_values.max_active_levels_var);
+	_initial_icv_values.cancel_var = starpu_get_env_string_var_default("OMP_CANCELLATION", boolean_strings, _initial_icv_values.cancel_var);
+	_initial_icv_values.default_device_var = starpu_get_env_number_default("OMP_DEFAULT_DEVICE", _initial_icv_values.default_device_var);
+	_initial_icv_values.max_task_priority_var = starpu_get_env_number_default("OMP_MAX_TASK_PRIORITY", _initial_icv_values.max_task_priority_var);
 
 	/* Avoid overflow e.g. in num_threads_list allocation */
 	STARPU_ASSERT_MSG(_initial_icv_values.max_active_levels_var > 0 && _initial_icv_values.max_active_levels_var < 1000000, "OMP_MAX_ACTIVE_LEVELS should have a reasonable value");
@@ -738,7 +623,7 @@ static void display_omp_environment(int verbosity_level)
 				break;
 		}
 		printf("'\n");
-				
+
 		printf("  [host] OMP_STACKSIZE = '%d'\n", _starpu_omp_initial_icv_values->stacksize_var);
 		printf("  [host] OMP_WAIT_POLICY = '%s'\n", _starpu_omp_initial_icv_values->wait_policy_var?"ACTIVE":"PASSIVE");
 		printf("  [host] OMP_MAX_ACTIVE_LEVELS = '%d'\n", _starpu_omp_initial_icv_values->max_active_levels_var);
@@ -873,8 +758,9 @@ static void display_omp_environment(int verbosity_level)
 void _starpu_omp_environment_init(void)
 {
 	read_omp_environment();
-	int display_env = 0;
-	read_display_env_var(&display_env);
+
+	const char *strings[] = { "false", "true", "verbose", NULL };
+	int display_env = starpu_get_env_string_var_default("OMP_DISPLAY_ENV", strings, 0);
 	if (display_env > 0)
 	{
 		display_omp_environment(display_env);