block.c 3.9 KB

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