From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <96075319106604.tru64-unix-managers.marc@archive.invalid> From: wenger () SIMBAD ! u-strasbg ! fr Date: Wed, 26 Apr 1995 09:21:58 +0000 To: tru64-unix-managers Subject: SUMMARY: Slow byte manipulation on alpha stations ? X-MARC-Message: https://marc.info/?l=tru64-unix-managers&m=96075319106604 My question concerned particular slowness noticed on alpha processors in byte manipulation applications. Thank you to all people who took time to answer my question: cherkus@unimaster.com rockwell@rch.dec.com alan@nabeth.cxo.dec.com iglesias@draco.acs.uci.edu gadbois@mcc.com reeves@zk3.dec.com intrepid!tb@uunet.uu.net rjackson@gmu.edu kpe@ne.dk henning@i4get.enet.dec.com The slowness of DEC alpha in handling bytes is known and documented in the architecture manuals. Also articles published in the Communications of the ACM (no precise issue given) explain the processor design and the tradeoffs made. The alpha processor has no direct byte manipulation instructions and needs to load a word and extract a byte to get it, load a word, mask a byte, insert it and store the word to write a byte to the memory. Concerning the example I gave (writing a loop to copy bytes): ... for (p1=str1, p2=str2; p1 < pf; ) *p1++ = *p2++; several people pointed out that this was the wrong way of doing it. Execution times are much better if this loop is replaced by a standard C library function like memcpy(), bcopy() or strcpy(). ==> Comment: that's of course true, but I used only this loop to highlight the probleme, not to claim that a string copy is slow. In a lot of applications, you often manipulate one byte after the other and cannot only use such functions on large areas of data. The very slow execution time found with grep, awk and sort, especially compared to DEC 5400 was explained by the fact that under OSF/1, these utilities are XPG4 internationalized and can deal with characters sets larger than 8 bits. ==> Comment: May be, but the price for this internationalization seems to be VERY high. And this explanation is not suffisant: all byte oriented applications, like latex, ghostscript, perl, gawk (not internationalized) perform slower on DEC alpha than it could be expected. Local Digital sales-persons said that the next generation of alpha processor will improve byte management. At last, a very interesting report was sent to me by John Henning, from DEC, CSD Performance Group. I just attached it at the end of this mail. It may contain interesting and useful informations for many people. Marc Wenger wenger@astro.u-strasbg.fr ============================================================================ >From henning@i4get.enet.dec.com Mon Apr 24 13:21:04 1995 In a message to alpha-osf-managers on 20-Apr-95, Marc Wenger inquires about the byte manipulation performance of Alpha. He introduces this short programme: main() { int i; char str1[8192], str2[8192], *p1, *p2, *pf ; pf = str1+8192; for (i=0; i < 10000; i++) for (p1=str1, p2=str2; p1 < pf; ) *p1++ = *p2++; } and says that when compiled with "cc -O2" and executed with the 'time' command on a DEC 3000-400 (Alpha 133 Mhz) it reports: 18.51u 0.01s 0:18 99% 0+1k 0+0io 15pf+0w a time which is much slower than he expected. I attempted to duplicate these results and investigate alternatives. My test system was a DEC 3000 Model 500 (150Mhz Alpha) running DEC OSF/1 V3.0. Using the same compile command, I got: 14.22u cc -O2 This speedup is slightly better than clock speed, but not unreasonable. (Note: for simplicity this report only gives the user time, followed by the command used for the compile. The remainder of the output from the 'time' command always showed that system time was close to zero, and the test had 99% of the system.) But there are actually two c compilers available. In DEC OSF/1 V3.x, these are known as "cc" and "cc -migrate". The latter invokes the DEC C product, which uses the same GEM optimizing back end as DEC FORTRAN. With DEC C, I got: 14.23u cc -migrate -O2 9.83u cc -migrate -O4 Ah-ha. The DEC C product is intended to provide additional ways to optimize code, and indeed in this case it shaves about 1/3 off the time using the (generally recommended) -O4 level of optimization. For completeness, here's the results of a couple of other options: 30.48u cc -O0 25.14u cc -migrate -O0 Next, please notice that there's something unfortunate about the original source: it has two arrays with dimensions that are a power-of-two. Two power-of-two arrays are likely to incur less than optimal performance in many RISC systems, as elements of the second array will map to the same cache lines as the first array. A well known technique for avoiding this problem is to bump up the array sizes slightly and thereby reduce the amount of cache thrashing. A one line change to the above source code: char str1[8300], str2[8300], *p1, *p2, *pf ; doubles the performance: Power-of-2 Not-Power Command ========== ======== ================ 30.48u 15.67u cc -O0 14.22u 6.50u cc -O2 25.14u 14.74u cc -migrate -O0 14.23u 6.04u cc -migrate -O2 9.83u 5.76u cc -migrate -O4 But this still doesn't match the SUN system that was reported to require 4.4u for this benchmark, whereas (to quote Mr. Wenger) "'normally' the alpha server is twice as fast as the Sparc." Why would this byte-manipulation benchmark run more slowly than other Alpha operations? The reason is that Alpha's byte manipulation instructions work on registers, not on memory. This was an intentional simplification made as a part of the philosophy that RISC->simplicity->Mhz->overall system performance. While it is true that the particular operation of byte operations on memory therefore needs more instructions, it is believed that realistic workloads have a mix of operations and the OVERALL mix benefits. Alpha therefore encourages programs to process 8 bytes at a time rather than 1 at a time. Various system utilities and libraries have been modified to do so -- for example, with DEC OSF/1 V3.2 several low level string processing library routines were re-written to process 8 bytes at a time, with major speedups. In addition, the DEC C compiler has learned to recognize various opportunities where it can merge 8 accesses to individual bytes into a single reference to the memory system. Unfortunately, it was apparently not able to do this 8-to-1 optimization for the above program. With my poorly-trained "C" skills, I attempted to modify the benchmark into a program that would have the same effect, but would use constructs that the compiler could recognize as candidates for merging. I wrote: main() { int i,j; typedef struct {char a,b,c,d,e,f,g,h;} s; static s str1[2300], str2[2300] ; for (i=0; i < 10000; i++) for (j=1; j <= 2048; j++ ) { str1[j].a = str2[j].a; str1[j].b = str2[j].b; str1[j].c = str2[j].c; str1[j].d = str2[j].d; str1[j].e = str2[j].e; str1[j].f = str2[j].f; str1[j].g = str2[j].g; str1[j].h = str2[j].h; } } Unfortunately, I overshot by a factor of two -- this program actually moves twice as many bytes as the original. That's what comes of writing "C" code late at night when it's not your native language. And sure enough, in the unoptimized case this program takes twice as long (or worse): Not-Power 2x Struct Command ======== ========= ================ 15.67u 25.26u cc -O0 6.50u 11.97u cc -O2 14.74u 54.97u cc -migrate -O0 6.04u 1.45u cc -migrate -O2 5.76u 1.00u cc -migrate -O4 But at O2 and O4 the DEC C compiler does amazing things! It does them very simply: by merging 8 byte references into single references to the memory system. The O0 code has scores of memory reference instructions (ldl, ldq, stl, stq) liberally sprinkled with ZAPNOTs to extract the needed bytes; the O2 code goes happily around a much smaller loop, and the program as a whole has a total of 3 memory references instructions. Please note that I am not suggesting that programs be re-written to use structs. Rather, it's up to Digital to modify the compiler over time to increase the number of cases that it recognizes as candidates for merging. The above is simply intended to give you a feel for the degree of benefit from merging. Finally, a DEC C engineer who reviewed this note adds that it is: "probably also worth mentioning that they should use the library routines whenever possible, as they have been tuned a lot. For example, using memcpy and compiling with -migrate -D_INTRINSICS reduced the execution time to 0.24u vs. 15.40u on my workstation." As mentioned above, Digital has tuned various library routines to process 8 bytes at a time, and several of these optimizations shipped with DEC OSF/1 V3.2. As to the other cases that the original note described (e.g. with grep), I didn't try to duplicate those. But I would suspect that over time the utilities such as grep are becoming smarter about processing multiple bytes at a time, for example as they are recompiled with DEC C, and as the low-level routines they call are also optimized. To summarize: - Real workloads have a mix of operations, and Alpha attempts to optimize the overall mix. - But if your application resembles this benchmark, you may wish to consider the following advice: * Use DEC OSF/1 V3.2 * Use DEC C * Use memcpy & other string library routines * Avoid power-of-two-arrays * As a last resort, consider source changes to help the compiler see opportunities for merging. I hope this is helpful. /John Henning CSD Performance Group Digital Equipment Corporation henning@zko.dec.com Speaking for myself, not Digital