Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <37ca1f0c.1a115.187bc73b1d2.Coremail.zhangfei@nj.iscas.ac.cn>
Date: Wed, 26 Apr 2023 15:25:11 +0800 (GMT+08:00)
From: 张飞 <zhangfei@...iscas.ac.cn>
To: musl@...ts.openwall.com
Subject: Re: Re: Re: Re: memset_riscv64

Hi!
I used your test example and modified the code for the link you provided to enable it to
run on the riscv platform, just like test_memset1.c and test_memset2.c.

Here are the test results for test_memset1.c:
                                 First run result
------------------------------------------------------------------------------------
              |   C language implementation      | Basic instruction implementation
------------------------------------------------------------------------------------
size(bytes)   |  min_time(s)       speed(GB/s)   |  min_time(s)       speed(GB/s)
------------------------------------------------------------------------------------
16            |  0.000022152          0.18       |  0.000020818          0.20       
32            |  0.000026968          0.30       |  0.000023380          0.35       
48            |  0.000027450          0.45       |  0.000023860          0.52       
64            |  0.000029215          0.56       |  0.000024341          0.67       
96            |  0.000030178          0.81       |  0.000025302          0.97       
200           |  0.000033228          1.54       |  0.000027864          1.84       
300           |  0.000036279          2.12       |  0.000031227          2.46       
400           |  0.000039810          2.57       |  0.000033949          3.02       
600           |  0.000047836          3.21       |  0.000040515          3.79       
1024          |  0.000064531          4.06       |  0.000054127          4.84      
2048          |  0.000106910          4.90       |  0.000089998          5.83      
4096          |  0.000191828          5.47       |  0.000155656          6.74       
8192          |  0.000356046          5.89       |  0.000286811          7.31       
16384         |  0.000685124          6.12       |  0.000549441          7.63       
32768         |  0.001460304          5.74       |  0.001222189          6.86      
65536         |  0.012082280          1.39       |  0.012054872          1.39
------------------------------------------------------------------------------------

                                 Second run result 
------------------------------------------------------------------------------------
              |   C language implementation      | Basic instruction implementation
------------------------------------------------------------------------------------
size(bytes)   |  min_time(s)       speed(GB/s)   |  min_time(s)       speed(GB/s)
------------------------------------------------------------------------------------
16            |  0.000021755          0.19       |  0.000020750          0.20      
32            |  0.000026484          0.31       |  0.000022810          0.36      
48            |  0.000026957          0.46       |  0.000023601          0.52       
64            |  0.000028692          0.57       |  0.000023918          0.69       
96            |  0.000029638          0.83       |  0.000024868          0.99       
200           |  0.000032633          1.57       |  0.000027403          1.87       
300           |  0.000035628          2.16       |  0.000030887          2.49       
400           |  0.000038781          2.64       |  0.000033580          3.05        
600           |  0.000046979          3.27       |  0.000040233          3.82       
1024          |  0.000063532          4.13       |  0.000053538          4.90     
2048          |  0.000104993          4.99       |  0.000088861          5.90    
4096          |  0.000188389          5.57       |  0.000153804          6.82        
8192          |  0.000349664          6.00       |  0.000283691          7.39       
16384         |  0.000673000          6.23       |  0.000543464          7.72       
32768         |  0.001433181          5.85       |  0.001217448          6.89     
65536         |  0.011850111          1.42       |  0.011945281          1.40
------------------------------------------------------------------------------------

                                 Third run result 
------------------------------------------------------------------------------------
              |   C language implementation      | Basic instruction implementation
------------------------------------------------------------------------------------
size(bytes)   |  min_time(s)       speed(GB/s)   |  min_time(s)       speed(GB/s)
------------------------------------------------------------------------------------
16            |  0.000021885          0.19       |  0.000020816          0.20      
32            |  0.000026642          0.31       |  0.000023040          0.36      
48            |  0.000027118          0.45       |  0.000023676          0.52       
64            |  0.000028863          0.57       |  0.000024311          0.67      
96            |  0.000029814          0.82       |  0.000024947          0.99      
200           |  0.000034413          1.49       |  0.000027648          1.85       
300           |  0.000035841          2.14       |  0.000031144          2.47       
400           |  0.000039329          2.60       |  0.000034005          3.01        
600           |  0.000047259          3.25       |  0.000040360          3.81       
1024          |  0.000063752          4.11       |  0.000053867          4.87     
2048          |  0.000105620          4.96       |  0.000089302          5.87   
4096          |  0.000189513          5.53       |  0.000154610          6.78       
8192          |  0.000351749          5.96       |  0.000284591          7.37        
16384         |  0.000676855          6.20       |  0.000545187          7.69        
32768         |  0.001440141          5.82       |  0.001208756          6.94      
65536         |  0.011974218          1.40       |  0.011976172          1.40
------------------------------------------------------------------------------------

