tips-tricks.texi 2.4 KB

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