openmp_runtime_support_environment.c 22 KB

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