Here are the test results for test_memset2.c:

                        C language implementation
Random memset (bytes/ns):
           memset_call 32K: 0.36 64K: 0.29 128K: 0.25 256K: 0.23 512K: 0.22 1024K: 0.21 avg 0.25

Medium memset (bytes/ns):
           memset_call 8B: 0.28 16B: 0.30 32B: 0.48 64B: 0.86 128B: 1.55 256B: 2.60 512B: 3.86
Large memset (bytes/ns):
           memset_call 1K: 4.82 2K: 5.40 4K: 5.83 8K: 6.09 16K: 6.22 32K: 6.14 64K: 1.39

                        Basic instruction implementation
Random memset (bytes/ns):
           memset_call 32K: 0.45 64K: 0.35 128K: 0.30 256K: 0.28 512K: 0.27 1024K: 0.25 avg 0.30

Medium memset (bytes/ns):
           memset_call 8B: 0.18 16B: 0.48 32B: 0.91 64B: 1.63 128B: 2.71 256B: 4.40 512B: 5.67
Large memset (bytes/ns):
           memset_call 1K: 6.62 2K: 7.03 4K: 7.47 8K: 7.71 16K: 7.83 32K: 7.64 64K: 1.40

From the test results, it can be seen that the memset implemented in C language performs better 
at around 8 bytes, while in other cases, the assembly implementation will perform better.

