variable_size.c 10 KB

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