RStan size

66 views
Skip to first unread message

Jiqiang Guo

unread,
Jul 6, 2012, 11:07:03 PM7/6/12
to stan...@googlegroups.com
As we are trying to make the source of RStan small, do we need to worry about the binary version size?
Now, if I run a R CMD check on the source, one note is 

* checking installed package size ... NOTE
 installed size is 230.0Mb
 sub-directories of 1Mb or more:
   include  109.4Mb
   libs      74.1Mb
   libstan   46.2Mb

Here libstan folder includes libstan.so, the shared version of libstan.a;
and libs includes rstan.so, the shared version of stanc, which would be loaded as RStan is loaded. 
(the include folder still include boost for now). 

On a side note, the above is compiled with g++.  When clang++ is used, the total size is even larger. 

Compared with the static versions, the above *.so's are really big.  
-rw-r--r-- 1 jq jq 3.1M Jul  6 21:37 libstan.a
-rw-r--r-- 1 jq jq 6.7M Jul  6 21:41 libstanc.a
-rwxr-xr-x 1 jq jq 2.3M Jul  6 21:41 stanc

--
Jiqiang 


Ben Goodrich

unread,
Jul 7, 2012, 1:39:46 AM7/7/12
to stan...@googlegroups.com
On Friday, July 6, 2012 11:07:03 PM UTC-4, Jiqiang Guo wrote:
As we are trying to make the source of RStan small, do we need to worry about the binary version size?

Yes, although I don't know if there are any hard and fast rules about the installed size.
 
Now, if I run a R CMD check on the source, one note is 

* checking installed package size ... NOTE
 installed size is 230.0Mb
 sub-directories of 1Mb or more:
   include  109.4Mb
   libs      74.1Mb
   libstan   46.2Mb


include/ should ultimately be empty, once we rely on the RcppEigen package and the system Boost.

But that is still a lot bigger than any other R package I have installed

goodrich@CYBERPOWERPC:/usr/lib/R/site-library$ du -h -d 1 | sort -h | tail
3.3M    ./rgl
3.7M    ./fPortfolio
4.1M    ./Rcmdr
5.0M    ./VGAM
5.6M    ./qtl
9.5M    ./GenABEL
11M     ./Rcpp
18M     ./RGtk2
33M     ./mapdata
195M    .

You can cut RStan down a little bit more by excluding src/test/ and logos/. Also, it seems that make clean-all does not eliminate the .o files in the subdirectories of src/ so that adds to the size. Even still, CRAN is probably not going to be too happy.

 Ben

Jiqiang Guo

unread,
Jul 7, 2012, 11:03:16 AM7/7/12
to stan...@googlegroups.com
Thanks. 

You can cut RStan down a little bit more by excluding src/test/ and logos/. Also, it seems that make clean-all does not eliminate the .o files in the subdirectories of src/ so that adds to the size. Even still, CRAN is probably not going to be too happy.

All .o files and src/test, src/doc have already excluded by .Rbuildignore.  logos/ is not in Rstan as we only have link to stan/src and stan/lib. The source file would be fairly small after future excluding boost and eigen.  The main concern is two compiled shared objects inside the binary package:

jq@jq-tpw510:~/R/x86_64-pc-linux-gnu-library/2.15/rstan$ ls -lh libs
total 75M
-rwxr-xr-x 1 jq jq 75M Jul  6 22:33 rstan.so
jq@jq-tpw510:~/R/x86_64-pc-linux-gnu-library/2.15/rstan$ ls -lh libstan/
total 47M
-rwxr-xr-x 1 jq jq 47M Jul  6 22:33 libstan.so

--
Jiqiang 

Ben Goodrich

unread,
Jul 7, 2012, 1:31:36 PM7/7/12
to stan...@googlegroups.com
The size will probably be even bigger on Windows. The only things that I know to do to reduce the size of compiled objects are

-- Use -Os instead of -O2 or -O3
-- Eliminate the -g flag
-- Include the -s flag

Ben

Ben Goodrich

unread,
Jul 7, 2012, 1:51:36 PM7/7/12
to stan...@googlegroups.com

And with those options I get

* checking installed package size ...
NOTE
  installed size
is 117.5Mb

 
sub-directories of 1Mb or more:

    include  
113.6Mb
    libs      
2.7Mb
    libstan    
1.0Mb

and yet the examples still work.

Ben

P.S. Unrelated thing, but it would be better to write the *.csv files to tempdir() rather than getwd() because the working directory might not be writable by the user. But the *.csv files will get deleted when the user closes R, so we would need to direct the user to copy them elsewhere if desired.

Jiqiang Guo

unread,
Jul 7, 2012, 2:51:54 PM7/7/12
to stan...@googlegroups.com
Thanks to Ben.

The size will probably be even bigger on Windows 

