Home > Programming > Switching to Linux: A Windows developer’s view

Switching to Linux: A Windows developer’s view

September 14th, 2009

A few weeks ago, I switched my development environment from Windows to Linux, on a project which was developed so far on Windows only. In this post, I want to describe the issues that brought me to this switch, a short overview how I did the actual port, and some observations on Linux for developers. This is the first post in a series of at least two, the second post will describe the tools I use on Linux right now.

Background

The project I’m working on is written in C++, with some Python tools mixed in. My original development environment was Visual Studio 2005 on Windows XP. This is already the first issue: Updating Visual Studio or Windows is not trivial, as both the OS upgrade as well as IDE updates require new licenses, and especially in companies new versions are not bought immediately.

The problems became apparent when I tried to multi-thread parts of the application. At the core, it’s doing a lot of number crunching, in small work blocks which can be processed independently. As I couldn’t use OpenMP due to dependency issues (a 3rd party library could not be linked when OpenMP was enabled), I was threading manually. Unfortunately, the application had to allocate some memory in each thread, and as it turned out, the scaling on XP was catastrophic. While I did get a speedup from 1->3 cores, it became slower from 3->4 – clearly, I was hitting some issues with either the scheduler or the memory subsystem, as my code didn’t have any I/O in it.

A quick try on Vista showed that the same application ran more than twice as fast, but unfortunately, I couldn’t install my IDE on Vista as well, and developing on Windows XP and testing on Vista was out of question. Again, with a free IDE, the change would have been no problem (and the express editions don’t have x64 support*, nor OpenMP, more on this later. * See below for an update!).

On the other hand, getting a recent Linux with a recent compiler was not a problem. With Wubi, Linux can run side-by-side with Windows while giving you a full Linux based development environment. Running side-by-side is especially important when you’re in a corporate environment, as you usually cannot simply erase the disk and install Linux without making the IT angry.

Linux

I used Wubi, with the Kubuntu flavour, as I like the KDE environment a bit more than GNOME – especially as I use Qt for UI development now. Specifically, I used Kubuntu 9.04 x64, while I used a x86 Windows XP previously.

Porting

I started by checking out the code from SVN, so no problems here. Even though the application was written in standard C++ and didn’t depended on Windows-specific functionality, it was built using Visual Studio project files and used a few WinAPI calls. As a first step, I ported everything to CMake – something I could/should have done on Windows already. With CMake, I was able to quickly convert one project after the other, and immediately check for compile errors. This proved to be the best way, as I never came into the situation where I would get huge amounts of compile & linker errors at the same time; I had that when I moved an already CMake based project from Windows to Linux and tried to get it running in its entirety. During porting it’s best to port on subproject at a time, even if the project is originally using a portable build system.

As I mentioned, I used explicit threading on Windows, which I replaced by OpenMP on Linux. Now I could also throw out all configuration stuff for threading; among other things, I wouldn’t have to reduce the priority of my application on start-up – this was necessary on XP, as the machine would become unresponsive during processing otherwise. Boost.Threads might have been a valuable alternative here, but OpenMP is well suited to the kind of loop-parallelism I had in my code, and even simplified it compared to the explicit splitting/execution I used previously.

For graphics, I was already using OpenGL. As I could easily get the nVidia binary drivers running, I had no trouble on this side. Overall, it took me half a day to port, including the time to set up the Linux installation.

Results and some thoughts

The net result is interesting: The same application is running 5-10x faster now when using all four cores, so porting to Linux was really worth the hassle. I assume that with Visual Studio 2010, running on Windows 7, I would get similar performance, but the key point to take with you here is: Getting your stuff to work on Linux only costs you time, and not too much if you are a bit careful. Using CMake on Windows (or another portable build system), writing more or less clean C++ and using portable libraries makes porting to Linux easy, and the switch itself is not too complicated. At the moment, the tooling on Linux is reasonably comfortable (more on this in the next post), and the “pain factor” to switch from Visual Studio to for instance KDevelop or Eclipse CDT is no longer there.

