Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VC10.0

Use Intel C++ compiler with C++/CLI

4.60/5 (4 votes)
23 Mar 2013CPOL2 min read 23K   232  
Quick and easy way to use the Intel C++ compiler with C++/CLI.

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
MC++
namespace MathFuncs
{
    class MyMathFuncs
    {
    public:
        // Returns a + b 
        static double Add(double a, double b);
    };
}  
MathFuncsLib.cpp
MC++
#include "MathFuncsLib.h"

using namespace System;
using namespace System::Diagnostics;

namespace MathFuncs
{
    double MyMathFuncs::Add(double a, double b)
    {
        // Test some .NET stuff ..
	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 
MC++
#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 

MC++
#ifdef __cplusplus
   extern "C" {
#endif
 
   double MyMathFuncs_Add(double a);
   
#ifdef __cplusplus
}
#endif 

MathFuncsLib.cpp 

MC++
#include "MathFuncsLib.h"

using namespace System;
using namespace System::Diagnostics;

double MyMathFuncs_Add(double a)
{
   // Test some .NET stuff ..
   Process::GetCurrentProcess()->PriorityClass = ProcessPriorityClass::High;
   String^ s1 = "100";
   int x = Convert::ToInt32(s1);

   return a + x;
} 

 

The console app in pure C: 

MyExecRefsLib.c  

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 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)