hello_world_top.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /*
  18. * This examples demonstrates how to construct and submit a task to StarPU and
  19. * more precisely:
  20. * - how to allocate a new task structure (starpu_task_create)
  21. * - how to describe a multi-versionned computational kernel (ie. a codelet)
  22. * - how to pass an argument to the codelet (task->cl_arg)
  23. * - how to declare a callback function that is called once the task has been
  24. * executed
  25. * - how to specify if starpu_task_submit is a blocking or non-blocking
  26. * operation (task->synchronous)
  27. */
  28. #include <stdio.h>
  29. #include <stdint.h>
  30. #include <starpu.h>
  31. #include <stdlib.h>
  32. #include <time.h>
  33. /* Example of enum param */
  34. char* names[] = {"Paul", "Jean", "Jaques", "Alain", "Brian"};
  35. int names_len = 5;
  36. int name_selected=2; //must be between 0 and names_len-1
  37. /* Exemple of int param */
  38. int number_of_addition = 30;
  39. /* Exemple of bool param */
  40. int stop_after_5_task = 0;
  41. /* When the task is done, task->callback_func(task->callback_arg) is called. Any
  42. * callback function must have the prototype void (*)(void *).
  43. * NB: Callback are NOT allowed to perform potentially blocking operations */
  44. void callback_func(void *callback_arg)
  45. {
  46. printf("Callback function got argument %p\n", callback_arg);
  47. }
  48. /* Every implementation of a codelet must have this prototype, the first
  49. * argument (buffers) describes the buffers/streams that are managed by the
  50. * DSM; the second arguments references read-only data that is passed as an
  51. * argument of the codelet (task->cl_arg). Here, "buffers" is unused as there
  52. * are no data input/output managed by the DSM (cl.nbuffers = 0) */
  53. struct params
  54. {
  55. int i;
  56. float f;
  57. };
  58. void cpu_func(void *buffers[], void *cl_arg)
  59. {
  60. struct params *params = (struct params *) cl_arg;
  61. //loosing time for top example...
  62. int sum = 0;
  63. int i = 0;
  64. while(i<number_of_addition*1000000)
  65. {
  66. sum+=rand();
  67. i++;
  68. }
  69. printf("Hello %s (params = {%i, %f} ) sum=%d\n",
  70. names[name_selected],
  71. params->i,
  72. params->f,
  73. sum);
  74. }
  75. void callback_name_changed(struct starpu_top_param* param)
  76. {
  77. char* message = (char *) malloc(256);
  78. sprintf(message, "Name have been changed to %s", names[name_selected]);
  79. starpu_top_debug_log(message);
  80. }
  81. void callback_number_addition_changed(struct starpu_top_param* param)
  82. {
  83. char* message = (char *) malloc(256);
  84. sprintf(message, "Number of addition is now %d", number_of_addition);
  85. starpu_top_debug_log(message);
  86. }
  87. struct starpu_codelet cl =
  88. {
  89. /* this codelet may only be executed on a CPU, and its cpu
  90. * implementation is function "cpu_func" */
  91. .cpu_funcs = {cpu_func, NULL},
  92. /* the codelet does not manipulate any data that is managed
  93. * by our DSM */
  94. .nbuffers = 0
  95. };
  96. int main(int argc, char **argv)
  97. {
  98. int ret;
  99. srand ( time(NULL) );
  100. /* initialize StarPU : passing a NULL argument means that we use
  101. * default configuration for the scheduling policies and the number of
  102. * processors/accelerators */
  103. ret = starpu_init(NULL);
  104. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  105. /*init starpu_top*/
  106. struct starpu_top_data * loop_count =
  107. starpu_top_add_data_integer("Loop count", 0,124,1);
  108. struct starpu_top_data * remain_count =
  109. starpu_top_add_data_integer("Remaining loop", 0,124,1);
  110. struct starpu_top_data * midle_reach =
  111. starpu_top_add_data_boolean("Midle reached", 1);
  112. struct starpu_top_param* name =
  113. starpu_top_register_parameter_enum("Your name : ",
  114. &name_selected,
  115. names,
  116. names_len,
  117. callback_name_changed);
  118. struct starpu_top_param * number_of_addition_param =
  119. starpu_top_register_parameter_integer("Number of Millions of addition",
  120. &number_of_addition,
  121. 0,
  122. 50,
  123. callback_number_addition_changed);
  124. STARPU_ASSERT(number_of_addition_param != NULL);
  125. struct starpu_top_param * stop5_param =
  126. starpu_top_register_parameter_boolean("Stop after 5 task ?",
  127. &stop_after_5_task,
  128. NULL);
  129. STARPU_ASSERT(stop5_param != NULL);
  130. //all parameters are initialized, we can connect to UI
  131. starpu_top_init_and_wait("Serveur de test HelloWorld");
  132. //set "default value"
  133. starpu_top_update_data_boolean(midle_reach, 0);
  134. /* create a new task that is non-blocking by default : the task is not
  135. * submitted to the scheduler until the starpu_task_submit function is
  136. * called */
  137. /*
  138. * For this simple example, we make 124 iter
  139. */
  140. struct starpu_task *task[124];
  141. int i;
  142. for(i=0; i<124; i++)
  143. {
  144. starpu_top_update_data_integer(loop_count, i);
  145. starpu_top_update_data_integer(remain_count, 124-i);
  146. if(i==62)
  147. {
  148. starpu_top_update_data_boolean(midle_reach, 1);
  149. }
  150. if(i==25)
  151. {
  152. //changing name
  153. name_selected = 1;
  154. starpu_top_update_parameter(name);
  155. }
  156. if(i>4 && stop_after_5_task)
  157. {
  158. break;
  159. }
  160. task[i]=starpu_task_create();
  161. /* the task uses codelet "cl" */
  162. task[i]->cl = &cl;
  163. /* It is possible to pass buffers that are not managed by the DSM to the
  164. * kernels: the second argument of the "cpu_func" function is a pointer to a
  165. * buffer that contains information for the codelet (cl_arg stands for
  166. * codelet argument). In the case of accelerators, it is possible that
  167. * the codelet is given a pointer to a copy of that buffer: this buffer
  168. * is read-only so that any modification is not passed to other copies
  169. * of the buffer. For this reason, a buffer passed as a codelet
  170. * argument (cl_arg) is NOT a valid synchronization medium! */
  171. struct params params = { i, 2.0f };
  172. task[i]->cl_arg = &params;
  173. task[i]->cl_arg_size = sizeof(params);
  174. /* once the task has been executed, callback_func(0x42)
  175. * will be called on a CPU */
  176. task[i]->callback_func = callback_func;
  177. task[i]->callback_arg = (void*) (uintptr_t) 0x42;
  178. /* starpu_task_submit will be a blocking call */
  179. task[i]->synchronous = 1;
  180. /* submit the task to StarPU */
  181. if(number_of_addition==42)
  182. starpu_top_debug_lock("debug stop point because of 42 !");
  183. ret = starpu_task_submit(task[i]);
  184. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  185. }
  186. /* terminate StarPU: statistics and other debug outputs are not
  187. * guaranteed to be generated unless this function is called. Once it
  188. * is called, it is not possible to submit tasks anymore, and the user
  189. * is responsible for making sure all tasks have already been executed:
  190. * calling starpu_shutdown() before the termination of all the tasks
  191. * results in an undefined behaviour */
  192. starpu_shutdown();
  193. return 0;
  194. }