Skip to main content

MKL with C++

          C++ program with MKL 



i have  written a  c++ example for Matrix eigenvalue/eigenvector problem.
In the Documentation MKL all the examples are given in FORTRAN/C .
To find all the eigenvalue and optionally all the eigenvector of real symmetric matrix i have used the lapack function dsyev from MKL .
In This example i have created a matrix which correspond to 3D Tight Binding Hamiltonian. This is quit common in Condensed Matter Physics community.



#include
#include
#include "mkl.h"
#define dm 12
#define N dm*dm*dm
#define LDA N

int main()
{
    int n = N, lda = LDA, info, lwork;
    double wkopt;
    double w[N];
    double *a =new double [N*N];


    int i,j,k,l,m,o,pd[dm];
    for(i=0;i        pd[i]=(i+1)%dm;
    for(i=0;i    for(j=0;j        a[i*N + j] =0.00;


//******Matrix :: 3D Tight Binding Hamiltonian ***************
       for(i=0;i       for(j=0;j       for(k=0;k        {
                l=i*dm*dm + j*dm + k;
                m=pd[i]*dm*dm + j*dm + k;
                a[l*N + m] =-1.0;
                a[m*N + l] =-1.0;

                m=i*dm*dm + pd[j]*dm + k;
                a[l*N + m] =-1.0;
                a[m*N + l] =-1.0;

                m=i*dm*dm + j*dm + pd[k];
                a[l*N + m] =-1.0;
                a[m*N + l] =-1.0;
        }

//*******************Diagonalization*************************************
    lwork = -1;
    dsyev( "N", "U", &n, a, &lda, w, &wkopt, &lwork, &info );
    lwork = (int)wkopt;
    double *work= new double[lwork];
    dsyev( "N", "U", &n, a, &lda, w, work, &lwork, &info );
    delete[] a;
    delete[] work;
//*****************Write eigen values to a file :-------------------------
    std::ofstream dfile("eigen.dat");
    for(i=0;i    dfile << w[i] << std::endl;
    dfile.close();
//************************************************************************
    return 0;
}
To compile this program you need to use this links .
 icpc filename.cpp  -O2 -C -Wl,--start-group /export/apps/ics_2012.0.032/ics/2012.0.032/mkl/lib/intel64/libmkl_intel_lp64.a  /export/apps/ics_2012.0.032/ics/2012.0.032/mkl/lib/intel64/libmkl_core.a -Wl,--end-group -lpthread  -openmp -lmkl_intel_ilp64 -lmkl_intel_thread -lmkl_core -liomp5   -mcmodel large -shared-intel -traceback

I assume that you have save this program in  filename.cpp file . You should change the above linking path depending upon where you have installed the Intel libraries.


For Newer version just do this : 

icpc filename.cpp -mkl

Comments

Popular posts from this blog

Filling between curves with color gradient or cmap in Matplotlib

I was trying to plot fill_between () in matplotlib with color gradient or any cmap defined in pyplot.  After googleing a lot i couldn't find any solution.  An alternative method is to use imshow() . I have created this example :  import numpy as np import matplotlib.pyplot as plt from matplotlib.path import Path from matplotlib.patches import PathPatch xx=np.arange(0,10,0.01) yy=xx*np.exp(-xx) path = Path(np.array([xx,yy]).transpose()) patch = PathPatch(path, facecolor='none') plt.gca().add_patch(patch) im = plt.imshow(xx.reshape(yy.size,1),  cmap=plt.cm.Reds,interpolation="bicubic",                 origin='lower',extent=[0,10,-0.0,0.40],aspect="auto", clip_path=patch, clip_on=True) #im.set_clip_path(patch) plt.savefig("out.png") Source: Matplotlib Example  http://matplotlib.org/examples/pylab_examples/image_clip_path.html  

pm3d map of gnuplot in matplotlib

In gnuplot pm3d map plot a 3d data into a surface with color as value of "z". Lets say we have a data file something like this .. 0.00    0.00    0.00 0.00    1.00    0.00 0.00    2.00    0.00 0.00    3.00    1.9358 0.00    4.00    3.618 0.00    5.00    5.17 0.00    6.00    6.93 0.00    7.00    8.82 0.00    8.00    10.692 1.00    0.00    0.00 1.00    1.00    0.00 1.00    2.00    0.00 1.00    3.00    2.1318 .... ... In gnuplot prompt we will do something like this . set pm3d map; set dgrid  20,20 sp "data.dat" u 1:2:3    and this will produce a figure something like a...

Installing Latex in mac

Installing latex in mac is very easy if you want to install the full mactex package. But the only  problem is it huge in size (~2GB). An alternative way to install the smaller mactex package. This will   not install some files which might be needed for your .tex file to compile. If you compile by pdflatex it will give some warning like this ! LaTeX Error: File `footmisc.sty' not found. Type X to quit or to proceed, or enter new name. (Default extension: sty) Then you should paste this command in terminal  sudo tlmgr install footmisc collection-fontsrecommended  Similarly if pdflatex complain about any other missing .sty file then install that just  using the above command. I couldn't install psboxit package in the way I described above. This is because psboxit is an absolute package and it is no longer available from the repository. To install this package i did this . cd /usr/local/texlive/2015basic/texmf-dist/tex/latex/  (In...