@@ -373,3 +373,65 @@ def kernel(out):
373373 out = cuda .to_device (np .zeros (len (arrays ), dtype = np .float64 ))
374374 kernel [1 , 1 ](out )
375375 self .assertPreciseEqual (expected , out .copy_to_host ())
376+
377+ def test_count_nonzero_basic (self ):
378+ cases = (
379+ np .int64 ([1 , 0 , 2 , 0 , 3 ]),
380+ np .int64 ([0 , 0 , 0 , 0 ]),
381+ np .int64 ([1 , 2 , 3 , 4 ]),
382+ np .array (0 ),
383+ np .float64 ([]),
384+ np .float64 ([0.0 , - 0.0 , 1.5 , 0.0 ]),
385+ np .float64 ([1.0 , 2.0 , 0.0 , - 0.0 , 1.0 , - 1.5 ]),
386+ np .float64 ([np .nan , 0.0 , np .inf , - np .inf , 0.0 ]),
387+ )
388+
389+ @cuda .jit
390+ def kernel (out ):
391+ i = 0
392+ for case in literal_unroll (cases ):
393+ out [i ] = np .count_nonzero (case )
394+ i += 1
395+
396+ expected = np .array ([np .count_nonzero (a ) for a in cases ], dtype = np .intp )
397+ out = cuda .to_device (np .zeros (len (cases ), dtype = np .intp ))
398+ kernel [1 , 1 ](out )
399+ self .assertPreciseEqual (expected , out .copy_to_host ())
400+
401+ def test_count_nonzero_bool (self ):
402+ cases = (
403+ np .array ([True , False , True , False , True ]),
404+ np .array ([False , False , False ]),
405+ np .array ([True , True , True ]),
406+ )
407+
408+ @cuda .jit
409+ def kernel (out ):
410+ i = 0
411+ for case in literal_unroll (cases ):
412+ out [i ] = np .count_nonzero (case )
413+ i += 1
414+
415+ expected = np .array ([np .count_nonzero (a ) for a in cases ], dtype = np .intp )
416+ out = cuda .to_device (np .zeros (len (cases ), dtype = np .intp ))
417+ kernel [1 , 1 ](out )
418+ self .assertPreciseEqual (expected , out .copy_to_host ())
419+
420+ def test_count_nonzero_2d (self ):
421+ cases = (
422+ np .int64 ([[1 , 0 , 2 ], [0 , 3 , 0 ]]),
423+ np .int64 ([[0 , 0 ], [0 , 0 ]]),
424+ np .float64 ([[1.0 , 0.0 ], [- 0.0 , 2.5 ]]),
425+ )
426+
427+ @cuda .jit
428+ def kernel (out ):
429+ i = 0
430+ for case in literal_unroll (cases ):
431+ out [i ] = np .count_nonzero (case )
432+ i += 1
433+
434+ expected = np .array ([np .count_nonzero (a ) for a in cases ], dtype = np .intp )
435+ out = cuda .to_device (np .zeros (len (cases ), dtype = np .intp ))
436+ kernel [1 , 1 ](out )
437+ self .assertPreciseEqual (expected , out .copy_to_host ())
0 commit comments