public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH take 2] Emulate clock and other stubs on nvptx
@ 2021-08-24 16:48 Roger Sayle
  2021-08-25  8:25 ` Corinna Vinschen
  0 siblings, 1 reply; 2+ messages in thread
From: Roger Sayle @ 2021-08-24 16:48 UTC (permalink / raw)
  To: newlib

[-- Attachment #1: Type: text/plain, Size: 2652 bytes --]


Alas, I'm having trouble with "git send-email", but hopefully the attached patch
generated by "git format-patch" is close enough.  Many thanks also for the
speedy locale fix.  This revised patch takes advantage of a (upcoming) __PTX_SM__
macro that I'm just about to push to GCC, that allows newlib's clock to use an
accurate timer on more recent GPUs.  This avoids the clock speed issue.

Thanks in advance.
Roger
--

-----Original Message-----
From: Corinna Vinschen <vinschen@redhat.com> 
Sent: 19 August 2021 11:22
To: Roger Sayle <roger@nextmovesoftware.com>
Cc: newlib@sourceware.org
Subject: Re: [PATCH] Emulate clock and other stubs on nvptx

Hi Roger,

On Aug 18 17:43, Roger Sayle wrote:
> 
> Please be gentle, this is my first patch to newlib.
> 
> This patch to the libc/machine/nvptx port of newlib implements an 
> approximation of "clock" and provides some additional stub routines.
> These changes not only reduce the number of (link) failures in the GCC 
> testsuite when targeting nvptx-none, but also allow the NIST scimark4 
> benchmark to compile and run without modification.
> 
> newlib already contains support for backends to provide their own 
> clock implementations via -DCLOCK_PROVIDED.  That functionality is 
> used here to return an approximate elapsed time based on the NVidia 
> GPU's clock64 cycle counter.  Although not great, this is better than 
> the current behaviour of link error from the unresolved symbol 
> _times_r.
> 
> The other part of the patch is to add a small number of stub functions 
> to nvptx's misc.c.  Adding isatty, for example, resolves linking 
> problems in libc from the dependency in __smakebuf_r, and the sync 
> stub, for example, fixes the failure with GCC's
> testsuite/gfortran.dg/ISO_Fortran_binding_14.f90 [which simply tests 
> that gfortran can call a/any C function].
> 
> Hopefully, these changes are acceptable.  If so, if someone could 
> please commit

They are, but your patch submission is a bit weird and more work to apply than necessary.

Please commit your patches to your local git repo, including a nice commit message, create a git patch file via `git format-patch' and send it via `git send-email --to="newlib@..." to this mailing list.  Note that providing Makefile.in is nice, but you don't *have* to provide Makefile.in.  We'll regenerate that as required.

Another point:  Given the HW support for a monotonic clock tick, wouldn't it make more sense to implement clock_gettime() with support for CLOCK_REALTIME and CLOCK_MONOTONIC, and implement clock() by calling clock_gettime()?


Thanks,
Corinna


[-- Attachment #2: nvptx.patch --]
[-- Type: application/octet-stream, Size: 3355 bytes --]

From b6ad8e07eaa21ee504ae84c49a4f3f3ba29dd309 Mon Sep 17 00:00:00 2001
From: Roger Sayle <roger@nextmovesoftware.com>
Date: Tue, 24 Aug 2021 17:24:19 +0100
Subject: [PATCH] nvptx: Emulate clock and other machine stubs.

2021-08-24  Roger Sayle  <roger@nextmovesoftware.com>
	    Corinna Vinschen  <corinna@vinschen.de>

newlib/
	configure.host: Add -DCLOCK_PROVIDED to newlib_cflags on nvptx*.

newlib/libc/machine/nvptx
	Makefile.am: Add clock.c to lib_a_SOURCES.
	clock.c: New source file to implement/approximate clock().
	misc.c: Add stubs for fstat, isatty, open, sync and unlink.
---
 newlib/configure.host                 |  2 +-
 newlib/libc/machine/nvptx/Makefile.am |  2 +-
 newlib/libc/machine/nvptx/clock.c     | 19 +++++++++++++++++++
 newlib/libc/machine/nvptx/misc.c      | 26 ++++++++++++++++++++++++++
 4 files changed, 47 insertions(+), 2 deletions(-)
 create mode 100644 newlib/libc/machine/nvptx/clock.c

diff --git a/newlib/configure.host b/newlib/configure.host
index 4ac5334..ef481ff 100644
--- a/newlib/configure.host
+++ b/newlib/configure.host
@@ -289,7 +289,7 @@ case "${host_cpu}" in
 	;;
   nvptx*)
 	machine_dir=nvptx
