390_faq.doxy 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * This file is part of the StarPU Handbook.
  3. * Copyright (C) 2009--2011 Universit@'e de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2016, 2017 CNRS
  5. * Copyright (C) 2011, 2012 INRIA
  6. * See the file version.doxy for copying conditions.
  7. */
  8. /*! \page FrequentlyAskedQuestions Frequently Asked Questions
  9. \section HowToInitializeAComputationLibraryOnceForEachWorker How To Initialize A Computation Library Once For Each Worker?
  10. Some libraries need to be initialized once for each concurrent instance that
  11. may run on the machine. For instance, a C++ computation class which is not
  12. thread-safe by itself, but for which several instanciated objects of that class
  13. can be used concurrently. This can be used in StarPU by initializing one such
  14. object per worker. For instance, the <c>libstarpufft</c> example does the following to
  15. be able to use FFTW on CPUs.
  16. Some global array stores the instanciated objects:
  17. \code{.c}
  18. fftw_plan plan_cpu[STARPU_NMAXWORKERS];
  19. \endcode
  20. At initialisation time of libstarpu, the objects are initialized:
  21. \code{.c}
  22. int workerid;
  23. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++)
  24. {
  25. switch (starpu_worker_get_type(workerid))
  26. {
  27. case STARPU_CPU_WORKER:
  28. plan_cpu[workerid] = fftw_plan(...);
  29. break;
  30. }
  31. }
  32. \endcode
  33. And in the codelet body, they are used:
  34. \code{.c}
  35. static void fft(void *descr[], void *_args)
  36. {
  37. int workerid = starpu_worker_get_id();
  38. fftw_plan plan = plan_cpu[workerid];
  39. ...
  40. fftw_execute(plan, ...);
  41. }
  42. \endcode
  43. This however is not sufficient for FFT on CUDA: initialization has
  44. to be done from the workers themselves. This can be done thanks to
  45. starpu_execute_on_each_worker(). For instance <c>libstarpufft</c> does the following.
  46. \code{.c}
  47. static void fft_plan_gpu(void *args)
  48. {
  49. plan plan = args;
  50. int n2 = plan->n2[0];
  51. int workerid = starpu_worker_get_id();
  52. cufftPlan1d(&plan->plans[workerid].plan_cuda, n, _CUFFT_C2C, 1);
  53. cufftSetStream(plan->plans[workerid].plan_cuda, starpu_cuda_get_local_stream());
  54. }
  55. void starpufft_plan(void)
  56. {
  57. starpu_execute_on_each_worker(fft_plan_gpu, plan, STARPU_CUDA);
  58. }
  59. \endcode
  60. \section UsingTheDriverAPI Using The Driver API
  61. \ref API_Running_Drivers
  62. \code{.c}
  63. int ret;
  64. struct starpu_driver =
  65. {
  66. .type = STARPU_CUDA_WORKER,
  67. .id.cuda_id = 0
  68. };
  69. ret = starpu_driver_init(&d);
  70. if (ret != 0)
  71. error();
  72. while (some_condition)
  73. {
  74. ret = starpu_driver_run_once(&d);
  75. if (ret != 0)
  76. error();
  77. }
  78. ret = starpu_driver_deinit(&d);
  79. if (ret != 0)
  80. error();
  81. \endcode
  82. To add a new kind of device to the structure starpu_driver, one needs to:
  83. <ol>
  84. <li> Add a member to the union starpu_driver::id
  85. </li>
  86. <li> Modify the internal function <c>_starpu_launch_drivers()</c> to
  87. make sure the driver is not always launched.
  88. </li>
  89. <li> Modify the function starpu_driver_run() so that it can handle
  90. another kind of architecture.
  91. </li>
  92. <li> Write the new function <c>_starpu_run_foobar()</c> in the
  93. corresponding driver.
  94. </li>
  95. </ol>
  96. \section On-GPURendering On-GPU Rendering
  97. Graphical-oriented applications need to draw the result of their computations,
  98. typically on the very GPU where these happened. Technologies such as OpenGL/CUDA
  99. interoperability permit to let CUDA directly work on the OpenGL buffers, making
  100. them thus immediately ready for drawing, by mapping OpenGL buffer, textures or
  101. renderbuffer objects into CUDA. CUDA however imposes some technical
  102. constraints: peer memcpy has to be disabled, and the thread that runs OpenGL has
  103. to be the one that runs CUDA computations for that GPU.
  104. To achieve this with StarPU, pass the option
  105. \ref disable-cuda-memcpy-peer "--disable-cuda-memcpy-peer"
  106. to <c>./configure</c> (TODO: make it dynamic), OpenGL/GLUT has to be initialized
  107. first, and the interoperability mode has to
  108. be enabled by using the field
  109. starpu_conf::cuda_opengl_interoperability, and the driver loop has to
  110. be run by the application, by using the field
  111. starpu_conf::not_launched_drivers to prevent StarPU from running it in
  112. a separate thread, and by using starpu_driver_run() to run the loop.
  113. The examples <c>gl_interop</c> and <c>gl_interop_idle</c> show how it
  114. articulates in a simple case, where rendering is done in task
  115. callbacks. The former uses <c>glutMainLoopEvent</c> to make GLUT
  116. progress from the StarPU driver loop, while the latter uses
  117. <c>glutIdleFunc</c> to make StarPU progress from the GLUT main loop.
  118. Then, to use an OpenGL buffer as a CUDA data, StarPU simply needs to be given
  119. the CUDA pointer at registration, for instance:
  120. \code{.c}
  121. /* Get the CUDA worker id */
  122. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++)
  123. if (starpu_worker_get_type(workerid) == STARPU_CUDA_WORKER)
  124. break;
  125. /* Build a CUDA pointer pointing at the OpenGL buffer */
  126. cudaGraphicsResourceGetMappedPointer((void**)&output, &num_bytes, resource);
  127. /* And register it to StarPU */
  128. starpu_vector_data_register(&handle, starpu_worker_get_memory_node(workerid),
  129. output, num_bytes / sizeof(float4), sizeof(float4));
  130. /* The handle can now be used as usual */
  131. starpu_task_insert(&cl, STARPU_RW, handle, 0);
  132. /* ... */
  133. /* This gets back data into the OpenGL buffer */
  134. starpu_data_unregister(handle);
  135. \endcode
  136. and display it e.g. in the callback function.
  137. \section UsingStarPUWithMKL Using StarPU With MKL 11 (Intel Composer XE 2013)
  138. Some users had issues with MKL 11 and StarPU (versions 1.1rc1 and
  139. 1.0.5) on Linux with MKL, using 1 thread for MKL and doing all the
  140. parallelism using StarPU (no multithreaded tasks), setting the
  141. environment variable <c>MKL_NUM_THREADS</c> to <c>1</c>, and using the threaded MKL library,
  142. with <c>iomp5</c>.
  143. Using this configuration, StarPU only uses 1 core, no matter the value of
  144. \ref STARPU_NCPU. The problem is actually a thread pinning issue with MKL.
  145. The solution is to set the environment variable KMP_AFFINITY to <c>disabled</c>
  146. (http://software.intel.com/sites/products/documentation/studio/composer/en-us/2011Update/compiler_c/optaps/common/optaps_openmp_thread_affinity.htm).
  147. \section ThreadBindingOnNetBSD Thread Binding on NetBSD
  148. When using StarPU on a NetBSD machine, if the topology
  149. discovery library <c>hwloc</c> is used, thread binding will fail. To
  150. prevent the problem, you should at least use the version 1.7 of
  151. <c>hwloc</c>, and also issue the following call:
  152. \verbatim
  153. $ sysctl -w security.models.extensions.user_set_cpu_affinity=1
  154. \endverbatim
  155. Or add the following line in the file <c>/etc/sysctl.conf</c>
  156. \verbatim
  157. security.models.extensions.user_set_cpu_affinity=1
  158. \endverbatim
  159. \section PauseResume Interleaving StarPU and non-StarPU code
  160. If your application only partially uses StarPU, and you do not want to
  161. call starpu_init() / starpu_shutdown() at the beginning/end
  162. of each section, StarPU workers will poll for work between the
  163. sections. To avoid this behavior, you can "pause" StarPU with the
  164. starpu_pause() function. This will prevent the StarPU workers from
  165. accepting new work (tasks that are already in progress will not be
  166. frozen), and stop them from polling for more work.
  167. Note that this does not prevent you from submitting new tasks, but
  168. they won't execute until starpu_resume() is called. Also note
  169. that StarPU must not be paused when you call starpu_shutdown(), and
  170. that this function pair works in a push/pull manner, i.e you need to
  171. match the number of calls to these functions to clear their effect.
  172. One way to use these functions could be:
  173. \code{.c}
  174. starpu_init(NULL);
  175. starpu_pause(); // To submit all the tasks without a single one executing
  176. submit_some_tasks();
  177. starpu_resume(); // The tasks start executing
  178. starpu_task_wait_for_all();
  179. starpu_pause(); // Stop the workers from polling
  180. // Non-StarPU code
  181. starpu_resume();
  182. // ...
  183. starpu_shutdown();
  184. \endcode
  185. \section GPUEatingCores When running with CUDA or OpenCL devices, I am seeing less CPU cores
  186. Yes, this is on purpose.
  187. Since GPU devices are way faster than CPUs, StarPU needs to react quickly when
  188. a task is finished, to feed the GPU with another task (StarPU actually submits
  189. a couple of tasks in advance so as to pipeline this, but filling the pipeline
  190. still has to be happening often enough), and thus it has to dedicate threads for
  191. this, and this is a very CPU-consuming duty. StarPU thus dedicates one CPU core
  192. for driving each GPU by default.
  193. Such dedication is also useful when a codelet is hybrid, i.e. while kernels are
  194. running on the GPU, the codelet can run some computation, which thus be run by
  195. the CPU core instead of driving the GPU.
  196. One can choose to dedicate only one thread for all the CUDA devices by setting
  197. the STARPU_CUDA_THREAD_PER_DEV environment variable to 1. The application
  198. however should use STARPU_CUDA_ASYNC on its CUDA codelets (asynchronous
  199. execution), otherwise the execution of a synchronous CUDA codelet will
  200. monopolize the thread, and other CUDA devices will thus starve while it is
  201. executing.
  202. \section CUDADrivers StarPU does not see my CUDA device
  203. First make sure that CUDA is properly running outside StarPU: build and
  204. run the following program with -lcudart:
  205. \code{.c}
  206. #include <stdio.h>
  207. #include <cuda.h>
  208. #include <cuda_runtime.h>
  209. int main(void)
  210. {
  211. int n, i, version;
  212. cudaError_t err;
  213. err = cudaGetDeviceCount(&n);
  214. if (err)
  215. {
  216. fprintf(stderr,"cuda error %d\n", err);
  217. exit(1);
  218. }
  219. cudaDriverGetVersion(&version);
  220. printf("driver version %d\n", version);
  221. cudaRuntimeGetVersion(&version);
  222. printf("runtime version %d\n", version);
  223. printf("\n");
  224. for (i = 0; i < n; i++)
  225. {
  226. struct cudaDeviceProp props;
  227. printf("CUDA%d\n", i);
  228. err = cudaGetDeviceProperties(&props, i);
  229. if (err)
  230. {
  231. fprintf(stderr,"cuda error %d\n", err);
  232. continue;
  233. }
  234. printf("%s\n", props.name);
  235. printf("%0.3f GB\n", (float) props.totalGlobalMem / (1<<30));
  236. printf("%u MP\n", props.multiProcessorCount);
  237. printf("\n");
  238. }
  239. return 0;
  240. }
  241. \endcode
  242. If that program does not find your device, the problem is not at the StarPU
  243. level, but the CUDA drivers, check the documentation of your CUDA
  244. setup.
  245. \section OpenCLDrivers StarPU does not see my OpenCL device
  246. First make sure that OpenCL is properly running outside StarPU: build and
  247. run the following program with -lOpenCL:
  248. \code{.c}
  249. #include <CL/cl.h>
  250. #include <stdio.h>
  251. #include <assert.h>
  252. int main(void)
  253. {
  254. cl_device_id did[16];
  255. cl_int err;
  256. cl_platform_id pid, pids[16];
  257. cl_uint nbplat, nb;
  258. char buf[128];
  259. size_t size;
  260. int i, j;
  261. err = clGetPlatformIDs(sizeof(pids)/sizeof(pids[0]), pids, &nbplat);
  262. assert(err == CL_SUCCESS);
  263. printf("%u platforms\n", nbplat);
  264. for (j = 0; j < nbplat; j++)
  265. {
  266. pid = pids[j];
  267. printf(" platform %d\n", j);
  268. err = clGetPlatformInfo(pid, CL_PLATFORM_VERSION, sizeof(buf)-1, buf, &size);
  269. assert(err == CL_SUCCESS);
  270. buf[size] = 0;
  271. printf(" platform version %s\n", buf);
  272. err = clGetDeviceIDs(pid, CL_DEVICE_TYPE_ALL, sizeof(did)/sizeof(did[0]), did, &nb);
  273. assert(err == CL_SUCCESS);
  274. printf("%d devices\n", nb);
  275. for (i = 0; i < nb; i++)
  276. {
  277. err = clGetDeviceInfo(did[i], CL_DEVICE_VERSION, sizeof(buf)-1, buf, &size);
  278. buf[size] = 0;
  279. printf(" device %d version %s\n", i, buf);
  280. }
  281. }
  282. return 0;
  283. }
  284. \endcode
  285. If that program does not find your device, the problem is not at the StarPU
  286. level, but the OpenCL drivers, check the documentation of your OpenCL
  287. implementation.
  288. \section IncorrectPerformanceModelFile I keep getting a "Incorrect performance model file" error
  289. The performance model file, used by StarPU to record the performance of
  290. codelets, seem to have been corrupted. Perhaps a previous run of StarPU stopped
  291. abruptly, and thus could not save it properly. You can have a look at the file
  292. if you can fix it, but the simplest way is to just remove the file and run
  293. again, StarPU will just have to re-perform calibration for the corresponding codelet.
  294. */