vector_scal.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  4. * Copyright (C) 2010, 2011 Université de Bordeaux 1
  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 example demonstrates how to use StarPU to scale an array by a factor.
  19. * It shows how to manipulate data with StarPU's data management library.
  20. * 1- how to declare a piece of data to StarPU (starpu_vector_data_register)
  21. * 2- how to describe which data are accessed by a task (task->buffers[0])
  22. * 3- how a kernel can manipulate the data (buffers[0].vector.ptr)
  23. */
  24. #include <starpu.h>
  25. #include <starpu_opencl.h>
  26. #include <stdio.h>
  27. #define NX 2048000
  28. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  29. extern void scal_cpu_func(void *buffers[], void *_args);
  30. extern void scal_cpu_func_icc(void *buffers[], void *_args);
  31. extern void scal_sse_func(void *buffers[], void *_args);
  32. extern void scal_sse_func_icc(void *buffers[], void *_args);
  33. extern void scal_cuda_func(void *buffers[], void *_args);
  34. extern void scal_opencl_func(void *buffers[], void *_args);
  35. static struct starpu_perfmodel vector_scal_model = {
  36. .type = STARPU_HISTORY_BASED,
  37. .symbol = "vector_scale"
  38. };
  39. static struct starpu_perfmodel vector_scal_power_model = {
  40. .type = STARPU_HISTORY_BASED,
  41. .symbol = "vector_scale_power"
  42. };
  43. static starpu_codelet cl = {
  44. .where = STARPU_CPU | STARPU_CUDA | STARPU_OPENCL,
  45. /* CPU implementation of the codelet */
  46. .cpu_func = STARPU_MULTIPLE_CPU_IMPLEMENTATIONS,
  47. .cpu_funcs = {
  48. scal_cpu_func
  49. #ifdef STARPU_HAVE_ICC
  50. , scal_cpu_func_icc
  51. #endif
  52. #ifdef __SSE__
  53. , scal_sse_func
  54. #ifdef STARPU_HAVE_ICC
  55. , scal_sse_func_icc
  56. #endif
  57. #endif
  58. },
  59. #ifdef STARPU_USE_CUDA
  60. /* CUDA implementation of the codelet */
  61. .cuda_func = scal_cuda_func,
  62. #endif
  63. #ifdef STARPU_USE_OPENCL
  64. /* OpenCL implementation of the codelet */
  65. .opencl_func = scal_opencl_func,
  66. #endif
  67. .nbuffers = 1,
  68. .model = &vector_scal_model,
  69. .power_model = &vector_scal_power_model
  70. };
  71. #ifdef STARPU_USE_OPENCL
  72. struct starpu_opencl_program opencl_program;
  73. #endif
  74. int main(int argc, char **argv)
  75. {
  76. /* We consider a vector of float that is initialized just as any of C
  77. * data */
  78. float vector[NX];
  79. unsigned i;
  80. for (i = 0; i < NX; i++)
  81. vector[i] = (i+1.0f);
  82. FPRINTF(stderr, "BEFORE: First element was %f\n", vector[0]);
  83. FPRINTF(stderr, "BEFORE: Last element was %f\n", vector[NX-1]);
  84. /* Initialize StarPU with default configuration */
  85. starpu_init(NULL);
  86. #ifdef STARPU_USE_OPENCL
  87. starpu_opencl_load_opencl_from_file("examples/basic_examples/vector_scal_opencl_kernel.cl",
  88. &opencl_program, NULL);
  89. #endif
  90. /* Tell StaPU to associate the "vector" vector with the "vector_handle"
  91. * identifier. When a task needs to access a piece of data, it should
  92. * refer to the handle that is associated to it.
  93. * In the case of the "vector" data interface:
  94. * - the first argument of the registration method is a pointer to the
  95. * handle that should describe the data
  96. * - the second argument is the memory node where the data (ie. "vector")
  97. * resides initially: 0 stands for an address in main memory, as
  98. * opposed to an adress on a GPU for instance.
  99. * - the third argument is the adress of the vector in RAM
  100. * - the fourth argument is the number of elements in the vector
  101. * - the fifth argument is the size of each element.
  102. */
  103. starpu_data_handle vector_handle;
  104. starpu_vector_data_register(&vector_handle, 0, (uintptr_t)vector, NX, sizeof(vector[0]));
  105. float factor = 3.14;
  106. /* create a synchronous task: any call to starpu_task_submit will block
  107. * until it is terminated */
  108. struct starpu_task *task = starpu_task_create();
  109. task->synchronous = 1;
  110. task->cl = &cl;
  111. /* the codelet manipulates one buffer in RW mode */
  112. task->buffers[0].handle = vector_handle;
  113. task->buffers[0].mode = STARPU_RW;
  114. /* an argument is passed to the codelet, beware that this is a
  115. * READ-ONLY buffer and that the codelet may be given a pointer to a
  116. * COPY of the argument */
  117. task->cl_arg = &factor;
  118. task->cl_arg_size = sizeof(factor);
  119. /* execute the task on any eligible computational ressource */
  120. starpu_task_submit(task);
  121. /* StarPU does not need to manipulate the array anymore so we can stop
  122. * monitoring it */
  123. starpu_data_unregister(vector_handle);
  124. starpu_task_destroy(task);
  125. #ifdef STARPU_USE_OPENCL
  126. starpu_opencl_unload_opencl(&opencl_program);
  127. #endif
  128. /* terminate StarPU, no task can be submitted after */
  129. starpu_shutdown();
  130. FPRINTF(stderr, "AFTER: First element is %f\n", vector[0]);
  131. FPRINTF(stderr, "AFTER: Last element is %f\n", vector[NX-1]);
  132. return 0;
  133. }