hello_world.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #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. {
  46. int i;
  47. float f;
  48. };
  49. void cpu_func(void *buffers[], void *cl_arg)
  50. {
  51. struct params *params = (struct params *) cl_arg;
  52. FPRINTF(stdout, "Hello world (params = {%i, %f} )\n", params->i, params->f);
  53. }
  54. struct starpu_codelet cl = {};
  55. int main(int argc, char **argv)
  56. {
  57. struct starpu_task *task;
  58. struct params params = {1, 2.0f};
  59. int ret;
  60. /* initialize StarPU : passing a NULL argument means that we use
  61. * default configuration for the scheduling policies and the number of
  62. * processors/accelerators */
  63. ret = starpu_init(NULL);
  64. if (ret == -ENODEV)
  65. return 77;
  66. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  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. task = starpu_task_create();
  71. /* this codelet may only be executed on a CPU, and its cpu
  72. * implementation is function "cpu_func" */
  73. cl.where = STARPU_CPU;
  74. cl.cpu_funcs[0] = cpu_func;
  75. /* the codelet does not manipulate any data that is managed
  76. * by our DSM */
  77. cl.nbuffers = 0;
  78. /* the task uses codelet "cl" */
  79. task->cl = &cl;
  80. /* It is possible to pass buffers that are not managed by the DSM to the
  81. * kernels: the second argument of the "cpu_func" function is a pointer to a
  82. * buffer that contains information for the codelet (cl_arg stands for
  83. * codelet argument). In the case of accelerators, it is possible that
  84. * the codelet is given a pointer to a copy of that buffer: this buffer
  85. * is read-only so that any modification is not passed to other copies
  86. * of the buffer. For this reason, a buffer passed as a codelet
  87. * argument (cl_arg) is NOT a valid synchronization medium! */
  88. task->cl_arg = &params;
  89. task->cl_arg_size = sizeof(params);
  90. /* once the task has been executed, callback_func(0x42)
  91. * will be called on a CPU */
  92. task->callback_func = callback_func;
  93. task->callback_arg = (void*) (uintptr_t) 0x42;
  94. /* starpu_task_submit will be a blocking call */
  95. task->synchronous = 1;
  96. /* submit the task to StarPU */
  97. ret = starpu_task_submit(task);
  98. if (ret == -ENODEV) goto enodev;
  99. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  100. /* terminate StarPU: statistics and other debug outputs are not
  101. * guaranteed to be generated unless this function is called. Once it
  102. * is called, it is not possible to submit tasks anymore, and the user
  103. * is responsible for making sure all tasks have already been executed:
  104. * calling starpu_shutdown() before the termination of all the tasks
  105. * results in an undefined behaviour */
  106. starpu_shutdown();
  107. return 0;
  108. enodev:
  109. starpu_shutdown();
  110. return 77;
  111. }