blas daxpy dcopy函数的使用

来源:互联网 发布:手机编程用什么软件 编辑:程序博客网 时间:2024/06/05 02:07

关于blas和cblas的安装见CBLAS的安装使用。

daxpy函数的作用是将一个向量加上另一个向量的值,即:dy[i]=da*dx[i],其中da为常数,
函数的完整声明可以在cblas.h中看到,如下:
void cblas_daxpy(const int N, const double alpha, const double *X, const int incX, double *Y, const int incY);

各个参数含义为:

    int n;                           // array size     double da;                      // double constant     double *dx;                     // input double array     int incx;                        // input stride     double *dy;                      // output double array     int incy;                       // output stride 

dcopy函数的作为将一个向量赋值给另一个向量即:dy[i]=dx[i];函数的声明跟daxpy一致,只是少了常量da。

下面一个程序是这两个函数的一个举例:

#include <stdio.h>#include <stdlib.h>#include "cblas.h"int main(){        int n;                          /*! array size */        double da;                      /*! double constant */        double *dx;                     /*! input double array */        int incx;                        /*! input stride */        double *dy;                      /*! output double array */        int incy;                       /*! output stride */        int i;        n = 10;        da = 10;        dx = (double*)malloc(sizeof(double)*n);        incx = 1;        dy = (double*)malloc(sizeof(double)*n);        incy = 1;        for(i=0;i<n;i++){                dx[i] = 9-i;                dy[i] = i;                printf("%f ",dy[i]);    //输出原来的dy        }        printf("\n");        cblas_daxpy(n, da, dx,incx, dy, incy);  //运行daxpy程序    //    cblas_dcopy(n, dx,incx, dy, incy);      //运行dcopy程序        for(i=0;i<n;i++){            printf("%f ",dy[i]);   //输出计算后的dy        }        printf("\n");        return 0;   }  

参考:
http://www.netlib.org/blas/
http://www.netlib.org/blas/cblas.h
http://www.noao.edu/staff/mighell/mx/mx/src/sbin/mxsl_na/docs/html/daxpy_8c-source.html

注:
The benchmarks allowed us to vary the size of the vector used during execution, the number of iterations, and allowed us to control the throughput by sleeping between iterations.

0 0