Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add bool type #54

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ enum KokkosViewDataType {
Uint64,
Float32,
Float64,
Bool,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this Bool entity need to actually exist under the hood??

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not for the enum, this basically is just a definition

ViewDataTypesEnd
};

Expand Down
1 change: 1 addition & 0 deletions include/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ VIEW_DATA_TYPE(uint32_t, Uint32, "uint32", "unsigned", "unsigned_int")
VIEW_DATA_TYPE(uint64_t, Uint64, "uint64", "unsigned_long")
VIEW_DATA_TYPE(float, Float32, "float32", "float")
VIEW_DATA_TYPE(double, Float64, "float64", "double")
VIEW_DATA_TYPE(bool, Bool, "bool", "bool_")

//----------------------------------------------------------------------------//
// <data-type> <enum> <string identifiers>
Expand Down
1 change: 1 addition & 0 deletions kokkos/__init__.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ try:
"unsigned_long",
"float",
"double",
"bool",
"Serial", # devices
"Threads",
"OpenMP",
Expand Down
94 changes: 73 additions & 21 deletions kokkos/test/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ def test_view_access(self):
self.assertEqual(_data[0].create_mirror_view()[_zeros], 0)
self.assertEqual(_data[1].create_mirror_view()[_zeros], 0)
self.assertEqual(_data[0].create_mirror_view()[_idx], 1)
self.assertEqual(_data[1].create_mirror_view()[_idx], 2)
if _kwargs["dtype"] == kokkos.bool:
self.assertEqual(_data[1].create_mirror_view()[_idx], True)
else:
self.assertEqual(_data[1].create_mirror_view()[_idx], 2)
Comment on lines +143 to +146
Copy link
Contributor

@jrmadsen jrmadsen Oct 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if _kwargs["dtype"] == kokkos.bool:
self.assertEqual(_data[1].create_mirror_view()[_idx], True)
else:
self.assertEqual(_data[1].create_mirror_view()[_idx], 2)
value = lambda v : v if _kwargs["dtype"] != kokkos.bool else v > 0
self.assertEqual(_data[1].create_mirror_view()[_idx], value(2))

Just thought this scheme would be easier to maintain + more compact


def test_view_iadd(self):
"""view_iadd"""
Expand Down Expand Up @@ -169,8 +172,13 @@ def test_view_iadd(self):

self.assertEqual(_data[0].create_mirror_view()[_zeros], 0)
self.assertEqual(_data[1].create_mirror_view()[_zeros], 0)
self.assertEqual(_data[0].create_mirror_view()[_idx], 4)
self.assertEqual(_data[1].create_mirror_view()[_idx], 5)
if _kwargs["dtype"] == kokkos.bool:
# positive values are simply True
self.assertEqual(_data[0].create_mirror_view()[_idx], True)
self.assertEqual(_data[1].create_mirror_view()[_idx], True)
else:
self.assertEqual(_data[0].create_mirror_view()[_idx], 4)
self.assertEqual(_data[1].create_mirror_view()[_idx], 5)

def test_view_isub(self):
"""view_isub"""
Expand Down Expand Up @@ -199,8 +207,13 @@ def test_view_isub(self):

self.assertEqual(_data[0].create_mirror_view()[_zeros], 0)
self.assertEqual(_data[1].create_mirror_view()[_zeros], 0)
self.assertEqual(_data[0].create_mirror_view()[_idx], 7)
self.assertEqual(_data[1].create_mirror_view()[_idx], 17)
if _kwargs["dtype"] == kokkos.bool:
# positive values are simply True
self.assertEqual(_data[0].create_mirror_view()[_idx], True)
self.assertEqual(_data[1].create_mirror_view()[_idx], True)
else:
self.assertEqual(_data[0].create_mirror_view()[_idx], 7)
self.assertEqual(_data[1].create_mirror_view()[_idx], 17)

def test_view_imul(self):
"""view_imul"""
Expand Down Expand Up @@ -229,8 +242,13 @@ def test_view_imul(self):

self.assertEqual(_data[0].create_mirror_view()[_zeros], 0)
self.assertEqual(_data[1].create_mirror_view()[_zeros], 0)
self.assertEqual(_data[0].create_mirror_view()[_idx], 3)
self.assertEqual(_data[1].create_mirror_view()[_idx], 6)
if _kwargs["dtype"] == kokkos.bool:
# positive values are simply True
self.assertEqual(_data[0].create_mirror_view()[_idx], True)
self.assertEqual(_data[1].create_mirror_view()[_idx], True)
else:
self.assertEqual(_data[0].create_mirror_view()[_idx], 3)
self.assertEqual(_data[1].create_mirror_view()[_idx], 6)

#
def test_view_create_mirror(self):
Expand Down Expand Up @@ -261,8 +279,12 @@ def test_view_create_mirror(self):

self.assertEqual(_data[0].create_mirror_view()[_zeros], 0)
self.assertEqual(_data[1].create_mirror_view()[_zeros], 0)
self.assertEqual(_data[0].create_mirror_view()[_idx], 3)
self.assertEqual(_data[1].create_mirror_view()[_idx], 6)
if _kwargs["dtype"] == kokkos.bool:
self.assertEqual(_data[0].create_mirror_view()[_idx], True)
self.assertEqual(_data[1].create_mirror_view()[_idx], True)
else:
self.assertEqual(_data[0].create_mirror_view()[_idx], 3)
self.assertEqual(_data[1].create_mirror_view()[_idx], 6)

