hello_world.c 4.2 KB

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