# Need help setting up project with Intel MKL

**URL:** <https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260>\
**Category:** Uncategorized\
**Created:** [January 23, 2024, 2:35pm UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260 "2024-01-23T14:35:00Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tavi007](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@Tavi007](https://fortran-lang.discourse.group/u/Tavi007)\
**Post date:** [January 23, 2024, 2:35pm UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/1 "2024-01-23T14:35:00Z")

</div>

Hello Fortran Community!

I’m trying to start a new fluid simulation project and since this stuff is computation heavy, I would like to use the Intel Math Kernal Library. Because I’m working with Intel hardware, I thought this would be the best option to have optimized sparse algebra procedures ready to use. But I’m having trouble to set up the project. 🫤

I read various documentation, blog post or even threads in this forum, but ultimately nothing really worked. This is due to my inexperience with general project setup and the joy of using windows os. I know cmake/make exist, but I never really had to write my own build script, which makes fpm a godsend for me.  
Another stepping stone is to figure out how to incorporate an IDE so proper debugging is possible. Currently I’m using vscode (version 1.85.2) and I have working task and launch configurations to run/debug the application or my unittests (all with the help of fpm 🙏).

Since I would like to add the Intel MKL to a new project if possible, I tried to install the Intel oneApi Toolkit (Base and HPC, both are version 2023.2) and use it with my already installed visual studio (version 17.7.5), but that did not work. Apperently the vs version 17.7.x has problems with the oneApi toolkit (at least this was commented in one of the thread I found here). Also some of the marketplace extensions seem to only work with linux.

Since I don’t want to learn a new IDE (unless I really have to), I stopped trying with the visual studio 2022 setup and instead was wondering, if I can get the library working in a vscode project. Since I already installed the oneAPI toolkits, I assume that the library should exist somewhere on my computer. The toolkits appear to be correctly installed in C:\Program Files (x86)\Intel\oneAPI\ and running setvars.bat seems to work correctly too. (at least the command echo %ONEAPI\_ROOT% is returning the correct value). I also found the ifx compiler at C:\Program Files (x86)\Intel\oneAPI\compiler\2023.2.0\windows\bin.

But now I’m stumped and I would like to know, if there is a simple way to add the MKL to my project, such that I can still build it with fpm. If the answere is no, what is the next best option? Do other sparse BLAS libraries exist, that support a fpm.toml? If everything fails I could write my own, but I would rather not. I assume that existing libraries are much more optimized and tested than my own stuff would ever be. 😅

Best reagards  
Tavi007

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [January 23, 2024, 2:58pm UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/2 "2024-01-23T14:58:11Z")

</div>

> -qmkl[=lib]  
> Tells the compiler to link to certain  
> libraries in the Intel(R) oneAPI Math Kernel Library (oneMKL). On Windows systems, you must specify this option at compile time.

So whatever is building your project for you needs to include this option. I.e.

```plaintext
fpm build --compiler ifx --profile debug --flag "-qmkl"

```

---

<div class="post-metadata">

**Author:** ![Tavi007](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@Tavi007](https://fortran-lang.discourse.group/u/Tavi007)\
**Post date:** [January 24, 2024, 10:17am UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/3 "2024-01-24T10:17:03Z")

</div>

Hey, thanks for the reply.

After adding “C:\Program Files (x86)\Intel\oneAPI\compiler\latest\windows\bin” to my Path (something that apperently is not done automatically by the setvars.bat), fpm can at least find the ifx compiler and also compiles my fortran files, but the command still has trouble to compile the final .exe. This happens regardless of the “-qmkl” flag.

here is the full error message

> fpm build --compiler ifx --profile debug --flag “-qmkl”  
> inverse.f90 done.  
> libmain.a done.  
> main.f90 done.  
> main.exe compiling…  
> [75%] Compiling…:failed command ifx -qmkl /warn:all /check:all /error-limit:1 /Od /Z7 /assume:byterecl /standard-semantics build\ifx\_372E70A45AC2182B\main\app\_main.f90.o build\ifx\_A87C6B3A6E87C408\main\libmain.a -o build\ifx\_DA73C41FEF58AD66\app\main.exe\>build\ifx\_DA73C41FEF58AD66\app\main.exe.log 2\>&1  
> _run_:Termination status of the command-language interpreter cannot be obtained

I don’t understand the cause of the error and hope you/someone else can help me out. 🙂

---

<div class="post-metadata">

**Author:** ![wiremoons](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/wiremoons/32/764_2.png) [@wiremoons](https://fortran-lang.discourse.group/u/wiremoons)\
**Post date:** [January 24, 2024, 10:40am UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/4 "2024-01-24T10:40:37Z")

</div>

Hi @Tavi007

There is a _Visual Studio Code_ (not _Visual Studio_) help and setup page for the Intel compilers here: [Intel® oneAPI Extensions for Visual Studio Code](https://www.intel.com/content/www/us/en/docs/oneapi/user-guide-vs-code/2024-0/intel-oneapi-extensions-for-visual-studio-code.html)

Not sure if it will help at all - but just in case it does have useful information that might fix your problem 🙂

Simon

---

<div class="post-metadata">

**Author:** ![Tavi007](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@Tavi007](https://fortran-lang.discourse.group/u/Tavi007)\
**Post date:** [January 24, 2024, 12:52pm UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/5 "2024-01-24T12:52:26Z")

</div>

That documentation helped and I’m now quite a bit furter with getting my app build. Thanks for that.

So the root cause for the previous error seemed to be, that the setvars.bat did not set all necessary environment variables for vscode. I could fix this issue by running the setvars.bat in the vscode terminal. Since I’m using powershell, i used the command (taken directly from the README of one of the oneApi samples provided by the intel extension found on the marketplace)

```auto
cmd.exe "/K" '"C:\Program Files (x86)\Intel\oneAPI\setvars.bat" && powershell'

```

and with

```auto
$Env:Path.Split(';') 

```

i see a lot more variables than before. most noteably are

```auto
C:\Program Files (x86)\Intel\oneAPI\compiler\latest\windows\bin
C:\Program Files (x86)\Intel\oneAPI\compiler\latest\windows\lib
C:\Program Files (x86)\Intel\oneAPI\compiler\latest\windows\bin\intel64
C:\Program Files (x86)\Intel\oneAPI\compiler\latest\windows\redist\intel64_win\compiler

```

which are quite important in my windows setup, i assume.

Now i could finally build the exe with ifx compiler, though only manually. Currently I’m only testing a simple Hello world programm and the command “ifx ” worked fine. However running “fpm build --compiler ifx” still ends in a good old linker issue: build\ifx\_E0754568269DC30E\main\libmain.a : fatal error LNK1107  
This issue does not happen if I use gfortran or ifort as compiler and I don’t know what the exact problem is, because a libmain.a file was created, but it is somehow broken, even if I delete the build directory and make a clean build.

It feels like the end is almost reachable, so I hope you can help me out for the last bit too.

---

<div class="post-metadata">

**Author:** ![hkvzjal](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/hkvzjal/32/3055_2.png) [@hkvzjal](https://fortran-lang.discourse.group/u/hkvzjal)\
**Post date:** [January 24, 2024, 1:15pm UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/6 "2024-01-24T13:15:13Z")

</div>

Did you try to also set the mkl env vars before as well?

```auto
> "C:\Program Files (x86)\Intel\oneAPI\mkl\2023.2.0\env\vars.bat" intel64

```

---

<div class="post-metadata">

**Author:** ![Tavi007](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@Tavi007](https://fortran-lang.discourse.group/u/Tavi007)\
**Post date:** [January 24, 2024, 1:21pm UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/7 "2024-01-24T13:21:07Z")

</div>

No, not yet, since I’m currently trying to get the project build with fpm and the ifx compiler. The next step would be to get the mkl linked, so I will keep this script in mind when I get to this part 🙂

---

<div class="post-metadata">

**Author:** ![mEm](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@mEm](https://fortran-lang.discourse.group/u/mEm)\
**Post date:** [January 24, 2024, 1:54pm UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/8 "2024-01-24T13:54:07Z")

</div>

I think `setvars.bat` should setup the global environment, MKL included:

```auto
D:\>setvars
:: initializing oneAPI environment...
   Initializing Visual Studio command-line environment...
   Visual Studio version 16.11.31 environment configured.
   "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\"
   Visual Studio command-line environment initialized for: 'x64'
   ...
: mkl -- latest
   ...
:: oneAPI environment initialized ::

```

---

<div class="post-metadata">

**Author:** ![hkvzjal](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/hkvzjal/32/3055_2.png) [@hkvzjal](https://fortran-lang.discourse.group/u/hkvzjal)\
**Post date:** [January 24, 2024, 1:59pm UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/9 "2024-01-24T13:59:37Z")

</div>

In principle that should do it, but I have had similar issues before, and setting both was the only solution to actually get to link to mkl with “-qmkl” or “-Qmkl”(for windows).

---

<div class="post-metadata">

**Author:** ![Tavi007](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@Tavi007](https://fortran-lang.discourse.group/u/Tavi007)\
**Post date:** [January 25, 2024, 9:37am UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/10 "2024-01-25T09:37:36Z")

</div>

I’m still struggling with the linker error mentioned above (LNK1107). Could somebody please help me?

---

<div class="post-metadata">

**Author:** ![Tavi007](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@Tavi007](https://fortran-lang.discourse.group/u/Tavi007)\
**Post date:** [January 30, 2024, 8:48am UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/11 "2024-01-30T08:48:44Z")

</div>

So I still could not figure out a solution for the linker issue. But since the programm compiles just fine without fpm (instead compiling it directly with ifx/ifort), I tried to get the mkl working in a minimal project. But sadly I run into problems here too.

Here is what a tried. I have this small program

```auto
program main
   use mkl_blas, only: dgemm
   implicit none

   ! Matrix dimensions
   integer, parameter :: N = 3

   ! Matrices
   real(8) :: A(N, N), B(N, N), C(N, N)

   ! Initialize matrices A and B with some values
   A = reshape([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], [N, N])
   B = reshape([9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0], [N, N])

   ! Perform matrix multiplication: C = A * B
   call dgemm('N', 'N', N, N, N, 1.0d0, A, N, B, N, 0.0d0, C, N)

   ! Print the result matrix C
   print *, 'Result Matrix C:'
   print *, C
end program main

```

generated by chatgpt, which hopefully uses the a valid mkl procedure.

Now in the terminal I do the following commands:

```auto
"C:\Program Files (x86)\Intel\oneAPI\setvars.bat"
echo %MKLROOT%
ifx -o mkl_example app/main.f90 -qmkl 

```

The setvars.bat should have initialized the mkl correctly

```auto
:: initializing oneAPI environment...
   Initializing Visual Studio command-line environment...
   Visual Studio version 17.7.5 environment configured.
   "C:\Program Files\Microsoft Visual Studio\2022\Community\"
   Visual Studio command-line environment initialized for: 'x64'
: advisor -- latest
: compiler -- latest
: dal -- latest
: debugger -- latest
: dev-utilities -- latest
: dnnl -- latest
: dpcpp-ct -- latest
: dpl -- latest
: inspector -- latest
: ipp -- latest
: ippcp -- latest
: itac -- latest
: mkl -- latest
: mpi -- latest
: tbb -- latest
: vtune -- latest
:: oneAPI environment initialized ::

```

and MKLROOT is also set to “C:\Program Files (x86)\Intel\oneAPI\mkl\latest”  
However the compile command fails, because the mkl procedure can not be found. the compiler also ignored the -qmkl option, because the option is unknown…

```auto
Intel(R) Fortran Compiler for applications running on Intel(R) 64, Version 2023.2.0 Build 20230627
Copyright (C) 1985-2023 Intel Corporation. All rights reserved.

ifx: command line warning #10006: ignoring unknown option '/qmkl'
app/main.f90(2): error #7002: Error in opening the compiled module file. Check INCLUDE paths. [MKL_BLAS]
   use mkl_blas, only: dgemm
-------^
app/main.f90(16): error #6406: Conflicting attributes or multiple declaration of name. [DGEMM]
   call dgemm('N', 'N', N, N, N, 1.0d0, A, N, B, N, 0.0d0, C, N)
--------^
app/main.f90(2): error #6580: Name in only-list does not exist or is not accessible. [DGEMM]
   use mkl_blas, only: dgemm
-----------------------^
compilation aborted for app/main.f90 (code 1)

```

Can someone please explain why the compiler does not know the -qmkl option?

---

<div class="post-metadata">

**Author:** ![mEm](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@mEm](https://fortran-lang.discourse.group/u/mEm)\
**Post date:** [January 30, 2024, 8:56am UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/12 "2024-01-30T08:56:11Z")

</div>

Have you tried to capitalise the `q`, i.e. `-Qmkl` ?

---

<div class="post-metadata">

**Author:** ![Tavi007](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@Tavi007](https://fortran-lang.discourse.group/u/Tavi007)\
**Post date:** [January 30, 2024, 9:00am UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/13 "2024-01-30T09:00:36Z")

</div>

Good call. The compiler at least now knows the option, but sadly the build still fails, because the mkl\_blas module could not be found.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [January 30, 2024, 9:27am UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/14 "2024-01-30T09:27:14Z")

</div>

> [@Tavi007](#):
>
> `use mkl_blas`

The BLAS procedures are external. Only if you use the BLAS 95 interface, you need to add `use blas95`.

If you want to get interface checking, you should add

```auto
include "mkl_blas.fi"

```

in the declaration section of the sub-program where needed.

---

<div class="post-metadata">

**Author:** ![Tavi007](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@Tavi007](https://fortran-lang.discourse.group/u/Tavi007)\
**Post date:** [January 30, 2024, 10:00am UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/15 "2024-01-30T10:00:49Z")

</div>

Thanks for the help.

I also found this [issue](https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/error-7002-Error-in-opening-the-compiled-module-file-Check/td-p/1132599), that is similar to mine. If I understood it correctly, then not every file provided by Intel oneApi is yet compiled. One can find the fortran source files for the api modules in …/mkl//include. From here go into the intel64 folder and you see subfolder for specific compiler options. From the link:

> IFort-32-bit-targets, copy to …\mkl\include\ia32.  
> If for 64-bit targets, and your default integer is 4-bytes, copy to …\mkl\include\intel64\Lp64.  
> If for 64-bit targets and default 8-byte integers, use the option /integer-size:8 and copy the module file to …\mkl\include\intel64\Ilp64.

Since there isn’t any mkl\_blas.mod file in any of the folder, the compile won’t find the module while compiling.  
In my case there was already the blas95.mod file and adjusting my program to use this module instead, I could finally compile it.

```auto
program main
   use blas95
   implicit none

   ! Matrix dimensions
   integer, parameter :: N = 3
   integer :: i, j, k

   ! Matrices
   real(8) :: A(N, N), B(N, N), C(N, N), D(N, N)

   ! Initialize matrices A and B with some values
   A = reshape([1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 3.0], [N, N])
   B = reshape([1.0, 1.0, 1.0, 10.0, 10.0, 10.0, 100.0, 100.0, 100.0], [N, N])

   ! Perform matrix multiplication: C = A * B
   C = matmul(A, B)
   !call dgemm('N', 'N', N, N, N, 1.0d0, A, N, B, N, 0.0d0, C, N)
   !call gemm(A, B, C, 'N', 'N', 1.0, 0.0)

   ! just checking if the procedure is correct
   do i = 1, N
      do j = 1, N
         D(i, j) = 0.0
         do k = 1, N
            D(i, j) = D(i, j) + A(i, k)*B(k, j)
         end do
      end do
   end do

   ! Print the result matrix C
   print *, 'Matrix A:'
   print *, A
   print *, 'Matrix B:'
   print *, B
   print *, 'Result Matrix C:'
   print *, C
   print *, 'Result Matrix D:'
   print *, D
end program main

```

I will now try to get the mkl\_blas module running and will come back if I have more questions or found a solution (so future people with the same problem might find this thread useful 😉 ).

---

<div class="post-metadata">

**Author:** ![Tavi007](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@Tavi007](https://fortran-lang.discourse.group/u/Tavi007)\
**Post date:** [February 6, 2024, 12:25pm UTC](https://fortran-lang.discourse.group/t/need-help-setting-up-project-with-intel-mkl/7260/16 "2024-02-06T12:25:51Z")

</div>

A week later and I think I got it finally working.

Here is what I learned. So first of, it is sufficient to compile the needed mkl files with the compile flags of your choice and add them to the correct subfolder of …\mkl\include.

To use the function, you have to declare them as external first, so the compiler knows, that these procedures should be provided when linking to the mkl.

I highly recommend to write your own wrapper module, if you tend to use the procedure more than once. So something like this:

```auto
module MKLWrapperModule
   implicit none

   external mkl_dgemv

contains

   function full_mat_vec(A, x) result(y)
      real*8, allocatable :: A(:, :)
      real*8, allocatable :: x(:), y(:)
      call mkl_dgemv('N', size(A, 1), size(A, 2), 1.0d0, A, size(x, 1), x, 1, 0.0d0, y, 1)
   end function full_mat_vec

end module MKLWrapperModule

```
