Introduction
Having a talent of creative laziness I wonder how to make use of the nice speed boost and automatic parallel optimizations of the Intel C++ compiler while keeping connected to the .NET world with C++/CLI to get complex tasks done quick.
Background
COM Interop and P/Invoke code examples give me a headache when I just think about them. Why so complicated if it can be done plain simple. The compiler can do all the work for us !
What we are doing is to create a lib of the C++/CLI code compiled with the MS VC++ compiler and link that to our exe compiled with the Intel C++ compiler. So we end up having both worlds combined in an easy way with a minimum of work.
So any heavy duty pure C++ code can be placed into the Intel part and .NET related tasks stay in the MS part.
The scope of this article is not to analyse any speed advantages using a different compiler. I leave that to the experts. Some topics about this on Stackexchange.com and Stackoverflow.com.
Using the code
This example uses Visual Studio 2012 with IntelĀ® C++ Studio XE for Windows 2013 Update 2 installed. The code is based on the Microsoft example Walkthrough: Creating and Using a Static Library.
First lets make our Lib which compiles using the VC++ compiler:
MathFuncsLib.h
namespace MathFuncs
{
class MyMathFuncs
{
public:
static double Add(double a, double b);
};
}
MathFuncsLib.cpp
#include "MathFuncsLib.h"
using namespace System;
using namespace System::Diagnostics;
namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b)
{
Process::GetCurrentProcess()->PriorityClass = ProcessPriorityClass::High;
String^ s1 = "100";
int x = Convert::ToInt32(s1);
return a + x;
}
}
The double b
in this example is not used.
Now let's make a simple console app which links the above lib and compiles the console app with the Intel C++ compiler:
MyExecRefsLib.cpp
#include <iostream>
#include "MathFuncsLib.h"
using namespace std;
int main()
{
double a = 7.4;
int b = 99;
cout << "a + b = " <<
MathFuncs::MyMathFuncs::Add(a, b) << endl;
cin.get();
return 0;
}
Result:
a + b = 107.4
Note: Press Enter to quit the console app.
Pure C Code
Managed code always gets compiled as C++ code. However we can compile pure C code on the Intel part. Lets look at the modified example here:
MathFuncsLib.h
#ifdef __cplusplus
extern "C" {
#endif
double MyMathFuncs_Add(double a);
#ifdef __cplusplus
}
#endif
#include "MathFuncsLib.h"
using namespace System;
using namespace System::Diagnostics;
double MyMathFuncs_Add(double a)
{
Process::GetCurrentProcess()->PriorityClass = ProcessPriorityClass::High;
String^ s1 = "100";
int x = Convert::ToInt32(s1);
return a + x;
}
The console app in pure C:
MyExecRefsLib.c
#include <stdio.h>
#include <MathFuncsLib.h>
int main()
{
double a = 7.4;
double d = MyMathFuncs_Add(a);
printf("Value2: %f\n", d);
return 0;
}
Result:
Value2: 107.400000
Points of Interest
The IntelConsole.exe runs in High process priority as instructed by the .NET code. Funny is that you can use the same code in a DLL to change the process priority of the calling EXE.
Students can download a free version of the IntelĀ® C++ Studio XE for Windows here.
I will update this article in some weeks with benchmark values from a huge project I currently port to the Intel compiler.
History
- 23/03/2013 - Added C version of the example project
- 22/03/2013 - First C++ demo project published