-	newlib_cflags="${newlib_cflags} -DMALLOC_PROVIDED"
+	newlib_cflags="${newlib_cflags} -DCLOCK_PROVIDED -DMALLOC_PROVIDED"
 	;;
   or1k*|or1knd*)
 	machine_dir=or1k
diff --git a/newlib/libc/machine/nvptx/Makefile.am b/newlib/libc/machine/nvptx/Makefile.am
index 66664d3..b0d4c78 100644
--- a/newlib/libc/machine/nvptx/Makefile.am
+++ b/newlib/libc/machine/nvptx/Makefile.am
@@ -10,7 +10,7 @@ noinst_LIBRARIES = lib.a
 
 lib_a_SOURCES = calloc.c callocr.c malloc.c mallocr.c realloc.c reallocr.c \
 		free.c write.c assert.c puts.c putchar.c printf.c abort.c \
-		exit.c misc.c
+		exit.c misc.c clock.c
 lib_a_CFLAGS = $(AM_CFLAGS)
 
 ACLOCAL_AMFLAGS = -I ../../.. -I ../../../..
diff --git a/newlib/libc/machine/nvptx/clock.c b/newlib/libc/machine/nvptx/clock.c
new file mode 100644
index 0000000..7ca0f3a
--- /dev/null
+++ b/newlib/libc/machine/nvptx/clock.c
@@ -0,0 +1,19 @@
+/* clock.c
+ * Support file for nvptx in newlib.
+ */
+#include <time.h>
+
+clock_t
+clock ()
+{
+  unsigned long long now;
+#if __PTX_SM__ >= 310
+  asm volatile("mov.u64 %0, %%globaltimer;" : "=r"(now));
+  return now/((1000000000ull)/CLOCKS_PER_SEC);
+#else
+  asm volatile("mov.u64 %0, %%clock64;" : "=r"(now));
+  // Assume a GPU base clock frequency of 1250MHz.
+  return now/((1250000000ull)/CLOCKS_PER_SEC);
+#endif
+}
+
diff --git a/newlib/libc/machine/nvptx/misc.c b/newlib/libc/machine/nvptx/misc.c
index ed1bc34..3609241 100644
--- a/newlib/libc/machine/nvptx/misc.c
+++ b/newlib/libc/machine/nvptx/misc.c
@@ -15,6 +15,7 @@
 
 #include <errno.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #undef errno
 extern int errno;
 
@@ -23,12 +24,37 @@ close(int fd) {
   return -1;
 }
 
+int
+fstat (int fd, struct stat *buf) {
+  return -1;
+}
+
+int
+isatty (int fd) {
+  return fd == 1;
+}
+
 off_t
 lseek(int fd, off_t offset, int whence) {
   return 0;
 }
 
 int
+open (const char *pathname, int flags, ...) {
+  return -1;
+}
+
+int
 read(int fd, void *buf, size_t count) {
   return 0;
 }
+
+void
+sync (void) {
+}
+
+int
+unlink (const char *pathname) {
+  return -1;
+}
+
-- 
1.8.3.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH take 2] Emulate clock and other stubs on nvptx
  2021-08-24 16:48 [PATCH take 2] Emulate clock and other stubs on nvptx Roger Sayle
@ 2021-08-25  8:25 ` Corinna Vinschen
  0 siblings, 0 replies; 2+ messages in thread
From: Corinna Vinschen @ 2021-08-25  8:25 UTC (permalink / raw)
  To: Roger Sayle; +Cc: newlib

On Aug 24 17:48, Roger Sayle wrote:
> 
> Alas, I'm having trouble with "git send-email", but hopefully the attached patch
> generated by "git format-patch" is close enough.  Many thanks also for the
> speedy locale fix.  This revised patch takes advantage of a (upcoming) __PTX_SM__
> macro that I'm just about to push to GCC, that allows newlib's clock to use an
> accurate timer on more recent GPUs.  This avoids the clock speed issue.

Pushed.  I took the liberty to add the mail text from your first patch
submission to the commit message.  I kept the CVS-style commit message
part intact, but it's not needed anymore.  A nice descriptive text is
more helpful to the reader.


Thanks,
Corinna


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-25  8:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-24 16:48 [PATCH take 2] Emulate clock and other stubs on nvptx Roger Sayle
2021-08-25  8:25 ` Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).