# [Scilab-users] need a more efficient and faster code: suggestions welcome

20 messages
Open this post in threaded view
|

## [Scilab-users] need a more efficient and faster code: suggestions welcome

 Dear friends,problem [below] is solved, but I need a more efficient and faster code: this is a Monte-Carlo simulation of n random points in a 3d spherical volume of radius r. A typical case is n=20,000 and r=23 mm. And from this, I need the resulting probability distribution of nearest neighbours. First attempt with doubly nested for-loops took 6 hours execution time. Version below with more vecctor operations is down to 6 minutes, but suggestions for further improvements would be welcome. GreetingsHeinz n=20000;r=23;radius = r*grand(n,1,'def').^(1/3);phi = 2*%pi*grand(n,1, 'def');costheta = 1 - 2*grand(n,1, 'def');radsintheta = radius.*sin(acos(costheta));X = [radsintheta.*cos(phi),radsintheta.*sin(phi), radius.*costheta];ONE=(ones(1:n-1))';MinDist=[];for i=1:n;    this=X(i,:);XX=X;XX(i,:)=[];    DIFF=XX-ONE*this;    MinDist=[MinDist sqrt(min(sum(DIFF.^2,2)))];end;jj=-0.05:0.1:3.05;nj=length(jj);[0.05+jj(1:nj-1)' (histc(jj,MinDist))']```RESULTING OUTPUT DIST. PROBABILITY [mm] 0. 0.0003 0.1 0.0044 0.2 0.0202 0.3 0.0417 0.4 0.06735 0.5 0.0984 0.6 0.12555 0.7 0.13335 0.8 0.1357 0.9 0.1155 1. 0.09735 1.1 0.0684 1.2 0.0438 1.3 0.02495 1.4 0.01385 1.5 0.00535 1.6 0.0023 1.7 0.00095 1.8 0.0005 1.9 0.00005 2. 0.00005 2.1 0. ... ....```_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: {EXT} need a more efficient and faster code: suggestions welcome

 Hello, The following suggestions will probably not have a drastic influence (I don't see how it could be more vectorised) but his a little thing I see: > De : users [mailto:[hidden email]] De la part de Heinz Nabielek > Envoyé : mercredi 31 janvier 2018 00:13 > >    MinDist=[MinDist sqrt(min(sum(DIFF.^2,2)))]; Maybe you could concatenate the squares of the distance and then compute the square root of the whole vector in the end: sqMinDist=[sqMinDist min(sum(DIFF.^2,2))]; … end … MinDist = sqrt(sqMinDist) Hope this helps, Regards -- Christophe Dang Ngoc Chan Mechanical calculation engineer This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: {EXT} need a more efficient and faster code: suggestions welcome

 Thanks Christophe- I should have thought of it myself... I have difficulties handling the SciLab timer: how do you do this exactly? Heinz PS: Had you noticed that the resulting probability had been predicted by Prof Paul Hertz in Heidelberg, Mathematische Annalen, Vol 67 (1909), 387ff as early as 20 December 1908? My 'real' materials problem is much more complex, but this part shown here would determine the time consuming part. -----Original Message----- From: users [mailto:[hidden email]] On Behalf Of Dang Ngoc Chan, Christophe Sent: 31 January 2018 09:36 To: Users mailing list for Scilab <[hidden email]> Subject: Re: [Scilab-users] {EXT} need a more efficient and faster code: suggestions welcome Hello, The following suggestions will probably not have a drastic influence (I don't see how it could be more vectorised) but his a little thing I see: > De : users [mailto:[hidden email]] De la part de Heinz > Nabielek Envoyé : mercredi 31 janvier 2018 00:13 > >    MinDist=[MinDist sqrt(min(sum(DIFF.^2,2)))]; Maybe you could concatenate the squares of the distance and then compute the square root of the whole vector in the end: sqMinDist=[sqMinDist min(sum(DIFF.^2,2))]; … end … MinDist = sqrt(sqMinDist) Hope this helps, Regards -- Christophe Dang Ngoc Chan Mechanical calculation engineer This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: {EXT} need a more efficient and faster code: suggestions welcome

 In reply to this post by Christophe Dang Ngoc Chan Replacing      MinDist=[MinDist sqrt(min(sum(DIFF.^2,2)))]; by      MinDist=[MinDist sqrt(min(sum(DIFF.*DIFF,2)))]; will be at least twice faster. Crunching elapsed time could be done by using parallel_run (with 5.5.2 version) if you have a multi-core processor. S. Le 31/01/2018 à 09:36, Dang Ngoc Chan, Christophe a écrit : > Hello, > > The following suggestions will probably not have a drastic influence > (I don't see how it could be more vectorised) > but his a little thing I see: > >> De : users [mailto:[hidden email]] De la part de Heinz Nabielek >> Envoyé : mercredi 31 janvier 2018 00:13 >> >>     MinDist=[MinDist sqrt(min(sum(DIFF.^2,2)))]; > Maybe you could concatenate the squares of the distance > and then compute the square root of the whole vector in the end: > > sqMinDist=[sqMinDist min(sum(DIFF.^2,2))]; > > … > > end > > … > > MinDist = sqrt(sqMinDist) > > Hope this helps, > > Regards > > -- > Christophe Dang Ngoc Chan > Mechanical calculation engineer > This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. > _______________________________________________ > users mailing list > [hidden email] > http://lists.scilab.org/mailman/listinfo/users-- Stéphane Mottelet Ingénieur de recherche EA 4297 Transformations Intégrées de la Matière Renouvelable Département Génie des Procédés Industriels Sorbonne Universités - Université de Technologie de Compiègne CS 60319, 60203 Compiègne cedex Tel : +33(0)344234688 http://www.utc.fr/~mottelet_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: {EXT} need a more efficient and faster code: suggestions welcome

 moreover,      MinDist=[MinDist sqrt(min((DIFF.*DIFF)*[1;1;1]))]; is even faster. S. Le 31/01/2018 à 10:53, Stéphane Mottelet a écrit : > Replacing > >     MinDist=[MinDist sqrt(min(sum(DIFF.^2,2)))]; > > by > >     MinDist=[MinDist sqrt(min(sum(DIFF.*DIFF,2)))]; > > will be at least twice faster. Crunching elapsed time could be done by > using parallel_run (with 5.5.2 version) if you have a multi-core > processor. > > S. > > Le 31/01/2018 à 09:36, Dang Ngoc Chan, Christophe a écrit : >> Hello, >> >> The following suggestions will probably not have a drastic influence >> (I don't see how it could be more vectorised) >> but his a little thing I see: >> >>> De : users [mailto:[hidden email]] De la part de >>> Heinz Nabielek >>> Envoyé : mercredi 31 janvier 2018 00:13 >>> >>>     MinDist=[MinDist sqrt(min(sum(DIFF.^2,2)))]; >> Maybe you could concatenate the squares of the distance >> and then compute the square root of the whole vector in the end: >> >> sqMinDist=[sqMinDist min(sum(DIFF.^2,2))]; >> >> … >> >> end >> >> … >> >> MinDist = sqrt(sqMinDist) >> >> Hope this helps, >> >> Regards >> >> -- >> Christophe Dang Ngoc Chan >> Mechanical calculation engineer >> This e-mail may contain confidential and/or privileged information. >> If you are not the intended recipient (or have received this e-mail >> in error), please notify the sender immediately and destroy this >> e-mail. Any unauthorized copying, disclosure or distribution of the >> material in this e-mail is strictly forbidden. >> _______________________________________________ >> users mailing list >> [hidden email] >> http://lists.scilab.org/mailman/listinfo/users> > -- Stéphane Mottelet Ingénieur de recherche EA 4297 Transformations Intégrées de la Matière Renouvelable Département Génie des Procédés Industriels Sorbonne Universités - Université de Technologie de Compiègne CS 60319, 60203 Compiègne cedex Tel : +33(0)344234688 http://www.utc.fr/~mottelet_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: ?==?utf-8?q? {EXT} need a more efficient and faster code: suggestions welcome

 In reply to this post by mottelet Hello Stéphane, Sorry to hijack the discussion but I didn't know that there was such a difference between A.*A and A.^2. Could you tell us more about it? Why is is twice faster to use the A.*A form? Is this documented somewhere? Cheers, Antoine     Le Mercredi, Janvier 31, 2018 10:53 CET, Stéphane Mottelet <[hidden email]> a écrit:   > Replacing > >      MinDist=[MinDist sqrt(min(sum(DIFF.^2,2)))]; > > by > >      MinDist=[MinDist sqrt(min(sum(DIFF.*DIFF,2)))]; > > will be at least twice faster. Crunching elapsed time could be done by > using parallel_run (with 5.5.2 version) if you have a multi-core processor. > > S. > > Le 31/01/2018 à 09:36, Dang Ngoc Chan, Christophe a écrit : > > Hello, > > > > The following suggestions will probably not have a drastic influence > > (I don't see how it could be more vectorised) > > but his a little thing I see: > > > >> De : users [mailto:[hidden email]] De la part de Heinz Nabielek > >> Envoyé : mercredi 31 janvier 2018 00:13 > >> > >>     MinDist=[MinDist sqrt(min(sum(DIFF.^2,2)))]; > > Maybe you could concatenate the squares of the distance > > and then compute the square root of the whole vector in the end: > > > > sqMinDist=[sqMinDist min(sum(DIFF.^2,2))]; > > > > … > > > > end > > > > … > > > > MinDist = sqrt(sqMinDist) > > > > Hope this helps, > > > > Regards > > > > -- > > Christophe Dang Ngoc Chan > > Mechanical calculation engineer > > This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. > > _______________________________________________ > > users mailing list > > [hidden email] > > http://lists.scilab.org/mailman/listinfo/users> > > -- > Stéphane Mottelet > Ingénieur de recherche > EA 4297 Transformations Intégrées de la Matière Renouvelable > Département Génie des Procédés Industriels > Sorbonne Universités - Université de Technologie de Compiègne > CS 60319, 60203 Compiègne cedex > Tel : +33(0)344234688 > http://www.utc.fr/~mottelet> > _______________________________________________ > users mailing list > [hidden email] > http://lists.scilab.org/mailman/listinfo/users> _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: ?==?utf-8?q? {EXT} need a more efficient and faster code: suggestions welcome

 I am an old school guy and have learned scientific computing with Fortran... Using mutiplication instead of power elevation is an old trick which should not be necessary with a more clever interpreter (which should detect that 2 is actually a (small) integer and use multiplication instead) S. Le 31/01/2018 à 11:22, Antoine Monmayrant a écrit : > Hello Stéphane, > > Sorry to hijack the discussion but I didn't know that there was such a difference between A.*A and A.^2. > Could you tell us more about it? > Why is is twice faster to use the A.*A form? > Is this documented somewhere? > > Cheers, > > Antoine >   >   > Le Mercredi, Janvier 31, 2018 10:53 CET, Stéphane Mottelet <[hidden email]> a écrit: >   >> Replacing >> >>       MinDist=[MinDist sqrt(min(sum(DIFF.^2,2)))]; >> >> by >> >>       MinDist=[MinDist sqrt(min(sum(DIFF.*DIFF,2)))]; >> >> will be at least twice faster. Crunching elapsed time could be done by >> using parallel_run (with 5.5.2 version) if you have a multi-core processor. >> >> S. >> >> Le 31/01/2018 à 09:36, Dang Ngoc Chan, Christophe a écrit : >>> Hello, >>> >>> The following suggestions will probably not have a drastic influence >>> (I don't see how it could be more vectorised) >>> but his a little thing I see: >>> >>>> De : users [mailto:[hidden email]] De la part de Heinz Nabielek >>>> Envoyé : mercredi 31 janvier 2018 00:13 >>>> >>>>      MinDist=[MinDist sqrt(min(sum(DIFF.^2,2)))]; >>> Maybe you could concatenate the squares of the distance >>> and then compute the square root of the whole vector in the end: >>> >>> sqMinDist=[sqMinDist min(sum(DIFF.^2,2))]; >>> >>> … >>> >>> end >>> >>> … >>> >>> MinDist = sqrt(sqMinDist) >>> >>> Hope this helps, >>> >>> Regards >>> >>> -- >>> Christophe Dang Ngoc Chan >>> Mechanical calculation engineer >>> This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. >>> _______________________________________________ >>> users mailing list >>> [hidden email] >>> http://lists.scilab.org/mailman/listinfo/users>> >> -- >> Stéphane Mottelet >> Ingénieur de recherche >> EA 4297 Transformations Intégrées de la Matière Renouvelable >> Département Génie des Procédés Industriels >> Sorbonne Universités - Université de Technologie de Compiègne >> CS 60319, 60203 Compiègne cedex >> Tel : +33(0)344234688 >> http://www.utc.fr/~mottelet>> >> _______________________________________________ >> users mailing list >> [hidden email] >> http://lists.scilab.org/mailman/listinfo/users>> > _______________________________________________ > users mailing list > [hidden email] > http://lists.scilab.org/mailman/listinfo/users-- Stéphane Mottelet Ingénieur de recherche EA 4297 Transformations Intégrées de la Matière Renouvelable Département Génie des Procédés Industriels Sorbonne Universités - Université de Technologie de Compiègne CS 60319, 60203 Compiègne cedex Tel : +33(0)344234688 http://www.utc.fr/~mottelet_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: ?==?utf-8?q? ?==?utf-8?q? ?= {EXT} need a more efficient and faster code: suggestions welcom

 Argh, OK, I get it: scilab treats A.^2 exactly like A.^%pi and not like "square it". Makes sense, thank you for the info. I wonder how julia is performing with respect to A.^2 compared to A.*A... Antoine     Le Mercredi, Janvier 31, 2018 11:30 CET, Stéphane Mottelet <[hidden email]> a écrit:   > I am an old school guy and have learned scientific computing with > Fortran... Using mutiplication instead of power elevation is an old > trick which should not be necessary with a more clever interpreter > (which should detect that 2 is actually a (small) integer and use > multiplication instead) > > S. > > Le 31/01/2018 à 11:22, Antoine Monmayrant a écrit : > > Hello Stéphane, > > > > Sorry to hijack the discussion but I didn't know that there was such a difference between A.*A and A.^2. > > Could you tell us more about it? > > Why is is twice faster to use the A.*A form? > > Is this documented somewhere? > > > > Cheers, > > > > Antoine > >   > >   > > Le Mercredi, Janvier 31, 2018 10:53 CET, Stéphane Mottelet <[hidden email]> a écrit: > >   > >> Replacing > >> > >>       MinDist=[MinDist sqrt(min(sum(DIFF.^2,2)))]; > >> > >> by > >> > >>       MinDist=[MinDist sqrt(min(sum(DIFF.*DIFF,2)))]; > >> > >> will be at least twice faster. Crunching elapsed time could be done by > >> using parallel_run (with 5.5.2 version) if you have a multi-core processor. > >> > >> S. > >> > >> Le 31/01/2018 à 09:36, Dang Ngoc Chan, Christophe a écrit : > >>> Hello, > >>> > >>> The following suggestions will probably not have a drastic influence > >>> (I don't see how it could be more vectorised) > >>> but his a little thing I see: > >>> > >>>> De : users [mailto:[hidden email]] De la part de Heinz Nabielek > >>>> Envoyé : mercredi 31 janvier 2018 00:13 > >>>> > >>>>      MinDist=[MinDist sqrt(min(sum(DIFF.^2,2)))]; > >>> Maybe you could concatenate the squares of the distance > >>> and then compute the square root of the whole vector in the end: > >>> > >>> sqMinDist=[sqMinDist min(sum(DIFF.^2,2))]; > >>> > >>> … > >>> > >>> end > >>> > >>> … > >>> > >>> MinDist = sqrt(sqMinDist) > >>> > >>> Hope this helps, > >>> > >>> Regards > >>> > >>> -- > >>> Christophe Dang Ngoc Chan > >>> Mechanical calculation engineer > >>> This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. > >>> _______________________________________________ > >>> users mailing list > >>> [hidden email] > >>> http://lists.scilab.org/mailman/listinfo/users> >> > >> -- > >> Stéphane Mottelet > >> Ingénieur de recherche > >> EA 4297 Transformations Intégrées de la Matière Renouvelable > >> Département Génie des Procédés Industriels > >> Sorbonne Universités - Université de Technologie de Compiègne > >> CS 60319, 60203 Compiègne cedex > >> Tel : +33(0)344234688 > >> http://www.utc.fr/~mottelet> >> > >> _______________________________________________ > >> users mailing list > >> [hidden email] > >> http://lists.scilab.org/mailman/listinfo/users> >> > > _______________________________________________ > > users mailing list > > [hidden email] > > http://lists.scilab.org/mailman/listinfo/users> > > -- > Stéphane Mottelet > Ingénieur de recherche > EA 4297 Transformations Intégrées de la Matière Renouvelable > Département Génie des Procédés Industriels > Sorbonne Universités - Université de Technologie de Compiègne > CS 60319, 60203 Compiègne cedex > Tel : +33(0)344234688 > http://www.utc.fr/~mottelet> > _______________________________________________ > users mailing list > [hidden email] > http://lists.scilab.org/mailman/listinfo/users> _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: {EXT} need a more efficient and faster code: suggestions welcome

Open this post in threaded view
|

## Re: {EXT} need a more efficient and faster code: suggestions welcome

Open this post in threaded view
|

## Re: {EXT} need a more efficient and faster code: suggestions welcome

Open this post in threaded view
|

## Re: {EXT} need a more efficient and faster code: suggestions welcome

Open this post in threaded view
|

## Re: {EXT} need a more efficient and faster code: suggestions welcome

 Hi Heinz, Find herein a vectorised memory hog solution for Scilab 6, which is fast (~30 s for 20K points on Win7 laptop): // START OF CODE (Scilab 6) Clear n=20000; r=23; radius = r*grand(n,1,'def').^(1/3); phi = 2*%pi*grand(n,1, 'def'); costheta = 1 - 2*grand(n,1, 'def'); radsintheta = radius.*sin(acos(costheta)); X = [radsintheta.*cos(phi), radsintheta.*sin(phi), radius.*costheta]; tic; X2 = sum(X.*X, 'c'); X3 = X2.*.ones(n,1)'; X3 = X3 + X3'; D = abs(X3 - 2*(X*X'))'; D(1:n+1:\$) = [];  // remove diagonal 0's D = matrix(D,n-1,n); // reshape D to (n-1) x n size MinDist1 = sqrt(min(D,'r')); t1=toc(); disp(t1) jj=-0.05:0.1:3.05; H1 = histc(jj,MinDist1); disp([0.05+jj(1:\$-1)' H1']); // END OF CODE Regards, Rafael _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: {EXT} need a more efficient and faster code: suggestions welcome

 Hi, On a Intel(R) Core(TM) i3-3220T CPU @ 2.80GHz (Linux), the following code needs ~8s and it doesn't allocate that much memory. n=20000; r=23; radius = r*grand(n,1,'def').^(1/3); phi = 2*%pi*grand(n,1, 'def'); costheta = 1 - 2*grand(n,1, 'def'); radsintheta = radius.*sin(acos(costheta)); X = [radsintheta.*cos(phi),radsintheta.*sin(phi), radius.*costheta]; ONE=ones(n,1); NAN=[%nan,%nan,%nan]; SUM=[1;1;1]; MinDist=zeros(1,n); for i=1:n;     XX = X;     XX(i,:) = NAN;     DIFF = XX - ONE*X(i,:);     MinDist(i) = sqrt(min((DIFF.*DIFF)*SUM)); end; Cheers, wozai -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: {EXT} need a more efficient and faster code: suggestions welcome

Open this post in threaded view
|

## Re: {EXT} need a more efficient and faster code: suggestions welcome

Open this post in threaded view
|

## Re: {EXT} need a more efficient and faster code: suggestions welcome

Open this post in threaded view
|