From 65007a63040180ba7f84d7fbace5f11e51d7cb27 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Mon, 27 Nov 2023 12:45:19 +0000 Subject: [PATCH] add example of parallelizing a min --- examples/more/13_parallelize_min/example | Bin 0 -> 23152 bytes examples/more/13_parallelize_min/example.c | 67 +++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100755 examples/more/13_parallelize_min/example create mode 100644 examples/more/13_parallelize_min/example.c diff --git a/examples/more/13_parallelize_min/example b/examples/more/13_parallelize_min/example new file mode 100755 index 0000000000000000000000000000000000000000..92e31166613a83f99293b2f867c99ad93e83f6a5 GIT binary patch literal 23152 zcmeHvdwf&JmH(Ax2l3LCc{EN!8ik0Bl7eg#lHe^A31qk?CNaSzh8TpfBwN4OmLp4G z(u9(=4XBRubAP{VLw4KWhwXNEv!C7ll&1S&yKN&w0F&1mnsmt~DGnitgdq@FQc4Y4`EZG9OE4=FB-~&YU@OX71>EzExShw7_7HL^4X>kf_u*Ma4LI#h&|l z2EZxJmnP%;Ch4ovH7FM{oLcWx0czEBtRNejINk_Id?l300cWUaNkE|?LE_7oOcT`_ z0VRuCCcYxd^0FTO#Z4-mfTBL#UYS}i>EO9>DqcV>AKAvTP_4JD$c7T{x0OS|N9H0v zE9bLvJ^?MINUE;*0q!M~OVUz-QtkOyCqN1y+fhyGUJt@!9u9T2(nKb{Bwvpo3b zJov+T@E7yo?*iV6k3OvjBA1?hdGIxP@Z({(nR?+Ue}GT1biHIHMkEm*B+P>ra{Id^ zPgU&-kIx_WHwN1y{&4MzCC#B$f30_8vtROf8e2lGp7w}09PxNm(wv0;jy9<|)F`z- z5{?AeHx&a?TWGV?9&DxN8bd8@)MB}WjHl5b@kE-!ey`8d8f}r5-M6B~)8-9(o16X3 zX#&)6qIQzdz!mnig@dh;fTyA9VaeYSj6h9j*zhnRJOOX8S!#itP=nMSLBGOO-Kt>; zYz_w_e(90+P?%U7I=r4hu+`ffe3Uv&?eGq_c%gY9?Dwnc&2yAVq-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M##@r6wZ(L2e}zNH1m0E~oH*d%gnfZHSo-i`1( z8e5|d40Ic2^%f0kV>F11>w-RxaxJc!6Y(}mjJ(O)-XuAETUybC3L{~)hF+?`>lIlA z59-4HW-l@DTw8NQaxfQlAnRxhp-7o_e}m+R_&ZRfWd+nB7RMa^Cccm~`5;Y{R2k}u zqH>5ln)0>;Axve5J4z13$QG)6h~D+p4Tog_4$EoQnx)&7vO5Py1{9Oc@gT zi*sZF?VMknBkT3|qMptM1iv_E6R?g~kd9<$!7t8xgTPUj1;03t6R?ArG)Y&K9zh+Q z0SkU{ZYSVe&MWj6bOLTgJ)J3wvN+!pa1ZAv{Yg)uKSxWw$k3PwesK;c;4w}p+86wy z|Icv##hgx@7YZoO*@>Tc^!$GY9N9$3$3ugfIMknj>HX(>I({qX6R=EYaGiMRuXOz4 zoKiqR&$ZI4C+*Ski}OqYtygIOpLP7(_;Znf)A@c}-@Yi*3tn3LiStnb#qTwOUT?nx zoL`K;c#am(uG3$PpOE*ej$fR&3MlS974-V{-_r4mb6ElDeJyR$xBosW>3SO;cGBm! z;(k;;y(kO#F?eMSzc{}uSqLPOpi@hkvcO+L1^KRMU!3!a`))1Vur&WD2|nRhe*+wu zSn!K;s_$)O6;-_~?a z&CvNO>0hS9TbwF_AK#^GiI$QUrhQMXV&6`uTBxJoj6~Dm4t#|EBL2|tZ$fuu(`nZ| cYW9-Oe?+^2uo#(K{C~_JfUnmv=!(++2heLqCIA2c literal 0 HcmV?d00001 diff --git a/examples/more/13_parallelize_min/example.c b/examples/more/13_parallelize_min/example.c new file mode 100644 index 0000000..c7761ad --- /dev/null +++ b/examples/more/13_parallelize_min/example.c @@ -0,0 +1,67 @@ +#include "../../../squiggle.h" +#include "../../../squiggle_more.h" +#include +#include + +int main() +{ + /* Question: can we parallelize this? + A = normal(5,2) + B = min(A) + B * 20 + */ + + /* Option 1: parallelize taking from n samples */ + // Question being asked: what is the distribution of sampling 1000 times and taking the min? + double sample_min_of_n(uint64_t* seed, int n){ + double min = sample_normal(5, 2, seed); + for(int i=0; i<(n-2); i++){ + double sample = sample_normal(5, 2, seed); + if(sample < min){ + min = sample; + } + } + return min; + } + double sample_min_of_1000(uint64_t* seed) { + return sample_min_of_n(seed, 1000); + } + + int n_samples = 1000000, n_threads = 16; + double* results = malloc(n_samples * sizeof(double)); + parallel_sampler(sampler_result, results, n_threads, n_samples); + printf("Mean of the distribution of (taking the min of 1000 samples of a normal(5,2)): %f\n", array_mean(results, n_samples)); + free(results); + + /* Option 2: take the min from n samples cleverly using parallelism */ + // Question being asked: can we take the min of n samples cleverly? + double sample_n_parallel(int n){ + + int n_threads = 16; + int quotient = n / 16; + int remainder = n % 16; + + uint64_t seed = 1000; + double result_remainder = sample_min_of_n(&seed, remainder); + + double sample_min_of_quotient(uint64_t* seed) { + return sample_min_of_n(seed, quotient); + } + double* results_quotient = malloc(quotient * sizeof(double)); + parallel_sampler(sample_min_of_quotient, results_quotient, n_threads, quotient); + + double min = results_quotient[0]; + for(int i=1; i results_quotient[i]){ + min = results_quotient[i]; + } + } + if(min > result_remainder){ + min = results_remainder; + } + free(results_quotient); + return min; + } + printf("Minimum of 1M samples of normal(5,2): %f\n", sample_n_parallel(1000000)); + +}