openGPMP
Open Source Mathematics Package
mtx_avx2_arr_i16.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 #include <cassert>
34 #include <cstddef>
35 #include <cstdint>
36 #include <iostream>
37 #include <openGPMP/linalg/mtx.hpp>
38 #include <vector>
39 
40 #if defined(__x86_64__) || defined(__amd64__) || defined(__amd64)
41 
42 /************************************************************************
43  *
44  * Matrix Operations for AVX ISA
45  *
46  ************************************************************************/
47 #if defined(__AVX2__)
48 
49 // AVX family intrinsics
50 #include <immintrin.h>
51 
52 /************************************************************************
53  *
54  * Matrix Operations on Arrays
55  *
56  ************************************************************************/
57 // matrix addition for 16-bit integers using 256-bit SIMD registers
58 void gpmp::linalg::Mtx::mtx_add(const int16_t *A,
59  const int16_t *B,
60  int16_t *C,
61  int rows,
62  int cols) {
63  // BUG FIXME
64  for (int i = 0; i < rows; ++i) {
65  int j = 0;
66  for (; j < cols - 15; j += 16) {
67  __m256i a = _mm256_loadu_si256(
68  reinterpret_cast<const __m256i *>(&A[i * cols + j]));
69  __m256i b = _mm256_loadu_si256(
70  reinterpret_cast<const __m256i *>(&B[i * cols + j]));
71  __m256i c = _mm256_loadu_si256(
72  reinterpret_cast<const __m256i *>(&C[i * cols + j]));
73 
74  // Perform vectorized addition and accumulate the result
75  c = _mm256_add_epi16(c, _mm256_add_epi16(a, b));
76 
77  // Store the result back to the C matrix
78  _mm256_storeu_si256(reinterpret_cast<__m256i *>(&C[i * cols + j]),
79  c);
80  }
81 
82  for (; j < cols; ++j) {
83  C[i * cols + j] = A[i * cols + j] + B[i * cols + j];
84  }
85  }
86 }
87 
88 void gpmp::linalg::Mtx::mtx_sub(const int16_t *A,
89  const int16_t *B,
90  int16_t *C,
91  int rows,
92  int cols) {
93  for (int i = 0; i < rows; ++i) {
94  int j = 0;
95  for (; j < cols - 15; j += 16) {
96  __m256i a = _mm256_loadu_si256(
97  reinterpret_cast<const __m256i *>(&A[i * cols + j]));
98  __m256i b = _mm256_loadu_si256(
99  reinterpret_cast<const __m256i *>(&B[i * cols + j]));
100  __m256i c = _mm256_loadu_si256(
101  reinterpret_cast<const __m256i *>(&C[i * cols + j]));
102 
103  // Perform vectorized subtraction and accumulate the result
104  c = _mm256_sub_epi16(a, b);
105 
106  // Store the result back to the C matrix
107  _mm256_storeu_si256(reinterpret_cast<__m256i *>(&C[i * cols + j]),
108  c);
109  }
110 
111  for (; j < cols; ++j) {
112  C[i * cols + j] = A[i * cols + j] - B[i * cols + j];
113  }
114  }
115 }
116 
117 void gpmp::linalg::Mtx::mtx_mult(const int16_t *A,
118  const int16_t *B,
119  int16_t *C,
120  int rows_a,
121  int cols_a,
122  int cols_b) {
123  for (int i = 0; i < rows_a; ++i) {
124  for (int j = 0; j < cols_b; j += 16) {
125  __m256i c = _mm256_setzero_si256();
126 
127  for (int k = 0; k < cols_a; ++k) {
128  __m256i a = _mm256_set1_epi16(A[i * cols_a + k]);
129  __m256i b = _mm256_loadu_si256(
130  reinterpret_cast<const __m256i *>(&B[k * cols_b + j]));
131 
132  __m256i prod = _mm256_mullo_epi16(a, b);
133  c = _mm256_add_epi16(c, prod);
134  }
135 
136  _mm256_storeu_si256(reinterpret_cast<__m256i *>(&C[i * cols_b + j]),
137  c);
138  }
139 
140  // Handle remaining elements
141  for (int j = cols_b - cols_b % 16; j < cols_b; ++j) {
142  int sum = 0;
143 
144  for (int k = 0; k < cols_a; ++k) {
145  sum += A[i * cols_a + k] * B[k * cols_b + j];
146  }
147 
148  C[i * cols_b + j] = sum;
149  }
150  }
151 }
152 
153 #endif
154 
155 // x86
156 #endif
void mtx_mult(std::vector< double > A, std::vector< double > B, std::vector< double > C)
list C
Definition: linalg.py:24
list A
Definition: linalg.py:22
list B
Definition: linalg.py:23