Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagecpp
titlematmul_arm.cpp
linenumberstrue
#include <cassert>
#include <cstdlib>
#include <iostream>

using namespace std;

const int DIM       = 100;
const int mat_N     = DIM;
const int mat_K     = DIM;
const int mat_M     = DIM;

void mat_mpy(const float *A, const float *B, float *C, int mat_N, int mat_K, int mat_M)
{
    for (int col = 0; col < mat_M; ++col)
        for (int row = 0; row < mat_N; ++row)
        {
            C[row*mat_M+col] = 0;
            for (int i = 0; i < mat_K; ++i)
                C[row*mat_M+col] += A[row*mat_K+i] * B[i*mat_M+col];
        }
}

int main(int argc, char *argv[])
{
    size_t mat_size = DIM * DIM * sizeof(float);

    // Allocate matrices
    float *A      = (float *) malloc(mat_size);
    float *B      = (float *) malloc(mat_size);
    float *C      = (float *) malloc(mat_size);
    // Ensure memory was successfully allocated 
    assert(A != nullptr && B != nullptr && C != nullptr && C != nullptr);

    // Initialize matrices
    srand(time(0));
    for (int i=0; i < mat_N * mat_K; ++i) A[i] = rand() % 5 + 1;
    for (int i=0; i < mat_K * mat_M; ++i) B[i] = rand() % 5 + 1;
    for (int i=0; i < mat_N * mat_M; ++i) C[i] = 0.0;

    // Multiply matrices C = A x B
    mat_mpy(A, B, C, mat_N, mat_K, mat_M);

    free(A);
    free(B);
    free(C);

    return 0;
}

这个代码实现的是矩阵乘法的程序,参考 这个代码实现的是矩阵乘法的程序,请参考 phyCORE AM57x SDK 安装与使用 / phyCORE AM57x how to install SDK and use 编译 编译并运行。

Code Block
$CXX -std=c++11 matmul_arm.cpp -o matmul_arm

...