variable_size.c 11 KB

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