Article View: comp.windows.x.pex
Article #61Re: Future development of PEX/PHIGS
From: Allen Akin
Date: Thu, 27 Aug 1992 22:44
Date: Thu, 27 Aug 1992 22:44
85 lines
3084 bytes
3084 bytes
In article <p1f880s@fido.asd.sgi.com> I wrote:
| ...Here's a
| quick-and-dirty program to test memory-to-memory copy rates, assuming
| your local implementation of memcpy() is fully optimized: ...
If:
The size of the program's data arrays happened to be an integral
multiple of your machine's data cache size
and:
Your machine's memcpy() routine transferred less than one cache
line's worth of data for each iteration of its inner loop
-or-
The data arrays were not aligned on cache-line boundaries
then on some systems the program could report a copy rate lower than
you might expect. The first condition causes corresponding portions of
source and destination arrays to map to the same cache line, and the
second condition causes part of the destination cache line to be
written before all of the source cache line has been read (thus
forcing the source cache line to be ejected before all of it has
been used).
If it weren't for some programmers' tendencies to use power-of-2 array
sizes, this situation would be very rare in practice. :-)
A quick fix is to change the size of the data arrays. Since nearly all
cache sizes today are powers of 2, all we need to do is use a size that
isn't a power of 2. Here's a new version, which also uses an ANSI C
routine for timing:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*
* NBYTES must be greater than the machine's cache size, not an
* integral multiple of the machine's cache size, and less than half
* the amount of physical memory available to the process. This
* ensures we measure memory-to-memory copy speed rather than
* cache-to-cache or disk-to-memory speed.
*/
#define NBYTES (2 * 1000 * 1000)
/*
* NITERATIONS must be large enough to amortize error due to timer
* granularity and overhead.
*/
#define NITERATIONS 100
main()
{
char* source;
char* destination;
clock_t start;
clock_t finish;
register int i;
source = malloc(NBYTES);
destination = malloc(NBYTES);
memcpy(destination, source, NBYTES); /* touch all pages */
start = clock();
for (i = 0; i < NITERATIONS; ++i)
memcpy(destination, source, NBYTES);
finish = clock();
printf("%.1f MB/s\n",
((double) NBYTES * NITERATIONS / 1000000.0)
/ ((double) (finish - start) / CLOCKS_PER_SEC));
exit(0);
}
Thanks to Thad Beier for noticing an anomaly, and Kevin Meier for
tracking it down.
Allen
Technical Details
Message-ID:
<p44519c@fido.asd.sgi.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!dtix!darwin.sura.net!sgiblab!sgigate!odin!fido!akinReferences:
<or5vc3k@fido.asd.sgi.com> <1992Aug24.190727.19006@shograf.com> <p1f880s@fido.asd.sgi.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!dtix!darwin.sura.net!sgiblab!sgigate!odin!fido!akin From: akin@sgi.com (Allen Akin) Newsgroups: comp.windows.x.pex,comp.graphics,comp.sys.sgi Subject: Re: Future development of PEX/PHIGS Keywords: PEX, OpenGL Message-ID: <p44519c@fido.asd.sgi.com> Date: 27 Aug 92 22:44:16 GMT References: <or5vc3k@fido.asd.sgi.com> <1992Aug24.190727.19006@shograf.com> <p1f880s@fido.asd.sgi.com> Sender: news@fido.asd.sgi.com (Usenet News Admin) Organization: Silicon Graphics, Inc. Lines: 85