none
Using visual c# for gui's but c++ for number crunching? RRS feed

  • Question

  • Hi,

    I like the ease with which gui's can be created in visual c#.  I want to make gui's in visual c# and then call native c++ code to do the number crunching for the data.  A simple example would be a user form written in visual c# that displays the results of a numerical integration method written in c++.  I plan on using the forms for user interface but want c++ to eventually do large number crunching with large matrices etc.

    I have only done console programming in c++.  Can someone give me a simple explanation for the process required for such a task(described in my first paragraph)?  Also, if someone has an example of actual code that would be great, but first I want to understand the required procedure and what all of the steps mean.  Am I going about this the wrong way?

    Thanks!
    • Edited by emanbyu Tuesday, June 10, 2008 7:57 PM Clarification of question
    Tuesday, June 10, 2008 6:51 PM

Answers

  • If you want to write unmanaged C++ dlls and call them from C# you can.

    http://www.codeproject.com/KB/cs/unmanage.aspx
    I need a job!
    • Marked as answer by jack 321 Friday, June 13, 2008 8:19 AM
    Tuesday, June 10, 2008 11:24 PM
  • I will answer the last question first:

     would it be easier to write everything in c++ and then call code from visual c# for the gui

    You can do all your GUI in C++. It can be done in the designer. Just drag buttons to the form, pertty much as you do in C#.

    C++ has this advantage, you perhaps wil NEVER need for this task. It has many more classes accessible to it without using so called P/Invoke which is a wrapper for C++ classes not included in .NET. For instance, if you want to change the default gateway ip addess for your network card, forget C#, you have to do it in C++ or use P/invoke. P/invoke maps the input parameters for the function calls as well as the output value types (what you get out of the function call) from C++ to .NET (CLR). It is a major headache to do type casting in C# as well as C++.

    I don't have a master's in CS. I don't have any MS certification. I have no plans to acquire any of those. I am not condescending in regards to people who have them all but my way or learning is to learn from their droppings. So far it served me well. CS major will get you into much of crazy theory you will have trouble finding how to use.

    I think if you go for a MS cert that will give you a very practical (as well as needed theoretical) foundation.

    As cablehead said you can put a part of your code in a dll (be it C# or C++) and call it from C# or C++ main form (GUI). I think for the success of your task you should forget about the complexities. Your problem is straightforward. It does not require any low level coding where C++ is so powerful. You are not going to make movies about your chemical processes. If you are then it will be at a later stage when you've already integrated everything.

    Are you dealing with partial diff equations? If so, are the coordinates conducive to separation of variables? Is the problem eventually breakable down to some ordinary diff equations? Are you dealing with 2-D or 3-D processes or it is something in a phase space?

    My hunh is you will be working with cylindrical boundary conditions most of the time. So you may end up dealing with Euler or Bernulli equations in the end?


    AlexB
    Wednesday, June 11, 2008 12:21 AM

All replies

  • I don't know how much you know about the task ahead. It is quite possible that you know much more than I do but I still want to splash out my meager knowledge on the issue.

    Numerical integration over a large number of points with matrices of huge rank will be a formidable procedure computationally and time consuming. If you take on it headlong by using for instance a Gauss method well known from the course of Algebra you will be wasting a lot of time and computer resources. There are numerous algorithms that work better but they require penetration. You have to read math sources. Also if your matrices for some reason have special properties, those may necessitate special algorithms to be applied to make them efficient. In my recollection there should be perhaps a few dozen methods out there.

    One thing you may consider is doing a polynomial approximation first because after that the integration will be trivial and no matrices will be needed. At the approximation stage you will need the inverse matrices for sure.

    Consider Chebyshev's. This seems to be the best least square method known. You will not be able to get a better accuracy with any other method, imho.

    All said and done I am not sure you will save much by using C++. It is still going to be C++/CLR so the optimization will be done for you. C# will be much easier to implement, imho.

    There are numerous, commercial packages out there that will do this job for you. If one can write code, however, definitely they should do it themselves. It is fairly elementary task overall.

    Code samples. I would love to give what I have, unfortunately you will not be able to use them. The Chebyshev's and inregration methods (Runge-Kutta for diff equations and others) were written years ago in VFP9 and I had to abandone all that stuff when I started Vista. It wants to have nothing to do with FP9. It is on my agenda to rewrite all this precious stuff in C# but it will take time.


    AlexB
    Tuesday, June 10, 2008 9:30 PM
  •  Alex,

    Thanks for the reply.  What I am really trying to do is quite complicated.  My dad has a doctorate in chemical engineering process control and I have a degree in chemical engineering.  We are investigating development of some advanced control software. Without getting into much detail speed is exteremly important, as all control is done in real time, and I was under the impression that c++ would be the fastest language to use(is this correct?).  There needs to be an interface as well, though, and from what I've read this looks pretty clunky with c++.  I am definitely new to the subject of using c++ for more than console level programming so maybe i'm wrong.  I can do the math and vector manipulations using the vector class(there will have to be some development in this area as well though).  What I am struggling with is the User interface.

    I don't think I clearly understand the effect of C++/CLR on the speed of the program.  What do you mean by the optimization will be done for me?  Does compiling c++ in visual studio make it run slower than if I compiled it on a unix system for example?

    To give you an idea of my background.  I have done a lot with VBA forms in excel and word(creating custom add-ins etc) and have used c++ for numerical methods.  I understand the basics of classes, all of the flow structures(different loops, cases etc.), variable types, and error introduced from each of these, pointers and the other basic programming stuff.  When I start to get into more advanced stuff like dll's, wrapping etc. I'm a little over my head.  I'm sure this stuff is not that complicated but it's always a matter of learning it and there is so much to learn and it's hard to know where to start. I'm working on it though!

    I'm sorry I know so little about the details of how the languages work.  I appreciate the help though.
    Tuesday, June 10, 2008 9:59 PM
  • The thing is that there are two C++ languages as a matter of fact. One is so called Native whereas .NET does not touch you code at all and you are responsible for the garbage collection. If you don't have years of practice in that your chemical engineering will have to wait a couple of years while you get some experience in that. The other one is  C++ managed code extensions. In this case you write C++ code and .NET uses its power to do the garbage collection and also the optimization at runtime. For instance if you tried to instantiate a class and then release it and instantiate it again and your app is losing time on it, .NET will make sure that this foolishness does not take place and the class will stay alive. This is just a primitive example.

    It is sort of unavoidable that .NET will mess up with your code but you still may explore your own analysis and optimize things on the level of your algorithms because .NET does not know if a certain operation may be avoided logically. In this respect you perhaps will not gain anything by using C++ in terms of the optimization and the coding in C# will be much easier, plus the integration will not require any effort.

    Overall, I think you are facing a very interesting task, it may be elementary overall, howevr, you may also get into issues of finding the best methods for this and that aspect of your physical process.

    The integration itself in real time--I dont think it will be very difficult. You should go ahead, write a program and test it. I hope it is not for tracing electron transitions from orbit to another or some spin physics. I doubt you will be able to do that in real time. Mass processes are a different business. Keep it in mind that thetime quantum of computing is 100 mcsecs (a tick) which is the smallest time you can actually measure or hope to measure under extremely optimal conditions. In reality it is even greater.

    If you have any questions post them but keep it in mind that I am not a top notch expert in the business of numerical integration.

    I don't think you will need any wrapping to worry about. Some dlls will come your way by default.
    AlexB
    Tuesday, June 10, 2008 10:40 PM
  • I cannot comment on Unix vs C++/CLR. I don't trust anything that is not Microsoftie. I doubt though that Unix will do you any good.
    AlexB
    Tuesday, June 10, 2008 10:51 PM
  • Hmm, okay so managed c++ is not really any faster than visual c#?  How do these languages compare to Java and VB for example with regard to speed.  I really like the feel of visual c#.  From what I can tell it feels like a combination of c++ and VB, although I guess it is supposed to be a combination of c++ and Java.

    Gosh, maybe I should go get a master's in CS.  Are the microsoft certification classes good?  Will they give me in depth knowledge about the details behind this stuff?  It seems like there ought to be an easy way to mix code.  I've read that c++ is great for being the main language of an application and interacting with different languages depending on what is needed.  It seems really difficult, however, to find information about how to actually go about doing this.

    If I decided to use c++, would it be easier to write everything in c++ and then call code from visual c# for the gui?  Or is it just so difficult to do that I just need to stick to one language for everything.

    Thanks

    Tuesday, June 10, 2008 11:07 PM
  • If you want to write unmanaged C++ dlls and call them from C# you can.

    http://www.codeproject.com/KB/cs/unmanage.aspx
    I need a job!
    • Marked as answer by jack 321 Friday, June 13, 2008 8:19 AM
    Tuesday, June 10, 2008 11:24 PM
  • I will answer the last question first:

     would it be easier to write everything in c++ and then call code from visual c# for the gui

    You can do all your GUI in C++. It can be done in the designer. Just drag buttons to the form, pertty much as you do in C#.

    C++ has this advantage, you perhaps wil NEVER need for this task. It has many more classes accessible to it without using so called P/Invoke which is a wrapper for C++ classes not included in .NET. For instance, if you want to change the default gateway ip addess for your network card, forget C#, you have to do it in C++ or use P/invoke. P/invoke maps the input parameters for the function calls as well as the output value types (what you get out of the function call) from C++ to .NET (CLR). It is a major headache to do type casting in C# as well as C++.

    I don't have a master's in CS. I don't have any MS certification. I have no plans to acquire any of those. I am not condescending in regards to people who have them all but my way or learning is to learn from their droppings. So far it served me well. CS major will get you into much of crazy theory you will have trouble finding how to use.

    I think if you go for a MS cert that will give you a very practical (as well as needed theoretical) foundation.

    As cablehead said you can put a part of your code in a dll (be it C# or C++) and call it from C# or C++ main form (GUI). I think for the success of your task you should forget about the complexities. Your problem is straightforward. It does not require any low level coding where C++ is so powerful. You are not going to make movies about your chemical processes. If you are then it will be at a later stage when you've already integrated everything.

    Are you dealing with partial diff equations? If so, are the coordinates conducive to separation of variables? Is the problem eventually breakable down to some ordinary diff equations? Are you dealing with 2-D or 3-D processes or it is something in a phase space?

    My hunh is you will be working with cylindrical boundary conditions most of the time. So you may end up dealing with Euler or Bernulli equations in the end?


    AlexB
    Wednesday, June 11, 2008 12:21 AM
  • Hi, I found your answers very informative and interesting:)

    Currently I am starting to write a scientific project (for my phD), which will involve heavily
    - partial diff equations
    - coordinates conducive to separation of variables
    - 2-D or 3-D processes or it is something in a phase space (Finite Element Methods)

    Also I am thinking to work extensively with GDI (creating plots) and OPENGL (for 3D representation of chemical reactor)
    I wont to create an MDI application  which eventually will receive data form PLC (something like SCADA)
    I am newbie in C#, with few projects on my shoulders, but already fascinated by it... so my question is ....

    Is c# adequate for this kind of application?

    I have found a lot of books for numerical methods written for C++, Java, Fortran and Matlab but none for C#.
    Can you provide me with some links to assist my work
    Monday, October 20, 2008 8:15 AM
  • Get a heads up.  Use Cuda.  There's a C# interface.
    Monday, October 20, 2008 9:35 AM
  • Hi ,
    thanks for sharing this.... but I think it is too advanced for me right now.
    I am just starting my project (setting up Original Values and general mass - energy balances).
    Any other web sites or bibliography about numeric methods would be really healpfull.

    Saturday, November 1, 2008 8:30 AM