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

Last change on this file since 437 was 437, checked in by alain, 4 years ago

Fix various bugs

File size: 10.6 KB
Line 
1/*
2 * dqdt.c - Distributed Quaternary Decision Tree implementation.
3 *
4 * Author : Alain Greiner (2016,2017,2018)
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 ) 
63                dqdt_global_print( local_node.children[i] );
64        }
65    }
66}
67
68////////////////////////////////////
69uint32_t dqdt_init( uint32_t x_size,
70                    uint32_t y_size,
71                    uint32_t y_width )
72{
73    assert( ((x_size <= 32) && (y_size <= 32)) , __FUNCTION__ , "illegal mesh size\n");
74
75        dqdt_node_t * node;
76    cxy_t         p_cxy;         // cluster coordinates for parent node
77    cxy_t         c_cxy;         // cluster coordinates for child node
78    uint32_t      level;         // node level in quad tree
79    uint32_t      mask;          // mask on node coordinates to compute existence condition
80    uint32_t      pmask;         // mask to compute parent coordinates from child coordinates
81    cluster_t   * cluster;       // pointer on local cluster
82
83    cluster = LOCAL_CLUSTER;
84
85    // compute level_max
86    uint32_t  x_size_ext = POW2_ROUNDUP( x_size );
87    uint32_t  y_size_ext = POW2_ROUNDUP( y_size );
88    uint32_t  size_ext   = MAX(x_size_ext , y_size_ext);
89    uint32_t  level_max  = (bits_log2(size_ext * size_ext) >> 1) + 1;
90
91    // get cluster coordinates
92    uint32_t    x       = local_cxy >> y_width;
93    uint32_t    y       = local_cxy & ((1<<y_width)-1);
94
95    // loop on local dqdt nodes (at most one node per level)
96    for( level = 0 ; level < level_max ; level++ )
97    {
98        // get pointer on the node to be initialised
99        node = &cluster->dqdt_tbl[level];
100
101        // set default values
102        node->level       = level;
103        node->arity       = 0;
104        node->threads     = 0;
105        node->pages       = 0;
106        node->parent      = XPTR_NULL;
107        node->children[0] = XPTR_NULL;
108        node->children[1] = XPTR_NULL;
109        node->children[2] = XPTR_NULL;
110        node->children[3] = XPTR_NULL;
111
112        // compute masks depending on level : 0x1, 0x3, 0x7, 0xF, 0x1F etc.
113        mask  = (1<<level)-1;
114        pmask = (1<<(level+1))-1;
115
116        // check the node  existence condition at each level
117        if( ((x & mask) == 0) && ((y & mask) == 0) )
118        {
119            // set parent extended pointer
120            p_cxy = ((x & ~pmask)<<y_width) + (y & ~pmask);
121            node->parent = XPTR( p_cxy , &cluster->dqdt_tbl[level+1] );
122
123            // set child[0] extended pointer (same [x,y] coordinates)
124            if ( level > 0 )
125            {
126                c_cxy = local_cxy;
127                node->children[0] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1]);
128                node->arity++;
129            }
130
131            // set child[1] extended pointer (coordinates may overflow)
132            if ( (level > 0) && ((y + (1<<(level-1))) < y_size) )
133            {
134                c_cxy = local_cxy + (1<<(level-1));
135                node->children[1] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1] );
136                node->arity++;
137            }
138
139            // set child[2] extended pointer (coordinates may overflow)
140            if ( (level > 0) && ((x + (1<<(level-1))) < x_size) )
141            {
142                c_cxy = local_cxy + ((1<<(level-1))<<y_width);
143                node->children[2] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1]);
144                node->arity++;
145            }
146
147            // set child[3] extended pointer (coordinates may overflow)
148            if ( (level > 0) && 
149                 ((x + (1<<(level-1))) < x_size) && 
150                 ((y + (1<<(level-1))) < y_size) )
151            {
152                c_cxy = local_cxy + ((1<<(level-1))<<y_width) + (1<<(level-1));
153                node->children[3] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1]);
154                node->arity++;
155            }
156        }  // end if existence condition
157    }  // end for level
158
159    return level_max;
160
161} // end dqdt_init()
162
163
164///////////////////////////////////////////////////////////////////////////
165// This recursive function is called by the dqdt_global_update() function.
166// It traverses the quad tree from clusters to root.
167///////////////////////////////////////////////////////////////////////////
168static void dqdt_propagate( xptr_t  node,         // extended pointer on current node
169                            int32_t threads_var,  // number of threads variation
170                            int32_t pages_var )   // number of pages variation
171{
172    // get current node cluster identifier and local pointer
173    cxy_t         cxy = (cxy_t)GET_CXY( node );
174    dqdt_node_t * ptr = (dqdt_node_t *)GET_PTR( node );
175
176    // update current node threads number
177    hal_remote_atomic_add( XPTR( cxy , &ptr->threads ) , threads_var );
178
179    // update current node pages number
180    hal_remote_atomic_add( XPTR( cxy , &ptr->pages ) , pages_var );
181
182    // get extended pointer on parent node
183    xptr_t parent = (xptr_t)hal_remote_lwd( XPTR( cxy , &ptr->parent ) );
184
185    // propagate if required
186    if ( parent != XPTR_NULL )
187    {
188        dqdt_propagate( parent, threads_var, pages_var );
189    }
190}
191
192/////////////////////////
193void dqdt_global_update()
194{
195        cluster_t   * cluster = LOCAL_CLUSTER;
196    dqdt_node_t * node    = &cluster->dqdt_tbl[0];
197
198    // get variations
199    int32_t      threads_var = cluster->threads_var;
200    int32_t      pages_var   = cluster->pages_var;
201
202    // propagate this variation to DQDT upper levels
203    if( (threads_var || pages_var) && (node->parent != XPTR_NULL) )
204    {
205        dqdt_propagate( node->parent, threads_var, pages_var );
206    }
207
208    // update variations
209    hal_atomic_add( &cluster->threads_var , -threads_var );
210    hal_atomic_add( &cluster->pages_var   , -pages_var   );
211}
212
213///////////////////////////////////////////////////
214void dqdt_local_update_threads( int32_t increment )
215{
216        cluster_t * cluster = LOCAL_CLUSTER;
217
218    // register change for future propagation in DQDT
219    hal_atomic_add( &cluster->threads_var , increment );
220
221    // update DQDT node level 0
222    hal_atomic_add( &cluster->dqdt_tbl[0].threads , increment );
223}
224
225/////////////////////////////////////////////////
226void dqdt_local_update_pages( int32_t increment )
227{
228        cluster_t * cluster = LOCAL_CLUSTER;
229
230    // register change for future propagation in DQDT
231    hal_atomic_add( &cluster->pages_var , increment );
232
233    // update DQDT node level 0
234    hal_atomic_add( &cluster->dqdt_tbl[0].pages , increment );
235}
236
237////////////////////////////////////////////////////////////////////////////////
238// This recursive function is called by both the dqdt_get_cluster_for_process()
239// and by the dqdt_get_cluster_for_memory() functions to select the cluster
240// with smallest number of thread, or smallest number of allocated pages.
241// It traverses the quad tree from root to clusters.
242///////////////////////////////////////////////////////////////////////////////
243static cxy_t dqdt_select_cluster( xptr_t node,
244                                  bool_t for_memory )
245{
246    dqdt_node_t   node_copy;     // local copy of the current DQDT node
247    uint32_t      i;             // index in the loop on children
248    uint32_t      select;        // index of selected child
249    xptr_t        child;         // extended pointer on a DQDT child node
250    cxy_t         cxy;           // DQDT child node cluster identifier
251    dqdt_node_t * ptr;           // pointer on a DQDT child node
252    uint32_t      load;          // load of the child (threads or pages)
253    uint32_t      load_min;      // current value of the minimal load
254
255    // get DQDT node local copy
256    hal_remote_memcpy( XPTR( local_cxy , &node_copy ), node , sizeof(dqdt_node_t) );
257
258    // return cluster identifier for a terminal mode
259    if( node_copy.level == 0 ) return GET_CXY(node);
260
261    // analyse load for all children in non terminal node
262    load_min = 0xFFFFFFFF;
263    select   = 0;
264    for( i = 0 ; i < 4 ; i++ )
265    {
266        child = node_copy.children[i];
267        if( child != XPTR_NULL )
268        {
269            cxy  = (cxy_t)GET_CXY( child );
270            ptr  = (dqdt_node_t *)GET_PTR( child );
271            if( for_memory ) load = hal_remote_lw( XPTR( cxy , &ptr->pages ) );
272            else             load = hal_remote_lw( XPTR( cxy , &ptr->threads ) );
273            if( load < load_min )
274            {
275                load_min = load;
276                select   = i;
277            }
278        }
279    }
280
281    // select the child with the lowest load
282    return dqdt_select_cluster( node_copy.children[select], for_memory );
283}
284
285////////////////////////////////////
286cxy_t dqdt_get_cluster_for_process()
287{
288    // build extended pointer on DQDT root node
289        cluster_t * cluster = LOCAL_CLUSTER;
290    uint32_t    level   = cluster->dqdt_root_level;
291    xptr_t      root    = XPTR( 0 , &cluster->dqdt_tbl[level] );
292
293    // call recursive function
294    return dqdt_select_cluster( root , false );
295}
296
297////////////////////////////////////
298cxy_t dqdt_get_cluster_for_memory()
299{
300    // build extended pointer on DQDT root node
301        cluster_t * cluster = LOCAL_CLUSTER;
302    uint32_t    level   = cluster->dqdt_root_level;
303    xptr_t      root    = XPTR( 0 , &cluster->dqdt_tbl[level] );
304
305    // call recursive function
306    return dqdt_select_cluster( root , true );
307}
308
Note: See TracBrowser for help on using the repository browser.