| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708 | /* * This file is part of the StarPU Handbook. * Copyright (C) 2009--2011  Universit@'e de Bordeaux * Copyright (C) 2010, 2011, 2012, 2013, 2016  CNRS * Copyright (C) 2011, 2012 INRIA * See the file version.doxy for copying conditions. *//*! \page BasicExamples Basic Examples\section HelloWorldUsingTheCExtension Hello World Using The C ExtensionThis section shows how to implement a simple program that submits a taskto StarPU using the StarPU C extension (\ref cExtensions). The complete example, and additional examples,is available in the directory <c>gcc-plugin/examples</c> of the StarPUdistribution. A similar example showing how to directly use the StarPU's API is shownin \ref HelloWorldUsingStarPUAPI.GCC from version 4.5 permit to use the StarPU GCC plug-in (\ref cExtensions). This makes writing a task both simpler and less error-prone.In a nutshell, all it takes is to declare a task, declare and define itsimplementations (for CPU, OpenCL, and/or CUDA), and invoke the task likea regular C function.  The example below defines <c>my_task</c> whichhas a single implementation for CPU:\snippet hello_pragma.c To be included. You should update doxygen if you see this text.The code can then be compiled and linked with GCC and the flag <c>-fplugin</c>:\verbatim$ gcc `pkg-config starpu-1.3 --cflags` hello-starpu.c \    -fplugin=`pkg-config starpu-1.3 --variable=gccplugin` \    `pkg-config starpu-1.3 --libs`\endverbatimThe code can also be compiled without the StarPU C extension and willbehave as a normal sequential code.\verbatim$ gcc hello-starpu.chello-starpu.c:33:1: warning: ‘task’ attribute directive ignored [-Wattributes]$ ./a.outHello, world! With x = 42\endverbatimAs can be seen above, the C extensions allows programmers touse StarPU tasks by essentially annotating ``regular'' C code.\section HelloWorldUsingStarPUAPI Hello World Using StarPU's APIThis section shows how to achieve the same result as in the previoussection using StarPU's standard C API.\subsection RequiredHeaders Required HeadersThe header starpu.h should be included in any code using StarPU.\code{.c}#include <starpu.h>\endcode\subsection DefiningACodelet Defining A CodeletA codelet is a structure that represents a computational kernel. Such a codeletmay contain an implementation of the same kernel on different architectures(e.g. CUDA, x86, ...). For compatibility, make sure that the wholestructure is properly initialized to zero, either by using thefunction starpu_codelet_init(), or by letting thecompiler implicitly do it as examplified below.The field starpu_codelet::nbuffers specifies the number of data buffers that aremanipulated by the codelet: here the codelet does not access or modify any datathat is controlled by our data management library.We create a codelet which may only be executed on CPUs. When a CPUcore will execute a codelet, it will call the function<c>cpu_func</c>, which \em must have the following prototype:\code{.c}void (*cpu_func)(void *buffers[], void *cl_arg);\endcodeIn this example, we can ignore the first argument of this function which gives adescription of the input and output buffers (e.g. the size and the location ofthe matrices) since there is none. We also ignore the second argumentwhich is a pointer to optional arguments for the codelet.\code{.c}void cpu_func(void *buffers[], void *cl_arg){    printf("Hello world\n");}struct starpu_codelet cl ={    .cpu_funcs = { cpu_func },    .nbuffers = 0};\endcode\subsection SubmittingATask Submitting A TaskBefore submitting any tasks to StarPU, starpu_init() must be called. The<c>NULL</c> argument specifies that we use the default configuration.Tasks can then be submitted until the termination of StarPU -- done by acall to starpu_shutdown().In the example below, a task structure is allocated by a call tostarpu_task_create(). This function allocates and fills thetask structure with its default settings, it does notsubmit the task to StarPU.The field starpu_task::cl is a pointer to the codelet which the task willexecute: in other words, the codelet structure describes which computationalkernel should be offloaded on the different architectures, and the taskstructure is a wrapper containing a codelet and the piece of data on which thecodelet should operate.If the field starpu_task::synchronous is non-zero, task submissionwill be synchronous: the function starpu_task_submit() will not returnuntil the task has been executed. Note that the function starpu_shutdown()does not guarantee that asynchronous tasks have been executed beforeit returns, starpu_task_wait_for_all() can be used to that effect, ordata can be unregistered (starpu_data_unregister()), which willimplicitly wait for all the tasks scheduled to work on it, unlessexplicitly disabled thanks tostarpu_data_set_default_sequential_consistency_flag() orstarpu_data_set_sequential_consistency_flag().\code{.c}int main(int argc, char **argv){    /* initialize StarPU */    starpu_init(NULL);    struct starpu_task *task = starpu_task_create();    task->cl = &cl; /* Pointer to the codelet defined above */    /* starpu_task_submit will be a blocking call. If unset,    starpu_task_wait() needs to be called after submitting the task. */    task->synchronous = 1;    /* submit the task to StarPU */    starpu_task_submit(task);    /* terminate StarPU */    starpu_shutdown();    return 0;}\endcode\subsection ExecutionOfHelloWorld Execution Of Hello World\verbatim$ make hello_worldcc $(pkg-config --cflags starpu-1.3) hello_world.c -o hello_world $(pkg-config --libs starpu-1.3)$ ./hello_worldHello world\endverbatim\subsection PassingArgumentsToTheCodelet Passing Arguments To The CodeletThe optional field starpu_task::cl_arg field is a pointer to a buffer(of size starpu_task::cl_arg_size) with some parameters for the kerneldescribed by the codelet. For instance, if a codelet implements acomputational kernel that multiplies its input vector by a constant,the constant could be specified by the means of this buffer, insteadof registering it as a StarPU data. It must however be noted thatStarPU avoids making copy whenever possible and rather passes thepointer as such, so the buffer which is pointed at must be kept allocateduntil the task terminates, and if several tasks are submitted withvarious parameters, each of them must be given a pointer to theirown buffer.\code{.c}struct params{    int i;    float f;};void cpu_func(void *buffers[], void *cl_arg){    struct params *params = cl_arg;    printf("Hello world (params = {%i, %f} )\n", params->i, params->f);}\endcodeAs said before, the field starpu_codelet::nbuffers specifies thenumber of data buffers that are manipulated by the codelet. It doesnot count the argument --- the parameter <c>cl_arg</c> of the function<c>cpu_func</c> --- since it is not managed by our data managementlibrary, but just contains trivial parameters.// TODO rewrite so that it is a little clearer ?Be aware that this may be a pointer to a\em copy of the actual buffer, and not the pointer given by the programmer:if the codelet modifies this buffer, there is no guarantee that the initialbuffer will be modified as well: this for instance implies that the buffercannot be used as a synchronization medium. If synchronization is needed, datahas to be registered to StarPU, see \ref VectorScalingUsingStarPUAPI.\code{.c}int main(int argc, char **argv){    /* initialize StarPU */    starpu_init(NULL);    struct starpu_task *task = starpu_task_create();    task->cl = &cl; /* Pointer to the codelet defined above */    struct params params = { 1, 2.0f };    task->cl_arg = ¶ms;    task->cl_arg_size = sizeof(params);    /* starpu_task_submit will be a blocking call */    task->synchronous = 1;    /* submit the task to StarPU */    starpu_task_submit(task);    /* terminate StarPU */    starpu_shutdown();    return 0;}\endcode\verbatim$ make hello_worldcc $(pkg-config --cflags starpu-1.3) hello_world.c -o hello_world $(pkg-config --libs starpu-1.3)$ ./hello_worldHello world (params = {1, 2.000000} )\endverbatim\subsection DefiningACallback Defining A CallbackOnce a task has been executed, an optional callback functionstarpu_task::callback_func is called when defined.While the computational kernel could be offloaded on various architectures, thecallback function is always executed on a CPU. The pointerstarpu_task::callback_arg is passed as an argument of the callbackfunction. The prototype of a callback function must be:\code{.c}void (*callback_function)(void *);\endcode\code{.c}void callback_func(void *callback_arg){    printf("Callback function (arg %x)\n", callback_arg);}int main(int argc, char **argv){    /* initialize StarPU */    starpu_init(NULL);    struct starpu_task *task = starpu_task_create();    task->cl = &cl; /* Pointer to the codelet defined above */    task->callback_func = callback_func;    task->callback_arg = 0x42;    /* starpu_task_submit will be a blocking call */    task->synchronous = 1;    /* submit the task to StarPU */    starpu_task_submit(task);    /* terminate StarPU */    starpu_shutdown();    return 0;}\endcode\verbatim$ make hello_worldcc $(pkg-config --cflags starpu-1.3) hello_world.c -o hello_world $(pkg-config --libs starpu-1.3) $ ./hello_worldHello worldCallback function (arg 42)\endverbatim\subsection WhereToExecuteACodelet Where To Execute A Codelet\code{.c}struct starpu_codelet cl ={    .where = STARPU_CPU,    .cpu_funcs = { cpu_func },    .cpu_funcs_name = { "cpu_func" },     .nbuffers = 0};\endcodeWe create a codelet which may only be executed on the CPUs. Theoptional field starpu_codelet::where is a bitmask that defines wherethe codelet may be executed. Here, the value ::STARPU_CPU means thatonly CPUs can execute this codelet. When the optional fieldstarpu_codelet::where is unset, its value is automatically set basedon the availability of the different fields <c>XXX_funcs</c>.TODO: explain starpu_codelet::cpu_funcs_name\section VectorScalingUsingTheCExtension Vector Scaling Using the C ExtensionThe previous example has shown how to submit tasks. In this section,we show how StarPU tasks can manipulate data.We will first show how to use the C language extensions provided bythe GCC plug-in (\ref cExtensions). The complete example, andadditional examples, is available in the directory <c>gcc-plugin/examples</c>of the StarPU distribution. These extensions map directlyto StarPU's main concepts: tasks, task implementations for CPU,OpenCL, or CUDA, and registered data buffers. The standard C versionthat uses StarPU's standard C programming interface is given in \ref VectorScalingUsingStarPUAPI.First of all, the vector-scaling task and its simple CPU implementationhas to be defined:\code{.c}/* Declare the `vector_scal' task.  */static void vector_scal (unsigned size, float vector[size],                         float factor)  __attribute__ ((task));/* Define the standard CPU implementation.  */static voidvector_scal (unsigned size, float vector[size], float factor){  unsigned i;  for (i = 0; i < size; i++)    vector[i] *= factor;}\endcodeNext, the body of the program, which uses the task defined above, can beimplemented:\snippet hello_pragma2.c To be included. You should update doxygen if you see this text.The function <c>main</c> above does several things:<ul><li>It initializes StarPU.</li><li>It allocates <c>vector</c> in the heap; it will automatically be freedwhen its scope is left.  Alternatively, good old <c>malloc</c> and<c>free</c> could have been used, but they are more error-prone andrequire more typing.</li><li>It registers the memory pointed to by <c>vector</c>.  Eventually,when OpenCL or CUDA task implementations are added, this will allowStarPU to transfer that memory region between GPUs and the main memory.Removing this <c>pragma</c> is an error.</li><li>It invokes the task <c>vector_scal</c>.  The invocation looks the sameas a standard C function call.  However, it is an asynchronousinvocation, meaning that the actual call is performed in parallel withthe caller's continuation.</li><li>It waits for the termination of the asynchronous call <c>vector_scal</c>.</li><li>Finally, StarPU is shut down.</li></ul>The program can be compiled and linked with GCC and the flag <c>-fplugin</c>:\verbatim$ gcc `pkg-config starpu-1.3 --cflags` vector_scal.c \    -fplugin=`pkg-config starpu-1.3 --variable=gccplugin` \    `pkg-config starpu-1.3 --libs`\endverbatimAnd voilà!\subsection AddingAnOpenCLTaskImplementation Adding an OpenCL Task ImplementationNow, this is all fine and great, but you certainly want to takeadvantage of these newfangled GPUs that your lab just bought, don't you?So, let's add an OpenCL implementation of the task <c>vector_scal</c>.We assume that the OpenCL kernel is available in a file,<c>vector_scal_opencl_kernel.cl</c>, not shown here.  The OpenCL taskimplementation is similar to that used with the standard C API(\ref DefinitionOfTheOpenCLKernel).  It is declared and definedin our C file like this:\code{.c}/* The OpenCL programs, loaded from 'main' (see below). */static struct starpu_opencl_program cl_programs;static void vector_scal_opencl (unsigned size, float vector[size],                                float factor)  __attribute__ ((task_implementation ("opencl", vector_scal)));static voidvector_scal_opencl (unsigned size, float vector[size], float factor){  int id, devid, err;  cl_kernel kernel;  cl_command_queue queue;  cl_event event;  /* VECTOR is GPU memory pointer, not a main memory pointer. */  cl_mem val = (cl_mem) vector;  id = starpu_worker_get_id ();  devid = starpu_worker_get_devid (id);  /* Prepare to invoke the kernel.  In the future, this will be largely automated.  */  err = starpu_opencl_load_kernel (&kernel, &queue, &cl_programs,                                   "vector_mult_opencl", devid);  if (err != CL_SUCCESS)    STARPU_OPENCL_REPORT_ERROR (err);  err = clSetKernelArg (kernel, 0, sizeof (size), &size);  err |= clSetKernelArg (kernel, 1, sizeof (val), &val);  err |= clSetKernelArg (kernel, 2, sizeof (factor), &factor);  if (err)    STARPU_OPENCL_REPORT_ERROR (err);  size_t global = 1, local = 1;  err = clEnqueueNDRangeKernel (queue, kernel, 1, NULL, &global,                                &local, 0, NULL, &event);  if (err != CL_SUCCESS)    STARPU_OPENCL_REPORT_ERROR (err);  clFinish (queue);  starpu_opencl_collect_stats (event);  clReleaseEvent (event);  /* Done with KERNEL. */  starpu_opencl_release_kernel (kernel);}\endcodeThe OpenCL kernel itself must be loaded from <c>main</c>, sometime afterthe pragma <c>initialize</c>:\code{.c}starpu_opencl_load_opencl_from_file ("vector_scal_opencl_kernel.cl",                                       &cl_programs, "");\endcodeAnd that's it.  The task <c>vector_scal</c> now has an additionalimplementation, for OpenCL, which StarPU's scheduler may choose to useat run-time.  Unfortunately, the <c>vector_scal_opencl</c> above stillhas to go through the common OpenCL boilerplate; in the future,additional extensions will automate most of it.\subsection AddingACUDATaskImplementation Adding a CUDA Task ImplementationAdding a CUDA implementation of the task is very similar, except thatthe implementation itself is typically written in CUDA, and compiledwith <c>nvcc</c>.  Thus, the C file only needs to contain an externaldeclaration for the task implementation:\code{.c}extern void vector_scal_cuda (unsigned size, float vector[size],                              float factor)  __attribute__ ((task_implementation ("cuda", vector_scal)));\endcodeThe actual implementation of the CUDA task goes into a separatecompilation unit, in a <c>.cu</c> file.  It is very close to theimplementation when using StarPU's standard C API (\ref DefinitionOfTheCUDAKernel).\snippet scal_pragma.cu To be included. You should update doxygen if you see this text.The complete source code, in the directory <c>gcc-plugin/examples/vector_scal</c>of the StarPU distribution, also shows how an SSE-specializedCPU task implementation can be added.For more details on the C extensions provided by StarPU's GCC plug-in, see\ref cExtensions.\section VectorScalingUsingStarPUAPI Vector Scaling Using StarPU's APIThis section shows how to achieve the same result as explained in theprevious section using StarPU's standard C API.The full source code forthis example is given in \ref FullSourceCodeVectorScal.\subsection SourceCodeOfVectorScaling Source Code of Vector ScalingProgrammers can describe the data layout of their application so that StarPU isresponsible for enforcing data coherency and availability across the machine.Instead of handling complex (and non-portable) mechanisms to perform datamovements, programmers only declare which piece of data is accessed and/ormodified by a task, and StarPU makes sure that when a computational kernelstarts somewhere (e.g. on a GPU), its data are available locally.Before submitting those tasks, the programmer first needs to declare thedifferent pieces of data to StarPU using the functions<c>starpu_*_data_register</c>. To ease the development of applicationsfor StarPU, it is possible to describe multiple types of data layout.A type of data layout is called an <b>interface</b>. There aredifferent predefined interfaces available in StarPU: here we willconsider the <b>vector interface</b>.The following lines show how to declare an array of <c>NX</c> elements of type<c>float</c> using the vector interface:\code{.c}float vector[NX];starpu_data_handle_t vector_handle;starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector, NX,                            sizeof(vector[0]));\endcodeThe first argument, called the <b>data handle</b>, is an opaque pointer whichdesignates the array within StarPU. This is also the structure which is used todescribe which data is used by a task. The second argument is the node numberwhere the data originally resides. Here it is ::STARPU_MAIN_RAM since the array <c>vector</c> is inthe main memory. Then comes the pointer <c>vector</c> where the data can be found in main memory,the number of elements in the vector and the size of each element.The following shows how to construct a StarPU task that will manipulate thevector and a constant factor.\code{.c}float factor = 3.14;struct starpu_task *task = starpu_task_create();task->cl = &cl;                      /* Pointer to the codelet defined below */task->handles[0] = vector_handle;    /* First parameter of the codelet */task->cl_arg = &factor;task->cl_arg_size = sizeof(factor);task->synchronous = 1;starpu_task_submit(task);\endcodeSince the factor is a mere constant float value parameter,it does not need a preliminary registration, andcan just be passed through the pointer starpu_task::cl_arg like in the previousexample.  The vector parameter is described by its handle.starpu_task::handles should be set with the handles of the data, theaccess modes for the data are defined in the fieldstarpu_codelet::modes (::STARPU_R for read-only, ::STARPU_W forwrite-only and ::STARPU_RW for read and write access).The definition of the codelet can be written as follows:\code{.c}void scal_cpu_func(void *buffers[], void *cl_arg){    unsigned i;    float *factor = cl_arg;    /* length of the vector */    unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);    /* CPU copy of the vector pointer */    float *val = (float *)STARPU_VECTOR_GET_PTR(buffers[0]);    for (i = 0; i < n; i++)        val[i] *= *factor;}struct starpu_codelet cl ={    .cpu_funcs = { scal_cpu_func },    .cpu_funcs_name = { "scal_cpu_func" },    .nbuffers = 1,    .modes = { STARPU_RW }};\endcodeThe first argument is an array that givesa description of all the buffers passed in the array starpu_task::handles. Thesize of this array is given by the field starpu_codelet::nbuffers. Forthe sake of genericity, this array contains pointers to the differentinterfaces describing each buffer.  In the case of the <b>vectorinterface</b>, the location of the vector (resp. its length) isaccessible in the starpu_vector_interface::ptr (resp.starpu_vector_interface::nx) of this interface. Since the vector isaccessed in a read-write fashion, any modification will automaticallyaffect future accesses to this vector made by other tasks.The second argument of the function <c>scal_cpu_func</c> contains apointer to the parameters of the codelet (given instarpu_task::cl_arg), so that we read the constant factor from thispointer.\subsection ExecutionOfVectorScaling Execution of Vector Scaling\verbatim$ make vector_scalcc $(pkg-config --cflags starpu-1.3) vector_scal.c -o vector_scal $(pkg-config --libs starpu-1.3)$ ./vector_scal0.000000 3.000000 6.000000 9.000000 12.000000\endverbatim\section VectorScalingOnAnHybridCPUGPUMachine Vector Scaling on an Hybrid CPU/GPU MachineContrary to the previous examples, the task submitted in this example may notonly be executed by the CPUs, but also by a CUDA device.\subsection DefinitionOfTheCUDAKernel Definition of the CUDA KernelThe CUDA implementation can be written as follows. It needs to be compiled witha CUDA compiler such as nvcc, the NVIDIA CUDA compiler driver. It must be notedthat the vector pointer returned by ::STARPU_VECTOR_GET_PTR is here apointer in GPU memory, so that it can be passed as such to thekernel call <c>vector_mult_cuda</c>.\snippet vector_scal_cuda.cu To be included. You should update doxygen if you see this text.\subsection DefinitionOfTheOpenCLKernel Definition of the OpenCL KernelThe OpenCL implementation can be written as follows. StarPU providestools to compile a OpenCL kernel stored in a file.\code{.c}__kernel void vector_mult_opencl(int nx, __global float* val, float factor){        const int i = get_global_id(0);        if (i < nx) {                val[i] *= factor;        }}\endcodeContrary to CUDA and CPU, ::STARPU_VECTOR_GET_DEV_HANDLE has to be used,which returns a <c>cl_mem</c> (which is not a device pointer, but an OpenCLhandle), which can be passed as such to the OpenCL kernel. The difference isimportant when using partitioning, see \ref PartitioningData.\snippet vector_scal_opencl.c To be included. You should update doxygen if you see this text.\subsection DefinitionOfTheMainCode Definition of the Main CodeThe CPU implementation is the same as in the previous section.Here is the source of the main application. You can notice that the fieldsstarpu_codelet::cuda_funcs and starpu_codelet::opencl_funcs are set todefine the pointers to the CUDA and OpenCL implementations of thetask.\snippet vector_scal_c.c To be included. You should update doxygen if you see this text.\subsection ExecutionOfHybridVectorScaling Execution of Hybrid Vector ScalingThe Makefile given at the beginning of the section must be extended togive the rules to compile the CUDA source code. Note that the sourcefile of the OpenCL kernel does not need to be compiled now, it willbe compiled at run-time when calling the functionstarpu_opencl_load_opencl_from_file().\verbatimCFLAGS  += $(shell pkg-config --cflags starpu-1.3)LDFLAGS += $(shell pkg-config --libs starpu-1.3)CC       = gccvector_scal: vector_scal.o vector_scal_cpu.o vector_scal_cuda.o vector_scal_opencl.o%.o: %.cu       nvcc $(CFLAGS) $< -c $@clean:       rm -f vector_scal *.o\endverbatim\verbatim$ make\endverbatimand to execute it, with the default configuration:\verbatim$ ./vector_scal0.000000 3.000000 6.000000 9.000000 12.000000\endverbatimor for example, by disabling CPU devices:\verbatim$ STARPU_NCPU=0 ./vector_scal0.000000 3.000000 6.000000 9.000000 12.000000\endverbatimor by disabling CUDA devices (which may permit to enable the use of OpenCL,see \ref EnablingOpenCL) :\verbatim$ STARPU_NCUDA=0 ./vector_scal0.000000 3.000000 6.000000 9.000000 12.000000\endverbatim*/
 |