openmp_runtime_support_environment.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #ifdef STARPU_OPENMP
  18. #include <util/openmp_runtime_support.h>
  19. #include <stdlib.h>
  20. #include <ctype.h>
  21. #include <strings.h>
  22. #include <limits.h>
  23. #include <core/sched_policy.h>
  24. #define _STARPU_INITIAL_PLACES_LIST_SIZE 4
  25. #define _STARPU_INITIAL_PLACE_ITEMS_LIST_SIZE 4
  26. #define _STARPU_DEFAULT_STACKSIZE 2097152
  27. static struct starpu_omp_initial_icv_values _initial_icv_values =
  28. {
  29. .dyn_var = 0,
  30. .nest_var = 0,
  31. .nthreads_var = NULL,
  32. .run_sched_var = starpu_omp_sched_static,
  33. .run_sched_chunk_var = 0,
  34. .def_sched_var = starpu_omp_sched_static,
  35. .def_sched_chunk_var = 0,
  36. .bind_var = NULL,
  37. .stacksize_var = _STARPU_DEFAULT_STACKSIZE,
  38. .wait_policy_var = 0,
  39. .max_active_levels_var = STARPU_OMP_MAX_ACTIVE_LEVELS,
  40. .active_levels_var = 0,
  41. .levels_var = 0,
  42. .place_partition_var = 0,
  43. .cancel_var = 0,
  44. .default_device_var = 0,
  45. .max_task_priority_var = 0
  46. };
  47. struct starpu_omp_initial_icv_values *_starpu_omp_initial_icv_values = NULL;
  48. static void remove_spaces(char *str)
  49. {
  50. int i = 0;
  51. int j = 0;
  52. while (str[j] != '\0')
  53. {
  54. if (isspace(str[j]))
  55. {
  56. j++;
  57. continue;
  58. }
  59. if (j > i)
  60. {
  61. str[i] = str[j];
  62. }
  63. i++;
  64. j++;
  65. }
  66. if (j > i)
  67. {
  68. str[i] = str[j];
  69. }
  70. }
  71. static int stringsn_cmp(const char *strings[], const char *str, size_t n)
  72. {
  73. int mode = 0;
  74. while (strings[mode])
  75. {
  76. if (strncasecmp(str, strings[mode], n) == 0)
  77. break;
  78. mode++;
  79. }
  80. if (strings[mode] == NULL)
  81. return -1;
  82. return mode;
  83. }
  84. static int read_int_var(const char *str, int *dst)
  85. {
  86. char *endptr;
  87. int val;
  88. long lval;
  89. if (!str)
  90. return 0;
  91. errno = 0; /* To distinguish success/failure after call */
  92. lval = strtol(str, &endptr, 10);
  93. /* Check for various possible errors */
  94. if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || (errno != 0 && lval == 0))
  95. return 0;
  96. if (lval < INT_MIN || lval > INT_MAX)
  97. return 0;
  98. val = (int) lval;
  99. /* No digits were found. */
  100. if (str == endptr)
  101. return 0;
  102. *dst = val;
  103. return 1;
  104. }
  105. int _strings_cmp(const char *strings[], const char *str)
  106. {
  107. int mode = 0;
  108. while (strings[mode])
  109. {
  110. if (strncasecmp(str, strings[mode], strlen(strings[mode])) == 0)
  111. break;
  112. mode++;
  113. }
  114. if (strings[mode] == NULL)
  115. return -1;
  116. return mode;
  117. }
  118. static void read_sched_var(const char *var, int *dest, unsigned long long *dest_chunk)
  119. {
  120. const char *env = starpu_getenv(var);
  121. if (env)
  122. {
  123. char *str = strdup(env);
  124. if (str == NULL)
  125. _STARPU_ERROR("memory allocation failed\n");
  126. remove_spaces(str);
  127. if (str[0] == '\0')
  128. {
  129. free(str);
  130. return;
  131. }
  132. static const char *strings[] = { "undefined", "static", "dynamic", "guided", "auto", NULL };
  133. int mode = _strings_cmp(strings, str);
  134. if (mode < 0)
  135. _STARPU_ERROR("parse error in variable %s\n", var);
  136. *dest = mode;
  137. int offset = strlen(strings[mode]);
  138. if (str[offset] == ',')
  139. {
  140. offset++;
  141. errno = 0;
  142. long long v = strtoll(str+offset, NULL, 10);
  143. if (errno != 0)
  144. _STARPU_ERROR("could not parse environment variable %s, strtol failed with error %s\n", var, strerror(errno));
  145. if (v < 0)
  146. _STARPU_ERROR("invalid negative modifier in environment variable %s\n", var);
  147. unsigned long long uv = (unsigned long long) v;
  148. *dest_chunk = uv;
  149. }
  150. else
  151. {
  152. *dest_chunk = 1;
  153. }
  154. free(str);
  155. }
  156. }
  157. static int convert_place_name(const char *str, size_t n)
  158. {
  159. static const char *strings[] = { "threads", "cores", "sockets", NULL };
  160. int mode = stringsn_cmp(strings, str, n);
  161. if (mode < 0)
  162. _STARPU_ERROR("place abstract name parse error\n");
  163. return mode+1; /* 0 is for undefined abstract name */
  164. }
  165. /* Note: this function modifies the string str */
  166. static void read_a_place_name(char *str, struct starpu_omp_place *places)
  167. {
  168. int i = 0;
  169. /* detect exclusion of abstract name expressed as '!' prefix */
  170. if (str[i] == '!')
  171. {
  172. places->abstract_excluded = 1;
  173. i++;
  174. }
  175. else
  176. {
  177. places->abstract_excluded = 0;
  178. }
  179. /* detect length value for abstract name expressed as '(length)' suffix) */
  180. char *begin_length_spec = strchr(str+i,'(');
  181. if (begin_length_spec != NULL)
  182. {
  183. char *end_length_spec = strrchr(begin_length_spec+1, ')');
  184. if (end_length_spec == NULL || end_length_spec <= begin_length_spec+1)
  185. _STARPU_ERROR("parse error in places list\n");
  186. *begin_length_spec = '\0';
  187. *end_length_spec = '\0';
  188. errno = 0;
  189. int v = (int)strtol(begin_length_spec+1, NULL, 10);
  190. if (errno != 0)
  191. _STARPU_ERROR("parse error in places list\n");
  192. places->abstract_length = v;
  193. }
  194. else
  195. {
  196. places->abstract_length = 1;
  197. }
  198. /* convert abstract place name string to corresponding value */
  199. {
  200. int mode = convert_place_name(str+i, strlen(str+i));
  201. STARPU_ASSERT(mode >= starpu_omp_place_threads && mode <= starpu_omp_place_sockets);
  202. places->abstract_name = mode;
  203. places->numeric_places = NULL;
  204. places->nb_numeric_places = 0;
  205. }
  206. }
  207. static void read_a_places_list(const char *str, struct starpu_omp_place *places)
  208. {
  209. if (str[0] == '\0')
  210. {
  211. places->numeric_places = NULL;
  212. places->nb_numeric_places = 0;
  213. places->abstract_name = starpu_omp_place_undefined;
  214. return;
  215. }
  216. enum
  217. {
  218. state_split,
  219. state_read_brace_prefix,
  220. state_read_opening_brace,
  221. state_read_numeric_prefix,
  222. state_read_numeric,
  223. state_split_numeric,
  224. state_read_closing_brace,
  225. state_read_brace_suffix,
  226. };
  227. struct starpu_omp_numeric_place *places_list = NULL;
  228. int places_list_size = 0;
  229. int nb_places = 0;
  230. int *included_items_list = NULL;
  231. int included_items_list_size = 0;
  232. int nb_included_items = 0;
  233. int *excluded_items_list = NULL;
  234. int excluded_items_list_size = 0;
  235. int nb_excluded_items = 0;
  236. int exclude_place_flag = 0;
  237. int exclude_item_flag = 0;
  238. int i = 0;
  239. int state = state_read_brace_prefix;
  240. while (1)
  241. {
  242. switch (state)
  243. {
  244. /* split a comma separated list of numerical places */
  245. case state_split:
  246. if (str[i] == '\0')
  247. {
  248. goto eol;
  249. }
  250. else if (str[i] != ',')
  251. _STARPU_ERROR("parse error in places list\n");
  252. i++;
  253. state = state_read_brace_prefix;
  254. break;
  255. /* read optional exclude flag '!' for numerical place */
  256. case state_read_brace_prefix:
  257. exclude_place_flag = 0;
  258. if (str[i] == '!')
  259. {
  260. exclude_place_flag = 1;
  261. i++;
  262. }
  263. state = state_read_opening_brace;
  264. break;
  265. /* read place opening brace */
  266. case state_read_opening_brace:
  267. if (str[i] != '{')
  268. _STARPU_ERROR("parse error in places list\n");
  269. i++;
  270. state = state_read_numeric_prefix;
  271. break;
  272. /* read optional exclude flag '!' for numerical item */
  273. case state_read_numeric_prefix:
  274. exclude_item_flag = 0;
  275. if (str[i] == '!')
  276. {
  277. exclude_item_flag = 1;
  278. i++;
  279. }
  280. state = state_read_numeric;
  281. break;
  282. /* read numerical item */
  283. case state_read_numeric:
  284. {
  285. char *endptr = NULL;
  286. errno = 0;
  287. int v = (int)strtol(str+i, &endptr, 10);
  288. if (errno != 0)
  289. _STARPU_ERROR("parse error in places list, strtol failed with error %s\n", strerror(errno));
  290. if (exclude_item_flag)
  291. {
  292. if (excluded_items_list_size == 0)
  293. {
  294. excluded_items_list_size = _STARPU_INITIAL_PLACE_ITEMS_LIST_SIZE;
  295. _STARPU_MALLOC(excluded_items_list, excluded_items_list_size * sizeof(int));
  296. }
  297. else if (nb_excluded_items == excluded_items_list_size)
  298. {
  299. excluded_items_list_size *= 2;
  300. _STARPU_REALLOC(excluded_items_list, excluded_items_list_size * sizeof(int));
  301. }
  302. excluded_items_list[nb_excluded_items] = v;
  303. nb_excluded_items++;
  304. }
  305. else
  306. {
  307. if (included_items_list_size == 0)
  308. {
  309. included_items_list_size = _STARPU_INITIAL_PLACE_ITEMS_LIST_SIZE;
  310. _STARPU_MALLOC(included_items_list, included_items_list_size * sizeof(int));
  311. }
  312. else if (nb_included_items == included_items_list_size)
  313. {
  314. included_items_list_size *= 2;
  315. _STARPU_REALLOC(included_items_list, included_items_list_size * sizeof(int));
  316. }
  317. included_items_list[nb_included_items] = v;
  318. nb_included_items++;
  319. }
  320. exclude_item_flag = 0;
  321. i = endptr - str;
  322. state = state_split_numeric;
  323. }
  324. break;
  325. /* read comma separated or colon separated numerical item list */
  326. case state_split_numeric:
  327. if (str[i] == ':')
  328. /* length and stride colon separated arguments not supported for now */
  329. _STARPU_ERROR("colon support unimplemented in numeric place list");
  330. if (str[i] == ',')
  331. {
  332. i++;
  333. state = state_read_numeric_prefix;
  334. }
  335. else
  336. {
  337. state = state_read_closing_brace;
  338. }
  339. break;
  340. /* read end of numerical item list */
  341. case state_read_closing_brace:
  342. if (str[i] != '}')
  343. _STARPU_ERROR("parse error in places list\n");
  344. if (places_list_size == 0)
  345. {
  346. places_list_size = _STARPU_INITIAL_PLACES_LIST_SIZE;
  347. _STARPU_MALLOC(places_list, places_list_size * sizeof(*places_list));
  348. }
  349. else if (nb_places == places_list_size)
  350. {
  351. places_list_size *= 2;
  352. _STARPU_REALLOC(places_list, places_list_size * sizeof(*places_list));
  353. }
  354. places_list[nb_places].excluded_place = exclude_place_flag;
  355. places_list[nb_places].included_numeric_items = included_items_list;
  356. places_list[nb_places].nb_included_numeric_items = nb_included_items;
  357. places_list[nb_places].excluded_numeric_items = excluded_items_list;
  358. places_list[nb_places].nb_excluded_numeric_items = nb_excluded_items;
  359. nb_places++;
  360. exclude_place_flag = 0;
  361. included_items_list = NULL;
  362. included_items_list_size = 0;
  363. nb_included_items = 0;
  364. excluded_items_list = NULL;
  365. excluded_items_list_size = 0;
  366. nb_excluded_items = 0;
  367. i++;
  368. state = state_read_brace_suffix;
  369. break;
  370. /* read optional place colon separated suffix */
  371. case state_read_brace_suffix:
  372. if (str[i] == ':')
  373. /* length and stride colon separated arguments not supported for now */
  374. _STARPU_ERROR("colon support unimplemented in numeric place list");
  375. state = state_split;
  376. break;
  377. default:
  378. _STARPU_ERROR("invalid state in parsing places list\n");
  379. }
  380. }
  381. eol:
  382. places->numeric_places = places_list;
  383. places->nb_numeric_places = nb_places;
  384. places->abstract_name = starpu_omp_place_numerical;
  385. }
  386. static void convert_places_string(const char *_str, struct starpu_omp_place *places)
  387. {
  388. char *str = strdup(_str);
  389. if (str == NULL)
  390. _STARPU_ERROR("memory allocation failed\n");
  391. remove_spaces(str);
  392. if (str[0] != '\0')
  393. {
  394. /* check whether this is the start of an abstract name */
  395. if (isalpha(str[0]) || (str[0] == '!' && isalpha(str[1])))
  396. {
  397. read_a_place_name(str, places);
  398. }
  399. /* else the string must contain a list of braces */
  400. else
  401. {
  402. read_a_places_list(str, places);
  403. }
  404. }
  405. free(str);
  406. }
  407. static void free_places(struct starpu_omp_place *places)
  408. {
  409. int i;
  410. for (i = 0; i < places->nb_numeric_places; i++)
  411. {
  412. if (places->numeric_places[i].nb_included_numeric_items > 0)
  413. {
  414. free(places->numeric_places[i].included_numeric_items);
  415. }
  416. if (places->numeric_places[i].nb_excluded_numeric_items > 0)
  417. {
  418. free(places->numeric_places[i].excluded_numeric_items);
  419. }
  420. }
  421. if (places->nb_numeric_places > 0)
  422. {
  423. free(places->numeric_places);
  424. }
  425. }
  426. static int _get_env_string_var(const char *str, const char *strings[], int *dst)
  427. {
  428. int val;
  429. if (!str)
  430. return 0;
  431. val = _strings_cmp(strings, str);
  432. if (val < 0)
  433. return 0;
  434. *dst = val;
  435. return 1;
  436. }
  437. static void read_proc_bind_var()
  438. {
  439. const int max_levels = _initial_icv_values.max_active_levels_var + 1;
  440. int *bind_list = NULL;
  441. char *env;
  442. _STARPU_CALLOC(bind_list, max_levels, sizeof(*bind_list));
  443. env = starpu_getenv("OMP_PROC_BIND");
  444. if (env)
  445. {
  446. static const char *strings[] = { "false", "true", "master", "close", "spread", NULL };
  447. char *saveptr, *token;
  448. int level = 0;
  449. token = strtok_r(env, ",", &saveptr);
  450. for (; token != NULL; token = strtok_r(NULL, ",", &saveptr))
  451. {
  452. int value;
  453. if (!_get_env_string_var(token, strings, &value))
  454. {
  455. _STARPU_MSG("StarPU: Invalid value for environment variable OMP_PROC_BIND\n");
  456. break;
  457. }
  458. bind_list[level++] = value;
  459. }
  460. }
  461. _initial_icv_values.bind_var = bind_list;
  462. }
  463. static void read_num_threads_var()
  464. {
  465. const int max_levels = _initial_icv_values.max_active_levels_var + 1;
  466. int *num_threads_list = NULL;
  467. char *env;
  468. _STARPU_CALLOC(num_threads_list, max_levels, sizeof(*num_threads_list));
  469. env = starpu_getenv("OMP_NUM_THREADS");
  470. if (env)
  471. {
  472. char *saveptr, *token;
  473. int level = 0;
  474. token = strtok_r(env, ",", &saveptr);
  475. for (; token != NULL; token = strtok_r(NULL, ",", &saveptr))
  476. {
  477. int value;
  478. if (!read_int_var(token, &value))
  479. {
  480. _STARPU_MSG("StarPU: Invalid value for environment variable OMP_NUM_THREADS\n");
  481. break;
  482. }
  483. num_threads_list[level++] = value;
  484. }
  485. }
  486. _initial_icv_values.nthreads_var = num_threads_list;
  487. }
  488. static void read_omp_environment(void)
  489. {
  490. const char *boolean_strings[] = { "false", "true", NULL };
  491. _initial_icv_values.dyn_var = starpu_get_env_string_var_default("OMP_DYNAMIC", boolean_strings, _initial_icv_values.dyn_var);
  492. _initial_icv_values.nest_var = starpu_get_env_string_var_default("OMP_NESTED", boolean_strings, _initial_icv_values.nest_var);
  493. read_sched_var("OMP_SCHEDULE", &_initial_icv_values.run_sched_var, &_initial_icv_values.run_sched_chunk_var);
  494. _initial_icv_values.stacksize_var = starpu_get_env_size_default("OMP_STACKSIZE", _initial_icv_values.stacksize_var);
  495. {
  496. const char *strings[] = { "passive", "active", NULL };
  497. _initial_icv_values.wait_policy_var = starpu_get_env_string_var_default("OMP_WAIT_POLICY", strings, _initial_icv_values.wait_policy_var);
  498. }
  499. _initial_icv_values.thread_limit_var = starpu_get_env_number_default("OMP_THREAD_LIMIT", _initial_icv_values.thread_limit_var);
  500. _initial_icv_values.max_active_levels_var = starpu_get_env_number_default("OMP_MAX_ACTIVE_LEVELS", _initial_icv_values.max_active_levels_var);
  501. _initial_icv_values.cancel_var = starpu_get_env_string_var_default("OMP_CANCELLATION", boolean_strings, _initial_icv_values.cancel_var);
  502. _initial_icv_values.default_device_var = starpu_get_env_number_default("OMP_DEFAULT_DEVICE", _initial_icv_values.default_device_var);
  503. _initial_icv_values.max_task_priority_var = starpu_get_env_number_default("OMP_MAX_TASK_PRIORITY", _initial_icv_values.max_task_priority_var);
  504. /* Avoid overflow e.g. in num_threads_list allocation */
  505. 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");
  506. /* TODO: check others */
  507. read_proc_bind_var();
  508. read_num_threads_var();
  509. /* read OMP_PLACES */
  510. {
  511. memset(&_initial_icv_values.places, 0, sizeof(_initial_icv_values.places));
  512. _initial_icv_values.places.abstract_name = starpu_omp_place_undefined;
  513. const char *env = starpu_getenv("OMP_PLACES");
  514. if (env)
  515. {
  516. convert_places_string(env, &_initial_icv_values.places);
  517. }
  518. }
  519. _starpu_omp_initial_icv_values = &_initial_icv_values;
  520. }
  521. static void free_omp_environment(void)
  522. {
  523. /**/
  524. _starpu_omp_initial_icv_values = NULL;
  525. /* OMP_DYNAMIC */
  526. /* OMP_NESTED */
  527. /* OMP_SCHEDULE */
  528. /* OMP_STACKSIZE */
  529. /* OMP_WAIT_POLICY */
  530. /* OMP_THREAD_LIMIT */
  531. /* OMP_MAX_ACTIVE_LEVELS */
  532. /* OMP_CANCELLATION */
  533. /* OMP_DEFAULT_DEVICE */
  534. /* OMP_MAX_TASK_PRIORITY */
  535. /* OMP_PROC_BIND */
  536. free(_initial_icv_values.bind_var);
  537. _initial_icv_values.bind_var = NULL;
  538. /* OMP_NUM_THREADS */
  539. free(_initial_icv_values.nthreads_var);
  540. _initial_icv_values.nthreads_var = NULL;
  541. /* OMP_PLACES */
  542. free_places(&_initial_icv_values.places);
  543. }
  544. static void display_omp_environment(int verbosity_level)
  545. {
  546. if (verbosity_level > 0)
  547. {
  548. printf("OPENMP DISPLAY ENVIRONMENT BEGIN\n");
  549. printf(" _OPENMP = 'xxxxxx'\n");
  550. printf(" [host] OMP_DYNAMIC = '%s'\n", _starpu_omp_initial_icv_values->dyn_var?"TRUE":"FALSE");
  551. printf(" [host] OMP_NESTED = '%s'\n", _starpu_omp_initial_icv_values->nest_var?"TRUE":"FALSE");
  552. printf(" [host] OMP_SCHEDULE = '");
  553. switch (_starpu_omp_initial_icv_values->run_sched_var)
  554. {
  555. case starpu_omp_sched_static:
  556. printf("STATIC, %llu", _starpu_omp_initial_icv_values->run_sched_chunk_var);
  557. break;
  558. case starpu_omp_sched_dynamic:
  559. printf("DYNAMIC, %llu", _starpu_omp_initial_icv_values->run_sched_chunk_var);
  560. break;
  561. case starpu_omp_sched_guided:
  562. printf("GUIDED, %llu", _starpu_omp_initial_icv_values->run_sched_chunk_var);
  563. break;
  564. case starpu_omp_sched_auto:
  565. printf("AUTO, %llu", _starpu_omp_initial_icv_values->run_sched_chunk_var);
  566. break;
  567. case starpu_omp_sched_undefined:
  568. default:
  569. break;
  570. }
  571. printf("'\n");
  572. printf(" [host] OMP_STACKSIZE = '%d'\n", _starpu_omp_initial_icv_values->stacksize_var);
  573. printf(" [host] OMP_WAIT_POLICY = '%s'\n", _starpu_omp_initial_icv_values->wait_policy_var?"ACTIVE":"PASSIVE");
  574. printf(" [host] OMP_MAX_ACTIVE_LEVELS = '%d'\n", _starpu_omp_initial_icv_values->max_active_levels_var);
  575. printf(" [host] OMP_CANCELLATION = '%s'\n", _starpu_omp_initial_icv_values->cancel_var?"TRUE":"FALSE");
  576. printf(" [host] OMP_DEFAULT_DEVICE = '%d'\n", _starpu_omp_initial_icv_values->default_device_var);
  577. printf(" [host] OMP_MAX_TASK_PRIORITY = '%d'\n", _starpu_omp_initial_icv_values->max_task_priority_var);
  578. printf(" [host] OMP_PROC_BIND = '");
  579. {
  580. int level;
  581. for (level = 0; level < _starpu_omp_initial_icv_values->max_active_levels_var; level++)
  582. {
  583. if (level > 0)
  584. {
  585. printf(", ");
  586. }
  587. switch (_starpu_omp_initial_icv_values->bind_var[level])
  588. {
  589. case starpu_omp_proc_bind_false:
  590. printf("FALSE");
  591. break;
  592. case starpu_omp_proc_bind_true:
  593. printf("TRUE");
  594. break;
  595. case starpu_omp_proc_bind_master:
  596. printf("MASTER");
  597. break;
  598. case starpu_omp_proc_bind_close:
  599. printf("CLOSE");
  600. break;
  601. case starpu_omp_proc_bind_spread:
  602. printf("SPREAD");
  603. break;
  604. default:
  605. break;
  606. }
  607. }
  608. }
  609. printf("'\n");
  610. printf(" [host] OMP_NUM_THREADS = '");
  611. {
  612. int level;
  613. for (level = 0; level < _starpu_omp_initial_icv_values->max_active_levels_var; level++)
  614. {
  615. if (level > 0)
  616. {
  617. printf(", ");
  618. }
  619. printf("%d", _starpu_omp_initial_icv_values->nthreads_var[level]);
  620. }
  621. }
  622. printf("'\n");
  623. printf(" [host] OMP_PLACES = '");
  624. {
  625. struct starpu_omp_place *places = &_starpu_omp_initial_icv_values->places;
  626. if (places->nb_numeric_places > 0)
  627. {
  628. int p;
  629. for (p = 0; p < places->nb_numeric_places; p++)
  630. {
  631. if (p > 0)
  632. {
  633. printf(",");
  634. }
  635. struct starpu_omp_numeric_place *np = &places->numeric_places[p];
  636. if (np->excluded_place)
  637. {
  638. printf("!");
  639. }
  640. printf("{");
  641. int i;
  642. for (i = 0; i < np->nb_included_numeric_items; i++)
  643. {
  644. if (i > 0)
  645. {
  646. printf(",");
  647. }
  648. printf("%d", np->included_numeric_items[i]);
  649. }
  650. for (i = 0; i < np->nb_excluded_numeric_items; i++)
  651. {
  652. if (i > 0 || np->nb_included_numeric_items)
  653. {
  654. printf(",");
  655. }
  656. printf("!%d", np->excluded_numeric_items[i]);
  657. }
  658. printf("}");
  659. /* TODO: print length/stride suffix */
  660. }
  661. }
  662. else
  663. {
  664. if (places->abstract_excluded)
  665. {
  666. printf("!");
  667. }
  668. switch (places->abstract_name)
  669. {
  670. case starpu_omp_place_threads:
  671. printf("THREADS");
  672. break;
  673. case starpu_omp_place_cores:
  674. printf("CORES");
  675. break;
  676. case starpu_omp_place_sockets:
  677. printf("SOCKETS");
  678. break;
  679. case starpu_omp_place_numerical:
  680. printf("<NUMERICAL>");
  681. break;
  682. case starpu_omp_place_undefined:
  683. default:
  684. break;
  685. }
  686. if (places->abstract_length)
  687. {
  688. printf("(%d)", places->abstract_length);
  689. }
  690. }
  691. }
  692. printf("'\n");
  693. printf(" [host] OMP_THREAD_LIMIT = '%d'\n", _initial_icv_values.thread_limit_var);
  694. if (verbosity_level > 1)
  695. {
  696. /* no vendor specific runtime variable */
  697. }
  698. printf("OPENMP DISPLAY ENVIRONMENT END\n");
  699. }
  700. }
  701. void _starpu_omp_environment_init(void)
  702. {
  703. read_omp_environment();
  704. const char *strings[] = { "false", "true", "verbose", NULL };
  705. int display_env = starpu_get_env_string_var_default("OMP_DISPLAY_ENV", strings, 0);
  706. if (display_env > 0)
  707. {
  708. display_omp_environment(display_env);
  709. }
  710. }
  711. int _starpu_omp_environment_check(void)
  712. {
  713. if (starpu_cpu_worker_get_count() == 0)
  714. {
  715. _STARPU_DISP("OpenMP support needs at least 1 CPU worker\n");
  716. return -EINVAL;
  717. }
  718. int i;
  719. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  720. {
  721. struct starpu_sched_policy *sched_policy = starpu_sched_ctx_get_sched_policy(i);
  722. if (sched_policy && (strcmp(sched_policy->policy_name, _starpu_sched_graph_test_policy.policy_name) == 0))
  723. {
  724. _STARPU_DISP("OpenMP support is not compatible with scheduler '%s' ('%s')\n", _starpu_sched_graph_test_policy.policy_name, _starpu_sched_graph_test_policy.policy_description);
  725. return -EINVAL;
  726. }
  727. }
  728. return 0;
  729. }
  730. void _starpu_omp_environment_exit(void)
  731. {
  732. free_omp_environment();
  733. }
  734. #endif /* STARPU_OPENMP */