Stan in Visual Studio

201 views
Skip to first unread message

shota...@gmail.com

unread,
Feb 24, 2014, 5:47:40 AM2/24/14
to stan-...@googlegroups.com
Hello Guys,

   I just made a little bit of progress to compile a program with stan header files using visual studio... 

The program was:

#include "stdafx.h"
#include<stan.hpp>
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
 
Yes it is minimal... Now, at least I am sure I can compile a program including stan.hpp. 

I probably build gtest and run the existing tests, or hopefully someone gives me a test program :)


Here is my little notes:
1.compile libstan.lib using visual studio

To do this,
1.0 change the platform to x64(simply because I had a bad experience with 32bit in g++).
1.1 In properties, Include boost_1.54.0, eigen_3.2.0, and stan headers which come along with the source codes of stan.
1.2 move src\stan\agrad\rev\ver_stack.hpp and src\stan\agrad\rev\ver_stack.cpp to your build directory.
1.3 then build the project with debug and release mode, which creates new folder x64. which contains Debug and Release folders. Inside them, you find libstan.lib

2.compile stan program
To do this,
2.0 start new empty console program and change the platform to x64.
2.1 In properties, Include boost_1.54.0, eigen_3.2.0, and stan headers which come along with the source codes of stan.
2.2 In properties, add library directories where they have libstan.lib for debug and release modes.
2.3 In properties, In linker, there should add libstan.lib in additional dependencies.
2.4 then you can build debug and release mode :)

3. test a simple stan program
I am here.

I am not interested in compiling stanc.exe, but if anyone is interested, I may try :)

Bob Carpenter

unread,
Feb 24, 2014, 6:07:24 AM2/24/14
to stan-...@googlegroups.com
Thanks for reporting back!

If you can get instructions that work for stanc (may need to
compile at a different optimization level), we could include
the instructions in our manual for CmdStan.

Are debug and release modes different optimization levels?
I'd be curious as to what the performance is vs. other Windows
compilers, especially the Rtools toolchain release we recommend.

stanc is the one that really stresses the template compiler
because of the way the Boost Spirit parser is implemented
in terms of grammar rules and semantic callbacks.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

shota...@gmail.com

unread,
Feb 24, 2014, 6:30:37 AM2/24/14
to stan-...@googlegroups.com
Hi Bob,

   I just compiled libstanc.lib in debug mode and release mode in 32bit/64bit. I did not check if they really work or not :P they was just compiled. that's it.
No modifications were needed.

