Click here to Skip to main content
Click here to Skip to main content

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

, 23 Mar 2013
Rate this:
Please Sign up or sign in to vote.
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
namespace MathFuncs
{
    class MyMathFuncs
    {
    public:
        // Returns a + b 
        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)
    {
        // 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 
#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 

MathFuncsLib.cpp 

#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  

#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)

About the Author

8MX
Chief Technology Officer
Canada Canada
No Biography provided

Comments and Discussions

 
QuestionNot an article PinmvpRichard MacCutchan23-Mar-13 8:30 
This should be changed to a Tip.
Use the best guess

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 24 Mar 2013
Article Copyright 2013 by 8MX
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid