source: trunk/kernel/kern/dqdt.c @ 375

Last change on this file since 375 was 374, checked in by max@…, 7 years ago

Use panic().

File size: 10.6 KB
Line 
1/*
2 * dqdt.c - Distributed Quaternary Decision Tree implementation.
3 *
4 * Author : Alain Greiner (2016)
5 *
6 * Copyright (c)  UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <kernel_config.h>
25#include <hal_types.h>
26#include <hal_special.h>
27#include <hal_atomic.h>
28#include <hal_remote.h>
29#include <printk.h>
30#include <cluster.h>
31#include <bits.h>
32#include <dqdt.h>
33
34
35///////////////////////////////////////////
36void dqdt_local_print( dqdt_node_t * node )
37{
38        printk("DQDT node : level = %d / cluster = %x / threads = %x / pages = %x\n",
39               node->level,
40               local_cxy,
41               node->threads,
42           node->pages );
43}
44
45/////////////////////////////////////////
46void dqdt_global_print( xptr_t  node_xp )
47{
48        uint32_t i;
49    dqdt_node_t local_node;
50
51    // get root node local copy
52    hal_remote_memcpy( XPTR( local_cxy , &local_node ), node_xp , sizeof(dqdt_node_t) );
53
54    // display DQDT node content
55    dqdt_local_print( &local_node );
56
57    // recursive call on children if node is not terminal
58    if ( local_node.level > 0 )
59    {
60        for ( i = 0 ; i < 4 ; i++ )
61        {
62            if ( local_node.children[i] != XPTR_NULL ) dqdt_global_print( local_node.children[i] );
63        }
64    }
65}
66
67////////////////////////////////////
68uint32_t dqdt_init( uint32_t x_size,
69                    uint32_t y_size,
70                    uint32_t y_width )
71{
72    if( (x_size > 32) || (y_size > 32) )
73    {
74        panic("illegal mesh size for DQDT support");
75    }
76
77        dqdt_node_t * node;
78    cxy_t         p_cxy;         // cluster coordinates for parent node
79    cxy_t         c_cxy;         // cluster coordinates for child node
80    uint32_t      level;         // node level in quad tree
81    uint32_t      mask;          // mask on node coordinates to compute existence condition
82    uint32_t      pmask;         // mask to compute parent coordinates from child coordinates
83    cluster_t   * cluster;       // pointer on local cluster
84
85    cluster = LOCAL_CLUSTER;
86
87    // compute level_max
88    uint32_t  x_size_ext = POW2_ROUNDUP( x_size );
89    uint32_t  y_size_ext = POW2_ROUNDUP( y_size );
90    uint32_t  size_ext   = MAX(x_size_ext , y_size_ext);
91    uint32_t  level_max  = (bits_log2(size_ext * size_ext) >> 1) + 1;
92
93    // get cluster coordinates
94    uint32_t    x       = local_cxy >> y_width;
95    uint32_t    y       = local_cxy & ((1<<y_width)-1);
96
97    // loop on local dqdt nodes (at most one node per level)
98    for( level = 0 ; level < level_max ; level++ )
99    {
100        // get pointer on the node to be initialised
101        node = &cluster->dqdt_tbl[level];
102
103        // set default values
104        node->level       = level;
105        node->arity       = 0;
106        node->threads     = 0;
107        node->pages       = 0;
108        node->parent      = XPTR_NULL;
109        node->children[0] = XPTR_NULL;
110        node->children[1] = XPTR_NULL;
111        node->children[2] = XPTR_NULL;
112        node->children[3] = XPTR_NULL;
113
114        // compute masks depending on level : 0x1, 0x3, 0x7, 0xF, 0x1F etc.
115        mask  = (1<<level)-1;
116        pmask = (1<<(level+1))-1;
117
118        // check the node  existence condition at each level
119        if( ((x & mask) == 0) && ((y & mask) == 0) )
120        {
121            // set parent extended pointer
122            p_cxy = ((x & ~pmask)<<y_width) + (y & ~pmask);
123            node->parent = XPTR( p_cxy , &cluster->dqdt_tbl[level+1] );
124
125            // set child[0] extended pointer (same [x,y] coordinates)
126            if ( level > 0 )
127            {
128                c_cxy = local_cxy;
129                node->children[0] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1]);
130                node->arity++;
131            }
132
133            // set child[1] extended pointer (coordinates may overflow)
134            if ( (level > 0) && ((y + (1<<(level-1))) < y_size) )
135            {
136                c_cxy = local_cxy + (1<<(level-1));
137                node->children[1] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1] );
138                node->arity++;
139            }
140
141            // set child[2] extended pointer (coordinates may overflow)
142            if ( (level > 0) && ((x + (1<<(level-1))) < x_size) )
143            {
144                c_cxy = local_cxy + ((1<<(level-1))<<y_width);
145                node->children[2] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1]);
146                node->arity++;
147            }
148
149            // set child[3] extended pointer (coordinates may overflow)
150            if ( (level > 0) && 
151                 ((x + (1<<(level-1))) < x_size) && 
152                 ((y + (1<<(level-1))) < y_size) )
153            {
154                c_cxy = local_cxy + ((1<<(level-1))<<y_width) + (1<<(level-1));
155                node->children[3] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1]);
156                node->arity++;
157            }
158        }  // end if existence condition
159    }  // end for level
160
161    return level_max;
162
163} // end dqdt_init()
164
165
166///////////////////////////////////////////////////////////////////////////
167// This recursive function is called by the dqdt_global_update() function.
168// It traverses the quad tree from clusters to root.
169///////////////////////////////////////////////////////////////////////////
170static void dqdt_propagate( xptr_t  node,         // extended pointer on current node
171                            int32_t threads_var,  // number of threads variation
172                            int32_t pages_var )   // number of pages variation
173{
174    // get current node cluster identifier and local pointer
175    cxy_t         cxy = (cxy_t)GET_CXY( node );
176    dqdt_node_t * ptr = (dqdt_node_t *)GET_PTR( node );
177
178    // update current node threads number
179    hal_remote_atomic_add( XPTR( cxy , &ptr->threads ) , threads_var );
180
181    // update current node pages number
182    hal_remote_atomic_add( XPTR( cxy , &ptr->pages ) , pages_var );
183
184    // get extended pointer on parent node
185    xptr_t parent = (xptr_t)hal_remote_lwd( XPTR( cxy , &ptr->parent ) );
186
187    // propagate if required
188    if ( parent != XPTR_NULL )
189    {
190        dqdt_propagate( parent, threads_var, pages_var );
191    }
192}
193
194/////////////////////////
195void dqdt_global_update()
196{
197        cluster_t   * cluster = LOCAL_CLUSTER;
198    dqdt_node_t * node    = &cluster->dqdt_tbl[0];
199
200    // get variations
201    int32_t      threads_var = cluster->threads_var;
202    int32_t      pages_var   = cluster->pages_var;
203
204    // propagate this variation to DQDT upper levels
205    if( (threads_var || pages_var) && (node->parent != XPTR_NULL) )
206    {
207        dqdt_propagate( node->parent, threads_var, pages_var );
208    }
209
210    // update variations
211    hal_atomic_add( &cluster->threads_var , -threads_var );
212    hal_atomic_add( &cluster->pages_var   , -pages_var   );
213}
214
215///////////////////////////////////////////////////
216void dqdt_local_update_threads( int32_t increment )
217{
218        cluster_t * cluster = LOCAL_CLUSTER;
219
220    // register change for future propagation in DQDT
221    hal_atomic_add( &cluster->threads_var , increment );
222
223    // update DQDT node level 0
224    hal_atomic_add( &cluster->dqdt_tbl[0].threads , increment );
225}
226
227/////////////////////////////////////////////////
228void dqdt_local_update_pages( int32_t increment )
229{
230        cluster_t * cluster = LOCAL_CLUSTER;
231
232    // register change for future propagation in DQDT
233    hal_atomic_add( &cluster->pages_var , increment );
234
235    // update DQDT node level 0
236    hal_atomic_add( &cluster->dqdt_tbl[0].pages , increment );
237}
238
239////////////////////////////////////////////////////////////////////////////////
240// This recursive function is called by both the dqdt_get_cluster_for_process()
241// and by the dqdt_get_cluster_for_memory() functions to select the cluster
242// with smallest number of thread, or smallest number of allocated pages.
243// It traverses the quad tree from root to clusters.
244///////////////////////////////////////////////////////////////////////////////
245static cxy_t dqdt_select_cluster( xptr_t node,
246                                  bool_t for_memory )
247{
248    dqdt_node_t   node_copy;     // local copy of the current DQDT node
249    uint32_t      i;             // index in the loop on children
250    uint32_t      select;        // index of selected child
251    xptr_t        child;         // extended pointer on a DQDT child node
252    cxy_t         cxy;           // DQDT child node cluster identifier
253    dqdt_node_t * ptr;           // pointer on a DQDT child node
254    uint32_t      load;          // load of the child (threads or pages)
255    uint32_t      load_min;      // current value of the minimal load
256
257    // get DQDT node local copy
258    hal_remote_memcpy( XPTR( local_cxy , &node_copy ), node , sizeof(dqdt_node_t) );
259
260    // return cluster identifier for a terminal mode
261    if( node_copy.level == 0 ) return GET_CXY(node);
262
263    // analyse load for all children in non terminal node
264    load_min = 0xFFFFFFFF;
265    select   = 0;
266    for( i = 0 ; i < 4 ; i++ )
267    {
268        child = node_copy.children[i];
269        if( child != XPTR_NULL )
270        {
271            cxy  = (cxy_t)GET_CXY( child );
272            ptr  = (dqdt_node_t *)GET_PTR( child );
273            if( for_memory ) load = hal_remote_lw( XPTR( cxy , &ptr->pages ) );
274            else             load = hal_remote_lw( XPTR( cxy , &ptr->threads ) );
275            if( load < load_min )
276            {
277                load_min = load;
278                select   = i;
279            }
280        }
281    }
282
283    // select the child with the lowest load
284    return dqdt_select_cluster( node_copy.children[select], for_memory );
285}
286
287////////////////////////////////////
288cxy_t dqdt_get_cluster_for_process()
289{
290    // build extended pointer on DQDT root node
291        cluster_t * cluster = LOCAL_CLUSTER;
292    uint32_t    level   = cluster->dqdt_root_level;
293    xptr_t      root    = XPTR( 0 , &cluster->dqdt_tbl[level] );
294
295    // call recursive function
296    return dqdt_select_cluster( root , false );
297}
298
299////////////////////////////////////
300cxy_t dqdt_get_cluster_for_memory()
301{
302    // build extended pointer on DQDT root node
303        cluster_t * cluster = LOCAL_CLUSTER;
304    uint32_t    level   = cluster->dqdt_root_level;
305    xptr_t      root    = XPTR( 0 , &cluster->dqdt_tbl[level] );
306
307    // call recursive function
308    return dqdt_select_cluster( root , true );
309}
310
Note: See TracBrowser for help on using the repository browser.