summaryrefslogtreecommitdiff
path: root/libc/src/stdlib/rand_util.cpp
diff options
context:
space:
mode:
authorJoseph Huber <35342157+jhuber6@users.noreply.github.com>2023-10-19 17:01:43 -0400
committerGitHub <noreply@github.com>2023-10-19 17:01:43 -0400
commit630037ede4cec8870764bed27c1289388ac33097 (patch)
tree36ddca7d6c0e42e2aa6f2bba494e8f4f950cfbdb /libc/src/stdlib/rand_util.cpp
parenta39215768b58bb39716820959cedf82c76648770 (diff)
[libc] Partially implement 'rand' for the GPU (#66167)
Summary: This patch partially implements the `rand` function on the GPU. This is partial because the GPU currently doesn't support thread local storage or static initializers. To implement this on the GPU. I use 1/8th of the local / shared memory quota to treak the shared memory as thread local storage. This is done by simply allocating enough storage for each thread in the block and indexing into this based off of the thread id. The downside to this is that it does not initialize `srand` correctly to be `1` as the standard says, it is also wasteful. In the future we should figure out a way to support TLS on the GPU so that this can be completely common and less resource intensive.
Diffstat (limited to 'libc/src/stdlib/rand_util.cpp')
-rw-r--r--libc/src/stdlib/rand_util.cpp6
1 files changed, 6 insertions, 0 deletions
diff --git a/libc/src/stdlib/rand_util.cpp b/libc/src/stdlib/rand_util.cpp
index fc2cd304e77d..1f3dbce9c786 100644
--- a/libc/src/stdlib/rand_util.cpp
+++ b/libc/src/stdlib/rand_util.cpp
@@ -11,8 +11,14 @@
namespace LIBC_NAMESPACE {
+#ifdef LIBC_TARGET_ARCH_IS_GPU
+// FIXME: Local GPU memory cannot be initialized so we cannot currently provide
+// a standard compliant default value.
+ThreadLocal<unsigned long> rand_next;
+#else
// C standard 7.10p2: If 'rand' is called before 'srand' it is to proceed as if
// the 'srand' function was called with a value of '1'.
LIBC_THREAD_LOCAL unsigned long rand_next = 1;
+#endif
} // namespace LIBC_NAMESPACE