首页 > 代码库 > Learn ZYNQ (7)

Learn ZYNQ (7)

矩阵相乘的例子

参考博客:http://blog.csdn.net/kkk584520/article/details/18812321

MatrixMultiply.c

    typedef int data_type;    #define N 5    void MatrixMultiply(data_type AA[N*N],data_type bb[N],data_type cc[N])    {        int i,j;        for(i = 0;i<N;i++)        {            data_type sum = 0;            for(j = 0;j<N;j++)            {                sum += AA[i*N+j]*bb[j];            }            cc[i] = sum;        }    }

修改后:

#include <ap_cint.h>typedef uint15 data_type;    #define N 5    void MatrixMultiply(data_type AA[N*N],data_type bb[N],data_type cc[N])    {        int i,j;        MatrixMultiply_label2:for(i = 0;i<N;i++)        {            data_type sum = 0;            MatrixMultiply_label1:for(j = 0;j<N;j++)            {                sum += AA[i*N+j]*bb[j];            }            cc[i] = sum;        }    }

测试文件:TestMatrixMultiply.c:

#include <stdio.h>#include <ap_cint.h>typedef uint15 data_type;#define N 5const data_type MatrixA[] = {    #include "a.h"};const data_type Vector_b[] = {    #include "b.h"};const data_type MatlabResult_c[] = {    #include "c.h"};data_type HLS_Result_c[N] = {0};void CheckResult(data_type * matlab_result,data_type * your_result);int main(void){	int i;     printf("Checking Results:\r\n");     MatrixMultiply(MatrixA,Vector_b,HLS_Result_c);     CheckResult(MatlabResult_c,HLS_Result_c);     return 0;}void CheckResult(data_type * matlab_result,data_type * your_result){     int i;     for(i = 0;i<N;i++)     {    	 printf("Idx %d: Error = %d \r\n",i,matlab_result[i]-your_result[i]);     }}

a.h

{82},  {10},  {16},  {15},  {66},{91},  {28},  {98},  {43},  {4},{13},  {55},  {96},  {92},  {85},{92},  {96},  {49},  {80},  {94},{64},  {97},  {81},  {96},  {68}

b.h

{76},{75},{40},{66},{18}

c.h

{9800},{15846},{16555},{23124},{22939}

d