| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 | @c -*-texinfo-*-@c This file is part of the StarPU Handbook.@c Copyright (C) 2009--2011  Universit@'e de Bordeaux 1@c Copyright (C) 2010, 2011, 2012  Centre National de la Recherche Scientifique@c Copyright (C) 2011 Institut National de Recherche en Informatique et Automatique@c See the file starpu.texi for copying conditions.@menu* Per-worker library initialization::  How to initialize a computation library once for each worker?@end menu@node Per-worker library initialization@section How to initialize a computation library once for each worker?Some libraries need to be initialized once for each concurrent instance thatmay run on the machine. For instance, a C++ computation class which is notthread-safe by itself, but for which several instanciated objects of that classcan be used concurrently. This can be used in StarPU by initializing one suchobject per worker. For instance, the libstarpufft example does the following tobe able to use FFTW.Some global array stores the instanciated objects:@cartouche@smallexamplefftw_plan plan_cpu[STARPU_NMAXWORKERS];@end smallexample@end cartoucheAt initialisation time of libstarpu, the objects are initialized:@cartouche@smallexampleint workerid;for (workerid = 0; workerid < starpu_worker_get_count(); workerid++) @{    switch (starpu_worker_get_type(workerid)) @{        case STARPU_CPU_WORKER:            plan_cpu[workerid] = fftw_plan(...);            break;    @}@}@end smallexample@end cartoucheAnd in the codelet body, they are used:@cartouche@smallexamplestatic void fft(void *descr[], void *_args)@{    int workerid = starpu_worker_get_id();    fftw_plan plan = plan_cpu[workerid];    ...    fftw_execute(plan, ...);@}@end smallexample@end cartoucheAnother way to go which may be needed is to execute some code from the workersthemselves thanks to @code{starpu_execute_on_each_worker}. This may be requiredby CUDA to behave properly due to threading issues. For instance, StarPU's@code{starpu_helper_cublas_init} looks like the following to call@code{cublasInit} from the workers themselves:@cartouche@smallexamplestatic void init_cublas_func(void *args STARPU_ATTRIBUTE_UNUSED)@{    cublasStatus cublasst = cublasInit();    cublasSetKernelStream(starpu_cuda_get_local_stream());@}void starpu_helper_cublas_init(void)@{    starpu_execute_on_each_worker(init_cublas_func, NULL, STARPU_CUDA);@}@end smallexample@end cartouche
 |