Yes. presumably windows has binaries for both 32 bits and 64 bits (I have not tested on windows yet).  I am not sure what would happen to Mac. 


On Sat, Jul 7, 2012 at 1:51 PM, Ben Goodrich <goodri...@gmail.com> wrote:
On Saturday, July 7, 2012 1:31:36 PM UTC-4, Ben Goodrich wrote:
The size will probably be even bigger on Windows. The only things that I know to do to reduce the size of compiled objects are

-- Use -Os instead of -O2 or -O3
-- Eliminate the -g flag
-- Include the -s flag

 
This is amazing.  I'll see if I can put those options in the package's src/Makevars file.


And with those options I get

* checking installed package size ...
NOTE
  installed size
is 117.5Mb

 
sub-directories of 1Mb or more:

    include  
113.6Mb
    libs      
2.7Mb
    libstan    
1.0Mb

and yet the examples still work.

Ben

P.S. Unrelated thing, but it would be better to write the *.csv files to tempdir() rather than getwd() because the working directory might not be writable by the user. But the *.csv files will get deleted when the user closes R, so we would need to direct the user to copy them elsewhere if desired.

I am glad that you point that out as in the end, we need a good user interface.  For that part, what I current implemented is that if the user has sample.file specified when calling stan, csv files would be written out.  Otherwise, csv files are not generated.  

Bob Carpenter

unread,
Jul 7, 2012, 6:14:28 PM7/7/12
to stan...@googlegroups.com


On 7/7/12 1:51 PM, Ben Goodrich wrote:
> On Saturday, July 7, 2012 1:31:36 PM UTC-4, Ben Goodrich wrote:
>
> The size will probably be even bigger on Windows. The only things that I know to do to reduce the size of compiled
> objects are
>
> -- Use -Os instead of -O2 or -O3

We want to step back from -O3 only as a last
resort. It's about 10 times faster than -O0;
how the -O1 or -O2 behave differs between clang++
and g++. The upside is that it's faster to compile
with less optimization.

> -- Eliminate the -g flag

Right.

> -- Include the -s flag

What does this do?

> P.S. Unrelated thing, but it would be better to write the *.csv files to tempdir() rather than getwd() because the
> working directory might not be writable by the user. But the *.csv files will get deleted when the user closes R, so we
> would need to direct the user to copy them elsewhere if desired.

Could we do this

1. if user supplies file name, use that if it's writable, if
not bomb and say it's not writable.

2. if user does not supply a file name, use getwd() if it's
writable.

3. as a last resort, use tempdir() and issue a warning

I don't know how easy it is to test file properties in R.

- Bob

Ben Goodrich

unread,
Jul 7, 2012, 6:20:43 PM7/7/12
to stan...@googlegroups.com
On Saturday, July 7, 2012 6:14:28 PM UTC-4, Bob Carpenter wrote:


On 7/7/12 1:51 PM, Ben Goodrich wrote:
> On Saturday, July 7, 2012 1:31:36 PM UTC-4, Ben Goodrich wrote:
>
>     The size will probably be even bigger on Windows. The only things that I know to do to reduce the size of compiled
>     objects are
>
>     -- Use -Os instead of -O2 or -O3

We want to step back from -O3 only as a last
resort.  It's about 10 times faster than -O0;
how the -O1 or -O2 behave differs between clang++
and g++.  The upside is that it's faster to compile
with less optimization.

-Os is approximately equal to -O2 in speed but optimizes the file size. But maybe excluding -g and including -s is good enough.

>     -- Include the -s flag

What does this do?

Strips out unused symbols

> P.S. Unrelated thing, but it would be better to write the *.csv files to tempdir() rather than getwd() because the
> working directory might not be writable by the user. But the *.csv files will get deleted when the user closes R, so we
> would need to direct the user to copy them elsewhere if desired.

Could we do this

   1.  if user supplies file name, use that if it's writable, if
       not bomb and say it's not writable.

   2.  if user does not supply a file name, use getwd() if it's
        writable.

   3.  as a last resort, use tempdir() and issue a warning

I don't know how easy it is to test file properties in R.

file.create(path) returns TRUE if successful and FALSE otherwise.

Ben

Jiqiang Guo

unread,
Jul 8, 2012, 6:38:24 PM7/8/12
to stan...@googlegroups.com
Finally, I managed to build rstan.so and libstan.so using -Os -s by adding a Makefile in src though Makefile is not encouraged due to it might be less portable. 

It is not trivial to have a package defined CXXFLAGS (and other flags) as what is essentially done in R is 

make -f 'Makevars' -f '/usr/lib/R/etc/Makeconf' -f '/usr/share/R/share/make/shlib.mk' -f '/home/jq/.R/Makevars' SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB='rstan.so'

The first Makevars is the package supplied src/Makevars.  So whatever I do regarding CXXFLAGS in src/Makevars would be overridden later. 

But for src/Makefile, R does

