This implementation is straightforward and intuitive but performs poorly,
because the same matrix elements will be loaded multiple times from device
memory, which is slow (some devices may have transparent data caches, but
they may not be large enough to hold the entire inputs at once).

It will be faster if we use a blocked algorithm to reduce accesses to the
device memory. HSA provides a fast shared memory
for workitems in a group to cooperately compute on a task. The following
implements a faster version of the square matrix multiplication using shared
memory:

Because the shared memory is a limited resources, the code preloads small
block at a time from the input arrays. Then, it calls
barrier() to wait until all threads have finished
preloading and before doing the computation on the shared memory.
It synchronizes again after the computation to ensure all threads
have finished with the data in shared memory before overwriting it
in the next loop iteration.