From 19dab4fa65f40b3bc6567b607f8ca8c888a9c43c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gr=C3=A9goire=20KUBLER?= <gregoire.kubler@proton.me>
Date: Thu, 12 Sep 2024 14:49:20 +0200
Subject: [PATCH] chore : moved broadcasting files from backend_cpu to core

---
 include/aidge/data/Broadcasting.hpp | 52 +++++++++++++++++++++++++++
 src/data/Broadcasting.cpp           | 55 +++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+)
 create mode 100644 include/aidge/data/Broadcasting.hpp
 create mode 100644 src/data/Broadcasting.cpp

diff --git a/include/aidge/data/Broadcasting.hpp b/include/aidge/data/Broadcasting.hpp
new file mode 100644
index 000000000..6e2fc22c8
--- /dev/null
+++ b/include/aidge/data/Broadcasting.hpp
@@ -0,0 +1,52 @@
+/********************************************************************************
+ * Copyright (c) 2024 CEA-List
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0.
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ ********************************************************************************/
+
+#ifndef AIDGE_CORE_DATA_BROADCASTING_H_
+#define AIDGE_CORE_DATA_BROADCASTING_H_
+
+#include <vector>
+
+namespace Aidge {
+
+// Function to broadCast an input dims vector into the same size as an outputDims vector
+
+    /**
+     * @brief  Broadcast an input dims vector into the same size as an outputDims vector
+     * @details The missing dimensions would be completed by 1
+     * @example T1 dims are [1,3,1,5]
+     *          T2 dims are [5]
+     * To simplify the computation we would like to have the same number of dimensions to 
+     * @param outputDims The vector of dimensions to follow 
+     * @param dimsToBroadcast The vecotr of dimensions to braodcast
+     * @return std::vector<std::size_t> a broadcasted vector by addding 1 on the missing dimensions.
+     */
+    std::vector<std::size_t> getBroadcastedDims(const std::vector<std::size_t>& outputDims, const std::vector<std::size_t>& dimsToBroadcast);
+
+    /**
+     * @brief Get a vector of indexes along the dimensions vector from a flattened index
+     * @param dimensions The vector of dimensions we want the indexes on
+     * @param idx The flattened index
+     * @return std::vector<std::size_t> vector of indexes along dimensions.
+     */
+    std::vector<std::size_t> getMultiDimIndices(const std::vector<std::size_t>& dimensions, std::size_t idx);
+
+    // Function to get a flattened index from multi-dimensional indices
+    /**
+     * @brief Get a flattened index the dimensions vector from a given vector of indices on a broadcasted vector
+     * @param dimensions The vector of dimensions we want the flattened index on
+     * @param indices The vector of indices we want to flatten
+     * @return std::size_t The flattened index on the dimensions vector
+     */
+    std::size_t getFlattenedIndex(const std::vector<std::size_t>& dimensions, const std::vector<std::size_t>& indices);
+
+} // namespace Aidge
+
+#endif // AIDGE_CORE_DATA_BROADCASTING_H_
diff --git a/src/data/Broadcasting.cpp b/src/data/Broadcasting.cpp
new file mode 100644
index 000000000..d322d2d34
--- /dev/null
+++ b/src/data/Broadcasting.cpp
@@ -0,0 +1,55 @@
+/********************************************************************************
+ * Copyright (c) 2024 CEA-List
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0.
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ ********************************************************************************/
+
+#include "aidge/data/Broadcasting.hpp"
+
+#include <vector>
+
+namespace Aidge {
+
+std::vector<std::size_t>
+getBroadcastedDims(const std::vector<std::size_t> &outputDims,
+                   const std::vector<std::size_t> &dimsToBroadcast) {
+  std::vector<std::size_t> broadcastedDims(outputDims.size(), 1);
+  for (int j = dimsToBroadcast.size() - 1; j >= 0; --j) {
+    std::size_t idx = outputDims.size() - (dimsToBroadcast.size() - j);
+    broadcastedDims[idx] = dimsToBroadcast[j];
+  }
+  return broadcastedDims;
+}
+
+std::vector<std::size_t>
+getMultiDimIndices(const std::vector<std::size_t> &dimensions,
+                   std::size_t idx) {
+  std::vector<std::size_t> indices(dimensions.size(), 0);
+
+  for (int i = dimensions.size() - 1; i >= 0; --i) {
+    indices[i] = idx % dimensions[i];
+    idx /= dimensions[i];
+  }
+
+  return indices;
+}
+
+std::size_t getFlattenedIndex(const std::vector<std::size_t> &dimensions,
+                              const std::vector<std::size_t> &indices) {
+  std::size_t flattenedIdx = 0;
+  std::size_t stride = 1;
+
+  for (int i = dimensions.size() - 1; i >= 0; --i) {
+    std::size_t idx = dimensions[i] > 1 ? indices[i] : 0;
+    flattenedIdx += idx * stride;
+    stride *= dimensions[i];
+  }
+  return flattenedIdx;
+}
+
+} // namespace Aidge
-- 
GitLab