I used the default optimization in visual studio 2012(I may have modified VS2012, which I don't remember.).
For the debug build, the optimization is /Od and for the release mode, the optimization is /O2.

I will prepare how to compile libstan.lib and libstanc.lib...with pics, later.
Anyway, I really need to read stan manual to start programming... 

2014年2月24日月曜日 20時07分24秒 UTC+9 Bob Carpenter:

shota...@gmail.com

unread,
Feb 25, 2014, 4:48:23 AM2/25/14
to stan-...@googlegroups.com
Hello Guys,

   I felt it takes a long time to write a procedure, so I made a visual studio projects(which I have attached) that should compile stan.lib, stanc.lib, print.exe, and stanc.exe.
To use this, simply save vs2012 folder in stan-2.10 folder, then import the solution and build the library with visual studio 2012 express(I do not know it will compile with previous versions).
I used the relative path, so you dont have to modify anything but the folder must be saved at an appropriate location.

When you build debug mode, it automatically generate everything in the Debug folder in the bin folder. If you build Release mode, it automatically generate everything in the bin folder.

I have compiled: 32bit Debug, 32bit Release, 64bit Debug, and 64bit Release(64 bit release is recommended).

Note that the makefile must be modified for visual studio compiler....

Enjoy.

2014年2月24日月曜日 20時30分37秒 UTC+9 shota...@gmail.com:
solution.zip

Bob Carpenter

unread,
Feb 25, 2014, 1:37:56 PM2/25/14
to stan-...@googlegroups.com
Great! If you could verify that stanc works and send us
instructions, we can include them in the CmdStan manual.

Until then, hopefully people will be able to find this
thread on our lists.

- Bob

shota...@gmail.com

unread,
Mar 11, 2014, 5:18:54 AM3/11/14
to
Hi Bob,


I realized some issues with stanc output in visual studio compilation.

1. missing #include"stdAfx.h"
This can be simply fixed by adding new function to do it.

I simply added new function in src/stan/gm/generator.hpp

    void generate_include_custom(const std::string& lib_name, std::ostream& o) {
      o << "#include" << " " << "\"" << lib_name << "\"" << EOL;
    }

and add the following code in "void generate_includes(std::ostream& o)" in the same source code

      #ifdef WIN32
         generate_include_custom("stdafx.h",o);
      #endif

in front of "generate_include("stan/model/model_header.hpp",o);"

2. compilation error of stanc outputs
I am not sure this is a bug (in visual studio) or not, but the output of stanc must be modified accordingly to avoid an error in visual studio.

I used the bernoulli example here, too. In the generated cpp file, there is lines(30-31):

bernoulli(stan::io::var_context& context__, std::ostream* pstream__ = 0) : prob_grad::prob_grad(0) 

this supposed to be 

bernoulli(stan::io::var_context& context__, std::ostream* pstream__ = 0) : prob_grad(0) 

I simply modified src/stan/gm/generator.hpp at line around 2306 from

o << INDENT2 << ": prob_grad::prob_grad(0) {" 

to

#ifdef WIN32
      o << INDENT2 << ": prob_grad(0) {" 
#else
      o << INDENT2 << ": prob_grad::prob_grad(0) {" 
#endif

3. Missing dynamic or static Boost library
In visual studio, we must compile boost to compile stan program.
Also, boost1.54.0 does not compatible with visual studio 2013, so stan program does not compile in vs2013. we must use vs2012 or we must upgrade boost to 1.56 which is under development. 

the last is good news. The bernoulli example ran!

I will give an instruction how to compile stan in visual studio when I have time.

2014年2月26日水曜日 3時37分56秒 UTC+9 Bob Carpenter:

Bob Carpenter

unread,
Mar 11, 2014, 8:54:15 AM3/11/14
to stan-...@googlegroups.com
Thanks for the update.

As to 1, I have no idea what stdAfx.h is.

Point 2 looks like a bug in the Visual Studio compiler. But
we should probably just change it to what'll work everywhere
rather than ifdef it out for Visual Studio.

Boost is an ongoing issue with versioning --- we're discussing
it on the dev list right now as it relates to R and some
of their built-in packages.

- Bob

On Mar 11, 2014, at 2:49 AM, shota...@gmail.com wrote:

> Hi Bob,
>
>
> I just compiled bernoulli example with visual studio.
>
> I realized some issues with stanc output in visual studio compilation.
>
> 1. missing #include"stdAfx.h"
> This can be simply fixed by adding new function to do it.
>
> I simply added new function in src/stan/gm/generator.hpp
>
> void generate_include_custom(const std::string& lib_name, std::ostream& o) {
> o << "#include" << " " << "\"" << lib_name << "\"" << EOL;
> }
>
> and add the following code in "void generate_includes(std::ostream& o)" in the same source code
>
> #ifdef WIN32
> generate_include_custom("stdafx.h",o);
> #endif
>
> in front of "generate_include("stan/model/model_header.hpp",o);"
>
> 2. compilation error of stanc outputs
> I am not sure this is a bug or not, but the output of stanc must be modified according to avoid an error in visual studio.
>
> I used the bernoulli example here, too. In the generated cpp file, there is lines(30-31):
>
> bernoulli(stan::io::var_context& context__, std::ostream* pstream__ = 0) : prob_grad::prob_grad(0)
>
> this supposed to be
>
> bernoulli(stan::io::var_context& context__, std::ostream* pstream__ = 0) : prob_grad(0)
>
> I simply modified src/stan/gm/generator.hpp at line around 2306 from
>
> o << INDENT2 << ": prob_grad::prob_grad(0) {"
>
> to
>
> #ifdef WIN32
> o << INDENT2 << ": prob_grad(0) {"
> #else
> o << INDENT2 << ": prob_grad::prob_grad(0) {"
> #endif
>
> 3. Missing dynamic or static Boost library
> In visual studio, we must compile boost to compile stan program.
> Also, boost1.54.0 does not compatible with visual studio 2013, so stan program does not compile in vs2013. we must use vs2012 or we must upgrade boost to 1.56 which is under development.
>
> the last is good news. The bernoulli example ran!
>
> I will give an instruction how to compile stan in visual studio when I have time.
>
> 2014年2月26日水曜日 3時37分56秒 UTC+9 Bob Carpenter:
> For more options, visit https://groups.google.com/d/optout.

shota...@gmail.com

unread,
Mar 11, 2014, 9:18:44 AM3/11/14
to stan-...@googlegroups.com
For 1, I can remove stdAfx.h from a program.
It was automatically added to visual studio project, but I can manually disable it. The downside of disabiling it is long compilation time because stdAfx.h contains precompiled headers.

2014年3月11日火曜日 21時54分15秒 UTC+9 Bob Carpenter:

Bob Carpenter

unread,
Mar 11, 2014, 9:30:40 AM3/11/14
to stan-...@googlegroups.com

On Mar 11, 2014, at 2:18 PM, shota...@gmail.com wrote:

> For 1, I can remove stdAfx.h from a program.
> It was automatically added to visual studio project, but I can manually disable it. The downside of disabiling it is long compilation time because stdAfx.h contains precompiled headers.

Thanks. I also just found this longer explanation:

http://stackoverflow.com/questions/4726155/whats-the-use-for-stdafx-h-in-visual-studio

Is "WIN32" only for Visual Studio? Or is there some other property
that would make sure it only gets included for WIN32?

We can't include a Visual Studio dependency for other versions of Windows
compilers.

I think most of our users are getting Windows through the Rtools distribution,
which I believe uses some bleeding edge pre-release version of mingw.
And some are using Cygwin.

- Bob

Daniel Lee

unread,
Mar 11, 2014, 9:42:23 AM3/11/14
to stan-...@googlegroups.com
On Tue, Mar 11, 2014 at 9:30 AM, Bob Carpenter <ca...@alias-i.com> wrote:

On Mar 11, 2014, at 2:18 PM, shota...@gmail.com wrote:

> For 1, I can remove stdAfx.h from a program.
> It was automatically added to visual studio project, but I can manually disable it. The downside of disabiling it is long compilation time because stdAfx.h contains precompiled headers.

Do you notice a difference when compiling Stan with and without stdAfx.h? Or was that a statement in general?

We've tried to use precompiled headers, but found no speed benefit due to the heavy use of templating. For other projects, I can see how it's helpful.

 

Thanks.  I also just found this longer explanation:

  http://stackoverflow.com/questions/4726155/whats-the-use-for-stdafx-h-in-visual-studio

Is "WIN32" only for Visual Studio?  Or is there some other property
that would make sure it only gets included for WIN32?

We can't include a Visual Studio dependency for other versions of Windows
compilers.

I think most of our users are getting Windows through the Rtools distribution,
which I believe uses some bleeding edge pre-release version of mingw.
And some are using Cygwin.

- Bob

>
> 2014年3月11日火曜日 21時54分15秒 UTC+9 Bob Carpenter:
> Thanks for the update.
>
> As to 1, I have no idea what stdAfx.h is.
>
> Point 2 looks like a bug in the Visual Studio compiler.  But
> we should probably just change it to what'll work everywhere
> rather than ifdef it out for Visual Studio.

Yeah -- it looks like Visual Studio isn't able to resolve the name of the constructor. We found this compiler issue with g++ 4.2 in a few spots and fixed it by fully specifying identifiers, even though it was ambiguous.

There should be no issues with Stan adding more specification in the C++ generated code. We can start with the few places you listed. If you find more, please let us know.

Bob Carpenter

unread,
Mar 11, 2014, 10:40:30 AM3/11/14
to stan-...@googlegroups.com
It looks like the class qualification needed to be removed
for WIN32, not added. Had to go back and ehckit was ifdef and
not ifndef.

- Bob

On Mar 11, 2014, at 2:42 PM, Daniel Lee <bea...@alum.mit.edu> wrote:

> ...
> Yeah -- it looks like Visual Studio isn't able to resolve the name of the constructor. We found this compiler issue with g++ 4.2 in a few spots and fixed it by fully specifying identifiers, even though it was ambiguous.


Original message:

> There should be no issues with Stan adding more specification in the C++ generated code. We can start with the few places you listed. If you find more, please let us know.
> ...

shota...@gmail.com

unread,
Mar 11, 2014, 8:55:36 PM3/11/14
to stan-...@googlegroups.com
Hi all,

   I have tested camel example in src\models\bugs_examples\col3\camel\camel.stan.

It turned out that visual studio failed to handle eiven matrix. 

To be specific, in the camel cpp source code at line 489,

        matrix_d Sigma = in__.cov_matrix_constrain(2);
        writer__.write(Sigma); #this returned an error


It seems like visual studio cannot find an appropriate function (which means it tried to use write(int n) at the line 74 instead of  void write(const Eigen::Matrix<double,R,C>& m)). 

Daniel,

I do not think there will be a big difference in compilation time because stan is a command line program.

2014年3月11日火曜日 23時40分30秒 UTC+9 Bob Carpenter:
Reply all
Reply to author
Forward
0 new messages