block.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010,2011,2013,2014 Université de Bordeaux
  4. * Copyright (C) 2011-2013 Inria
  5. * Copyright (C) 2010-2013,2015,2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include <math.h>
  20. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  21. extern void cpu_codelet(void *descr[], void *_args);
  22. #ifdef STARPU_USE_CUDA
  23. extern void cuda_codelet(void *descr[], void *_args);
  24. #endif
  25. #ifdef STARPU_USE_OPENCL
  26. extern void opencl_codelet(void *descr[], void *_args);
  27. struct starpu_opencl_program opencl_code;
  28. #endif
  29. typedef void (*device_func)(void **, void *);
  30. int execute_on(uint32_t where, device_func func, float *block, int pnx, int pny, int pnz, float multiplier)
  31. {
  32. struct starpu_codelet cl;
  33. starpu_data_handle_t block_handle;
  34. int i;
  35. starpu_block_data_register(&block_handle, STARPU_MAIN_RAM, (uintptr_t)block, pnx, pnx*pny, pnx, pny, pnz, sizeof(float));
  36. starpu_codelet_init(&cl);
  37. cl.where = where;
  38. cl.cuda_funcs[0] = func;
  39. cl.cpu_funcs[0] = func;
  40. cl.opencl_funcs[0] = func;
  41. cl.nbuffers = 1;
  42. cl.modes[0] = STARPU_RW,
  43. cl.model = NULL;
  44. cl.name = "block_scale";
  45. struct starpu_task *task = starpu_task_create();
  46. task->cl = &cl;
  47. task->callback_func = NULL;
  48. task->handles[0] = block_handle;
  49. task->cl_arg = &multiplier;
  50. task->cl_arg_size = sizeof(multiplier);
  51. int ret = starpu_task_submit(task);
  52. if (STARPU_UNLIKELY(ret == -ENODEV))
  53. {
  54. FPRINTF(stderr, "No worker may execute this task\n");
  55. task->destroy = 0;
  56. starpu_task_destroy(task);
  57. return 1;
  58. }
  59. starpu_task_wait_for_all();
  60. /* update the array in RAM */
  61. starpu_data_unregister(block_handle);
  62. for(i=0 ; i<pnx*pny*pnz; i++)
  63. {
  64. FPRINTF(stderr, "%f ", block[i]);
  65. }
  66. FPRINTF(stderr, "\n");
  67. return 0;
  68. }
  69. int main(void)
  70. {
  71. float *block, n=1.0;
  72. int i, j, k, ret;
  73. int nx=3;
  74. int ny=2;
  75. int nz=4;
  76. float multiplier=1.0;
  77. ret = starpu_init(NULL);
  78. if (ret == -ENODEV)
  79. return 77;
  80. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  81. block = (float*)malloc(nx*ny*nz*sizeof(float));
  82. assert(block);
  83. for(k=0 ; k<nz ; k++)
  84. {
  85. for(j=0 ; j<ny ; j++)
  86. {
  87. for(i=0 ; i<nx ; i++)
  88. {
  89. block[(k*nx*ny)+(j*nx)+i] = n++;
  90. }
  91. }
  92. }
  93. ret = execute_on(STARPU_CPU, cpu_codelet, block, nx, ny, nz, 1.0);
  94. if (!ret) multiplier *= 1.0;
  95. #ifdef STARPU_USE_OPENCL
  96. ret = starpu_opencl_load_opencl_from_file("examples/basic_examples/block_opencl_kernel.cl", &opencl_code, NULL);
  97. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  98. ret = execute_on(STARPU_OPENCL, opencl_codelet, block, nx, ny, nz, 2.0);
  99. if (!ret) multiplier *= 2.0;
  100. #endif
  101. #ifdef STARPU_USE_CUDA
  102. ret = execute_on(STARPU_CUDA, cuda_codelet, block, nx, ny, nz, 3.0);
  103. if (!ret) multiplier *= 3.0;
  104. #endif
  105. /* Check result is correct */
  106. ret=1;
  107. for(i=0 ; i<nx*ny*nz ; i++)
  108. {
  109. if (block[i] != (i+1) * multiplier)
  110. {
  111. ret=0;
  112. break;
  113. }
  114. }
  115. FPRINTF(stderr,"TEST %s\n", ret==1?"PASSED":"FAILED");
  116. free(block);
  117. starpu_shutdown();
  118. return (ret!=1);
  119. }