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

Last change on this file since 562 was 562, checked in by nicolas.van.phan@…, 5 years ago

Disable DQDT and remove y_max FOR GOOD

File size: 12.4 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_kernel_types.h>
26#include <hal_special.h>
27#include <hal_atomic.h>
28#include <hal_remote.h>
29#include <printk.h>
30#include <chdev.h>
31#include <cluster.h>
32#include <bits.h>
33#include <dqdt.h>
34
35
36///////////////////////////////////////////////////////////////////////////////////////////
37//      Extern variables
38///////////////////////////////////////////////////////////////////////////////////////////
39
40extern chdev_directory_t  chdev_dir;  // defined in chdev.h / allocated in kernel_init.c
41
42
43///////////////////////////////////////////////////////////////////////////////////////////
44// This static recursive function traverse the DQDT quad-tree from root to bottom.
45///////////////////////////////////////////////////////////////////////////////////////////
46static void dqdt_recursive_print( xptr_t  node_xp )
47{
48        uint32_t i;
49    dqdt_node_t node;
50
51    // get node local copy
52    hal_remote_memcpy( XPTR( local_cxy , &node ), node_xp , sizeof(dqdt_node_t) );
53
54    // display node content
55        nolock_printk("- level %d in cluster %x (node %x) : threads = %x / pages = %x\n",
56    node.level, GET_CXY( node_xp ), GET_PTR( node_xp ), node.threads, node.pages );
57
58    // recursive call on children if node is not terminal
59    if ( node.level > 0 )
60    {
61        for ( i = 0 ; i < 4 ; i++ )
62        {
63            if ( node.children[i] != XPTR_NULL ) dqdt_recursive_print( node.children[i] );
64        }
65    }
66}
67
68///////////////////
69void dqdt_display( void )
70{
71    /*
72    reg_t   save_sr;
73
74    // build extended pointer on DQDT root node
75        cluster_t * cluster = LOCAL_CLUSTER;
76    uint32_t    level   = cluster->dqdt_root_level;
77    xptr_t      root_xp = XPTR( 0 , &cluster->dqdt_tbl[level] );
78
79    // get pointers on TXT0 chdev
80    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
81    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
82    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
83
84    // get extended pointer on remote TXT0 chdev lock
85    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
86
87    // get TXT0 lock in busy waiting mode
88    remote_spinlock_lock_busy( lock_xp , &save_sr );
89
90    // print header
91    nolock_printk("\n***** DQDT state\n\n");
92
93    // call recursive function
94    dqdt_recursive_print( root_xp );
95
96    // release lock
97    remote_spinlock_unlock_busy( lock_xp , save_sr );
98    */
99}
100
101////////////////////////////////////
102uint32_t dqdt_init( uint32_t x_size,
103                    uint32_t y_size,
104                    uint32_t y_width )
105{
106    assert( ((x_size <= 32) && (y_size <= 32)) , "illegal mesh size\n");
107
108        dqdt_node_t * node;
109    cxy_t         p_cxy;         // cluster coordinates for parent node
110    cxy_t         c_cxy;         // cluster coordinates for child node
111    uint32_t      level;         // node level in quad tree
112    uint32_t      mask;          // mask on node coordinates to compute existence condition
113    uint32_t      pmask;         // mask to compute parent coordinates from child coordinates
114    cluster_t   * cluster;       // pointer on local cluster
115
116    cluster = LOCAL_CLUSTER;
117
118    // compute level_max
119    uint32_t  x_size_ext = POW2_ROUNDUP( x_size );
120    uint32_t  y_size_ext = POW2_ROUNDUP( y_size );
121    uint32_t  size_ext   = MAX(x_size_ext , y_size_ext);
122    uint32_t  level_max  = (bits_log2(size_ext * size_ext) >> 1) + 1;
123
124    return level_max;
125
126    /*
127    // get cluster coordinates
128    uint32_t    x       = local_cxy >> y_width;
129    uint32_t    y       = local_cxy & ((1<<y_width)-1);
130
131    // loop on local dqdt nodes (at most one node per level)
132    for( level = 0 ; level < level_max ; level++ )
133    {
134        // get pointer on the node to be initialised
135        node = &cluster->dqdt_tbl[level];
136
137        // set default values
138        node->level       = level;
139        node->arity       = 0;
140        node->threads     = 0;
141        node->pages       = 0;
142        node->parent      = XPTR_NULL;
143        node->children[0] = XPTR_NULL;
144        node->children[1] = XPTR_NULL;
145        node->children[2] = XPTR_NULL;
146        node->children[3] = XPTR_NULL;
147
148        // compute masks depending on level : 0x1, 0x3, 0x7, 0xF, 0x1F etc.
149        mask  = (1<<level)-1;
150        pmask = (1<<(level+1))-1;
151
152        // check the node  existence condition at each level
153        if( ((x & mask) == 0) && ((y & mask) == 0) )
154        {
155            // set parent extended pointer
156            p_cxy = ((x & ~pmask)<<y_width) + (y & ~pmask);
157            node->parent = XPTR( p_cxy , &cluster->dqdt_tbl[level+1] );
158
159            // set child[0] extended pointer (same [x,y] coordinates)
160            if ( level > 0 )
161            {
162                c_cxy = local_cxy;
163                node->children[0] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1]);
164                node->arity++;
165            }
166
167            // set child[1] extended pointer (coordinates may overflow)
168            if ( (level > 0) && ((y + (1<<(level-1))) < y_size) )
169            {
170                c_cxy = local_cxy + (1<<(level-1));
171                node->children[1] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1] );
172                node->arity++;
173            }
174
175            // set child[2] extended pointer (coordinates may overflow)
176            if ( (level > 0) && ((x + (1<<(level-1))) < x_size) )
177            {
178                c_cxy = local_cxy + ((1<<(level-1))<<y_width);
179                node->children[2] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1]);
180                node->arity++;
181            }
182
183            // set child[3] extended pointer (coordinates may overflow)
184            if ( (level > 0) &&
185                 ((x + (1<<(level-1))) < x_size) &&
186                 ((y + (1<<(level-1))) < y_size) )
187            {
188                c_cxy = local_cxy + ((1<<(level-1))<<y_width) + (1<<(level-1));
189                node->children[3] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1]);
190                node->arity++;
191            }
192        }  // end if existence condition
193    }  // end for level
194
195    return level_max;
196    */
197
198} // end dqdt_init()
199
200///////////////////////////////////////////////////////////////////////////
201// This recursive function is called by the dqdt_update_threads() function.
202// It traverses the quad tree from clusters to root.
203///////////////////////////////////////////////////////////////////////////
204// @ node       : extended pointer on current node
205// @ increment  : number of threads variation
206///////////////////////////////////////////////////////////////////////////
207static void dqdt_propagate_threads( xptr_t  node,
208                                    int32_t increment )
209{
210    // get current node cluster identifier and local pointer
211    cxy_t         cxy = GET_CXY( node );
212    dqdt_node_t * ptr = GET_PTR( node );
213
214    // update current node threads number
215    hal_remote_atomic_add( XPTR( cxy , &ptr->threads ) , increment );
216
217    // get extended pointer on parent node
218    xptr_t parent = (xptr_t)hal_remote_lwd( XPTR( cxy , &ptr->parent ) );
219
220    // propagate if required
221    if ( parent != XPTR_NULL ) dqdt_propagate_threads( parent, increment );
222}
223
224///////////////////////////////////////////////////////////////////////////
225// This recursive function is called by the dqdt_update_pages() function.
226// It traverses the quad tree from clusters to root.
227///////////////////////////////////////////////////////////////////////////
228// @ node       : extended pointer on current node
229// @ increment  : number of pages variation
230///////////////////////////////////////////////////////////////////////////
231static void dqdt_propagate_pages( xptr_t  node,
232                                  int32_t increment )
233{
234    // get current node cluster identifier and local pointer
235    cxy_t         cxy = GET_CXY( node );
236    dqdt_node_t * ptr = GET_PTR( node );
237
238    // update current node threads number
239    hal_remote_atomic_add( XPTR( cxy , &ptr->pages ) , increment );
240
241    // get extended pointer on parent node
242    xptr_t parent = (xptr_t)hal_remote_lwd( XPTR( cxy , &ptr->parent ) );
243
244    // propagate if required
245    if ( parent != XPTR_NULL ) dqdt_propagate_pages( parent, increment );
246}
247
248/////////////////////////////////////////////
249void dqdt_update_threads( int32_t increment )
250{
251    return;
252    /*
253        cluster_t   * cluster = LOCAL_CLUSTER;
254    dqdt_node_t * node    = &cluster->dqdt_tbl[0];
255
256    // update DQDT node level 0
257    hal_atomic_add( &node->threads , increment );
258
259    // propagate to DQDT upper levels
260    if( node->parent != XPTR_NULL ) dqdt_propagate_threads( node->parent , increment );
261    */
262}
263
264///////////////////////////////////////////
265void dqdt_update_pages( int32_t increment )
266{
267    return;
268    /*
269        cluster_t   * cluster = LOCAL_CLUSTER;
270    dqdt_node_t * node    = &cluster->dqdt_tbl[0];
271
272    // update DQDT node level 0
273    hal_atomic_add( &node->pages , increment );
274
275    // propagate to DQDT upper levels
276    if( node->parent != XPTR_NULL ) dqdt_propagate_pages( node->parent , increment );
277    */
278}
279
280
281////////////////////////////////////////////////////////////////////////////////
282// This recursive function is called by both the dqdt_get_cluster_for_process()
283// and by the dqdt_get_cluster_for_memory() functions to select the cluster
284// with smallest number of thread, or smallest number of allocated pages.
285// It traverses the quad tree from root to clusters.
286///////////////////////////////////////////////////////////////////////////////
287static cxy_t dqdt_select_cluster( xptr_t node,
288                                  bool_t for_memory )
289{
290    dqdt_node_t   node_copy;     // local copy of the current DQDT node
291    uint32_t      i;             // index in the loop on children
292    uint32_t      select;        // index of selected child
293    xptr_t        child;         // extended pointer on a DQDT child node
294    cxy_t         cxy;           // DQDT child node cluster identifier
295    dqdt_node_t * ptr;           // pointer on a DQDT child node
296    uint32_t      load;          // load of the child (threads or pages)
297    uint32_t      load_min;      // current value of the minimal load
298
299    // get DQDT node local copy
300    hal_remote_memcpy( XPTR( local_cxy , &node_copy ), node , sizeof(dqdt_node_t) );
301
302    // return cluster identifier for a terminal mode
303    if( node_copy.level == 0 ) return GET_CXY(node);
304
305    // analyse load for all children in non terminal node
306    load_min = 0xFFFFFFFF;
307    select   = 0;
308    for( i = 0 ; i < 4 ; i++ )
309    {
310        child = node_copy.children[i];
311        if( child != XPTR_NULL )
312        {
313            cxy  = (cxy_t)GET_CXY( child );
314            ptr  = (dqdt_node_t *)GET_PTR( child );
315            if( for_memory ) load = hal_remote_lw( XPTR( cxy , &ptr->pages ) );
316            else             load = hal_remote_lw( XPTR( cxy , &ptr->threads ) );
317            if( load < load_min )
318            {
319                load_min = load;
320                select   = i;
321            }
322        }
323    }
324
325    // select the child with the lowest load
326    return dqdt_select_cluster( node_copy.children[select], for_memory );
327}
328
329////////////////////////////////////
330cxy_t dqdt_get_cluster_for_process( void )
331{
332    return cluster_random_select();
333    /*
334    // build extended pointer on DQDT root node
335        cluster_t * cluster = LOCAL_CLUSTER;
336    uint32_t    level   = cluster->dqdt_root_level;
337    xptr_t      root_xp = XPTR( 0 , &cluster->dqdt_tbl[level] );
338
339    // call recursive function
340    return dqdt_select_cluster( root_xp , false );
341    */
342}
343
344////////////////////////////////////
345cxy_t dqdt_get_cluster_for_memory( void )
346{
347    return cluster_random_select();
348    /*
349    // build extended pointer on DQDT root node
350        cluster_t * cluster = LOCAL_CLUSTER;
351    uint32_t    level   = cluster->dqdt_root_level;
352    xptr_t      root_xp = XPTR( 0 , &cluster->dqdt_tbl[level] );
353
354    // call recursive function
355    return dqdt_select_cluster( root_xp , true );
356    */
357}
358
Note: See TracBrowser for help on using the repository browser.