1

这是一个小程序,其中包含对英特尔MKL库函数和DLIB优化例程的调用find_min_using_approximate_derivatives

使用 intel 编译器编译时,代码运行完美C++icpc使用调用:

    icpc main.cpp -I /Users/Username/Code/dlib-18.16 -DDLIB_USE_BLAS -I$MKLROOT/include -L$MKLROOT/lib/ -lmkl_core -lmkl_intel_thread -lpthread -lm -lmkl_intel_lp64 -DENABLE_DLIB -DDLIB_USE_BLAS

或通过以下方式禁用DLIB代码的相关部分:

clang++ main.cpp -I$MKLROOT/include -L$MKLROOT/lib/ -lmkl_core -lmkl_intel_thread -lpthread -lm -lmkl_intel_lp64

C++ 代码

    // #define ENABLE_DLIB

    #ifdef ENABLE_DLIB
      #include "dlib/optimization.h"
    #endif

    #include <iostream>
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include "mkl.h"

    using namespace std;
    #ifdef ENABLE_DLIB
    using namespace dlib;
    #endif

    template<typename T>
    void printArray(T *data, string name, int len){
        cout << name << "\n";
        for(int i=0;i<len;i++){
            cout << data[i] << " ";
        }
        cout << "\n";
    }

    #ifdef ENABLE_DLIB
    typedef matrix<double,0,1> column_vector;

    double rosen (const column_vector& m)
    {
        const double x = m(0);
        const double y = m(1);
        return 100.0*pow(y - x*x,2) + pow(1 - x,2);
    }
    #endif

    int main()
    {
    #ifdef ENABLE_DLIB
        column_vector starting_point(2);
        starting_point = 4, 8;

        find_min_using_approximate_derivatives(bfgs_search_strategy(),
                                               objective_delta_stop_strategy(1e-7),
                                               rosen, starting_point, -1);
        cout << "\nBFGS rosen minimum lies at:\n";
        cout << "x = " << starting_point(0) << endl;
        cout << "y = " << starting_point(1) << endl;
    #endif

        int len=10;
        double *x=new double[len];
        double *y=new double[len];
        for(int i=0;i<len;i++){
            x[i]=(double)std::rand()/RAND_MAX;
            y[i]=(double)std::rand()/RAND_MAX;
        }
        printArray<double>(x, "x", len);
        printArray<double>(y, "y", len);

        //sum(x)
        double x_sum=cblas_dasum(len,x,1);
        cout<< "sum(x): "<< x_sum <<"\n";
    }

然而,它无法用以下类型的多个错误替换上面的内容icpcclang++

错误

    In file included from /opt/intel/composer_xe_2015.3.187/mkl/include/mkl.h:48:
    /opt/intel/composer_xe_2015.3.187/mkl/include/mkl_cblas.h:584:6: error: conflicting types for 'cblas_cgemm'
    void cblas_cgemm(const  CBLAS_LAYOUT Layout, const  CBLAS_TRANSPOSE TransA,
         ^
    /Users/Username/Code/dlib-18.16/dlib/matrix/matrix_blas_bindings.h:75:18: note: previous declaration is here
                void cblas_cgemm(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA,

...表明cblas_*反映在MKL. 的文档DLIB建议使用DLIB_USE_BLAS预处理器指令以使其链接,MKL但显然它似乎无助于使用clang++.

我该如何进行这项工作?

4

0 回答 0