openmp_runtime_support_environment.c 23 KB

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