[1] | 1 | /* |
---|
| 2 | * dqdt.c - Distributed Quaternary Decision Tree implementation. |
---|
[19] | 3 | * |
---|
[437] | 4 | * Author : Alain Greiner (2016,2017,2018) |
---|
[1] | 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 | |
---|
[14] | 24 | #include <kernel_config.h> |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[1] | 26 | #include <hal_special.h> |
---|
| 27 | #include <hal_atomic.h> |
---|
| 28 | #include <hal_remote.h> |
---|
| 29 | #include <printk.h> |
---|
[438] | 30 | #include <chdev.h> |
---|
[1] | 31 | #include <cluster.h> |
---|
| 32 | #include <bits.h> |
---|
| 33 | #include <dqdt.h> |
---|
| 34 | |
---|
| 35 | |
---|
[438] | 36 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 37 | // Extern variables |
---|
| 38 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 39 | |
---|
[438] | 40 | extern 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 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 46 | static void dqdt_recursive_print( xptr_t node_xp ) |
---|
[1] | 47 | { |
---|
| 48 | uint32_t i; |
---|
[438] | 49 | dqdt_node_t node; |
---|
[1] | 50 | |
---|
[438] | 51 | // get node local copy |
---|
| 52 | hal_remote_memcpy( XPTR( local_cxy , &node ), node_xp , sizeof(dqdt_node_t) ); |
---|
[1] | 53 | |
---|
[438] | 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 ); |
---|
[1] | 57 | |
---|
| 58 | // recursive call on children if node is not terminal |
---|
[438] | 59 | if ( node.level > 0 ) |
---|
[1] | 60 | { |
---|
| 61 | for ( i = 0 ; i < 4 ; i++ ) |
---|
| 62 | { |
---|
[438] | 63 | if ( node.children[i] != XPTR_NULL ) dqdt_recursive_print( node.children[i] ); |
---|
[1] | 64 | } |
---|
| 65 | } |
---|
[19] | 66 | } |
---|
| 67 | |
---|
[438] | 68 | /////////////////// |
---|
[485] | 69 | void dqdt_display( void ) |
---|
[438] | 70 | { |
---|
[562] | 71 | /* |
---|
[438] | 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 ); |
---|
[562] | 98 | */ |
---|
[438] | 99 | } |
---|
| 100 | |
---|
[1] | 101 | //////////////////////////////////// |
---|
| 102 | uint32_t dqdt_init( uint32_t x_size, |
---|
| 103 | uint32_t y_size, |
---|
| 104 | uint32_t y_width ) |
---|
| 105 | { |
---|
[492] | 106 | assert( ((x_size <= 32) && (y_size <= 32)) , "illegal mesh size\n"); |
---|
[1] | 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 | |
---|
[562] | 124 | return level_max; |
---|
| 125 | |
---|
| 126 | /* |
---|
[1] | 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; |
---|
[19] | 147 | |
---|
[1] | 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 |
---|
[286] | 153 | if( ((x & mask) == 0) && ((y & mask) == 0) ) |
---|
[1] | 154 | { |
---|
[19] | 155 | // set parent extended pointer |
---|
[1] | 156 | p_cxy = ((x & ~pmask)<<y_width) + (y & ~pmask); |
---|
| 157 | node->parent = XPTR( p_cxy , &cluster->dqdt_tbl[level+1] ); |
---|
| 158 | |
---|
[19] | 159 | // set child[0] extended pointer (same [x,y] coordinates) |
---|
[1] | 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) |
---|
[23] | 184 | if ( (level > 0) && |
---|
| 185 | ((x + (1<<(level-1))) < x_size) && |
---|
| 186 | ((y + (1<<(level-1))) < y_size) ) |
---|
[1] | 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; |
---|
[562] | 196 | */ |
---|
[1] | 197 | |
---|
| 198 | } // end dqdt_init() |
---|
| 199 | |
---|
| 200 | /////////////////////////////////////////////////////////////////////////// |
---|
[438] | 201 | // This recursive function is called by the dqdt_update_threads() function. |
---|
[1] | 202 | // It traverses the quad tree from clusters to root. |
---|
| 203 | /////////////////////////////////////////////////////////////////////////// |
---|
[438] | 204 | // @ node : extended pointer on current node |
---|
| 205 | // @ increment : number of threads variation |
---|
| 206 | /////////////////////////////////////////////////////////////////////////// |
---|
| 207 | static void dqdt_propagate_threads( xptr_t node, |
---|
| 208 | int32_t increment ) |
---|
[1] | 209 | { |
---|
| 210 | // get current node cluster identifier and local pointer |
---|
[438] | 211 | cxy_t cxy = GET_CXY( node ); |
---|
| 212 | dqdt_node_t * ptr = GET_PTR( node ); |
---|
[1] | 213 | |
---|
| 214 | // update current node threads number |
---|
[438] | 215 | hal_remote_atomic_add( XPTR( cxy , &ptr->threads ) , increment ); |
---|
[1] | 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 |
---|
[438] | 221 | if ( parent != XPTR_NULL ) dqdt_propagate_threads( parent, increment ); |
---|
[1] | 222 | } |
---|
| 223 | |
---|
[438] | 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 | /////////////////////////////////////////////////////////////////////////// |
---|
| 231 | static void dqdt_propagate_pages( xptr_t node, |
---|
| 232 | int32_t increment ) |
---|
[1] | 233 | { |
---|
[438] | 234 | // get current node cluster identifier and local pointer |
---|
| 235 | cxy_t cxy = GET_CXY( node ); |
---|
| 236 | dqdt_node_t * ptr = GET_PTR( node ); |
---|
[1] | 237 | |
---|
[438] | 238 | // update current node threads number |
---|
| 239 | hal_remote_atomic_add( XPTR( cxy , &ptr->pages ) , increment ); |
---|
[1] | 240 | |
---|
[438] | 241 | // get extended pointer on parent node |
---|
| 242 | xptr_t parent = (xptr_t)hal_remote_lwd( XPTR( cxy , &ptr->parent ) ); |
---|
[1] | 243 | |
---|
[438] | 244 | // propagate if required |
---|
| 245 | if ( parent != XPTR_NULL ) dqdt_propagate_pages( parent, increment ); |
---|
[1] | 246 | } |
---|
| 247 | |
---|
[438] | 248 | ///////////////////////////////////////////// |
---|
| 249 | void dqdt_update_threads( int32_t increment ) |
---|
[1] | 250 | { |
---|
[562] | 251 | return; |
---|
| 252 | /* |
---|
[438] | 253 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
| 254 | dqdt_node_t * node = &cluster->dqdt_tbl[0]; |
---|
[19] | 255 | |
---|
[438] | 256 | // update DQDT node level 0 |
---|
| 257 | hal_atomic_add( &node->threads , increment ); |
---|
[1] | 258 | |
---|
[438] | 259 | // propagate to DQDT upper levels |
---|
| 260 | if( node->parent != XPTR_NULL ) dqdt_propagate_threads( node->parent , increment ); |
---|
[562] | 261 | */ |
---|
[1] | 262 | } |
---|
| 263 | |
---|
[438] | 264 | /////////////////////////////////////////// |
---|
| 265 | void dqdt_update_pages( int32_t increment ) |
---|
[1] | 266 | { |
---|
[562] | 267 | return; |
---|
| 268 | /* |
---|
[438] | 269 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
| 270 | dqdt_node_t * node = &cluster->dqdt_tbl[0]; |
---|
[19] | 271 | |
---|
[438] | 272 | // update DQDT node level 0 |
---|
| 273 | hal_atomic_add( &node->pages , increment ); |
---|
[1] | 274 | |
---|
[438] | 275 | // propagate to DQDT upper levels |
---|
| 276 | if( node->parent != XPTR_NULL ) dqdt_propagate_pages( node->parent , increment ); |
---|
[562] | 277 | */ |
---|
[1] | 278 | } |
---|
| 279 | |
---|
[438] | 280 | |
---|
[1] | 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 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 287 | static 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; |
---|
[5] | 307 | select = 0; |
---|
[1] | 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 ); |
---|
[19] | 314 | ptr = (dqdt_node_t *)GET_PTR( child ); |
---|
[1] | 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; |
---|
[19] | 320 | select = i; |
---|
| 321 | } |
---|
[1] | 322 | } |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | // select the child with the lowest load |
---|
[19] | 326 | return dqdt_select_cluster( node_copy.children[select], for_memory ); |
---|
[1] | 327 | } |
---|
| 328 | |
---|
| 329 | //////////////////////////////////// |
---|
[485] | 330 | cxy_t dqdt_get_cluster_for_process( void ) |
---|
[1] | 331 | { |
---|
[562] | 332 | return cluster_random_select(); |
---|
| 333 | /* |
---|
[1] | 334 | // build extended pointer on DQDT root node |
---|
| 335 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
| 336 | uint32_t level = cluster->dqdt_root_level; |
---|
[438] | 337 | xptr_t root_xp = XPTR( 0 , &cluster->dqdt_tbl[level] ); |
---|
[1] | 338 | |
---|
| 339 | // call recursive function |
---|
[438] | 340 | return dqdt_select_cluster( root_xp , false ); |
---|
[562] | 341 | */ |
---|
[1] | 342 | } |
---|
| 343 | |
---|
| 344 | //////////////////////////////////// |
---|
[485] | 345 | cxy_t dqdt_get_cluster_for_memory( void ) |
---|
[1] | 346 | { |
---|
[562] | 347 | return cluster_random_select(); |
---|
| 348 | /* |
---|
[1] | 349 | // build extended pointer on DQDT root node |
---|
| 350 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
| 351 | uint32_t level = cluster->dqdt_root_level; |
---|
[438] | 352 | xptr_t root_xp = XPTR( 0 , &cluster->dqdt_tbl[level] ); |
---|
[1] | 353 | |
---|
| 354 | // call recursive function |
---|
[438] | 355 | return dqdt_select_cluster( root_xp , true ); |
---|
[562] | 356 | */ |
---|
[1] | 357 | } |
---|
| 358 | |
---|