Problems of PREFETCH instruction on UltraSparc T1
Hi, everyone,
I don't really know if it is proper to post this topic here.
In order to accelerate our applications, I adopted the PREFETCH instruction of UltraSparc. However, the results are not expected: the one with prefetch works even a little bit slower. The whole program is listed below.
I read the Ultrasparc specification and found that "when executed in nonprivileged or privileged mode, PREFETCH has the same observable effect as a NOP". I don't know how to understand this sentence. What are the nonprivileged, privileged, and hyperprivileged modes in Linux? Could that be user mode and kernel mode? I mean, does it mean I can only make use of that instruction in kernel mode?
Thanks a lot!
testprefetch.c:
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#define MEGA 1024*1024
#define TIMES 1
#define LASTITEM 100*MEGA-1
int main(){
struct timeval begin,end;
register int i,j;
int foo, bar;
int *data=(int *)malloc(100*sizeof(int)*MEGA); //400MB in
order to avoid being cached in L2 cache, which is 3M
register int *buf, *lastone;
lastone=&data[LASTITEM];
gettimeofday(&begin,NULL);
for(i=0;i<TIMES;i++){
bufÚta;
while(buf<lastone){
__asm__ __volatile__("prefetch\t[%0],
#n_writes"::"r"(buf));
__asm__ __volatile__("prefetch\t[%0],
#n_writes"::"r"(buf+16));
__asm__ __volatile__("prefetch\t[%0],
#n_writes"::"r"(buf+32));
__asm__ __volatile__("prefetch\t[%0],#n_writes"::"r"(buf+48));
for(j=0;j<100;j++) foo+=j; //do some trivial
things to pass time
bar=foo;
*buf=i;
*(buf+16)=i;
*(buf+32)=i;
*(buf+48)=i;
buf+d;
}
}
gettimeofday(&end,NULL);
printf("time in usec is%d\n",((end.tv_sec*1000000+end.tv_usec)-(begin.tv_sec*1000000+begin.tv_usec))); }
--
Best regards,
Jiaqi
From: "jiaqi zhang" <[email protected]>
Date: Wed, 22 Aug 2007 12:01:19 +1200
Quoted 6 lines
I don't really know if it is proper to post this topic here.
In order to accelerate our applications, I adopted the PREFETCH instruction of UltraSparc. However, the results are not expected: the one with prefetch works even a little bit slower. The whole program is listed below.
Different processors can only have so many prefetches in flight at a time. If you perform too many, they will only consume cpu cycles.
UltraSPARC-I does not implement prefetch at all, and it just takes up an instruction issue slot.
UltraSPARC-II, IIi and IIe implement prefetch, with up to 3 in flight, but they has several limitations and the prefetches are not as effective as they could be.
UltraSPARC-III and IIIi can have up to 5 prefetches in flight and implements it very well.
Niagara 1 and 2 can have up to about 3 prefetches in flight and it implements this reasonable well.
Also, prefetches only help if there is a large amount of time between the prefetch and the actual access of the data.
So what a loop will usually do is queue up a few prefetches before the loop, perhaps forward about 2 or 3 cachelines, and then the loop will end with a single prefetch that same number of cachelines forward. See my suggested loop below.
==========
for(j=0;j<100;j++) foo+=j; //do some trivial
things to pass time
==========
Since you don't really use foo or bar, the compiler can see that this loop does nothing, so it will be eliminated, at least by more recent versions of gcc.
Also you are doing worthless prefetches, as the cacheline size is 64 bytes, so doing 16-byte offset prefetches accomplishes nothing.
Reconstruct your loop as follows:
prefetch buffer + (0 * 64)
prefetch buffer + (1 * 64)
prefetch buffer + (2 * 64)
while (buf < lastone) {
*buf = i;
*(buf+16)=i;
*(buf+32)=i;
*(buf+48)=i;
buf += 64;
prefetch buffer + (2 * 64);
}This should work significantly better than what your code is doing right now.
Thanks very much for this thorough explaination. It is really of great help. I changed the program as you have instructed, and it is obviously better than the former one. However, it still performs a little bit worse than the one without prefetchs (simply delete all the prefetch statements). So I again added the "foo bar" loop, and to prevent the optimization of compiler, I printed the result at the end of program( again, whole program below). And this makes the one with prefetch run faster than the one without. Also, a careful tunning indicates that a "foo bar" loop of 6 iterations will achieve the best performance.
However, the problem is the performance gain is so tiny even after great efforts of tunning: the whole program runs about 60 seconds and it runs only 3 seconds faster with prefetches, more excatly, 4.3% faster. I don't know if this is really the case. Because the cycles cost by a memory access is ten times more than a L2 cache access, I suppose the performance gain should be more significant.
Also, if I simply delete "*(buf+48)=i;" and change "buf+d" to "buf+H", the performance gain would drop markedly (about a half). Does it indicate that this prefetch work is so tricky that I shouldn't consider it when I don't know the workload of a real application?
Thanks!
--testprefetch.c--
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#define MEGA 1024*1024
#define TIMES 100
#define LASTITEM 100*MEGA-1
int main(){
struct timeval begin,end;
register int i,j;
int foo,bar;
int *data=(int *)malloc(100*sizeof(int)*MEGA);
register int *buf, *lastone;
lastone=&data[LASTITEM];
gettimeofday(&begin,NULL);
for(i=0;i<TIMES;i++){
bufÚta;
__asm__ __volatile__("prefetch\t[%0], #n_writes\n\t"
"prefetch\t[%0+64], #n_writes\n\t"
"prefetch\t[%0+128], #n_writes"
::"r"(buf));
while(buf<lastone){
for(j=0;j<6;j++)foo+=j;
bar+=foo;
*buf=i;
*(buf+16)=i;
*(buf+32)=i;
*(buf+48)=i;
buf+d;
__asm__ __volatile__("prefetch\t[%0],
#n_writes"::"r"(buf+128));
}
}
gettimeofday(&end,NULL);
printf("time in usec is
%d\n",((end.tv_sec*1000000+end.tv_usec)-(begin.tv_sec*1000000+begin.tv_usec)));
printf("bar is %d\n",bar);
}Quoted 65 lines · David Miller
From: "jiaqi zhang" <[email protected]>
Date: Wed, 22 Aug 2007 12:01:19 +1200Quoted 6 lines
I don't really know if it is proper to post this topic here.
In order to accelerate our applications, I adopted the PREFETCH instruction of UltraSparc. However, the results are not expected: the one with prefetch works even a little bit slower. The whole program is listed below.
Different processors can only have so many prefetches in flight at a time. If you perform too many, they will only consume cpu cycles.
UltraSPARC-I does not implement prefetch at all, and it just takes up an instruction issue slot.
UltraSPARC-II, IIi and IIe implement prefetch, with up to 3 in flight, but they has several limitations and the prefetches are not as effective as they could be.
UltraSPARC-III and IIIi can have up to 5 prefetches in flight and implements it very well.
Niagara 1 and 2 can have up to about 3 prefetches in flight and it implements this reasonable well.
Also, prefetches only help if there is a large amount of time between the prefetch and the actual access of the data.
So what a loop will usually do is queue up a few prefetches before the loop, perhaps forward about 2 or 3 cachelines, and then the loop will end with a single prefetch that same number of cachelines forward. See my suggested loop below.
==========
for(j=0;j<100;j++) foo+=j; //do some trivialthings to pass time
==========Since you don't really use foo or bar, the compiler can see that this loop does nothing, so it will be eliminated, at least by more recent versions of gcc.
Also you are doing worthless prefetches, as the cacheline size is 64 bytes, so doing 16-byte offset prefetches accomplishes nothing.
Reconstruct your loop as follows:
prefetch buffer + (0 * 64) prefetch buffer + (1 * 64) prefetch buffer + (2 * 64) while (buf < lastone) { *buf = i; *(buf+16)=i; *(buf+32)=i; *(buf+48)=i; buf += 64; prefetch buffer + (2 * 64); }This should work significantly better than what your code is doing right now.
From: "jiaqi zhang" <[email protected]>
Date: Thu, 23 Aug 2007 13:11:13 +1200
Quoted 2 lines
However, the problem is the performance gain is so tiny even after great efforts of tunning:
On stores, UltraSPARC-T1 isn't going to gain much from use of prefetches, it is better to use prefetches for data being read.
For stores, use the STORE-INIT alternate space store instructions. Which preclear the cache line and force it into owned state without any bus transactions.
When using store-init instructions, you must be writing to the whole 64 bytes of the 64-byte aligned cache line. The first store to the cache line should be the one at offset zero, and for best performance you should do consequetive increasing stores so that they all compress in the store buffer.
For loads you should use the same alternate space ID, but with load instructions and prefetch. These require evenly numbered register pairs, must be used with "ldxa" and loads two 8-byte registers at a time.
You really should read the UltraSPARC-T1 supplement to the UltraSPARC-2005 architecture manual found at the opensparc.org web site, it explains all of this precisely.
Another example of good usage for high performance memory operations is the arch/sparc64/lib/NGmemcpy.S memcpy() implementation in the kernel sources.
Anyways, here is a test program I put together quickly so you can see how to use the store-init instructions. With USE_STORE_INIT it should run about 4 times faster.
Can you figure out the rest yourself now? :-/ I've a lot of time teaching you the very basics how the chip works, when it's clearly documented in the UltraSPARC-T1 programmers manual.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#define BUF_SIZE (400 * 1024 * 1024)
#define ASI_BLK_INIT_QUAD_LDD_P 0xe2 /* (NG) init-store, twin load, * primary, implicit */
#define USE_STORE_INIT
static unsigned long tick_read_cost(void)
{
unsigned long tbest = ~0UL;
int i;
for (i = 0; i < 8; i++) {
unsigned long t2, t1;
__asm__ __volatile__("rd %%tick, %0"
: "=r" (t1));
__asm__ __volatile__("rd %%tick, %0"
: "=r" (t2));
t2 -= t1;
if (t2 < tbest)
tbest = t2;
}
return tbest;
}int main(void)
{
unsigned long overhead = tick_read_cost();
unsigned long tbest = ~0UL;
void *p = valloc(BUF_SIZE);
int iters;
__asm__ __volatile__("wr %%g0, %0, %%asi\n\t"
: : "i" (ASI_BLK_INIT_QUAD_LDD_P));
for (iters = 0; iters < 8; iters++) {
unsigned long off, t2, t1;
void *buf = p;
__asm__ __volatile__("rd %%tick, %0"
: "=r" (t1));
for (off = 0; off < BUF_SIZE; off += 64) {
unsigned int *x = (unsigned int *) (buf + off);
unsigned long data = 0x12341234;#ifdef USE_STORE_INIT
__asm__ __volatile__(
"stxa %0, [%1 + 0x00] %%asi\n\t"
"stxa %0, [%1 + 0x08] %%asi\n\t"
"stxa %0, [%1 + 0x10] %%asi\n\t"
"stxa %0, [%1 + 0x18] %%asi\n\t"
"stxa %0, [%1 + 0x20] %%asi\n\t"
"stxa %0, [%1 + 0x28] %%asi\n\t"
"stxa %0, [%1 + 0x30] %%asi\n\t"
"stxa %0, [%1 + 0x38] %%asi\n\t"
: /* no outputs */
: "r" (data), "r" (x));
#else
__asm__ __volatile__(
"stx %0, [%1 + 0x00]\n\t"
"stx %0, [%1 + 0x08]\n\t"
"stx %0, [%1 + 0x10]\n\t"
"stx %0, [%1 + 0x18]\n\t"
"stx %0, [%1 + 0x20]\n\t"
"stx %0, [%1 + 0x28]\n\t"
"stx %0, [%1 + 0x30]\n\t"
"stx %0, [%1 + 0x38]\n\t"
: /* no outputs */
: "r" (data), "r" (x));
#endif
}
__asm__ __volatile__("rd %%tick, %0"
: "=r" (t2));
t2 -= t1;
if (t2 < tbest)
tbest = t2;
}
printf("Best is %lu cycles\n", tbest - overhead);
return 0;
}