make -f '/usr/lib/R/etc/Makeconf' -f 'Makefile' -f '/home/jq/.R/Makevars'

--
Jiqiang 


On Sat, Jul 7, 2012 at 2:51 PM, Jiqiang Guo <guo...@gmail.com> wrote:
On Saturday, July 7, 2012 1:31:36 PM UTC-4, Ben Goodrich wrote:
The size will probably be even bigger on Windows. The only things that I know to do to reduce the size of compiled objects are

-- Use -Os instead of -O2 or -O3
-- Eliminate the -g flag
-- Include the -s flag

Ben Goodrich

unread,
Jul 8, 2012, 6:45:42 PM7/8/12
to stan...@googlegroups.com
Is it feasible with this approach to make stanc small with -Os and -s but make libstan fast with -O3?

Ben

Jiqiang Guo

unread,
Jul 8, 2012, 8:31:37 PM7/8/12
to stan...@googlegroups.com
Is it feasible with this approach to make stanc small with -Os and -s but make libstan fast with -O3?

No if we still allow those minority of R users to set CXXFLAGS (in HOME/.R/Makevars) themselves for building packages. 

--
Jiqiang 

Bob Carpenter

unread,
Jul 9, 2012, 3:22:30 PM7/9/12
to stan...@googlegroups.com
If I understood the previous exchange correctly, your plan is to
use -Os and -s for the RStan builds by default and let
users modify HOME/.R/Makevars if they want to build a fast
version of Stan?

Do you know how much slower -Os -s is than -O3?

Given Andrew's concerns for speed, I don't think we're going
to want to release something that's significantly slower than
-O3.

In my tests, I found -O0 to be about 1/10 as fast as -O3.

- Bob


On 7/8/12 8:31 PM, Jiqiang Guo wrote:
>
> Is it feasible with this approach to make stanc small with -Os and
> -s but make libstan fast with -O3?
>
>
> No if we still allow those minority of R users to set CXXFLAGS (in
> HOME/.R/Makevars) themselves for building packages.
>
>



> Jiqiang
>
> On Sun, Jul 8, 2012 at 6:45 PM, Ben Goodrich <goodri...@gmail.com
> <mailto:goodri...@gmail.com>> wrote:
>
> Is it feasible with this approach to make stanc small with -Os and
> -s but make libstan fast with -O3?
>
> Ben
>
>
> On Sunday, July 8, 2012 6:38:24 PM UTC-4, Jiqiang Guo wrote:
>
> Finally, I managed to build rstan.so and libstan.so using -Os
> -s by adding a Makefile in src though Makefile is not
> encouraged due to it might be less portable.
>
> It is not trivial to have a package defined CXXFLAGS (and
> other flags) as what is essentially done in R is
>
> |
> make -f 'Makevars' -f '/usr/lib/R/etc/Makeconf' -f '/usr/share/R/share/make/shlib.mk
> <http://shlib.mk>' -f '/home/jq/.R/Makevars' SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB='rstan.so'

Jiqiang Guo

unread,
Jul 9, 2012, 4:19:02 PM7/9/12
to stan...@googlegroups.com
On Mon, Jul 9, 2012 at 3:22 PM, Bob Carpenter <ca...@alias-i.com> wrote:
If I understood the previous exchange correctly, your plan is to
use -Os and -s for the RStan builds by default and let
users modify HOME/.R/Makevars if they want to build a fast
version of Stan?

I guess there is no plan yet.  It is that just CRAN is too picky.  Even what I did not is 
not encouraged as the doc says O3 or O2 might not be options for other C++ compilers. 
If O3 is workable, I am happy to use that (the main problem might be the -g options which 
is the default options in my installed R.)  But I have not built the package under different 
options to see what size we end up with.  And actually what works for me now might only 
work on Linux.  On different platforms, R might treat those Makevar and Makefile differently,
which I am not sure and I do not think it is well documented.  At least I know I still 
could not make RStan work on Windows after some efforts. 

In addition, we might be able to build libstan under O3.  But when compiling the models, 
the options are up to the user's R configuration.
 

Do you know how much slower -Os -s is than -O3?

Not tested yet.  

Ben Goodrich

unread,
Jul 9, 2012, 4:50:55 PM7/9/12
to stan...@googlegroups.com
On Monday, July 9, 2012 3:22:30 PM UTC-4, Bob Carpenter wrote:
If I understood the previous exchange correctly, your plan is to
use -Os and -s for the RStan builds by default and let
users modify HOME/.R/Makevars if they want to build a fast
version of Stan?

Do you know how much slower -Os -s is than -O3?

-Os is "supposed" to be comparable to -O2, but we'll have to benchmark it.

Another consideration is what are the implications of compiling libstan with -Os but compiling the user's model with -O3? In other words, where do the performance gains come from?

Ben

Reply all
Reply to author
Forward
0 new messages