Actually, the switch is so simple that Microsoft should get concerned. For instance, I have been developing mainly on Windows since several years, and I occasionally tried Linux, but I never did a complete switch due to various smaller and bigger problems. However, since 1-2 years, the Linux desktop, together with the tools, is good enough to provide some real benefit, especially if you cannot access the latest Microsoft products. Microsoft used to have the best developer tools by far, and quite stable APIs, which were in my opinions the corner stones of their success. However, they’re changing APIs now rather quickly (WinForms? WPF? WinAPI?), they provide new platforms which require rewriting your applications (I’m still waiting for an application like AutoCAD which has a C# UI and a C++ backend), and the tool release cycle is simply too long – waiting 2 years to get a compiler bug fixed is just ridiculous.

On the other hand, developing on Linux means you have an extremely stable API (POSIX isn’t going to be replaced with PoseFX, for instance), the UI side is rather clear (GTK or Qt, you choose), and the tools are getting better as well (GCC and LLVM are getting better quickly, and installing a new GCC does not require buying a new license). If Microsoft does not turn around the ship with Visual Studio 2010 and some clear statements on the APIs, I assume that more and more developers will find that Linux can be also a very nice environment. Again, more on this next time!

[Update] A few notes, as this article is getting a lot of attention and there are some misunderstandings. Regarding the library, I had access to the source, and I could have built it on Windows — it’s simply very time-consuming, as it depends on many libraries like zlib, which I would have to compile with the new settings on Windows as well. Getting these dependencies on Linux is obviously much easier.

OpenMP vs. manual threading: This did not improve the performance compared to the manual threading, but it was a nice bonus as it cleaned up the code a bit. On Windows, I could not use OpenMP due to link issues. The other bonus came from the switch from x86 to x64. Finally, the Linux memory allocator is much better in multithreaded environments, and this was the biggest performance improvement. In total, the application runs several times faster now, without changes on my side.

Summary: In this particular case, the switch to Linux took me just a few hours, while the benefit for me was rather big: Improved performance, and less hassle with dependencies. The drawbacks — getting used to Linux, different tools — are out weighted by the advantages for me; and that’s the main point of this blog post. I was surprised how easy it was to switch from Windows to Linux completely on this project; as I expected a lot of problems (like for instance, not being able to get the stuff running at all!)

Things might have been different if the library and the dependencies would be easier to build on Windows, and I would have gotten access to a new Windows version/new compiler version, but this is simply how things turned out, and I don’t miss my Windows setup at work.

[Update] The x64 editions of Visual C++ Express require some setup to support x64, but it is possible to add x64 support manually using the compilers from the Windows SDK. Check out the official C++ Express feature list and a guide how to enable x64.

Related posts:

  1. Linux developer tools
  2. CMake – A build tool that just works
  3. Test on multiple platforms
  4. Fedora Linux: An easy to use Linux for work (with CUDA)
  5. Building Qt from source with Visual Studio

Anteru Programming

  1. Billy Fish
    September 14th, 2009 at 14:40 | #1

    “On the other hand, developing on Linux means you have an extremely stable API (POSIX isn’t going to be replaced with PoseFX, for instance)”

    On the contrary there are still gapholes in POSIX standart: check this as an example: http://lwn.net/SubscriberLink/351422/ff7aa6bc3812f375/

    Also POSIX isn’t everything, it’s just small part of most unix systems and most programmers do not talk directly to it. Win32 is windows equivalent of POSIX with X11 and WinForms and WPF are equivalents of GTK and Qt.

    “However, they’re changing APIs now rather quickly”

    Major APIs and their introductions:

    Win32 – 1993 with Windows NT 3.1
    MFC – 1992, C++ toolkit, first version for Win16
    WinForms – 2002, shipped with .net 1.0
    WPF – 2006

    I don’t think that’s too quick and again we are talking about CS – if they used only one GUI toolkit for 20 years you’d be ranting about how outdated their technology is..

  2. September 14th, 2009 at 15:39 | #2

    Well, gapholes doesn’t mean changes are happening; for instance, there’s nothing wrong with using fsync(). Depending on implementation details is always wrong, POSIX cannot help here.

    Well, new APIs: WCF has been also introduced (as part of .NET), but a new native SOAP library is coming as well, and we have now 3 different Ribbon classes from Microsoft (WPF/MFC/Scenic — introduced in the same year?), the old shell interfaces are deprecated and have switched to being COM based …

    If the UI toolkit would provide one clean update path without requiring breaking changes, no, I wouldn’t be ranting. I have no problem if they change for instance from a C to a COM UI, as this is the same technology underneath, and it doesn’t force you to rewrite your app. That’s why I also don’t rant about .NET remoting being replaced by WCF, that’s all fine, but WinForms (GDI) to WPF (DX) to now D2D and DirectWrite (which are not supported from WPF), that’s the kind of stuff I don’t like. Even with WPF, they have Silverlight and WPF on Desktop (remember: You can also write a standalone Silverlight application).

    Worse still, some APIs are bound to certain Windows versions, and updating Windows is not as easy as getting a new Linux. How does the Scenic Ribbon help if your customer is stuck with XP until the IT decides to buy Windows 7?

    There are two pretty nice posts on this from Joel, which sum up some of the issues Microsoft is facing pretty well: http://www.joelonsoftware.com/articles/APIWar.html and http://www.joelonsoftware.com/articles/fog0000000339.html

  3. DaveK
    September 14th, 2009 at 15:41 | #3

    I agree. I moved from Windows development to Linux recently. I found that UI programming, while a major struggle to learn, is better under Linux. I can write an app using Python that can swap out GUIs (GTK, QT, Wx) with no problem. Writing a cross-platform app on Windows can be tricky. One wrong move tying your program into any Windows API and you are trapped.

    However I don’t really know of any development environment that compares to Visual Studio. I know there is Eclipse but it crashes far too often to be of any use to me. Maybe you could write an article about visual development tools next?

  4. iGraham
    September 14th, 2009 at 15:51 | #4

    I’ve actually had to do the reverse of this relatively recently. I’ve done C++ development on Linux for quite some time now, and recently (last couple of years) had to start doing it on Windows for my job and found that the windows tools are really painful and slow to use in comparison to what I’ve been used to. I was quite shocked by what was considered to be good tools (Visual Studio) on Windows being as bad as they actually seem to be…

  5. September 14th, 2009 at 15:55 | #5

    DaveK :

    Maybe you could write an article about visual development tools next?

    Yep, I’m planning to write about that actually ;)

  6. mos
    September 14th, 2009 at 17:54 | #6

    Forgive me if I missed it in your post, but what are you using to edit your code now? I do a little cross-platform coding, and I pretty much use Visual Studio as my editor in all cases, even when I’m writing non-Windows code.

  7. September 14th, 2009 at 17:58 | #7

    @mos
    At the moment, I use QtCreator + Kate/vim on Linux at work, and I’m quite happy so far. Most of the time I’m in QtCreator, including for debugging, which is good for productivity. For the additional tools in this project — which are written in Python — I use either Vim or Kate.

    For another project, which is mainly developed on Windows (personal stuff ;) ), I use Kate on Linux. However, for that project, I usually only fix porting problems on Linux, as all feature development is on Windows.

  8. Rich Paul
    September 14th, 2009 at 18:00 | #8

    This sounds very interesting but I’m confused . . . what is the target environment? You are developing an app using Qt and GCC that can be compiled for both Windows and Linux, is that correct? To develop, you boot Linux on your Windows machine via Wubi, develop and test in Linux, then a simple compile and you have a Windows binary that you can then test in Windows (after rebooting out of Wubi)?

  9. Billa
    September 14th, 2009 at 18:07 | #9

    I pretty much use QT 4.x. It is a proper API (not just a GUI library) and it it crossplatform. QT creator has matured quite fast too and is pretty neat to do GUI development.

  10. Alan
    September 14th, 2009 at 18:15 | #10

    I suspect that the majority of the speedup is due to a better malloc(). Try using Doug Lea’s malloc(), or jemalloc() on Windows. You might just get a 2x speedup over a stock Windows build.

    My experience doing cross-platform builds of CPU limited software:

    Linux = 1x (time for the final application to run)
    Windows + replacement malloc: ~1.2x
    Stock Windows: 2-3x.

  11. Matt Nowack
    September 14th, 2009 at 18:22 | #11

    @Anteru
    Did you look into using QtCreator for Windows, I’m just curious why you decided to boot up wubi to use a cross-platform tool.

    Great article by the by, when I switched from using Eclipse for a year for Java Development back to Visual Studio for C# development I was blown away by how utterly lacking VS is, most .Net developers would cast me out for such blasphemy, but when you see how much better FREE alternatives are you realize how lacking VS really is.

  12. September 14th, 2009 at 18:23 | #12

    @Rich Paul
    At work, I’m using exclusively Linux, using GCC. Every morning I come in, boot Linux (Wubi in my case), and get to work.

    @Alan
    For my personal project, I use nedmalloc, but I didn’t have time to switch at work, plus why the trouble, if I can use the built-in on from Linux :)

  13. Robin C
    September 14th, 2009 at 18:27 | #13

    @Billy Fish
    Gapholes? Aren’t all holes gaps? I think you just coined a new word!

    :)

  14. Michael G
    September 14th, 2009 at 18:36 | #14

    Regarding the initial problem, and the observation that Vista speeds things up: Vista, by default, enables the “Low Fragmentation Heap”. But you can turn on the Low Fragmentation Heap manually on XP. I wonder if that was the bottleneck.

  15. September 14th, 2009 at 18:39 | #15

    @Michael G
    Yep, enabling the LFH was the first call in my application, but it didn’t help.

  16. Billy Fish
    September 14th, 2009 at 19:00 | #16

    “Worse still, some APIs are bound to certain Windows versions, and updating Windows is not as easy as getting a new Linux.”

    And the unofficial motto of linux sound subsysten: “Now you hear it, now you don’t”

    I think the fact that many still prefer clean install of Ubunto than distro upgrade is telling. (not that Windows is any better, just that Linux isn’t all roses as some want to make it)
    http://www.intranetjournal.com/articles/200810/ij_10_30_08a.html

  17. Rich Paul
    September 14th, 2009 at 19:01 | #17

    @Anteru
    OK, but when you are done, the app you are writing will primarily be run by its users in Windows? (And it could also be run in Linux with a simple recompile.) I’m just trying to understand if you are switching your development for *Windows* targets to a Linux development environment, at least for this app, or if all you are doing is developing an app for Linux, using Linux tools, within Wubi, which is not nearly as interesting.

  18. Rick
    September 14th, 2009 at 19:07 | #18

    @Matt Nowack

    You need the resharper plugin. That coupled with a superior language (C# over Java), and Visual Studio wins hands down.

  19. September 14th, 2009 at 19:12 | #19

    @Rich Paul
    Yeah, it still works on Windows (without OpenMP, and crappy performance) — but this is research, it is likely to remain a closed solution (read: Will run on whatever I use). However, that’s not the point of the post. The interesting part for me is that I could switch rather painlessly from Windows to Linux only, and I think this might be interesting for other Windows developers as well who think that this switch is too dramatic. I never developed full-time on Linux before, I tried from time to time, but the tools and the comfort was simply not there, but this time I have no reason to boot up Windows at work. My hope is that other Windows developers simply consider Linux when selecting a development environment — for my number-crunching graphics app, a modern Linux turned out to be much better than an outdated Windows setup.

  20. Rich Paul
    September 14th, 2009 at 20:00 | #20

    @Anteru
    OK, thanks for the clarification. I am highly interested in knowing when it is practical to build a complex app with a good UI that will run on Windows and Linux both, with a simple recompile (Mac target would be nice too :) . It sounds like you are doing something close to that, and we are getting closer to a world where that is possible.

  21. Sam
    September 14th, 2009 at 20:13 | #21

    Why couldn’t you install vs 2005 on Vista? It work on Vista. You may need to install the patch but otherwise it’s fine. I’ve used it so I’m speaking from experience.

  22. Xarxnd
    September 14th, 2009 at 20:24 | #22

    “A quick try on Vista showed that the same application ran more than twice as fast” Holly shit!! Does that mean that Vista is faster than XP?

  23. Xarxnd
    September 14th, 2009 at 20:26 | #23

    “a 3rd party library could not be linked when OpenMP was enabled” Humm is there a port of that library on linux or what?

  24. Gabatxoa
    September 14th, 2009 at 22:11 | #24

    > While I did get a speedup from 1->3 cores, it became slower from 3->4 –
    > clearly, I was hitting some issues with either the scheduler or the
    > memory subsystem, as my code didn’t have any I/O in it.
    This is not always true (even if actually was in your case): this could be due to cache bounces or too coarse grained locking.

  25. Eric Lawless
    September 14th, 2009 at 22:39 | #25

    Could you name the 3rd-party library which cannot link with OpenMP enabled? Did you look into Intel’s Threading Building Blocks? Cilk++? Why wouldn’t you use Boost.Thread, it sounds perfect for this application? If you’re using Qt, why not use their cross-platform threading library?

    It sounds like you’re not interested at all in doing cross-platform development. It almost sounds like you’re actually a Linux developer who did a brief stint in Windows-land so that you could write this article extolling the virtues of Linux, in places they may or may not exist.

    Having used Visual Studio with Whole Tomato’s Visual Assist X for six months, followed by Qt Creator on both Windows and Linux for the same amount of time, I’ve found Qt Creator to lag behind VS+VAX in a number of areas, especially in terms of refactoring, semantic highlighting and code completion. I agree that Qt Creator is the best Linux C++ IDE by lightyears, but it isn’t yet equal to VS+VAX.

    I know a number of C++ developers targeting Linux who have modified Visual Studio to be aware of their Linux system libraries, and who do all of their Linux development in Windows because the tools are better. I’ve done the same, it makes my life substantially easier.

    I disagree wholeheartedly with your assessment. Either the whole thing was dishonest, or you didn’t take the appropriate time to know what your options were, and you’re completely whitewashing the current state of Linux C++ development.

Comment pages
1 2 3 4
  1. September 14th, 2009 at 18:51 | #1
  2. September 14th, 2009 at 21:04 | #2
  3. September 15th, 2009 at 07:05 | #3
  4. September 15th, 2009 at 08:00 | #4
  5. September 15th, 2009 at 09:47 | #5
  6. September 15th, 2009 at 13:23 | #6
  7. September 15th, 2009 at 13:51 | #7
  8. September 15th, 2009 at 14:00 | #8
  9. September 16th, 2009 at 07:16 | #9
  10. September 16th, 2009 at 07:40 | #10
  11. September 16th, 2009 at 07:43 | #11
  12. September 16th, 2009 at 17:42 | #12
  13. September 16th, 2009 at 22:32 | #13
  14. September 17th, 2009 at 14:40 | #14
  15. September 18th, 2009 at 00:45 | #15
  16. September 18th, 2009 at 06:17 | #16
  17. September 20th, 2009 at 09:17 | #17
  18. September 20th, 2009 at 10:38 | #18
  19. September 23rd, 2009 at 19:57 | #19
  20. September 28th, 2009 at 07:40 | #20
  21. November 20th, 2009 at 01:52 | #21