block.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 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. #include <starpu.h>
  18. #include <starpu_opencl.h>
  19. #include <pthread.h>
  20. #include <math.h>
  21. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  22. extern void cpu_codelet(void *descr[], void *_args);
  23. #ifdef STARPU_USE_CUDA
  24. extern void cuda_codelet(void *descr[], void *_args);
  25. #endif
  26. #ifdef STARPU_USE_OPENCL
  27. extern void opencl_codelet(void *descr[], void *_args);
  28. struct starpu_opencl_program opencl_code;
  29. #endif
  30. typedef void (*device_func)(void **, void *);
  31. int execute_on(uint32_t where, device_func func, float *block, int pnx, int pny, int pnz, float multiplier)
  32. {
  33. starpu_codelet cl = {};
  34. starpu_data_handle block_handle;
  35. int i;
  36. starpu_block_data_register(&block_handle, 0, (uintptr_t)block, pnx, pnx*pny, pnx, pny, pnz, sizeof(float));
  37. cl.where = where;
  38. cl.cuda_func = func;
  39. cl.cpu_func = func;
  40. cl.opencl_func = func;
  41. cl.nbuffers = 1;
  42. cl.model = NULL;
  43. struct starpu_task *task = starpu_task_create();
  44. task->cl = &cl;
  45. task->callback_func = NULL;
  46. task->buffers[0].handle = block_handle;
  47. task->buffers[0].mode = STARPU_RW;
  48. task->cl_arg = &multiplier;
  49. int ret = starpu_task_submit(task);
  50. if (STARPU_UNLIKELY(ret == -ENODEV)) {
  51. FPRINTF(stderr, "No worker may execute this task\n");
  52. return 1;
  53. }
  54. starpu_task_wait_for_all();
  55. /* update the array in RAM */
  56. starpu_data_unregister(block_handle);
  57. for(i=0 ; i<pnx*pny*pnz; i++) {
  58. FPRINTF(stderr, "%f ", block[i]);
  59. }
  60. FPRINTF(stderr, "\n");
  61. return 0;
  62. }
  63. int main(int argc, char **argv)
  64. {
  65. float *block, n=1.0;
  66. int i, j, k, ret;
  67. int nx=3;
  68. int ny=2;
  69. int nz=4;
  70. float multiplier=1.0;
  71. starpu_init(NULL);
  72. block = (float*)malloc(nx*ny*nz*sizeof(float));
  73. assert(block);
  74. for(k=0 ; k<nz ; k++) {
  75. for(j=0 ; j<ny ; j++) {
  76. for(i=0 ; i<nx ; i++) {
  77. block[(k*nx*ny)+(j*nx)+i] = n++;
  78. }
  79. }
  80. }
  81. ret = execute_on(STARPU_CPU, cpu_codelet, block, nx, ny, nz, 1.0);
  82. if (!ret) multiplier *= 1.0;
  83. #ifdef STARPU_USE_OPENCL
  84. starpu_opencl_load_opencl_from_file("examples/basic_examples/block_opencl_kernel.cl", &opencl_code, NULL);
  85. ret = execute_on(STARPU_OPENCL, opencl_codelet, block, nx, ny, nz, 2.0);
  86. if (!ret) multiplier *= 2.0;
  87. #endif
  88. #ifdef STARPU_USE_CUDA
  89. ret = execute_on(STARPU_CUDA, cuda_codelet, block, nx, ny, nz, 3.0);
  90. if (!ret) multiplier *= 3.0;
  91. #endif
  92. /* Check result is correct */
  93. ret=1;
  94. for(i=0 ; i<nx*ny*nz ; i++) {
  95. if (block[i] != (i+1) * multiplier) {
  96. ret=0;
  97. break;
  98. }
  99. }
  100. FPRINTF(stderr,"TEST %s\n", ret==1?"PASSED":"FAILED");
  101. free(block);
  102. starpu_shutdown();
  103. return 0;
  104. }