tips-tricks.texi 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 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. @smallexample
  22. fftw_plan plan_cpu[STARPU_NMAXWORKERS];
  23. @end smallexample
  24. At initialisation time of libstarpu, the objects are initialized:
  25. @smallexample
  26. int workerid;
  27. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++) @{
  28. switch (starpu_worker_get_type(workerid)) @{
  29. case STARPU_CPU_WORKER:
  30. plan_cpu[workerid] = fftw_plan(...);
  31. break;
  32. @}
  33. @}
  34. @end smallexample
  35. And in the codelet body, they are used:
  36. @smallexample
  37. static void fft(void *descr[], void *_args)
  38. @{
  39. int workerid = starpu_worker_get_id();
  40. fftw_plan plan = plan_cpu[workerid];
  41. ...
  42. fftw_execute(plan, ...);
  43. @}
  44. @end smallexample
  45. Another way to go which may be needed is to execute some code from the workers
  46. themselves thanks to @code{starpu_execute_on_each_worker}. This may be required
  47. by CUDA to behave properly due to threading issues. For instance, StarPU's
  48. @code{starpu_helper_cublas_init} looks like the following to call
  49. @code{cublasInit} from the workers themselves:
  50. @smallexample
  51. static void init_cublas_func(void *args STARPU_ATTRIBUTE_UNUSED)
  52. @{
  53. cublasStatus cublasst = cublasInit();
  54. cublasSetKernelStream(starpu_cuda_get_local_stream());
  55. @}
  56. void starpu_helper_cublas_init(void)
  57. @{
  58. starpu_execute_on_each_worker(init_cublas_func, NULL, STARPU_CUDA);
  59. @}
  60. @end smallexample