Fei Zhang
&gt; -----原始邮件-----
&gt; 发件人: "Szabolcs Nagy" <nsz@...t70.net>
&gt; 发送时间: 2023-04-21 21:30:34 (星期五)
&gt; 收件人: "张飞" <zhangfei@...iscas.ac.cn>
&gt; 抄送: musl@...ts.openwall.com
&gt; 主题: Re: Re: Re: [musl] memset_riscv64
&gt; 
&gt; * 张飞 <zhangfei@...iscas.ac.cn> [2023-04-20 16:17:10 +0800]:
&gt; &gt; Hi!
&gt; &gt; I listened to your suggestions and referred to string.c in Musl's test set(libc-bench), 
&gt; &gt; and then modified the test cases. Since BUFLEN is a fixed value in strlen.c, I modified 
&gt; &gt; it to a variable as a parameter in my own test case and passed it to the memset function. 
&gt; &gt; I adjusted the LOOP_TIMES has been counted up to 500 times and the running time has been 
&gt; &gt; sorted, only recording the running time of the middle 300 times.
&gt; &gt; 
&gt; &gt; I took turns executing two programs on the SiFive chip three times each, and the results 
&gt; &gt; are shown below.
&gt; &gt;                              First run result
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; length(byte)  C language implementation(s)   Basic instruction implementation(s)
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; 100                 0.002208102                     0.002304056
&gt; &gt; 200                 0.005053208                     0.004629598
&gt; &gt; 400                 0.008666684                     0.007739176
&gt; &gt; 800                 0.014065196                     0.012372702
&gt; &gt; 1600                0.023377685                     0.020090966
&gt; &gt; 3200                0.040221849                     0.034059631
&gt; &gt; 6400                0.072095377                     0.060028906
&gt; &gt; 12800               0.134040475                     0.110039387
&gt; &gt; 25600               0.257426806                     0.210710952
&gt; &gt; 51200               1.173755160                     1.121833227
&gt; &gt; 102400              3.693170402                     3.637194098
&gt; &gt; 204800              8.919975455                     8.865504460
&gt; &gt; 409600             19.410922418                    19.360956493
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; 
&gt; &gt;                              Second run result 
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; length(byte)  C language implementation(s)   Basic instruction implementation(s)
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; 100                 0.002208109                     0.002293857
&gt; &gt; 200                 0.005057374                     0.004640669
&gt; &gt; 400                 0.008674218                     0.007760795
&gt; &gt; 800                 0.014068582                     0.012417084
&gt; &gt; 1600                0.023381095                     0.020124496
&gt; &gt; 3200                0.040225138                     0.034093181
&gt; &gt; 6400                0.072098744                     0.060069574
&gt; &gt; 12800               0.134043954                     0.110088141
&gt; &gt; 25600               0.256453187                     0.208578633
&gt; &gt; 51200               1.166602505                     1.118972796
&gt; &gt; 102400              3.684957231                     3.635116808
&gt; &gt; 204800              8.916302592                     8.861590734
&gt; &gt; 409600             19.411057216                    19.358777670
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; 
&gt; &gt;                              Third run result 
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; length(byte)  C language implementation(s)   Basic instruction implementation(s)
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; 100                 0.002208111                     0.002293227
&gt; &gt; 200                 0.005056101                     0.004628539
&gt; &gt; 400                 0.008677756                     0.007748687
&gt; &gt; 800                 0.014085242                     0.012404443
&gt; &gt; 1600                0.023397782                     0.020115710
&gt; &gt; 3200                0.040242985                     0.034084435
&gt; &gt; 6400                0.072116665                     0.060063767
&gt; &gt; 12800               0.134060262                     0.110082427
&gt; &gt; 25600               0.257865186                     0.209101754
&gt; &gt; 51200               1.174257177                     1.117753408
&gt; &gt; 102400              3.696518162                     3.635417503
&gt; &gt; 204800              8.929357747                     8.858765915
&gt; &gt; 409600             19.426520562                     19.356515671
&gt; &gt; --------------------------------------------------------------------------------
&gt; &gt; 
&gt; &gt; From the test results, it can be seen that the runtime of memset implemented using the basic 
&gt; &gt; instruction set assembly is basically shorter than that implemented using the C language. 
&gt; &gt; May I ask if the test results are convincing?
&gt; 
&gt; small sizes are much more common than large sizes, memsets can be
&gt; distributed such that sizes [0,100), [100,1000), [1000,inf) are
&gt; used for 1/3 of all memsets each (not the call count, but the
&gt; amount of bytes memset using such sizes), i.e. if you speed up
&gt; the size = [100,1000) and [1000,inf) cases by 10% but regress the
&gt; [0,100) case by 20% then the overall performance roughly stays
&gt; the same. (of course this is very workload dependent, but across
&gt; a system this is what i'd expect, probably even more skewed to
&gt; smaller sizes).
&gt; 
&gt; so we need to know what happens in the [0,100) range. what i see
&gt; is a ~4% regression there while there is a ~10% improvement in
&gt; the [100,1000) case and ~15% improvement in the [1000,inf) case
&gt; (it would be nice to know why the 25k case is so much faster and
&gt; why that speed up only applies to that size, we don't want to
&gt; optimize for some obscure cpu bug that will go away next year)
&gt; 
&gt; on practical workloads i would expect &lt; 10% speedup overall from
&gt; the asm code (but we need more data in the [0,100) range to tell).
&gt; this may not be enough to justify the asm code.
&gt; 
&gt; rich already said he prefers a different style of implementation
&gt; (where the body of the function is in c but the inner loop is in
&gt; asm if that helps e.g. via simd).
&gt; 
&gt; here is an example of a benchmark that takes input distribution
&gt; into account from a workload:
&gt; https://github.com/ARM-software/optimized-routines/blob/master/string/bench/memset.c#L53
&gt; 
&gt; &gt; #include <stdio.h>
&gt; &gt; #include <stdlib.h>
&gt; &gt; #include <string.h>
&gt; &gt; #include <time.h>
&gt; &gt; 
&gt; &gt; #define BUFLEN 500000
&gt; &gt; #define LOOP_TIMES 500
&gt; &gt; 
&gt; &gt; int cmp(const void *a, const void *b) {
&gt; &gt;     double x = *(double *)a;
&gt; &gt;     double y = *(double *)b;
&gt; &gt;     if (x &lt; y) return -1;
&gt; &gt;     if (x &gt; y) return 1;
&gt; &gt;     return 0;
&gt; &gt; }
&gt; &gt; 
&gt; &gt; int main(){
&gt; &gt;         char *buf = malloc(BUFLEN);
&gt; &gt; 	double *arr = malloc(sizeof(double) * LOOP_TIMES);
&gt; &gt;         size_t i,j,k;
&gt; &gt;         struct timespec tv0,tv;
&gt; &gt; 	double times;
&gt; &gt; 
&gt; &gt;         for(j=100; j<buflen; j*="2){"> &gt;           for(k=0; k<loop_times; k++){=""> &gt;             for (i=0; i&lt;100; i++)
&gt; &gt;                   memset(buf+i, i, j-i);
&gt; &gt;           }
&gt; &gt;         }
&gt; &gt; 
&gt; &gt;         for(j=100; j<buflen; j*="2){"> &gt;           for(k=0; k<loop_times; k++){=""> &gt;             clock_gettime(CLOCK_REALTIME, &amp;tv0);
&gt; &gt;             for (i=0; i&lt;100; i++)
&gt; &gt;                   memset(buf+i, i, j-i);
&gt; 
&gt; alignment only matters up to 64 byte alignment and usually inputs
&gt; are at least 8byte aligned.
&gt; 
&gt; value is almost always 0. (we probably don't even need to test
&gt; non-0 case: a 0 check is correctly predicted in practice.)
&gt; 
&gt; i think length should have a small variation, just enough to add
&gt; penalty to small size checks where implementations may use many
&gt; branches.
&gt; 
&gt; so something like this may be better (madeup off,al numbers):
&gt; 
&gt; 	buf = malloc((1&lt;&lt;16)+32);
&gt; 	size_t sz[] = {16, 32, 48, 64, 96, 200, 300, 400, 600, 1&lt;&lt;10, 1&lt;&lt;11, 1&lt;&lt;12, 1&lt;&lt;13, 1&lt;&lt;14, 1&lt;&lt;15, 1&lt;&lt;16, 0};
&gt; 	size_t off[16] = {0, 0, 0, -8, 8, 16, 0, 0, -16, -12, 0, 4, -4, 0, 0, 12};
&gt; 	size_t al[16] = {0, 0, 8, 4, 8, 0, 8, 16, 8, 16, 4, 2, 1, 8, 16, 1};
&gt; 	for (j=0; sz[j]; j++)
&gt; 		for (k=0; k&lt;20; k++) {
&gt; 			t0 = tic();
&gt; 			// large loop count is important for small sizes
&gt; 			for (i=0; i&lt;256; i++)
&gt; 				memset(buf + al[i%16], 0, sz[j] + off[i%16]);
&gt; 			t1 = tic();
&gt; 			tmin = min(tmin,t1-t0);
&gt; 		}
&gt; 
&gt; large memset (&gt;=1k) can be tested separately (no.need to add off,al
&gt; variaion then, less inner loop is enough, but it should not hurt to
&gt; include them here).
&gt; 
&gt; &gt;             clock_gettime(CLOCK_REALTIME, &amp;tv);
&gt; &gt;             tv.tv_sec -= tv0.tv_sec;
&gt; &gt;             if ((tv.tv_nsec -= tv0.tv_nsec) &lt; 0) {
&gt; &gt;                 tv.tv_nsec += 1000000000;
&gt; &gt;                 tv.tv_sec--;
&gt; &gt;             }
&gt; &gt; 	    arr[k] = tv.tv_sec + (double)tv.tv_nsec/1000000000;
&gt; &gt;           }
&gt; &gt;           qsort(arr, 500, sizeof(double), cmp); 
&gt; 
&gt; just take the minimum. we want to know the fastest execution.
&gt; 
&gt; &gt;           
&gt; &gt; 	  for (int m = 100; m &lt; LOOP_TIMES - 100; m++) {
&gt; &gt;               times += arr[m];
&gt; &gt;           }
&gt; &gt; 	  printf("len: %ld  time: %.9lf\n",j, times);
&gt; 
&gt; you can also print GB/s which is 256*sz[j]/tmin in my example.
&gt; 
&gt; &gt; 	}
&gt; &gt;         free(buf);
&gt; &gt;         return 0;
&gt; &gt; }
</loop_times;></buflen;></loop_times;></buflen;></time.h></string.h></stdlib.h></stdio.h></zhangfei@...iscas.ac.cn></zhangfei@...iscas.ac.cn></nsz@...t70.net>
View attachment "test_memset1.c" of type "text/plain" (1395 bytes)

View attachment "test_memset2.c" of type "text/plain" (7461 bytes)

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.