From 1436ee4e42cc0d327a92f5fd25552f6f6d14d686 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 3 Jun 2023 10:50:06 -0600 Subject: [PATCH] compare using a struct instead of a pointer, reorg --- C/README.md | 2 +- C/samples.c | 2 + C/scratchpad/makefile | 2 - C/xorshift-scratchpad/makefile | 15 ++++++ C/xorshift-scratchpad/notes.md | 1 + .../out/xorshift-pointer} | Bin 16848 -> 16816 bytes C/xorshift-scratchpad/out/xorshift-struct | Bin 0 -> 16816 bytes .../xorshift-pointer.c} | 12 +++-- C/xorshift-scratchpad/xorshift-struct.c | 46 ++++++++++++++++++ 9 files changed, 72 insertions(+), 8 deletions(-) delete mode 100644 C/scratchpad/makefile create mode 100644 C/xorshift-scratchpad/makefile create mode 100644 C/xorshift-scratchpad/notes.md rename C/{scratchpad/xorshift => xorshift-scratchpad/out/xorshift-pointer} (53%) create mode 100755 C/xorshift-scratchpad/out/xorshift-struct rename C/{scratchpad/xorshift.c => xorshift-scratchpad/xorshift-pointer.c} (78%) create mode 100644 C/xorshift-scratchpad/xorshift-struct.c diff --git a/C/README.md b/C/README.md index 261ef8a7..5f7a7035 100644 --- a/C/README.md +++ b/C/README.md @@ -12,7 +12,7 @@ This repository contains a few implementations of a simple botec (back-of-the-en - [ ] Add CUDA? - [x] Added results of perf. `rand_r` seems like a big chunk of it, but I'm hesitant to use lower-quality random numbers - [x] used xorshift instead - - [ ] Use xorshift with a struct instead of a pointer? idk, could be faster for some reason? + - [-] Use xorshift with a struct instead of a pointer? idk, could be faster for some reason? => Tested, it takes the same time. - [x] Update repository with correct timing - [x] Use better profiling approach to capture timing with 1M samples. - [x] See if program can be reworded so as to use multithreading effectively, e.g., so that you see speed gains proportional to the number of threads used diff --git a/C/samples.c b/C/samples.c index b1d2e96e..e9f13702 100644 --- a/C/samples.c +++ b/C/samples.c @@ -84,6 +84,8 @@ uint32_t xorshift32(uint32_t* seed) // Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" // See // https://en.wikipedia.org/wiki/Xorshift + // Also some drama: , + uint32_t x = *seed; x ^= x << 13; x ^= x >> 17; diff --git a/C/scratchpad/makefile b/C/scratchpad/makefile deleted file mode 100644 index 819c0de8..00000000 --- a/C/scratchpad/makefile +++ /dev/null @@ -1,2 +0,0 @@ -build: - gcc xorshift.c -o xorshift diff --git a/C/xorshift-scratchpad/makefile b/C/xorshift-scratchpad/makefile new file mode 100644 index 00000000..50e4d4b2 --- /dev/null +++ b/C/xorshift-scratchpad/makefile @@ -0,0 +1,15 @@ +build: + gcc xorshift-pointer.c -o out/xorshift-pointer + gcc xorshift-struct.c -o out/xorshift-struct + +run-pointer: + ./out/xorshift-pointer + +run-struct: + ./out/xorshift-struct + +time-pointer: + /bin/time -f "Time: %es" ./out/xorshift-pointer && echo + +time-struct: + /bin/time -f "Time: %es" ./out/xorshift-struct && echo diff --git a/C/xorshift-scratchpad/notes.md b/C/xorshift-scratchpad/notes.md new file mode 100644 index 00000000..ac64d1d9 --- /dev/null +++ b/C/xorshift-scratchpad/notes.md @@ -0,0 +1 @@ +Using a pointer or a struct turns out to be pretty equivalent in terms of speed. I don't think there are gains to be found here, but I find the pointer implementation clearer. diff --git a/C/scratchpad/xorshift b/C/xorshift-scratchpad/out/xorshift-pointer similarity index 53% rename from C/scratchpad/xorshift rename to C/xorshift-scratchpad/out/xorshift-pointer index 2a32b0b133ca1a1fab869175b01a745b7fd962bf..9f5c4c34226b649f977e8ec38aac536e01144828 100755 GIT binary patch delta 2258 zcmZ`*ZEO@p7@oPiwzpEb+qTEv3HJ8*2yIDkZ(HaU>9yU^4NHVjlq$v`R}`cPu~cd- zMoQ~h(@Sk~?aL2Bq5?)^g&)^6#u7tvfKb#Qpf*^N8cQOk1PPRn*ebZbyF1$;(Mx8Z z_w$*Vcjlek#RN?!Xv&}0;U(s;)&g>CWnamsTxGVUR`y+%xGtxqbOBE#B~2~OM#2aT zvyqi9W&YT|G48PRozO`IKEt9iEGh#7^aAe|t(aMaq+@I7{}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RpjVn1)t@JdQsOm zm|#{~O3#5+F0VONAHu)XX--U2h zu3R+9;?71B(Mc$`IeVj%$@LTs^^lq@ILCZyBAQj{1vNq9QY<=2WBVt~2TqMer_I4e;es&iT@ti7>VENP?L2}BE6xt=C&I9#cIkP8F3dMD>&%1Y_8n1 zh*WN0gmv__(Fe?HHeyf2pY)riy@t3RaR{*!@d9EBu^VyUoTg17evHU&Q~otgb0RwD zaV-#2(BWK8%b?$>2z^&IEewa9%jh9E<5cV^w7odiXiKs42L948=XB5yL3TN6e!x6S z*s4YElU_BgEzFC?%w{Vs$ma(ik0rT|zj4^?YNj_vj=HqPqH(dpaJtST4;G^zn>`0) z5N_5v=n&Z59y$OmZU;@lPB-fK1z{JLH5E>{9db2)@z|wetqZQW+o%dP9tZV8i)RPT zz_6!^t{<5|1_RD{6?C|KrDhRBxO@uj(yhg$8n0b5?Sy{x6_IWjM&E#L$yn61&qF1A zZ8mzqnFi})1?`)q8MyPoc(G z2t%_EJCv}<>H3iPu5Oi(kKjsu6&(iO3Z?fmox^oM(@4einI&+)t!1Cpi4c^$M23ntl-wZy_(6 z?EleQ@#s47qgCRU@<;_56)C^4&1d26CfER5`0cOJhLn;#L>L8v_g1#<)HzOf>e=Fti6f zFdCE~74*v8{0tcAg^8eN;U5oU@dPv2n%ttXtAo(e)QlG;-Q*FCLnpz-CM*D_&RQ3U zp@3){RMdw85Ds}^C?tu-vChE7P+;N45AqG#1aerTdqIu_#LUWEv?gee1n3_Sk5v5& DSL6ku diff --git a/C/xorshift-scratchpad/out/xorshift-struct b/C/xorshift-scratchpad/out/xorshift-struct new file mode 100755 index 0000000000000000000000000000000000000000..3c0af4ab4b499b245a5fe9b1411f28645913fb8f GIT binary patch literal 16816 zcmeHOYiwM_6`u9Ri37=c6Jjud#+Q&v3Dg@q1lNJOcpa~ET{({?b_s%Tz4or-mDaoN z?%iUe#x-3)Ss9n84;7)dqUA>`;D`TIN-I@33E)umkyH^)McTR*Q4QhYB+#0YWc!`D zbJl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qKuH~5?!@#K>4j~=qDS1Yx_-IhEOA$y9518Q|pqa9; zpL4MUjs*>hKq6^I1N#%1z+fsl7)_bOHmWBR!=bNOL{Y((_F6N%Ur6=-aJpXvBEtz( zVWF9_O1>0LrDMrNkzk-K6^)0{g%cy?Qf-|rZCy`YMv zF6Rkv#YOMvY|rn3j2g=9iQnIuYe2?szgN8%GOlAqh5X!Py4_*V@0E-iD=Ku_-wXb) zc*p1V^E!x;>*sZlv;AS%(;A5F`8}0!5@nR=9x2Xj&+C;KIJ#xF=l5C09Lns8eAv?$ zAv!J&x0QX1 z(&Kd>W1Y>OV&=5}J~)aA`=_r`nG`zTAf5gH9VpxFHD$-h^((JV_u_{Rdw$Pn ze!^kjtqd47HdM&|7=PlhA5{g6A8^Mw?Ah&$4tri-F!KK!Sl=1Hmz8~?8ck^` +#include +#include + +struct xorshift32_state { + uint32_t a; +}; + +uint32_t xorshift32(struct xorshift32_state *state) +{ + /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */ + uint32_t x = state->a; + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + return state->a = x; +} + +float rand_xorshift32(struct xorshift32_state *state){ + return (float) xorshift32(state) / (float) UINT32_MAX; +} + + +int main(){ + struct xorshift32_state** states = malloc(sizeof(struct xorshift32_state*) * 4); + for(int i=0; i<4;i++){ + states[i] = malloc(sizeof(struct xorshift32_state)); + states[i]->a = (uint32_t) i + 1; + } + + for(int i=0; i<1000000000; i++){ + uint32_t x = xorshift32(states[0]); + float y = rand_xorshift32(states[1]); + // printf("%u\n", x); + // printf("%f\n", y); + } + + for(int i=0; i<4;i++){ + free(states[i]); + } + free(states); + + + return 0; +} +