Document that zero copy api mostly offers only memory footprint benefit
[charm.git] / doc / charm++ / callbacks.tex
blob9605e842a3502307e65f96f83d90722c2c897337
1 \label{callbacks}
3 Callbacks provide a generic way to store the information required to
4 invoke a communication target, such as a chare's entry method, at a
5 future time. Callbacks are often encountered when writing library
6 code, where they provide a simple way to transfer control back to a
7 client after the library has finished. For example, after finishing a
8 reduction, you may want the results passed to some chare's entry
9 method. To do this, you would create an object of type
10 \kw{CkCallback} with the chare's \kw{CkChareID} and entry method
11 index, and pass this callback object to the reduction library.
13 \section{Creating a CkCallback Object}
14 \label{sec:callbacks/creating}
16 \index{CkCallback}
18 There are several different types of \kw{CkCallback} objects; the type
19 of the callback specifies the intended behavior upon invocation of the
20 callback. Callbacks must be invoked with the \charmpp{} message of the
21 type specified when creating the callback. If the callback is being
22 passed into a library which will return its result through the
23 callback, it is the user's responsibility to ensure that the type of
24 the message delivered by the library is the same as that specified in
25 the callback. Messages delivered through a callback are not
26 automatically freed by the Charm RTS. They should be freed or stored
27 for future use by the user.
29 Callbacks that target chares require an ``entry method index'', an
30 integer that identifies which entry method will be called. An entry
31 method index is the \charmpp{} version of a function pointer. The
32 entry method index can be obtained using the syntax:
34 \begin{alltt}
35 \uw{int myIdx} = CkIndex_\uw{ChareName}::\uw{EntryMethod}(\uw{parameters});
36 \end{alltt}
38 Here, \uw{ChareName} is the name of the chare (group, or array)
39 containing the desired entry method, \uw{EntryMethod} is the name of
40 that entry method, and \uw{parameters} are the parameters taken by the
41 method. These parameters are only used to resolve the proper
42 \uw{EntryMethod}; they are otherwise ignored.
44 Under most circumstances, entry methods to be invoked through a
45 CkCallback must take a single message pointer as argument. As such, if
46 the entry method specified in the callback is not overloaded, using
47 NULL in place of parameters will suffice in fully specifying the
48 intended target. If the entry method is overloaded, a message pointer
49 of the appropriate type should be defined and passed in as a parameter
50 when specifying the entry method. The pointer does not need to be
51 initialized as the argument is only used to resolve the target entry
52 method.
54 The intended behavior upon a callback's invocation is specified
55 through the choice of callback constructor used when creating the callback.
56 Possible constructors are:
58 \begin{enumerate}
59 \item \kw{CkCallback(void (*CallbackFn)(void *, void *), void *param)} -
60 When invoked, the callback will pass \uw{param} and the result message
61 to the given C function, which should have a prototype
62 like:
64 \begin{alltt}
65 void \uw{myCallbackFn}(void *param, void *message)
66 \end{alltt}
68 This function will be called on the processor where the callback was created,
69 so \uw{param} is allowed to point to heap-allocated data. Hence, this
70 constructor should be used only when it is known that the callback target (which by definition here
71 is just a C-like function) will be on the same processor as from where the constructor was called.
72 Of course, you
73 are required to free any storage referenced by \uw{param}.
75 \item \kw{CkCallback(CkCallback::ignore)} - When invoked, the callback
76 will do nothing. This can be useful if a \charmpp{} library requires
77 a callback, but you don't care when it finishes, or will find out some
78 other way.
80 \item \kw{CkCallback(CkCallback::ckExit)} - When invoked, the callback
81 will call CkExit(), ending the Charm++ program.
83 \item \kw{CkCallback(int ep, const CkChareID \&id)} - When invoked, the
84 callback will send its message to the given entry method (specified by the
85 entry point index - \kw{ep}) of the given
86 Chare (specified by the chare \kw{id}). Note that a chare proxy will also work in place of a chare id:
88 \begin{alltt}
89 CkCallback myCB(CkIndex_myChare::myEntry(NULL), myChareProxy);
90 \end{alltt}
92 \item \kw{CkCallback(int ep, const CkArrayID \&id)} -
93 When invoked,
94 the callback will broadcast its message to the given entry method
95 of the given array. An array proxy will work in the place of an array id.
97 \item \kw{CkCallback(int ep, const CkArrayIndex \&idx, const CkArrayID \&id)} -
98 When invoked,
99 the callback will send its message to the given entry method
100 of the given array element.
102 \item \kw{CkCallback(int ep, const CkGroupID \&id)} -
103 When invoked,
104 the callback will broadcast its message to the given entry method
105 of the given group.
107 \item \kw{CkCallback(int ep, int onPE, const CkGroupID \&id)} -
108 When invoked, the callback will send its message to the given entry
109 method of the given group member.
111 \end{enumerate}
113 One final type of callback, \kw{CkCallbackResumeThread()}, can only be
114 used from within threaded entry methods. This callback type is
115 discussed in section \ref{sec:ckcallbackresumethread}.
117 \section{CkCallback Invocation}
119 \label{libraryInterface}
121 A properly initialized \kw{CkCallback} object stores a global
122 destination identifier, and as such can be freely copied, marshalled,
123 and sent in messages. Invocation of a CkCallback is done by calling
124 the function \kw{send} on the callback with the result message as an
125 argument. As an example, a library which accepts a CkCallback object
126 from the user and then invokes it to return a result may have the
127 following interface:
129 \begin{alltt}
130 //Main library entry point, called by asynchronous users:
131 void myLibrary(...library parameters...,const CkCallback \&cb)
133 ..start some parallel computation, store cb to be passed to myLibraryDone later...
136 //Internal library routine, called when computation is done
137 void myLibraryDone(...parameters...,const CkCallback \&cb)
139 ...prepare a return message...
140 cb.send(msg);
142 \end{alltt}
144 A \kw{CkCallback} will accept any message type, or even NULL. The
145 message is immediately sent to the user's client function or entry
146 point. A library which returns its result through a callback should
147 have a clearly documented return message type. The type of the message
148 returned by the library must be the same as the type accepted by the
149 entry method specified in the callback.
151 As an alternative to ``send'', the callback can be used in a {\em
152 contribute} collective operation. This will internally invoke the
153 ``send'' method on the callback when the contribute operation has
154 finished.
156 For examples of how to use the various callback types, please
157 see \testreffile{megatest/callback.C}
159 \section{Synchronous Execution with CkCallbackResumeThread}
161 \label{sec:ckcallbackresumethread}
163 Threaded entry methods can be suspended and resumed through the {\em
164 CkCallbackResumeThread} class. {\em CkCallbackResumeThread} is
165 derived from {\em CkCallback} and has specific functionality for
166 threads. This class automatically suspends the thread when the
167 destructor of the callback is called. A suspended threaded client
168 will resume when the ``send'' method is invoked on the associated
169 callback. It can be used in situations when the return value is not
170 needed, and only the synchronization is important. For example:
172 \begin{alltt}
173 // Call the "doWork" method and wait until it has completed
174 void mainControlFlow() \{
175 ...perform some work...
176 // call a library
177 doWork(...,CkCallbackResumeThread());
178 // or send a broadcast to a chare collection
179 myProxy.doWork(...,CkCallbackResumeThread());
180 // callback goes out of scope; the thread is suspended until doWork calls 'send' on the callback
182 ...some more work...
184 \end{alltt}
186 Alternatively, if doWork returns a value of interest, this can be retrieved by
187 passing a pointer to {\em CkCallbackResumeThread}. This pointer will be modified
188 by {\em CkCallbackResumeThread} to point to the incoming message. Notice that
189 the input pointer has to be cast to {\em (void*\&)}:
191 \begin{alltt}
192 // Call the "doWork" method and wait until it has completed
193 void mainControlFlow() \{
194 ...perform some work...
195 MyMessage *mymsg;
196 myProxy.doWork(...,CkCallbackResumeThread((void*\&)mymsg));
197 // The thread is suspended until doWork calls send on the callback
199 ...some more work using "mymsg"...
201 \end{alltt}
203 Notice that the instance of {\em CkCallbackResumeThread} is constructed
204 as an anonymous parameter to the ``doWork'' call. This insures that the callback
205 is destroyed as soon as the function returns, thereby suspending the thread.
207 It is also possible to allocate a {\em CkCallbackResumeThread} on the heap or on
208 the stack. We suggest that programmers avoid such usage, and favor the anonymous instance construction
209 shown above. For completeness, we still present the code for heap and stack
210 allocation of CkCallbackResumeThread callbacks below.
212 For heap allocation, the user must explicitly ``delete'' the callback in order to
213 suspend the thread.
215 \begin{alltt}
216 // Call the "doWork" method and wait until it has completed
217 void mainControlFlow() \{
218 ...perform some work...
219 CkCallbackResumeThread cb = new CkCallbackResumeThread();
220 myProxy.doWork(...,cb);
221 ...do not suspend yet, continue some more work...
222 delete cb;
223 // The thread suspends now
225 ...some more work after the thread resumes...
227 \end{alltt}
229 For a callback that is allocated on the stack, its destructor will be called only
230 when the callback variable goes out of scope. In this
231 situation, the function ``thread\_delay'' can be invoked on the callback to
232 force the thread to suspend. This also works for heap allocated callbacks.
234 \begin{alltt}
235 // Call the "doWork" method and wait until it has completed
236 void mainControlFlow() \{
237 ...perform some work...
238 CkCallbackResumeThread cb;
239 myProxy.doWork(...,cb);
240 ...do not suspend yet, continue some more work...
241 cb.thread\_delay();
242 // The thread suspends now
244 ...some more work after the thread is resumed...
246 \end{alltt}
248 In all cases a {\em CkCallbackResumeThread} can be used to suspend a thread
249 only once.\\
250 (See \examplerefdir{barnes-charm} for a complete example).\\
251 {\em Deprecated usage}: in the past, ``thread\_delay'' was used to retrieve the
252 incoming message from the callback. While that is still allowed for backward
253 compatibility, its usage is deprecated. The old usage is subject to memory
254 leaks and dangling pointers.