Project Ne10
An open, optimized software library for the ARM architecture.
NE10_sample_matrix_multiply.c
Go to the documentation of this file.
1 /*
2  * Copyright 2011-16 ARM Limited and Contributors.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of ARM Limited nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY ARM LIMITED AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL ARM LIMITED AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #include "NE10.h"
31 
32 #define MATRICES 3
33 
34 static void initialise_matrix_column(ne10_mat_row3f *col);
35 static void initialise_matrix(ne10_mat3x3f_t *mat);
36 
42 {
43  ne10_mat3x3f_t src[MATRICES]; // A source array of `MATRICES` input matrices
44  ne10_mat3x3f_t mul[MATRICES]; // An array of matrices to multiply those in `src` by
45  ne10_mat3x3f_t dst[MATRICES]; // A destination array for the multiplication results
46 
47  // Initialise Ne10, using hardware auto-detection to set library function pointers
48  if (ne10_init() != NE10_OK)
49  {
50  fprintf(stderr, "Failed to initialise Ne10.\n");
51  return 1;
52  }
53 
54  // Generate test input values
55  for (int i = 0; i < MATRICES; i++)
56  {
57  initialise_matrix(&src[i]);
58  initialise_matrix(&mul[i]);
59  }
60 
61  // Perform the multiplication of the matrices in `src` by those in `mul`
62  ne10_mulmat_3x3f(dst, src, mul, MATRICES);
63 
64  // Display the results (src[i] * mul[i] == dst[i])
65  for (int i = 0; i < MATRICES; i++)
66  {
67  printf("[ %5.2f %5.2f %5.2f [ %5.2f %5.2f %5.2f [ %5.2f %5.2f %5.2f\n",
68  src[i].c1.r1, src[i].c2.r1, src[i].c3.r1,
69  mul[i].c1.r1, mul[i].c2.r1, mul[i].c3.r1,
70  dst[i].c1.r1, dst[i].c2.r1, dst[i].c3.r1);
71  printf(" %5.2f %5.2f %5.2f * %5.2f %5.2f %5.2f = %5.2f %5.2f %5.2f\n",
72  src[i].c1.r2, src[i].c2.r2, src[i].c3.r2,
73  mul[i].c1.r2, mul[i].c2.r2, mul[i].c3.r2,
74  dst[i].c1.r2, dst[i].c2.r2, dst[i].c3.r2);
75  printf(" %5.2f %5.2f %5.2f ] %5.2f %5.2f %5.2f ] %5.2f %5.2f %5.2f ]\n",
76  src[i].c1.r3, src[i].c2.r3, src[i].c3.r3,
77  mul[i].c1.r3, mul[i].c2.r3, mul[i].c3.r3,
78  dst[i].c1.r3, dst[i].c2.r3, dst[i].c3.r3);
79  printf("\n");
80  }
81 
82  return 0;
83 }
84 
85 void initialise_matrix(ne10_mat3x3f_t *mat)
86 {
87  initialise_matrix_column(&mat->c1);
88  initialise_matrix_column(&mat->c2);
89  initialise_matrix_column(&mat->c3);
90 }
91 
92 void initialise_matrix_column(ne10_mat_row3f *col)
93 {
94  col->r1 = (ne10_float32_t)rand() / RAND_MAX * 5.0f;
95  col->r2 = (ne10_float32_t)rand() / RAND_MAX * 5.0f;
96  col->r3 = (ne10_float32_t)rand() / RAND_MAX * 5.0f;
97 }
ne10_float32_t r2
Definition: NE10_types.h:145
float ne10_float32_t
Definition: NE10_types.h:80
ne10_mat_row3f c3
Definition: NE10_types.h:153
ne10_float32_t r1
Definition: NE10_types.h:144
ne10_result_t ne10_init(void)
Definition: NE10_init.c:44
ne10_float32_t r3
Definition: NE10_types.h:146
#define MATRICES
#define NE10_OK
Definition: NE10_types.h:65
ne10_result_t(* ne10_mulmat_3x3f)(ne10_mat3x3f_t *dst, ne10_mat3x3f_t *src1, ne10_mat3x3f_t *src2, ne10_uint32_t count)
Multiplies the 3x3 matrices of one input array by those of the same index in another, storing the results in an output array.
ne10_mat_row3f c1
Definition: NE10_types.h:151
int matrix_multiply_sample_main(void)
ne10_mat_row3f c2
Definition: NE10_types.h:152