Writing performance critical C# code in C++
I'm currently working on some performance critical code, and I have a particular situation where I'd love to write the whole application in C#, but performance reasons mean C++ ends up being FAR faster.
I did some benchmarking on two different implementations of some code (One in C#, another in C++) and the timings showed that the C++ version was 8 times faster, both versions in release mode and with all optimizations enabled. (Actually, the C# had the advantage of being compiled as 64-bit. I forgot to enable this in the C++ timing)
So I figure, I can write the majority of the code base in C# (Which C# makes very easy to write), and then write native versions of things where the performance is critical. The particular code piece I tested in C# and C++ was one of the critical areas where > 95% of processing time was spent.
What's the recommended wisdom on writing native code here though? I've never written a C# application that calls native C++, so I have no idea what to do. I want to do this in a way that minimizes the cost of having to do the native calls as much as possible.
Thanks!
Edit: Below is most of the code that I'm actually trying to work on. It's for a n-body simulation. 95-99% of the CPU time will be spent in Body.Pairwise().
class Body
{
public double Mass;
public Vector Position;
public Vector Velocity;
public Vector Acceleration;
// snip
public void Pairwise(Body b)
{
Vector dr = b.Position - this.Position;
double r2 = dr.LengthSq();
double r3i = 1 / (r2 * Math.Sqrt(r2));
Vector da = r3i * dr;
this.Acceleration += (b.Mass * da);
b.Acceleration -= (this.Mass * da);
}
public void Predict(double dt)
{
Velocity += (0.5 * dt) * Acceleration;
Position += dt * Velocity;
}
public void Correct(double dt)
{
Velocity += (0.5 * dt) * Acceleration;
Acceleration.Clear();
}
}
I also have a class that just drives the simulation with the following methods:
public static void Pairwise(Body[] b, int n)
{
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
b[i].Pairwise(b[j]);
}
public static void Predict(Body[] b, int n, double dt)
{
for (int i = 0; i < n; i++)
b[i].Predict(dt);
}
public static void Correct(Body[] b, int n, double dt)
{
for (int i = 0; i < n; i++)
b[i].Correct(dt);
}
The main loop looks just like:
for (int s = 0; s < steps; s++)
{
Predict(bodies, n, dt);
Pairwise(bodies, n);
Correct(bodies, n, dt);
}
The above is just the bare minimum of a larger application I'm actu开发者_JAVA百科ally working on. There's some more things going on, but the most performance critical things occur in these three functions. I know the pairwise function is slow (It's n^2), and I do have other methods that are faster (Barnes-hutt for one, which is n log n) but that's beyond the scope of what I'm asking for in this question.
The C++ code is nearly identical:
struct Body
{
public:
double Mass;
Vector Position;
Vector Velocity;
Vector Acceleration;
void Pairwise(Body &b)
{
Vector dr = b.Position - this->Position;
double r2 = dr.LengthSq();
double r3i = 1 / (r2 * sqrt(r2));
Vector da = r3i * dr;
this->Acceleration += (b.Mass * da);
b.Acceleration -= (this->Mass * da);
}
void Predict(double dt)
{
Velocity += (0.5 * dt) * Acceleration;
Position += dt * Velocity;
}
void Correct(double dt)
{
Velocity += (0.5 * dt) * Acceleration;
Acceleration.Clear();
}
};
void Pairwise(Body *b, int n)
{
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
b[i].Pairwise(b[j]);
}
void Predict(Body *b, int n, double dt)
{
for (int i = 0; i < n; i++)
b[i].Predict(dt);
}
void Correct(Body *b, int n, double dt)
{
for (int i = 0; i < n; i++)
b[i].Correct(dt);
}
Main loop:
for (int s = 0; s < steps; s++)
{
Predict(bodies, n, dt);
Pairwise(bodies, n);
Correct(bodies, n, dt);
}
There also exists a Vector class, that works just like a regular mathematical vector, which I'm not including for brevity.
You'll need to interface to the native code. You could put it in a DLL and pinvoke. Okay when you don't transition very often and the interface is thin. The most flexible and speediest solution is to write a ref class wrapper in the C++/CLI language. Have a look at this magazine article for an introduction.
Last but not least, you really ought to profile the C# code. A factor of 8 is quite excessive. Don't get started on this until you at least have half an idea why it is that slow. You don't want to repro the cause in the C++ code, that would ruin a week of work.
And beware of the wrong instincts. 64-bit code is not actually faster, it is usually a bit slower than x86 code. It's got a bunch of extra registers which is very nice. But all the pointers are double the size and you don't get double the cpu cache. .
You have two choices: P/Invoking and C++/CLI.
P/Invoking
By using P/Invoke, or Platform Invoke, it is possible for .NET (and therefore C#) to call into unmanaged code (your C++ code). It can be a bit overwhelming, but it is definitely possible to have your C# code call into performance critical C++ code.
Some MSDN links to get you started:
- Consuming Unmanaged DLL Functions
- Interop Marshaling
- DllImportAttribute
Basically, you will create a C++ DLL that has defined all the unmanaged functions you want to call from C#. Then, in C# you will use the DllImportAttribute
to import that function into C#.
For instance, you have a C++ project that creates a Monkey.dll with the following function:
extern "C" __declspec(dllexport) void FastMonkey();
You will then have a definition in C# as follows:
class NativeMethods
{
[DllImport("Monkey.dll", CallingConvention=CallingConvention.CDecl)]
public static extern void FastMonkey();
}
You can then call the C++ function in C# by calling NativeMethods.FastMonkey
.
Few common gotchas and notes:
- Spend time learning Interop Marshaling. Understanding this will greatly help creating proper P/Invoking definitions.
- The default calling convention is StdCall, but C++ will default to CDecl.
- The default character set is ANSI, so if you want to marshal Unicode strings, you will have to update your
DllImport
definition (see MSDN - DllImport.CharSet documentation). - http://www.pinvoke.net/ is a useful resource for knowing how to P/Invoke standard Windows functions call. You can also use that for a clue how to marshal something if you know of a Windows function call that is similar.
C++/CLI
C++/CLI is a series of extensions to C++ created by Microsoft to create .NET assemblies with C++. C++/CLI also allows you to mix unmanaged and managed code together into a "mixed" assembly. You can create a C++/CLI assembly that contains both your performance critical code and any .NET class wrapper around it you want.
For more information with C++/CLI, I recommended starting with MSDN - Language Features for Targeting the CLR and MSDN - Native and .NET Interoperability.
I recommend you start with the P/Invoking route. I have found having a clear separation between unmanaged and managed code helps to simplify things.
In C#, is Vector a class or struct? I suspect it's a class, and Arthur Stankevich hit the nail on the head with his observation that you may be allocating many of these. Try making Vector a struct, or reusing the same Vector objects.
Easiest way to do it is create C++ ActiveX dlls.
Then you can reference them in the C# project, Visual Studio will create interops that will wrap the ActiveX COM Object.
You can use the interop Code like C# code, no additional wrapping code.
More about AciveX/ C#:
Create and Use a C++ ActiveX component within a .NET environment
"I did some benchmarking on two different implementations of some code (One in C#, another in C++) and the timings showed that the C++ version was 8 times faster"
I did some numerical calculation in C#, C++, Java and a bit of F# and the biggest diffrence between C# and C++ was 3.5.
Profile your C# version and find the bottleneck (maybe there are some IO - related problems, unnecessary allocation)
P/Invoke is definitely easier than COM Interop for the simple case. However, if you do bigger chunks of a class model in C++, you might really want to consider C++/CLI or COM Interop.
ATL makes you whip up a class in no time, and once the object is instantiated, the invocation overhead is basically as small as with P/Invoke (unless you use dynamic dispatch, IDispatch, but that should be obvious).
Of course, C++/CLI is the very best option there, but that's not going to work everywhere. P/Invoke can be made to work everywhere. COM interop is supported on Mono up to degree
Looks like you are doing a lot of implicit Vector class allocations in your code:
Vector dr = b.Position - this.Position;
...
Vector da = r3i * dr;
this.Acceleration += (b.Mass * da);
b.Acceleration -= (this.Mass * da);
Try reusing already allocated memory.
精彩评论