hello_world.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010 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. /* When the task is done, task->callback_func(task->callback_arg) is called. Any
  32. * callback function must have the prototype void (*)(void *).
  33. * NB: Callback are NOT allowed to perform potentially blocking operations */
  34. void callback_func(void *callback_arg)
  35. {
  36. printf("Callback function got argument %p\n", callback_arg);
  37. }
  38. /* Every implementation of a codelet must have this prototype, the first
  39. * argument (buffers) describes the buffers/streams that are managed by the
  40. * DSM; the second arguments references read-only data that is passed as an
  41. * argument of the codelet (task->cl_arg). Here, "buffers" is unused as there
  42. * are no data input/output managed by the DSM (cl.nbuffers = 0) */
  43. struct params {
  44. int i;
  45. float f;
  46. };
  47. void cpu_func(void *buffers[], void *cl_arg)
  48. {
  49. struct params *params = cl_arg;
  50. printf("Hello world (params = {%i, %f} )\n", params->i, params->f);
  51. }
  52. starpu_codelet cl =
  53. {
  54. /* this codelet may only be executed on a CPU, and its cpu
  55. * implementation is function "cpu_func" */
  56. .where = STARPU_CPU,
  57. .cpu_func = cpu_func,
  58. /* the codelet does not manipulate any data that is managed
  59. * by our DSM */
  60. .nbuffers = 0
  61. };
  62. int main(int argc, char **argv)
  63. {
  64. /* initialize StarPU : passing a NULL argument means that we use
  65. * default configuration for the scheduling policies and the number of
  66. * processors/accelerators */
  67. starpu_init(NULL);
  68. /* create a new task that is non-blocking by default : the task is not
  69. * submitted to the scheduler until the starpu_task_submit function is
  70. * called */
  71. struct starpu_task *task = starpu_task_create();
  72. /* the task uses codelet "cl" */
  73. task->cl = &cl;
  74. /* It is possible to pass buffers that are not managed by the DSM to the
  75. * kernels: the second argument of the "cpu_func" function is a pointer to a
  76. * buffer that contains information for the codelet (cl_arg stands for
  77. * codelet argument). In the case of accelerators, it is possible that
  78. * the codelet is given a pointer to a copy of that buffer: this buffer
  79. * is read-only so that any modification is not passed to other copies
  80. * of the buffer. For this reason, a buffer passed as a codelet
  81. * argument (cl_arg) is NOT a valid synchronization medium! */
  82. struct params params = { 1, 2.0f };
  83. task->cl_arg = &params;
  84. task->cl_arg_size = sizeof(params);
  85. /* once the task has been executed, callback_func(0x42)
  86. * will be called on a CPU */
  87. task->callback_func = callback_func;
  88. task->callback_arg = (void*) (uintptr_t) 0x42;
  89. /* starpu_task_submit will be a blocking call */
  90. task->synchronous = 1;
  91. /* submit the task to StarPU */
  92. starpu_task_submit(task);
  93. /* terminate StarPU: statistics and other debug outputs are not
  94. * guaranteed to be generated unless this function is called. Once it
  95. * is called, it is not possible to submit tasks anymore, and the user
  96. * is responsible for making sure all tasks have already been executed:
  97. * calling starpu_shutdown() before the termination of all the tasks
  98. * results in an undefined behaviour */
  99. starpu_shutdown();
  100. return 0;
  101. }