openmp_runtime_support_environment.c 23 KB

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