multiformat.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique
  4. *
  5. * StarPU 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. * StarPU 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. #include <starpu.h>
  17. #ifdef STARPU_USE_OPENCL
  18. #include <starpu_opencl.h>
  19. #endif
  20. #include "multiformat_types.h"
  21. static struct struct_of_arrays global_struct_of_arrays;
  22. static starpu_data_handle global_struct_of_arrays_handle;
  23. static void
  24. multiformat_scal_cpu_func(void *buffers[], void *args)
  25. {
  26. struct struct_of_arrays *s;
  27. unsigned int n, i;
  28. s = STARPU_MULTIFORMAT_GET_PTR(buffers[0]);
  29. n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  30. for (i = 0; i < n; i++) {
  31. s->x[i] *= s->y[i];
  32. }
  33. }
  34. #ifdef STARPU_USE_CUDA
  35. extern starpu_codelet cpu_to_cuda_cl;
  36. extern starpu_codelet cuda_to_cpu_cl;
  37. #endif
  38. #ifdef STARPU_USE_OPENCL
  39. extern starpu_codelet cpu_to_opencl_cl;
  40. extern starpu_codelet opencl_to_cpu_cl;
  41. #endif
  42. static struct starpu_multiformat_data_interface_ops format_ops = {
  43. #ifdef STARPU_USE_CUDA
  44. .cuda_elemsize = sizeof(struct point),
  45. .cpu_to_cuda_cl = &cpu_to_cuda_cl,
  46. .cuda_to_cpu_cl = &cuda_to_cpu_cl,
  47. #endif
  48. #ifdef STARPU_USE_OPENCL
  49. .opencl_elemsize = sizeof(struct point),
  50. .cpu_to_opencl_cl = &cpu_to_opencl_cl,
  51. .opencl_to_cpu_cl = &opencl_to_cpu_cl,
  52. #endif
  53. .cpu_elemsize = sizeof(global_struct_of_arrays),
  54. };
  55. #ifdef STARPU_USE_CUDA
  56. extern void multiformat_scal_cuda_func(void *buffers[], void *arg);
  57. #endif
  58. #ifdef STARPU_USE_OPENCL
  59. extern void multiformat_scal_opencl_func(void *buffers[], void *arg);
  60. #endif
  61. static struct starpu_perfmodel_t conversion_model = {
  62. .type = STARPU_HISTORY_BASED,
  63. .symbol = "multiformat_conversion_model"
  64. };
  65. static starpu_codelet cl = {
  66. .where = STARPU_CUDA | STARPU_OPENCL,
  67. .cpu_func = multiformat_scal_cpu_func,
  68. #ifdef STARPU_USE_CUDA
  69. .cuda_func = multiformat_scal_cuda_func,
  70. #endif
  71. #ifdef STARPU_USE_OPENCL
  72. .opencl_func = multiformat_scal_opencl_func,
  73. #endif
  74. .nbuffers = 1,
  75. .conversion_model = &conversion_model
  76. };
  77. /*
  78. * Main functions
  79. */
  80. static void
  81. init_problem_data(void)
  82. {
  83. int i;
  84. for (i = 0; i < N_ELEMENTS; i++) {
  85. global_struct_of_arrays.x[i] = 1.0f + i;
  86. global_struct_of_arrays.y[i] = 42.0;
  87. }
  88. }
  89. static void
  90. register_data(void)
  91. {
  92. starpu_multiformat_data_register(&global_struct_of_arrays_handle,
  93. 0,
  94. &global_struct_of_arrays,
  95. N_ELEMENTS,
  96. &format_ops);
  97. }
  98. static void
  99. create_and_submit_tasks(void)
  100. {
  101. struct starpu_task *task = starpu_task_create();
  102. task->cl = &cl;
  103. task->synchronous = 1;
  104. task->buffers[0].handle = global_struct_of_arrays_handle;
  105. task->buffers[0].mode = STARPU_RW;
  106. task->cl_arg = NULL;
  107. task->cl_arg_size = 0;
  108. starpu_task_submit(task);
  109. struct starpu_task *task2 = starpu_task_create();
  110. task2->cl = &cl;
  111. task2->synchronous = 1;
  112. task2->buffers[0].handle = global_struct_of_arrays_handle;
  113. task2->buffers[0].mode = STARPU_RW;
  114. task2->cl_arg = NULL;
  115. task2->cl_arg_size = 0;
  116. starpu_task_submit(task2);
  117. }
  118. static void
  119. unregister_data(void)
  120. {
  121. starpu_data_unregister(global_struct_of_arrays_handle);
  122. }
  123. static void
  124. print_it(void)
  125. {
  126. int i;
  127. for (i = 0; i < N_ELEMENTS; i++) {
  128. fprintf(stderr, "(%.2f %.2f) ",
  129. global_struct_of_arrays.x[i],
  130. global_struct_of_arrays.y[i]);
  131. }
  132. fprintf(stderr, "\n");
  133. }
  134. #ifdef STARPU_USE_OPENCL
  135. struct starpu_opencl_program opencl_program;
  136. struct starpu_opencl_program opencl_conversion_program;
  137. #endif
  138. int
  139. main(void)
  140. {
  141. starpu_init(NULL);
  142. #ifdef STARPU_USE_OPENCL
  143. starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_opencl_kernel.cl",
  144. &opencl_program, NULL);
  145. starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_conversion_codelets_opencl_kernel.cl",
  146. &opencl_conversion_program, NULL);
  147. #endif
  148. init_problem_data();
  149. print_it();
  150. register_data();
  151. create_and_submit_tasks();
  152. unregister_data();
  153. print_it();
  154. #ifdef STARPU_USE_OPENCL
  155. starpu_opencl_unload_opencl(&opencl_program);
  156. starpu_opencl_unload_opencl(&opencl_conversion_program);
  157. #endif
  158. starpu_shutdown();
  159. return 0;
  160. }