openmp_runtime_support_environment.c 24 KB

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