hello_world.c 4.5 KB

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