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

[Metal] Changes to enable bitmasked on Metal! #661

Merged
merged 1 commit into from
Mar 27, 2020
Merged

Conversation

k-ye
Copy link
Member

@k-ye k-ye commented Mar 26, 2020

Related issue id = #593

[Click here for the format server]

Feel like the tests are pretty weak...

@k-ye k-ye requested a review from yuanming-hu March 26, 2020 10:37
@archibate
Copy link
Collaborator

archibate commented Mar 26, 2020

Interesting! Is there any pre-requirements for OpenGL to also support this? Not familiar to what bitmasked mean yet...

@ti.kernel
def sum():
for i, j in x:
ti.atomic_add(c[None], ti.is_active(bm, [i, j]))
Copy link
Collaborator

Choose a reason for hiding this comment

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

So, the difference bitmasked introduced is, only those assigned slots are marked as is_active?

Copy link
Collaborator

Choose a reason for hiding this comment

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

If I'm thinking right, this can be used for performance optimization, like Game of Life - only the non-static cells are calculated?

Copy link
Member Author

Choose a reason for hiding this comment

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

Exactly. For sparse data structures, struct_for will only loop over the active indices (it could still iterate over more slots due to POT paddings, but not all of them).

Copy link
Member

Choose a reason for hiding this comment

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

If I'm thinking right, this can be used for performance optimization, like Game of Life - only the non-static cells are calculated?

Great idea! If you are interested, having a game of life demo with sparse grids would be super cool!!

@k-ye
Copy link
Member Author

k-ye commented Mar 26, 2020

Not familiar to what bitmasked mean yet...

It's the simplest sparse data structure in taichi. Under the hood, it is dense, with each element annotated by 1 bit to indicate its activeness.

if (DenseMeta_get_bitmasked(dmeta)) {
auto element_size = StructMeta_get_element_size(smeta);
auto num_elements = Dense_get_num_elements(meta, node);
auto data_section_size = element_size * num_elements;
auto mask_begin = (uint64 *)(node + data_section_size);
atomic_or_u64(&mask_begin[i / 64], 1UL << (i % 64));
}

Is there any pre-requirements for OpenGL to also support this?

For each bitmasked (or any other sparse) SNode, it is associated with a ListManager stored on the device side. The active indices, along with some other properties, are appended to this list. So if you preallocate the list memory, and use an atomic to simulate the append behavior, then i guess OpenGL can also support it.

  • Metal

int append(device ListManager *list,
thread const T &elem,
device byte *data_addr) {
thread char *elem_ptr = (thread char *)(&elem);
int me = atomic_fetch_add_explicit(
reinterpret_cast<device atomic_int *>(&(list->next)), 1,
metal::memory_order_relaxed);
device byte *ptr =
data_addr + list->mem_begin + (me * list->element_stride);
for (int i = 0; i < list->element_stride; ++i) {
*ptr = *elem_ptr;
++ptr;
++elem_ptr;
}
return me;

  • LLVM

void ListManager::append(void *data_ptr) {
auto ptr = allocate();
std::memcpy(ptr, data_ptr, element_size);
}

If you want to support sparse SNodes on OpenGL, i think https://github.com/taichi-dev/taichi/blob/master/taichi/runtime/llvm/runtime.cpp is a really good start.

Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thanks so much for contributing this!!

Looking at the recurring pattern snode->is_dense && snode->is_bitmasked, I'm wondering if we should promote bitmasked to be a standalone SNodeType, instead of a decorator to dense. We used to have dense.pointer, yet later pointers are made a standalone data structure (thanks to @KLozes). Maybe we should do the same for bitmasked since

  • bitmasked is always used with dense, so maybe we should save the verbosity;
  • dense.bitmasked is too different from dense so bitmasked worths a standalone SNodeType.

@k-ye
Copy link
Member Author

k-ye commented Mar 27, 2020

Maybe we should do the same for bitmasked since

SGTM! Shall I make the change before merging this?

@yuanming-hu
Copy link
Member

That doesn't sound like a small change to me. Let's merge this in first :-)

@k-ye k-ye merged commit e326b39 into taichi-dev:master Mar 27, 2020
@k-ye k-ye deleted the bm branch March 27, 2020 01:43
archibate pushed a commit to archibate/taichi that referenced this pull request Mar 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants