tips-tricks.texi 2.7 KB

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