public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Emulate clock and other stubs on nvptx
@ 2021-08-18 16:43 Roger Sayle
  2021-08-19 10:22 ` Corinna Vinschen
  2021-08-19 12:59 ` [PATCH] Emulate clock and other stubs on nvptx Joel Sherrill
  0 siblings, 2 replies; 6+ messages in thread
From: Roger Sayle @ 2021-08-18 16:43 UTC (permalink / raw)
  To: newlib

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


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
them for me that would be much appreciated (as I don't have repository
access).
Likewise, these changes should be obvious enough to not require a copyright
assignment, but to avoid any objections, I'm happy to put them in the public
domain.

2021-08-18  Roger Sayle  <roger@nextmovesoftware.com>

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.
	Makefile.in: Regenerate.

Many thanks in advance,
Roger
--
Roger Sayle
NextMove Software Limited
Cambridge, UK


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

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..0cb20cc 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/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;
+}
+

[-- Attachment #3: clock.c --]
[-- Type: text/plain, Size: 802 bytes --]

/* clock.c
 * Support file for nvptx in newlib.
 */
#include <time.h>

/* PTX's clock64 is a cycle count, whose frequency varies with the GPU's
   operating frequency, which may be boosted or thermally throttled at
   run-time.  The value provided is suitable for relative timings, but
   must be corrected [by the host] for accurate elapsed/wall clock times.  */
/* NVidia RTX-2080 Super	1650-1815 MHz.  */
/* NVidia GTX-1070		1506-1683 MHz.  */
/* NVidia RTX-2070		1410 MHz.  */
/* NVidia GTX-1050 Ti		1290-1392 MHz.  */
/* NVidia Quadro P400		1228-1252 MHz.  */
/* NVidia GTX 660		823 MHz.   */

#define NVPTX_MHZ 1250

clock_t
clock ()
{
  unsigned long long now;
  asm volatile("mov.u64 %0, %%clock64;" : "=r"(now));
  return now/((NVPTX_MHZ*1000000)/CLOCKS_PER_SEC);
}


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

* Re: [PATCH] Emulate clock and other stubs on nvptx
  2021-08-18 16:43 [PATCH] Emulate clock and other stubs on nvptx Roger Sayle
@ 2021-08-19 10:22 ` Corinna Vinschen
  2021-08-19 10:34   ` Corinna Vinschen
  2021-08-19 12:59 ` [PATCH] Emulate clock and other stubs on nvptx Joel Sherrill
  1 sibling, 1 reply; 6+ messages in thread
From: Corinna Vinschen @ 2021-08-19 10:22 UTC (permalink / raw)
  To: Roger Sayle; +Cc: newlib

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


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

* Re: [PATCH] Emulate clock and other stubs on nvptx
  2021-08-19 10:22 ` Corinna Vinschen
@ 2021-08-19 10:34   ` Corinna Vinschen
  2021-08-19 10:45     ` [Fwd: Re: [PATCH] Emulate clock and other stubs on nvptx] Yago Rubio
  0 siblings, 1 reply; 6+ messages in thread
From: Corinna Vinschen @ 2021-08-19 10:34 UTC (permalink / raw)
  To: Roger Sayle; +Cc: newlib

On Aug 19 12:22, Corinna Vinschen wrote:
> 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()?

Oh, btw., since your comment enumerates the CPU variants, isn't there
a runtime method to check the CPU and use more accurate values per
CPU?  Just an idea...


Corinna


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

* [Fwd: Re: [PATCH] Emulate clock and other stubs on nvptx]
  2021-08-19 10:34   ` Corinna Vinschen
@ 2021-08-19 10:45     ` Yago Rubio
  0 siblings, 0 replies; 6+ messages in thread
From: Yago Rubio @ 2021-08-19 10:45 UTC (permalink / raw)
  To: Iago Rubio Sanfiz; +Cc: newlib

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




[-- Attachment #2: Forwarded message — Re: [PATCH] Emulate clock and other stubs on nvptx --]
[-- Type: message/rfc822, Size: 6917 bytes --]

From: Corinna Vinschen <vinschen@redhat.com>
To: Roger Sayle <roger@nextmovesoftware.com>
Cc: newlib@sourceware.org
Subject: Re: [PATCH] Emulate clock and other stubs on nvptx
Date: Thu, 19 Aug 2021 12:34:06 +0200
Message-ID: <YR4znn5zjjWYix0Y@calimero.vinschen.de>

On Aug 19 12:22, Corinna Vinschen wrote:
> 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()?

Oh, btw., since your comment enumerates the CPU variants, isn't there
a runtime method to check the CPU and use more accurate values per
CPU?  Just an idea...


Corinna


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

* Re: [PATCH] Emulate clock and other stubs on nvptx
  2021-08-18 16:43 [PATCH] Emulate clock and other stubs on nvptx Roger Sayle
  2021-08-19 10:22 ` Corinna Vinschen
@ 2021-08-19 12:59 ` Joel Sherrill
  2021-08-19 13:57   ` Roger Sayle
  1 sibling, 1 reply; 6+ messages in thread
From: Joel Sherrill @ 2021-08-19 12:59 UTC (permalink / raw)
  To: Roger Sayle; +Cc: Newlib

Should these be in a machine directory or a libgloss directory? If you ran
a different run-time/OS on the CPU, at least some of these wouldn't make
sense to be there.

--joel

On Wed, Aug 18, 2021, 11:43 AM Roger Sayle <roger@nextmovesoftware.com>
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
> them for me that would be much appreciated (as I don't have repository
> access).
> Likewise, these changes should be obvious enough to not require a copyright
> assignment, but to avoid any objections, I'm happy to put them in the
> public
> domain.
>
> 2021-08-18  Roger Sayle  <roger@nextmovesoftware.com>
>
> 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.
>         Makefile.in: Regenerate.
>
> Many thanks in advance,
> Roger
> --
> Roger Sayle
> NextMove Software Limited
> Cambridge, UK
>
>

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

* RE: [PATCH] Emulate clock and other stubs on nvptx
  2021-08-19 12:59 ` [PATCH] Emulate clock and other stubs on nvptx Joel Sherrill
@ 2021-08-19 13:57   ` Roger Sayle
  0 siblings, 0 replies; 6+ messages in thread
From: Roger Sayle @ 2021-08-19 13:57 UTC (permalink / raw)
  To: joel; +Cc: 'Newlib'

 

Hi Joel,

 

The nvptx-none target doesn’t have an OS, a run-time nor a BIOS.

The only reason that clock may be emulated, is that the hardware has

a register containing the cycle count.  Pretty much the only function

provided is _vprintf, and the entire newlib-cygwin port builds off

of that.  Hence, newlib + the libc/machine/nvptx directory is the

entire operating system.

 

See https://sourceware.org/legacy-ml/newlib/2018/msg00299.html

 

Cheers,

Roger

--

 

From: Joel Sherrill <joel@rtems.org> 
Sent: 19 August 2021 13:59
To: Roger Sayle <roger@nextmovesoftware.com>
Cc: Newlib <newlib@sourceware.org>
Subject: Re: [PATCH] Emulate clock and other stubs on nvptx

 

Should these be in a machine directory or a libgloss directory? If you ran a different run-time/OS on the CPU, at least some of these wouldn't make sense to be there. 

 

--joel

 

On Wed, Aug 18, 2021, 11:43 AM Roger Sayle <roger@nextmovesoftware.com <mailto:roger@nextmovesoftware.com> > 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
them for me that would be much appreciated (as I don't have repository
access).
Likewise, these changes should be obvious enough to not require a copyright
assignment, but to avoid any objections, I'm happy to put them in the public
domain.

2021-08-18  Roger Sayle  <roger@nextmovesoftware.com>

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.
        Makefile.in: Regenerate.

Many thanks in advance,
Roger
--
Roger Sayle
NextMove Software Limited
Cambridge, UK


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

end of thread, other threads:[~2021-08-19 13:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-18 16:43 [PATCH] Emulate clock and other stubs on nvptx Roger Sayle
2021-08-19 10:22 ` Corinna Vinschen
2021-08-19 10:34   ` Corinna Vinschen
2021-08-19 10:45     ` [Fwd: Re: [PATCH] Emulate clock and other stubs on nvptx] Yago Rubio
2021-08-19 12:59 ` [PATCH] Emulate clock and other stubs on nvptx Joel Sherrill
2021-08-19 13:57   ` Roger Sayle

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).