1 /* reducer_impl.h                  -*-C++-*-
2  *
3  *************************************************************************
4  *
5  *  @copyright
6  *  Copyright (C) 2009-2013, Intel Corporation
7  *  All rights reserved.
8  *
9  *  @copyright
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
13  *
14  *    * Redistributions of source code must retain the above copyright
15  *      notice, this list of conditions and the following disclaimer.
16  *    * Redistributions in binary form must reproduce the above copyright
17  *      notice, this list of conditions and the following disclaimer in
18  *      the documentation and/or other materials provided with the
19  *      distribution.
20  *    * Neither the name of Intel Corporation nor the names of its
21  *      contributors may be used to endorse or promote products derived
22  *      from this software without specific prior written permission.
23  *
24  *  @copyright
25  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  *  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
32  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33  *  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
35  *  WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  *  POSSIBILITY OF SUCH DAMAGE.
37  **************************************************************************/
38 
39 /**
40  * @file reducer_impl.h
41  *
42  * @brief Functions to implement reducers in the runtime.
43  */
44 
45 #ifndef INCLUDED_REDUCER_IMPL_DOT_H
46 #define INCLUDED_REDUCER_IMPL_DOT_H
47 
48 #include <cilk/common.h>
49 #include <internal/abi.h>
50 #include "rts-common.h"
51 
52 __CILKRTS_BEGIN_EXTERN_C
53 
54 /**
55  * Construct an empty reducer map from the memory pool associated with the
56  * given worker.  This reducer map must be destroyed before the worker's
57  * associated global context is destroyed.
58  *
59  * @param w __cilkrts_worker the cilkred_map is being created for.
60  *
61  * @return Pointer to the initialized cilkred_map.
62  */
63 COMMON_SYSDEP
64 cilkred_map *__cilkrts_make_reducer_map(__cilkrts_worker *w);
65 
66 /**
67  * Destroy a reducer map.  The map must have been allocated from the worker's
68  * global context and should have been allocated from the same worker.
69  *
70  * @param w __cilkrts_worker the cilkred_map was created for.
71  * @param h The cilkred_map to be deallocated.
72  */
73 COMMON_SYSDEP
74 void __cilkrts_destroy_reducer_map(__cilkrts_worker *w,
75                                    cilkred_map *h);
76 
77 /**
78  * Set the specified reducer map as the leftmost map if is_leftmost is true,
79  * otherwise, set it to not be the leftmost map.
80  *
81  * @param h The cilkred_map to be modified.
82  * @param is_leftmost true if the reducer map is leftmost.
83  */
84 COMMON_SYSDEP
85 void __cilkrts_set_leftmost_reducer_map(cilkred_map *h,
86                                         int is_leftmost);
87 
88 /**
89  * Merge reducer map RIGHT_MAP into LEFT_MAP and return the result of the
90  * merge.  Both maps must be allocated from the global context associated
91  * with the specified worker.  The returned reducer map must be destroyed
92  * before the worker's associated global context is destroyed.
93  *
94  * If two cilkred_maps are specified, one will be destroyed and the other
95  * one will be returned as the merged cilkred_map.
96  *
97  * When reducers can contain nested parallelism, execution can return
98  * on a different worker than when it started (but still using the
99  * same stack).
100  *
101  * Upon return, *w_ptr stores the pointer to the worker that execution
102  * returns on.
103  *
104  * @param w_ptr      Pointer to the currently executing worker.
105  * @param left_map   The left cilkred_map.
106  * @param right_map  The right cilkred_map.
107  *
108  * @return pointer to merged cilkred_map.
109  */
110 extern
111 cilkred_map *merge_reducer_maps(__cilkrts_worker **w_ptr,
112 				cilkred_map *left_map,
113 				cilkred_map *right_map);
114 
115 /**
116  * Similar to merge_reducer_maps(), except that after merging
117  * RIGHT_MAP into LEFT_MAP, it repeatedly merges (*w_ptr)->reducer_map
118  * into LEFT_MAP.  This procedure ensures that any new reducers
119  * created by the reductions themselves also get merged into LEFT_MAP.
120  */
121 extern
122 cilkred_map *repeated_merge_reducer_maps(__cilkrts_worker **w_ptr,
123 					 cilkred_map *left_map,
124 					 cilkred_map *right_map);
125 
126 __CILKRTS_END_EXTERN_C
127 
128 #endif // ! defined(INCLUDED_REDUCER_IMPL_DOT_H)
129