_mirror_data = [
kokkos.create_mirror(_data[0], copy=True),
Expand All @@ -271,8 +293,12 @@ def test_view_create_mirror(self):

self.assertEqual(_mirror_data[0][_zeros], 0)
self.assertEqual(_mirror_data[1][_zeros], 0)
self.assertEqual(_mirror_data[0][_idx], 3)
self.assertEqual(_mirror_data[1][_idx], 6)
if _kwargs["dtype"] == kokkos.bool:
self.assertEqual(_mirror_data[0][_idx], True)
self.assertEqual(_mirror_data[1][_idx], True)
else:
self.assertEqual(_mirror_data[0][_idx], 3)
self.assertEqual(_mirror_data[1][_idx], 6)

_mirror_data = [
kokkos.create_mirror(_data[0], copy=False),
Expand Down Expand Up @@ -312,8 +338,13 @@ def test_view_create_mirror_view(self):

self.assertEqual(_data[0].create_mirror_view()[_zeros], 0)
self.assertEqual(_data[1].create_mirror_view()[_zeros], 0)
self.assertEqual(_data[0].create_mirror_view()[_idx], 3)
self.assertEqual(_data[1].create_mirror_view()[_idx], 6)
if _kwargs["dtype"] == kokkos.bool:
# positive values are simply True
self.assertEqual(_data[0].create_mirror_view()[_idx], True)
self.assertEqual(_data[1].create_mirror_view()[_idx], True)
else:
self.assertEqual(_data[0].create_mirror_view()[_idx], 3)
self.assertEqual(_data[1].create_mirror_view()[_idx], 6)

_mirror_data = [
kokkos.create_mirror_view(_data[0], copy=True),
Expand All @@ -322,8 +353,13 @@ def test_view_create_mirror_view(self):

self.assertEqual(_mirror_data[0][_zeros], 0)
self.assertEqual(_mirror_data[1][_zeros], 0)
self.assertEqual(_mirror_data[0][_idx], 3)
self.assertEqual(_mirror_data[1][_idx], 6)
if _kwargs["dtype"] == kokkos.bool:
# positive values are simply True
self.assertEqual(_mirror_data[0][_idx], True)
self.assertEqual(_mirror_data[1][_idx], True)
else:
self.assertEqual(_mirror_data[0][_idx], 3)
self.assertEqual(_mirror_data[1][_idx], 6)

_mirror_data = [
kokkos.create_mirror_view(_data[0], copy=False),
Expand All @@ -334,12 +370,18 @@ def test_view_create_mirror_view(self):
self.assertEqual(_mirror_data[1][_zeros], 0)

if kokkos.get_host_accessible(_data[0].space):
self.assertEqual(_mirror_data[0][_idx], 3)
if _kwargs["dtype"] == kokkos.bool:
self.assertEqual(_mirror_data[0][_idx], True)
else:
self.assertEqual(_mirror_data[0][_idx], 3)
else:
self.assertNotEqual(_mirror_data[0][_idx], 3)

if kokkos.get_host_accessible(_data[1].space):
self.assertEqual(_mirror_data[1][_idx], 6)
if _kwargs["dtype"] == kokkos.bool:
self.assertEqual(_mirror_data[1][_idx], True)
else:
self.assertEqual(_mirror_data[1][_idx], 6)
else:
self.assertNotEqual(_mirror_data[1][_idx], 6)

Expand Down Expand Up @@ -374,17 +416,27 @@ def test_view_deep_copy(self):

self.assertEqual(_data[0].create_mirror_view()[_zeros], 0)
self.assertEqual(_data[1].create_mirror_view()[_zeros], 0)
self.assertEqual(_data[0].create_mirror_view()[_idx], 3)
self.assertEqual(_data[1].create_mirror_view()[_idx], 6)
if _kwargs["dtype"] == kokkos.bool:
# positive values are simply True
self.assertEqual(_data[0].create_mirror_view()[_idx], True)
self.assertEqual(_data[1].create_mirror_view()[_idx], True)
else:
self.assertEqual(_data[0].create_mirror_view()[_idx], 3)
self.assertEqual(_data[1].create_mirror_view()[_idx], 6)

_copied_data = conf.generate_variant(_shape, **_kwargs)
kokkos.deep_copy(_copied_data[0], _data[0])
kokkos.deep_copy(_copied_data[1], _data[1])

self.assertEqual(_copied_data[0].create_mirror_view()[_zeros], 0)
self.assertEqual(_copied_data[1].create_mirror_view()[_zeros], 0)
self.assertEqual(_copied_data[0].create_mirror_view()[_idx], 3)
self.assertEqual(_copied_data[1].create_mirror_view()[_idx], 6)
if _kwargs["dtype"] == kokkos.bool:
# positive values are simply True
self.assertEqual(_copied_data[0].create_mirror_view()[_idx], True)
self.assertEqual(_copied_data[1].create_mirror_view()[_idx], True)
else:
self.assertEqual(_copied_data[0].create_mirror_view()[_idx], 3)
self.assertEqual(_copied_data[1].create_mirror_view()[_idx], 6)


# main runner
Expand Down
2 changes: 2 additions & 0 deletions kokkos/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def read_dtype(_dtype):
return lib.float32
elif _dtype == np.float64:
return lib.float64
elif _dtype == np.bool_:
return lib.bool
except ImportError:
pass

Expand Down
2 changes: 1 addition & 1 deletion src/variants/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TARGET_LINK_LIBRARIES(libpykokkos-variants PUBLIC

SET(_types concrete dynamic)
SET(_variants layout memory_trait)
SET(_data_types Int8 Int16 Int32 Int64 Uint8 Uint16 Uint32 Uint64 Float32 Float64)
SET(_data_types Int8 Int16 Int32 Int64 Uint8 Uint16 Uint32 Uint64 Float32 Float64 Bool)

SET(layout_enums Right)
SET(memory_trait_enums Managed)
Expand Down