Quick Link Intel® MKL In Xcode* IDE: A Fortran Sample

Since version 10.x , Intel® Math Kernel Library (Intel MKL) for Mac OS* comes bundled with Intel® Parallel Studio XE for Mac OS*, which was integrated into Xcode Integrated Development Environment (IDE) after integrated installation. This provided us a quick link way to access Intel MKL in Xcode IDE.

This tutorial is intended to show the quick way using XCode 6.1 and Intel®  Parallel Studio XE 2015 Composer Edition for Fortran for OS X*.  but we hope that it will be helpful for users of the other versions. The latest Intel® MKL 11.2 and Intel® Fortran compiler 15.0 were integrated in Intel® Parallel Studio XE 2015 for OS X*,  Here is the fortran sample

For general link way, please refer to Intel MKL User's Guide and User and Reference Guide for the Intel® Fortran Compiler
Compiling and linking Intel MKL with Xcode*  
How to link application against Intel MKL using XCode IDE 

The specific instructions for performing these steps depend on your version of the Xcode developer software. Please refer to the Xcode Help for more details.

1.    Create a new Xcode* project

a)   Copy sample source code: dgemmx.f, common_func.f and data/dgemmx.d from the fortran sample or Intel mkl example package to your work directory, for example, /Users/yhu5/blas

Intel MKL provide example code : C and fortran source code to show how to call Intel MKL functions. User can get the example package from Intel MKL install directory </opt/intel/composer_xe_2015.0.077/mkl/examples>.  $tar -xzvf  tar -xzvf examples_core_f.tgz -C /Users/yhu5/.

b)   Launch the Xcode application, typically found in the Applications folder.

c)   In Xcode Menu, select File > New > New Project.... , The New Project Assistant window opens. Select Application in the left pane. Select a template, for example, Command Line Tool, and click Next. Name your project, for example, MKL_dgemm_f, enter a string fro Company Identifier, and select a type C. Uncheck Use Automatic Reference Counting. Click Next. Specify a directory for your project  /Users/yhu5/blas. and optionally select Create local git repository for this project to place your project under version control, Click Create.


d)     Xcode creates the named project directory, with an .xcodeproj extension and several new source files like main.c etc.  Remove main.c, etc. file  and add dgemmx.f and common_func.f into ‘Source' project folder. 


Each Xcode project has its own Project Editor window that displays project source files, targets, and executables.

2. Setting  the Intel® Fortran Compiler in Xcode IDE

a) Select the target you want to change and click Build Rules.

b) Add a new rule by clicking "+"  or Editor > Add Build Rule , .under Process, choose Fortran source files Under Using, select Intel® Fortran Compiler XE 15.0

3. Setting MKL Options in Xcode IDE

The Intel® Fortran Compiler now comes bundled with the Intel® Math Kernel Library. You can access this library in Xcode*, using the Use Intel® Math Kernel Library property, located in the Performance Library Build Components category

In the Xcode project, select the target and click Build Setting. Under the Build Settings tab, click All. Scroll down until you see Intel® Fortran Compiler XE 15.0 (limited Feature) - Performance Library Build Components, Select "Use threaded Intel® Math Kernel Library. (-mkl=parallel) (or "Use non-thread Intel @ Math Kernal Library. (-mkl=sequential))

4. Build and Run the Project

Select Product > Build. Or Click the  ‘Run' button on project tools bar, the project will build successfully. 
But may run with error message dyld: Library not loaded: libmkl_intel_lp64.dylib

(other compiler errors, like files is not found or Open file failed, please change the path of the files and the code in dgemmx.f, line 49, OPEN(UNIT=1, FILE='/Users/xx/xx/dgemmx.d',STATUS='OLD' so Xcode can find them.)

5. Set Run Environment

Open the Scheme Editor  by select menu Product > Scheme > Edit Scheme...., Under the Arguments tab, under Environment Variables , click the button. add DYLD_LIBRARY_PATH as an environment variable.

Set the Value as the full path to the Intel compiler's /lib directory and MKL directory

Click Run button again, you will get the result in Output box.

6 Troubleshooting

 dyld: Library not loaded: libmkl_intel_lp64.dylib

Please either set the environment variable DYLD_LIBRARY_PATH in Xcode environment as step 5  or use mkl environment setting tool before run the executable file in Command line windows, i.e .

>source /opt/intel/composer_xe_2013/mkl/bin/mklvars.sh intel 64
> executable

Please refer to Intel Compiler User and Reference Guide =>Building Applications with Xcode* IDE"
or "Configuring the Apple Xcode* Developer Software to Link with Intel MKL" section in Intel MKL user guide for more details. 
And refer to "Linking Your Application with Intel® MKL" section in the Getting Started document to understand which libraries required by your code.

Operating System:

Mac OS*
Para obter informações mais completas sobre otimizações do compilador, consulte nosso aviso de otimização.