MCPcopy Create free account
hub / github.com/NVIDIA/cuda-samples / run_binary_search

Function run_binary_search

python/2_CoreConcepts/binarySearch/binarySearch.py:64–104  ·  view source on GitHub ↗
(h_data: np.ndarray, h_values: np.ndarray)

Source from the content-addressed store, hash-verified

62
63
64def run_binary_search(h_data: np.ndarray, h_values: np.ndarray) -> bool:
65 d_data = cp.asarray(h_data)
66 d_values = cp.asarray(h_values)
67
68 d_lb = cp.empty(len(h_values), dtype=np.uintp)
69 d_ub = cp.empty(len(h_values), dtype=np.uintp)
70
71 cuda.compute.lower_bound(
72 d_data=d_data,
73 num_items=len(d_data),
74 d_values=d_values,
75 num_values=len(d_values),
76 d_out=d_lb,
77 )
78 cuda.compute.upper_bound(
79 d_data=d_data,
80 num_items=len(d_data),
81 d_values=d_values,
82 num_values=len(d_values),
83 d_out=d_ub,
84 )
85
86 got_lb = cp.asnumpy(d_lb)
87 got_ub = cp.asnumpy(d_ub)
88 expected_lb = np.searchsorted(h_data, h_values, side="left").astype(np.uintp)
89 expected_ub = np.searchsorted(h_data, h_values, side="right").astype(np.uintp)
90
91 ok_lb = np.array_equal(got_lb, expected_lb)
92 ok_ub = np.array_equal(got_ub, expected_ub)
93
94 print(f" data = {h_data.tolist()}")
95 print(f" values = {h_values.tolist()}")
96 print(
97 f" lower_bound: got {got_lb.tolist()} "
98 f"expected {expected_lb.tolist()} {'OK' if ok_lb else 'FAIL'}"
99 )
100 print(
101 f" upper_bound: got {got_ub.tolist()} "
102 f"expected {expected_ub.tolist()} {'OK' if ok_ub else 'FAIL'}"
103 )
104 return ok_lb and ok_ub
105
106
107def main():

Callers 1

mainFunction · 0.85

Calls 1

emptyMethod · 0.45

Tested by

no test coverage detected