block.c 3.5 KB

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