variable_size.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2017 CNRS
  4. * Copyright (C) 2017 Inria
  5. * Copyright (C) 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. #include "../helper.h"
  20. /*
  21. * This is a dumb test for variable size
  22. * We defined a dumb interface for data whose size increase over kernel execution
  23. */
  24. #ifdef STARPU_HAVE_MEMCHECK_H
  25. #include <valgrind/memcheck.h>
  26. #else
  27. #define VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(addr, size) (void)0
  28. #endif
  29. #include <core/simgrid.h>
  30. #define FULLSIZE (5*1024*1024ULL)
  31. #define INCREASE 0.80
  32. #ifdef STARPU_QUICK_CHECK
  33. #define N 5
  34. #define LIMIT "60"
  35. #else
  36. #define N 20
  37. #define LIMIT "1000"
  38. #endif
  39. /* Define the interface */
  40. #if !defined(STARPU_HAVE_SETENV)
  41. #warning setenv is not defined. Skipping test
  42. int main(int argc, char **argv)
  43. {
  44. return STARPU_TEST_SKIPPED;
  45. }
  46. #else
  47. struct variable_size_interface
  48. {
  49. enum starpu_data_interface_id id;
  50. uintptr_t ptr;
  51. size_t size;
  52. /* Coordinates of the represented object, to model growth */
  53. unsigned x, y;
  54. };
  55. static struct starpu_data_interface_ops starpu_interface_variable_size_ops;
  56. static void register_variable_size(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  57. {
  58. struct variable_size_interface *variable_size_interface = data_interface;
  59. unsigned node;
  60. for (node = 0; node < STARPU_MAXNODES; node++)
  61. {
  62. struct variable_size_interface *local_interface =
  63. starpu_data_get_interface_on_node(handle, node);
  64. if (node == home_node)
  65. local_interface->ptr = variable_size_interface->ptr;
  66. local_interface->id = variable_size_interface->id;
  67. local_interface->size = variable_size_interface->size;
  68. local_interface->x = variable_size_interface->x;
  69. local_interface->y = variable_size_interface->y;
  70. }
  71. }
  72. void variable_size_data_register(starpu_data_handle_t *handleptr, unsigned x, unsigned y)
  73. {
  74. if (starpu_interface_variable_size_ops.interfaceid == STARPU_UNKNOWN_INTERFACE_ID)
  75. {
  76. starpu_interface_variable_size_ops.interfaceid = starpu_data_interface_get_next_id();
  77. }
  78. struct variable_size_interface interface =
  79. {
  80. .id = starpu_interface_variable_size_ops.interfaceid,
  81. .x = x,
  82. .y = y,
  83. };
  84. /* Simulate that tiles close to the diagonal are more dense */
  85. interface.size = FULLSIZE * (starpu_lrand48() % 1024 + 1024) / 2048. * (N-sqrt(abs(x-y)*N)) / N;
  86. /* Round to page size */
  87. interface.size -= interface.size & (65536-1);
  88. _starpu_simgrid_data_new(interface.size);
  89. starpu_data_register(handleptr, -1, &interface, &starpu_interface_variable_size_ops);
  90. }
  91. static size_t variable_size_get_size(starpu_data_handle_t handle)
  92. {
  93. struct variable_size_interface *interface =
  94. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  95. return interface->size;
  96. }
  97. static uint32_t variable_size_footprint(starpu_data_handle_t handle)
  98. {
  99. return starpu_hash_crc32c_be(variable_size_get_size(handle), 0);
  100. }
  101. static int variable_size_compare(void *data_interface_a, void *data_interface_b)
  102. {
  103. struct variable_size_interface *variable_a = data_interface_a;
  104. struct variable_size_interface *variable_b = data_interface_b;
  105. /* Two variables are considered compatible if they have the same size */
  106. return variable_a->size == variable_b->size;
  107. }
  108. static void display_variable_size(starpu_data_handle_t handle, FILE *f)
  109. {
  110. struct variable_size_interface *variable_interface =
  111. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  112. fprintf(f, "%lu\t", (unsigned long) variable_interface->size);
  113. }
  114. static starpu_ssize_t describe_variable_size(void *data_interface, char *buf, size_t size)
  115. {
  116. struct variable_size_interface *variable_interface = data_interface;
  117. return snprintf(buf, size, "vv%lu\t", (unsigned long) variable_interface->size);
  118. }
  119. /* returns the size of the allocated area */
  120. static starpu_ssize_t allocate_variable_size_on_node(void *data_interface,
  121. unsigned dst_node)
  122. {
  123. struct variable_size_interface *variable_interface = data_interface;
  124. variable_interface->ptr = starpu_malloc_on_node_flags(dst_node, variable_interface->size, STARPU_MALLOC_PINNED | STARPU_MALLOC_COUNT | STARPU_MEMORY_OVERFLOW);
  125. if (dst_node == STARPU_MAIN_RAM)
  126. _starpu_simgrid_data_alloc(variable_interface->size);
  127. STARPU_ASSERT(variable_interface->ptr);
  128. return 0;
  129. }
  130. static void free_variable_size_on_node(void *data_interface,
  131. unsigned node)
  132. {
  133. struct variable_size_interface *variable_interface = data_interface;
  134. starpu_free_on_node(node, variable_interface->ptr, variable_interface->size);
  135. if (node == STARPU_MAIN_RAM)
  136. _starpu_simgrid_data_free(variable_interface->size);
  137. }
  138. static int variable_size_copy(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data)
  139. {
  140. struct variable_size_interface *src = src_interface;
  141. struct variable_size_interface *dst = dst_interface;
  142. if (src->size != dst->size)
  143. {
  144. /* size has been changed by the application in the meantime */
  145. starpu_free_on_node(dst_node, dst->ptr, dst->size);
  146. dst->ptr = starpu_malloc_on_node_flags(dst_node, src->size, STARPU_MALLOC_PINNED | STARPU_MALLOC_COUNT | STARPU_MEMORY_OVERFLOW);
  147. dst->size = src->size;
  148. }
  149. return starpu_interface_copy(src->ptr, 0, src_node,
  150. dst->ptr, 0, dst_node,
  151. src->size, async_data);
  152. }
  153. static const struct starpu_data_copy_methods variable_size_copy_data_methods =
  154. {
  155. .any_to_any = variable_size_copy,
  156. };
  157. static struct starpu_data_interface_ops starpu_interface_variable_size_ops =
  158. {
  159. .register_data_handle = register_variable_size,
  160. .allocate_data_on_node = allocate_variable_size_on_node,
  161. .free_data_on_node = free_variable_size_on_node,
  162. .copy_methods = &variable_size_copy_data_methods,
  163. .get_size = variable_size_get_size,
  164. .footprint = variable_size_footprint,
  165. .compare = variable_size_compare,
  166. .interfaceid = STARPU_UNKNOWN_INTERFACE_ID,
  167. .interface_size = sizeof(struct variable_size_interface),
  168. .display = display_variable_size,
  169. .pack_data = NULL,
  170. .unpack_data = NULL,
  171. .describe = describe_variable_size,
  172. /* We want to observe actual allocations/deallocations */
  173. .dontcache = 1,
  174. };
  175. static void kernel(void *descr[], void *cl_arg)
  176. {
  177. struct variable_size_interface *variable_interface = descr[0];
  178. unsigned workerid = starpu_worker_get_id_check();
  179. uintptr_t old = variable_interface->ptr;
  180. unsigned dst_node = starpu_worker_get_memory_node(workerid);
  181. (void) cl_arg;
  182. /* Simulate that tiles close to the diagonal fill up faster */
  183. size_t increase = (FULLSIZE - variable_interface->size) * (starpu_lrand48() % 1024 + 1024) / 2048. * INCREASE;
  184. /* Round to page size */
  185. increase -= increase & (65536-1);
  186. variable_interface->ptr = starpu_malloc_on_node_flags(dst_node, variable_interface->size + increase, STARPU_MALLOC_PINNED | STARPU_MALLOC_COUNT | STARPU_MEMORY_OVERFLOW);
  187. VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE((void*) variable_interface->ptr, variable_interface->size + increase);
  188. STARPU_ASSERT(variable_interface->ptr);
  189. /* fprintf(stderr,"increase from %lu by %lu\n", variable_interface->size, increase); */
  190. starpu_free_on_node_flags(dst_node, old, variable_interface->size, STARPU_MALLOC_PINNED | STARPU_MALLOC_COUNT | STARPU_MEMORY_OVERFLOW);
  191. variable_interface->size += increase;
  192. if (increase)
  193. _starpu_simgrid_data_increase(increase);
  194. starpu_sleep(0.010);
  195. }
  196. static double cost_function(struct starpu_task *t, struct starpu_perfmodel_arch *a, unsigned i)
  197. {
  198. (void)t; (void)a; (void)i;
  199. return 10000;
  200. }
  201. static struct starpu_perfmodel perf_model =
  202. {
  203. .type = STARPU_PER_ARCH,
  204. .arch_cost_function = cost_function,
  205. };
  206. static struct starpu_codelet cl =
  207. {
  208. .cpu_funcs = {kernel},
  209. /* dynamic size doesn't work on MIC */
  210. /*.cpu_funcs_name = {"kernel"},*/
  211. .nbuffers = 1,
  212. .modes = {STARPU_RW},
  213. .model = &perf_model,
  214. .flags = STARPU_CODELET_SIMGRID_EXECUTE,
  215. };
  216. static void init(void *descr[], void *cl_arg)
  217. {
  218. (void)cl_arg;
  219. struct variable_size_interface *variable_interface = descr[0];
  220. VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE((void*) variable_interface->ptr, variable_interface->size);
  221. }
  222. static struct starpu_codelet cl_init =
  223. {
  224. .cpu_funcs = {init},
  225. /* dynamic size doesn't work on MIC */
  226. /*.cpu_funcs_name = {"kernel"},*/
  227. .nbuffers = 1,
  228. .modes = {STARPU_W},
  229. .model = &starpu_perfmodel_nop,
  230. };
  231. int main(void)
  232. {
  233. int ret;
  234. int i;
  235. int x, y;
  236. starpu_data_handle_t handles[N][N];
  237. char s[128];
  238. snprintf(s, sizeof(s), "/tmp/%s-variable_size", getenv("USER"));
  239. setenv("STARPU_CALIBRATE_MINIMUM", LIMIT, 1);
  240. setenv("STARPU_LIMIT_CPU_MEM", LIMIT, 1);
  241. setenv("STARPU_DISK_SWAP", s, 0);
  242. setenv("STARPU_DISK_SWAP_SIZE", "100000", 1);
  243. #ifdef STARPU_LINUX_SYS
  244. setenv("STARPU_DISK_SWAP_BACKEND", "unistd_o_direct", 0);
  245. #else
  246. setenv("STARPU_DISK_SWAP_BACKEND", "unistd", 0);
  247. #endif
  248. ret = starpu_init(NULL);
  249. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  250. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  251. for (x = 0; x < N; x++)
  252. for (y = 0; y < N; y++)
  253. {
  254. variable_size_data_register(&handles[x][y], x, y);
  255. ret = starpu_task_insert(&cl_init, STARPU_W, handles[x][y], 0);
  256. if (ret == ENODEV)
  257. goto enodev;
  258. #ifdef STARPU_SIMGRID
  259. starpu_sleep(0.0005);
  260. #endif
  261. }
  262. starpu_task_wait_for_all();
  263. /* Cholesky-like accesses */
  264. for (i = 0; i < N; i++)
  265. for (x = i; x < N; x++)
  266. for (y = x; y < N; y++)
  267. starpu_task_insert(&cl, STARPU_RW, handles[x][y], STARPU_PRIORITY, (2*N-x-y), 0);
  268. starpu_task_wait_for_all();
  269. #if 0
  270. /* Look at the values */
  271. for (x = 0; x < N; x++)
  272. for (y = 0; y < N; y++)
  273. {
  274. starpu_data_acquire(handles[x][y], STARPU_R);
  275. starpu_data_release(handles[x][y]);
  276. }
  277. #endif
  278. for (x = 0; x < N; x++)
  279. for (y = 0; y < N; y++)
  280. starpu_data_unregister(handles[x][y]);
  281. starpu_shutdown();
  282. return EXIT_SUCCESS;
  283. enodev:
  284. for (x = 0; x < N; x++)
  285. for (y = 0; y < N; y++)
  286. starpu_data_unregister(handles[x][y]);
  287. fprintf(stderr, "WARNING: No one can execute this task\n");
  288. /* yes, we do not perform the computation but we did detect that no one
  289. * could perform the kernel, so this is not an error from StarPU */
  290. starpu_shutdown();
  291. return STARPU_TEST_SKIPPED;
  292. }
  293. #endif