tips-tricks.texi 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. @c -*-texinfo-*-
  2. @c This file is part of the StarPU Handbook.
  3. @c Copyright (C) 2009--2011 Universit@'e de Bordeaux 1
  4. @c Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  5. @c Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique
  6. @c See the file starpu.texi for copying conditions.
  7. @node Tips and Tricks
  8. @chapter Tips and Tricks to know about
  9. @menu
  10. * Per-worker library initialization:: How to initialize a computation library once for each worker?
  11. @end menu
  12. @node Per-worker library initialization
  13. @section How to initialize a computation library once for each worker?
  14. Some libraries need to be initialized once for each concurrent instance that
  15. may run on the machine. For instance, a C++ computation class which is not
  16. thread-safe by itself, but for which several instanciated objects of that class
  17. can be used concurrently. This can be used in StarPU by initializing one such
  18. object per worker. For instance, the libstarpufft example does the following to
  19. be able to use FFTW.
  20. Some global array stores the instanciated objects:
  21. @cartouche
  22. @smallexample
  23. fftw_plan plan_cpu[STARPU_NMAXWORKERS];
  24. @end smallexample
  25. @end cartouche
  26. At initialisation time of libstarpu, the objects are initialized:
  27. @cartouche
  28. @smallexample
  29. int workerid;
  30. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++) @{
  31. switch (starpu_worker_get_type(workerid)) @{
  32. case STARPU_CPU_WORKER:
  33. plan_cpu[workerid] = fftw_plan(...);
  34. break;
  35. @}
  36. @}
  37. @end smallexample
  38. @end cartouche
  39. And in the codelet body, they are used:
  40. @cartouche
  41. @smallexample
  42. static void fft(void *descr[], void *_args)
  43. @{
  44. int workerid = starpu_worker_get_id();
  45. fftw_plan plan = plan_cpu[workerid];
  46. ...
  47. fftw_execute(plan, ...);
  48. @}
  49. @end smallexample
  50. @end cartouche
  51. Another way to go which may be needed is to execute some code from the workers
  52. themselves thanks to @code{starpu_execute_on_each_worker}. This may be required
  53. by CUDA to behave properly due to threading issues. For instance, StarPU's
  54. @code{starpu_helper_cublas_init} looks like the following to call
  55. @code{cublasInit} from the workers themselves:
  56. @cartouche
  57. @smallexample
  58. static void init_cublas_func(void *args STARPU_ATTRIBUTE_UNUSED)
  59. @{
  60. cublasStatus cublasst = cublasInit();
  61. cublasSetKernelStream(starpu_cuda_get_local_stream());
  62. @}
  63. void starpu_helper_cublas_init(void)
  64. @{
  65. starpu_execute_on_each_worker(init_cublas_func, NULL, STARPU_CUDA);
  66. @}
  67. @end smallexample
  68. @end cartouche