<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div><span>Hello,</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><br><span></span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><span>Thanks to both of you for the advice, I have obtained performance improvements using (declare .. (block) (not safe)) and removing the assignment to sum. My Gambit was configured with </span><span>--enable-single-host only. I have to try to add </span>--enable-gcc-opts <br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><br></div><div style="color: rgb(0, 0, 0);
 font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;">Removing the assignment to the result vector would be a bit more work I guess, but incidentally that is what the "flat" code avoids:</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><a href="https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul342.scm#L31" target="_blank">https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul342.scm#L31</a></div><div><br></div><div>...which brings me to Marc's questions:</div><div><br></div><div>"There are many things you can do to improve the performance of your 
code, but I wonder what you are trying to achieve.  For instance, your code is multiplying matrices of small exact integers.  Is this a given 
in your problem or you want the code to work also for floating point numbers?  Also, do you want the code to conform to a specific standard 
of Scheme, or you are willing to adapt your code to a specific Scheme 
implementation?  For Gambit you could store the numbers in a homogeneous
 numerical vector (e.g. s16vector, f64vector, etc) instead of a plain 
vector, and use fixnum specific operations for computing the indices of 
the matrix."</div><div><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;">I first wanted to check whether generating flat code would help to bring big performance improvements without too much specific work - basically looking for a sweet spot. So I wrote such "flat code" on this small example - manually translating from the generated JavaScript code, see the links below - and wrote to the mailing list here to make sure that I am not missing something obvious while compiling, as well as in the baselines I have been comparing with (classic and list_of_lists).<br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color:
 transparent; font-style: normal;">Now the real deal is to be able to express more complex mathematical expressions, and have "flat" code automatically generated:<br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;">http://glat.info/flatorize/#dft</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;">http://glat.info/flatorize/#push-dft1024</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;">You can click on the "Try flatorize" buttons to see the generated JavaScript code. It was not straightforward to get the code generation to work in a tractable time for significantly recursive cases such as the 1024-point DFT.<br></div><div style="color:
 rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;">Back to Marc's questions: my first goal is to remain generic and see how far one can go with the code generation - at this point of time I only wanted to eliminate big mistakes, thanks for the help.<br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;">Now, in addition, since Gambit provides many interesting optimization possibilities, I certainly would like to try them as well. The challenge
 would be to provide means to specify where the generated code should use them (instead of writing or changing everything manually). Suggestions are welcome. <br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;">Now, just speculating: could the Gambit compiler automatically detect and flatten such mathematical expressions? In such a case it would not be necessary to "pre-generate" flat code.<br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color:
 transparent; font-style: normal;">On a side note a quick try in C showed a speedup as well:</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;">https://github.com/glathoud/flatorize/blob/master/explore/C/cAll_matmul.results.txt</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;">Best regards,</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;">Guillaume<br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color:
 transparent; font-style: normal;"><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><br></div>  <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div dir="ltr"> <font face="Arial" size="2"> <hr size="1">  <b><span style="font-weight:bold;">De :</span></b> "gambit-list-request@iro.umontreal.ca" <gambit-list-request@iro.umontreal.ca><br> <b><span style="font-weight: bold;">À :</span></b> gambit-list@iro.umontreal.ca <br> <b><span style="font-weight: bold;">Envoyé le :</span></b> Mardi 9 avril 2013 18h00<br> <b><span style="font-weight: bold;">Objet :</span></b> Gambit-list Digest, Vol 103, Issue 10<br> </font> </div> <br>Today's Topics:<br><br>   1. code optimization & compilation (Guillaume
 Lathoud)<br>   2. Re: code optimization & compilation (Alex Queiroz)<br>   3. Re: code optimization & compilation (Alex Queiroz)<br>   4. Re: code optimization & compilation (Marc Feeley)<br><br><br>----------------------------------------------------------------------<br><br>Message: 1<br>Date: Tue, 9 Apr 2013 05:36:00 +0100 (BST)<br>From: Guillaume Lathoud <<a ymailto="mailto:glathoud@yahoo.fr" href="mailto:glathoud@yahoo.fr">glathoud@yahoo.fr</a>><br>Subject: [gambit-list] code optimization & compilation<br>To: Gambit List <<a ymailto="mailto:gambit-list@iro.umontreal.ca" href="mailto:gambit-list@iro.umontreal.ca">gambit-list@iro.umontreal.ca</a>><br>Message-ID:<br>    <<a ymailto="mailto:1365482160.5341.YahooMailNeo@web171306.mail.ir2.yahoo.com"
 href="mailto:1365482160.5341.YahooMailNeo@web171306.mail.ir2.yahoo.com">1365482160.5341.YahooMailNeo@web171306.mail.ir2.yahoo.com</a>><br>Content-Type: text/plain; charset="utf-8"<br><br>Hello,<br><br>on a simple task - multiply two matrices mat_a and mat_b <a href="https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul_common.scm" target="_blank">https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul_common.scm</a> - I've been comparing 3 implementations, using interpretation and compilation.<br><br>To compile I used the line below. Is there a better way, at least a few straightforward optimization (options) ?<br><br>gsc -exe -o scheme_matmul_classic.bin tmp.scmDetails are below.<br><br>Best regards,<br>Guillaume<br><br><br>1. Functional implementation: <br><a href="https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul_list_of_lists.scm#L50"
 target="_blank">https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul_list_of_lists.scm#L50</a><br><br>2. Ugly imperative implementation: <br><a href="https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul_classic.scm#L33" target="_blank">https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul_classic.scm#L33</a><br><br>3. Flat implementation (for the specific matrix sizes): <br><a href="https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul342.scm#L31" target="_blank">https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul342.scm#L31</a><br><br><br><br>To run both tests (interpreted and compiled), I used this: <br><br><a href="https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul.sh#L16" target="_blank">https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul.sh#L16</a><br><br>Results: <br><br><a
 href="https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul.results.txt" target="_blank">https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul.results.txt</a><br>-------------- next part --------------<br>An HTML attachment was scrubbed...<br>URL: <a href="http://webmail.iro.umontreal.ca/pipermail/gambit-list/attachments/20130409/df28cd69/attachment-0001.html" target="_blank">http://webmail.iro.umontreal.ca/pipermail/gambit-list/attachments/20130409/df28cd69/attachment-0001.html</a> <br><br>------------------------------<br><br>Message: 2<br>Date: Tue, 9 Apr 2013 09:32:49 +0200<br>From: Alex Queiroz <<a ymailto="mailto:asandroq@gmail.com" href="mailto:asandroq@gmail.com">asandroq@gmail.com</a>><br>Subject: Re: [gambit-list] code optimization & compilation<br>To: Guillaume Lathoud <<a ymailto="mailto:glathoud@yahoo.fr" href="mailto:glathoud@yahoo.fr">glathoud@yahoo.fr</a>><br>Cc: Gambit List <<a
 ymailto="mailto:gambit-list@iro.umontreal.ca" href="mailto:gambit-list@iro.umontreal.ca">gambit-list@iro.umontreal.ca</a>><br>Message-ID:<br>    <CABpjPc+<a ymailto="mailto:x4-vqacb79VLiX2Z26y7boPQLPunFxL3SipF0i64DGg@mail.gmail.com" href="mailto:x4-vqacb79VLiX2Z26y7boPQLPunFxL3SipF0i64DGg@mail.gmail.com">x4-vqacb79VLiX2Z26y7boPQLPunFxL3SipF0i64DGg@mail.gmail.com</a>><br>Content-Type: text/plain; charset=UTF-8<br><br>Hallo,<br><br>On Tue, Apr 9, 2013 at 6:36 AM, Guillaume Lathoud <<a ymailto="mailto:glathoud@yahoo.fr" href="mailto:glathoud@yahoo.fr">glathoud@yahoo.fr</a>> wrote:<br>><br>> To compile I used the line below. Is there a better way, at least a few<br>> straightforward optimization (options) ?<br>><br><br>Add this to the source file:<br><br>(declare<br>   (block)<br>   ; (not safe)<br>   (standard-bindings)<br>   (extended-bindings))<br><br>You can try with and without `(not safe)`
 and compare the results :)<br><br>Cheers,<br>--<br>-alex<br><a href="http://unendli.ch/" target="_blank">http://unendli.ch/</a><br><br><br>------------------------------<br><br>Message: 3<br>Date: Tue, 9 Apr 2013 09:36:56 +0200<br>From: Alex Queiroz <<a ymailto="mailto:asandroq@gmail.com" href="mailto:asandroq@gmail.com">asandroq@gmail.com</a>><br>Subject: Re: [gambit-list] code optimization & compilation<br>To: Guillaume Lathoud <<a ymailto="mailto:glathoud@yahoo.fr" href="mailto:glathoud@yahoo.fr">glathoud@yahoo.fr</a>><br>Cc: Gambit List <<a ymailto="mailto:gambit-list@iro.umontreal.ca" href="mailto:gambit-list@iro.umontreal.ca">gambit-list@iro.umontreal.ca</a>><br>Message-ID:<br>    <<a ymailto="mailto:CABpjPcLwe_0D_jAUF7s1w8uxnnpvE3nrOJYXVHg16av_-JRYtw@mail.gmail.com"
 href="mailto:CABpjPcLwe_0D_jAUF7s1w8uxnnpvE3nrOJYXVHg16av_-JRYtw@mail.gmail.com">CABpjPcLwe_0D_jAUF7s1w8uxnnpvE3nrOJYXVHg16av_-JRYtw@mail.gmail.com</a>><br>Content-Type: text/plain; charset=UTF-8<br><br>Hallo again,<br><br>On Tue, Apr 9, 2013 at 9:32 AM, Alex Queiroz <<a ymailto="mailto:asandroq@gmail.com" href="mailto:asandroq@gmail.com">asandroq@gmail.com</a>> wrote:<br>><br>> Add this to the source file:<br>><br>> (declare<br>>    (block)<br>>    ; (not safe)<br>>    (standard-bindings)<br>>    (extended-bindings))<br>><br>> You can try with and without `(not safe)` and compare the results :)<br>><br><br>Also, have you configured Gambit-C with `--enable-single-host<br>--enable-gcc-opts`?<br><br>Cheers,<br>--<br>-alex<br><a href="http://unendli.ch/" target="_blank">http://unendli.ch/</a><br><br><br>------------------------------<br><br>Message: 4<br>Date: Tue, 9 Apr 2013
 09:26:07 -0400<br>From: Marc Feeley <<a ymailto="mailto:feeley@iro.umontreal.ca" href="mailto:feeley@iro.umontreal.ca">feeley@iro.umontreal.ca</a>><br>Subject: Re: [gambit-list] code optimization & compilation<br>To: Guillaume Lathoud <<a ymailto="mailto:glathoud@yahoo.fr" href="mailto:glathoud@yahoo.fr">glathoud@yahoo.fr</a>><br>Cc: Gambit List <<a ymailto="mailto:gambit-list@iro.umontreal.ca" href="mailto:gambit-list@iro.umontreal.ca">gambit-list@iro.umontreal.ca</a>><br>Message-ID: <<a ymailto="mailto:ACE203BD-9D74-429C-827D-10D93E70F357@iro.umontreal.ca" href="mailto:ACE203BD-9D74-429C-827D-10D93E70F357@iro.umontreal.ca">ACE203BD-9D74-429C-827D-10D93E70F357@iro.umontreal.ca</a>><br>Content-Type: text/plain; charset=us-ascii<br><br><br>On 2013-04-09, at 12:36 AM, Guillaume Lathoud <<a ymailto="mailto:glathoud@yahoo.fr" href="mailto:glathoud@yahoo.fr">glathoud@yahoo.fr</a>> wrote:<br><br>> Hello,<br>> <br>>
 on a simple task - multiply two matrices mat_a and mat_b <a href="https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul_common.scm" target="_blank">https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul_common.scm</a> - I've been comparing 3 implementations, using interpretation and compilation.<br>> <br>> To compile I used the line below. Is there a better way, at least a few straightforward optimization (options) ?<br>> gsc -exe -o scheme_matmul_classic.bin tmp.scm<br>> Details are below.<br>> <br>> Best regards,<br>> Guillaume<br><br>There are many things you can do to improve the performance of your code, but I wonder what you are trying to achieve.  For instance, your code is multiplying matrices of small exact integers.  Is this a given in your problem or you want the code to work also for floating point numbers?  Also, do you want the code to conform to a specific standard of
 Scheme, or you are willing to adapt your code to a specific Scheme implementation?  For Gambit you could store the numbers in a homogeneous numerical vector (e.g. s16vector, f64vector, etc) instead of a plain vector, and use fixnum specific operations for computing the indices of the matrix.  Finally, you should avoid using assignments if you can use iteration variables instead.  So, instead of<br><br>  (let ((sum 0))<br>    (let loop ((i 0))<br>      (if (< i n)<br>          (begin<br>            (set! sum (+ sum i))<br>            (loop (+ i 1)))))<br>    sum)<br><br>you should write<br><br>  (let loop ((i 0) (sum 0))<br>    (if (< i n)<br>        (loop (+ i 1) (+ sum i))<br>       
 sum))<br><br>Marc<br><br><br><br>------------------------------<br><br>_______________________________________________<br>Gambit-list mailing list<br><a ymailto="mailto:Gambit-list@iro.umontreal.ca" href="mailto:Gambit-list@iro.umontreal.ca">Gambit-list@iro.umontreal.ca</a><br><a href="https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list" target="_blank">https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list</a><br><br><br>End of Gambit-list Digest, Vol 103, Issue 10<br>********************************************<br><br><br> </div> </div>  </div></body></html>