1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
//------------------------------------------------------------------------------
// GB_binary_search.h: binary search in a sorted list
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// These methods are used for all kernels, include CUDA kernels, which is why
// GB_STATIC_INLINE is used. It becomes "static inline" on the CPU, and
// "static __device__ inline" on the GPU.
#ifndef GB_BINARY_SEARCH_H
#define GB_BINARY_SEARCH_H
//------------------------------------------------------------------------------
// GB_trim_binary_search: simple binary search
//------------------------------------------------------------------------------
// search for integer i in the list X [pleft...pright]; no zombies.
// The list X [pleft ... pright] is in ascending order. It may have
// duplicates.
GB_STATIC_INLINE void GB_trim_binary_search_32
(
const uint32_t i, // item to look for
const uint32_t *restrict X, // array to search; no zombies
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright
)
{
// binary search of X [pleft ... pright] for the integer i
while (*pleft < *pright)
{
// Both of following methods work on both the GPU and CPU, but the
// first method works fastest on the GPU, while the 2nd works fastest
// on the CPU.
#ifdef GB_CUDA_KERNEL
// binary search on the GPU
int64_t pmiddle = (*pleft + *pright) >> 1 ;
bool less = (X [pmiddle] < i) ;
*pleft = less ? (pmiddle+1) : *pleft ;
*pright = less ? *pright : pmiddle ;
#else
// binary search on the CPU
int64_t pmiddle = (*pleft + *pright) / 2 ;
if (X [pmiddle] < i)
{
// if in the list, it appears in [pmiddle+1..pright]
*pleft = pmiddle + 1 ;
}
else
{
// if in the list, it appears in [pleft..pmiddle]
*pright = pmiddle ;
}
#endif
}
// binary search is narrowed down to a single item
// or it has found the list is empty
ASSERT (*pleft == *pright || *pleft == *pright + 1) ;
}
GB_STATIC_INLINE void GB_trim_binary_search_64
(
const uint64_t i, // item to look for
const uint64_t *restrict X, // array to search; no zombies
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright
)
{
// binary search of X [pleft ... pright] for the integer i
while (*pleft < *pright)
{
#ifdef GB_CUDA_KERNEL
int64_t pmiddle = (*pleft + *pright) >> 1 ;
bool less = (X [pmiddle] < i) ;
*pleft = less ? (pmiddle+1) : *pleft ;
*pright = less ? *pright : pmiddle ;
#else
int64_t pmiddle = (*pleft + *pright) / 2 ;
if (X [pmiddle] < i)
{
// if in the list, it appears in [pmiddle+1..pright]
*pleft = pmiddle + 1 ;
}
else
{
// if in the list, it appears in [pleft..pmiddle]
*pright = pmiddle ;
}
#endif
}
// binary search is narrowed down to a single item
// or it has found the list is empty
ASSERT (*pleft == *pright || *pleft == *pright + 1) ;
}
GB_STATIC_INLINE void GB_trim_binary_search
(
const uint64_t i, // item to look for
const void *X, // array to search; no zombies
const bool X_is_32, // if true, X is 32-bit, else 64
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright
)
{
if (X_is_32)
{
GB_trim_binary_search_32 (i, (const uint32_t *) X, pleft, pright) ;
}
else
{
GB_trim_binary_search_64 (i, (const uint64_t *) X, pleft, pright) ;
}
}
//------------------------------------------------------------------------------
// GB_binary_search: binary search and check if found
//------------------------------------------------------------------------------
// If found is true then X [pleft == pright] == i. If duplicates appear then
// X [pleft] is any one of the entries with value i in the list.
// If found is false then
// X [original_pleft ... pleft-1] < i and
// X [pleft+1 ... original_pright] > i holds.
// The value X [pleft] may be either < or > i.
GB_STATIC_INLINE bool GB_binary_search_32
(
const uint32_t i, // item to look for
const uint32_t *restrict X, // array to search; no zombies
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright
)
{
GB_trim_binary_search_32 (i, X, pleft, pright) ;
return (*pleft == *pright && X [*pleft] == i) ;
}
GB_STATIC_INLINE bool GB_binary_search_64
(
const uint64_t i, // item to look for
const uint64_t *restrict X, // array to search; no zombies
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright
)
{
GB_trim_binary_search_64 (i, X, pleft, pright) ;
return (*pleft == *pright && X [*pleft] == i) ;
}
GB_STATIC_INLINE bool GB_binary_search
(
const uint64_t i, // item to look for
const void *X, // array to search; no zombies
const bool X_is_32, // if true, X is 32-bit, else 64
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright
)
{
if (X_is_32)
{
return (GB_binary_search_32 (i, (const uint32_t *) X, pleft, pright)) ;
}
else
{
return (GB_binary_search_64 (i, (const uint64_t *) X, pleft, pright)) ;
}
}
//------------------------------------------------------------------------------
// GB_split_binary_search: binary search, and then partition the list
//------------------------------------------------------------------------------
// If found is true then X [pleft] == i. If duplicates appear then X [pleft]
// is any one of the entries with value i in the list.
// If found is false then
// X [original_pleft ... pleft-1] < i and
// X [pleft ... original_pright] > i holds, and pleft-1 == pright
// If X has no duplicates, then whether or not i is found,
// X [original_pleft ... pleft-1] < i and
// X [pleft ... original_pright] >= i holds.
GB_STATIC_INLINE bool GB_split_binary_search_32
(
const uint32_t i, // item to look for
const uint32_t *restrict X, // array to search; no zombies
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright
)
{
bool found = GB_binary_search_32 (i, X, pleft, pright) ;
if (!found && (*pleft == *pright))
{
if (i > X [*pleft])
{
(*pleft)++ ;
}
else
{
(*pright)++ ;
}
}
return (found) ;
}
GB_STATIC_INLINE bool GB_split_binary_search_64
(
const uint64_t i, // item to look for
const uint64_t *restrict X, // array to search; no zombies
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright
)
{
bool found = GB_binary_search_64 (i, X, pleft, pright) ;
if (!found && (*pleft == *pright))
{
if (i > X [*pleft])
{
(*pleft)++ ;
}
else
{
(*pright)++ ;
}
}
return (found) ;
}
GB_STATIC_INLINE bool GB_split_binary_search
(
const uint64_t i, // item to look for
const void *X, // array to search; no zombies
const bool X_is_32, // if true, X is 32-bit, else 64
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright
)
{
if (X_is_32)
{
return (GB_split_binary_search_32 (i, (const uint32_t *) X,
pleft, pright)) ;
}
else
{
return (GB_split_binary_search_64 (i, (const uint64_t *) X,
pleft, pright)) ;
}
}
//------------------------------------------------------------------------------
// GB_trim_binary_search_zombie: binary search in the presence of zombies
//------------------------------------------------------------------------------
GB_STATIC_INLINE void GB_trim_binary_search_zombie_32
(
const uint32_t i, // item to look for
const int32_t *restrict X, // array to search; with zombies
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright
)
{
// binary search of X [pleft ... pright] for the integer i
while (*pleft < *pright)
{
#ifdef GB_CUDA_KERNEL
int64_t pmiddle = (*pleft + *pright) >> 1 ;
int64_t ix = X [pmiddle] ;
ix = GB_UNZOMBIE (ix) ;
bool less = (ix < i) ;
*pleft = less ? (pmiddle+1) : *pleft ;
*pright = less ? *pright : pmiddle ;
#else
int64_t pmiddle = (*pleft + *pright) / 2 ;
int64_t ix = X [pmiddle] ;
ix = GB_UNZOMBIE (ix) ;
if (ix < i)
{
// if in the list, it appears in [pmiddle+1..pright]
*pleft = pmiddle + 1 ;
}
else
{
// if in the list, it appears in [pleft..pmiddle]
*pright = pmiddle ;
}
#endif
}
// binary search is narrowed down to a single item
// or it has found the list is empty
ASSERT (*pleft == *pright || *pleft == *pright + 1) ;
}
GB_STATIC_INLINE void GB_trim_binary_search_zombie_64
(
const uint64_t i, // item to look for
const int64_t *restrict X, // array to search; with zombies
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright
)
{
// binary search of X [pleft ... pright] for the integer i
while (*pleft < *pright)
{
#ifdef GB_CUDA_KERNEL
int64_t pmiddle = (*pleft + *pright) >> 1 ;
int64_t ix = X [pmiddle] ;
ix = GB_UNZOMBIE (ix) ;
bool less = (ix < i) ;
*pleft = less ? (pmiddle+1) : *pleft ;
*pright = less ? *pright : pmiddle ;
#else
int64_t pmiddle = (*pleft + *pright) / 2 ;
int64_t ix = X [pmiddle] ;
ix = GB_UNZOMBIE (ix) ;
if (ix < i)
{
// if in the list, it appears in [pmiddle+1..pright]
*pleft = pmiddle + 1 ;
}
else
{
// if in the list, it appears in [pleft..pmiddle]
*pright = pmiddle ;
}
#endif
}
// binary search is narrowed down to a single item
// or it has found the list is empty
ASSERT (*pleft == *pright || *pleft == *pright + 1) ;
}
//------------------------------------------------------------------------------
// GB_binary_search_zombie: binary search with zombies; check if found
//------------------------------------------------------------------------------
GB_STATIC_INLINE bool GB_binary_search_zombie_32
(
const uint32_t i, // item to look for
const int32_t *restrict X, // array to search; with zombies
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright,
const bool may_see_zombies,
bool *is_zombie
)
{
bool found = false ;
*is_zombie = false ;
if (may_see_zombies)
{
GB_trim_binary_search_zombie_32 (i, X, pleft, pright) ;
if (*pleft == *pright)
{
int64_t i2 = X [*pleft] ;
*is_zombie = GB_IS_ZOMBIE (i2) ;
if (*is_zombie) i2 = GB_DEZOMBIE (i2) ;
found = (i == i2) ;
}
}
else
{
found = GB_binary_search_32 (i, (const uint32_t *) X, pleft, pright) ;
}
return (found) ;
}
GB_STATIC_INLINE bool GB_binary_search_zombie_64
(
const uint64_t i, // item to look for
const int64_t *restrict X, // array to search; with zombies
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright,
const bool may_see_zombies,
bool *is_zombie
)
{
bool found = false ;
*is_zombie = false ;
if (may_see_zombies)
{
GB_trim_binary_search_zombie_64 (i, X, pleft, pright) ;
if (*pleft == *pright)
{
int64_t i2 = X [*pleft] ;
*is_zombie = GB_IS_ZOMBIE (i2) ;
if (*is_zombie) i2 = GB_DEZOMBIE (i2) ;
found = (i == i2) ;
}
}
else
{
found = GB_binary_search_64 (i, (const uint64_t *) X, pleft, pright) ;
}
return (found) ;
}
GB_STATIC_INLINE bool GB_binary_search_zombie
(
const uint64_t i, // item to look for
const void *X, // array to search; with zombies
const bool X_is_32, // if true, X is 32-bit, else 64
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright,
const bool may_see_zombies,
bool *is_zombie
)
{
if (X_is_32)
{
return (GB_binary_search_zombie_32 (i, (const int32_t *) X,
pleft, pright, may_see_zombies, is_zombie)) ;
}
else
{
return (GB_binary_search_zombie_64 (i, (const int64_t *) X,
pleft, pright, may_see_zombies, is_zombie)) ;
}
}
//------------------------------------------------------------------------------
// GB_split_binary_search_zombie: binary search with zombies; then partition
//------------------------------------------------------------------------------
GB_STATIC_INLINE bool GB_split_binary_search_zombie_32
(
const uint32_t i, // item to look for
const int32_t *restrict X, // array to search; with zombies
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright,
const bool may_see_zombies,
bool *is_zombie
)
{
bool found = false ;
*is_zombie = false ;
if (may_see_zombies)
{
GB_trim_binary_search_zombie_32 (i, X, pleft, pright) ;
if (*pleft == *pright)
{
int64_t i2 = X [*pleft] ;
*is_zombie = GB_IS_ZOMBIE (i2) ;
if (*is_zombie) i2 = GB_DEZOMBIE (i2) ;
found = (i == i2) ;
if (!found)
{
if (i > i2)
{
(*pleft)++ ;
}
else
{
(*pright)++ ;
}
}
}
}
else
{
found = GB_split_binary_search_32 (i, (const uint32_t *) X,
pleft, pright) ;
}
return (found) ;
}
GB_STATIC_INLINE bool GB_split_binary_search_zombie_64
(
const uint64_t i, // item to look for
const int64_t *restrict X, // array to search; with zombies
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright,
const bool may_see_zombies,
bool *is_zombie
)
{
bool found = false ;
*is_zombie = false ;
if (may_see_zombies)
{
GB_trim_binary_search_zombie_64 (i, X, pleft, pright) ;
if (*pleft == *pright)
{
int64_t i2 = X [*pleft] ;
*is_zombie = GB_IS_ZOMBIE (i2) ;
if (*is_zombie) i2 = GB_DEZOMBIE (i2) ;
found = (i == i2) ;
if (!found)
{
if (i > i2)
{
(*pleft)++ ;
}
else
{
(*pright)++ ;
}
}
}
}
else
{
found = GB_split_binary_search_64 (i, (const uint64_t *) X,
pleft, pright) ;
}
return (found) ;
}
GB_STATIC_INLINE bool GB_split_binary_search_zombie
(
const uint64_t i, // item to look for
const void *X, // array to search; with zombies
const bool X_is_32, // if true, X is 32-bit, else 64
int64_t *pleft, // look in X [pleft:pright]
int64_t *pright,
const bool may_see_zombies,
bool *is_zombie
)
{
if (X_is_32)
{
return (GB_split_binary_search_zombie_32 (i, (const int32_t *) X,
pleft, pright, may_see_zombies, is_zombie)) ;
}
else
{
return (GB_split_binary_search_zombie_64 (i, (const int64_t *) X,
pleft, pright, may_see_zombies, is_zombie)) ;
}
}
#endif
|