| 133 | descriptor::Trivial, Functor_, Args...>>::value, |
| 134 | int>> |
| 135 | LIBRAPID_ALWAYS_INLINE void |
| 136 | assignParallel(array::ArrayContainer<ShapeType_, Storage<StorageScalar>> &lhs, |
| 137 | const detail::Function<descriptor::Trivial, Functor_, Args...> &function) { |
| 138 | using Function = detail::Function<descriptor::Trivial, Functor_, Args...>; |
| 139 | using Scalar = |
| 140 | typename array::ArrayContainer<ShapeType_, Storage<StorageScalar>>::Scalar; |
| 141 | |
| 142 | constexpr bool allowVectorisation = |
| 143 | typetraits::TypeInfo< |
| 144 | detail::Function<descriptor::Trivial, Functor_, Args...>>::allowVectorisation && |
| 145 | Function::argsAreSameType; |
| 146 | constexpr int64_t packetWidth = []() { |
| 147 | if constexpr (allowVectorisation) { |
| 148 | return typetraits::TypeInfo<Scalar>::packetWidth; |
| 149 | } else { |
| 150 | return 1; |
| 151 | } |
| 152 | }(); |
| 153 | |
| 154 | const size_t size = function.size(); |
| 155 | const size_t vectorSize = size - (size % packetWidth); |
| 156 | |
| 157 | LIBRAPID_ASSUME(vectorSize % packetWidth == 0); |
| 158 | |
| 159 | // Ensure the function can actually be assigned to the array container |
| 160 | // static_assert( |
| 161 | // typetraits::IsSame<Scalar, typename std::decay_t<decltype(function)>::Scalar>, |
| 162 | // "Function return type must be the same as the array container's scalar type"); |
| 163 | LIBRAPID_ASSERT(lhs.shape() == function.shape(), "Shapes must be equal"); |
| 164 | |
| 165 | if constexpr (allowVectorisation) { |
| 166 | #pragma omp parallel for shared(vectorSize, lhs, function) default(none) \ |
| 167 | num_threads(int(global::numThreads)) |
| 168 | for (int64_t index = 0; index < vectorSize; index += packetWidth) { |
| 169 | lhs.writePacket(index, function.packet(index)); |
| 170 | } |
| 171 | |
| 172 | // Assign the remaining elements |
| 173 | for (int64_t index = vectorSize; index < size; ++index) { |
| 174 | lhs.write(index, function.scalar(index)); |
| 175 | } |
| 176 | } else { |
| 177 | #pragma omp parallel for shared(vectorSize, lhs, function, size) default(none) \ |
| 178 | num_threads(int(global::numThreads)) |
| 179 | for (int64_t index = 0; index < size; ++index) { |
| 180 | lhs.write(index, function.scalar(index)); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /// Trivial assignment with fixed-size arrays and parallel execution |
| 186 | /// \tparam ShapeType_ The shape type of the array container |