|
| 1 | +/* Copyright 2021 Hauke Mewes, Simeon Ehrig, Victor |
| 2 | + * |
| 3 | + * This file is part of vikunja. |
| 4 | + * |
| 5 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | + */ |
| 9 | + |
| 10 | +#include <vikunja/reduce/reduce.hpp> |
| 11 | +#include <vikunja/mem/iterator/ZipIterator.hpp> |
| 12 | + |
| 13 | +#include <alpaka/alpaka.hpp> |
| 14 | + |
| 15 | +#include <iostream> |
| 16 | + |
| 17 | + |
| 18 | +template<typename IteratorTuple> |
| 19 | +void printTuple(IteratorTuple tuple) |
| 20 | +{ |
| 21 | + std::cout << "tuple("; |
| 22 | + int index = 0; |
| 23 | + int tupleSize = std::tuple_size<IteratorTuple>{}; |
| 24 | + for_each(tuple, [&index, tupleSize](auto &x) { std::cout << *x << (++index < tupleSize ? ", " : ""); }); |
| 25 | + std::cout << ")"; |
| 26 | +} |
| 27 | + |
| 28 | +template<std::size_t I = 0, typename FuncT, typename... Tp> |
| 29 | +inline typename std::enable_if<I == sizeof...(Tp), void>::type |
| 30 | + for_each(std::tuple<Tp...> &, FuncT) // Unused arguments are given no names |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | +template<std::size_t I = 0, typename FuncT, typename... Tp> |
| 35 | +inline typename std::enable_if<I < sizeof...(Tp), void>::type |
| 36 | + for_each(std::tuple<Tp...>& t, FuncT f) |
| 37 | + { |
| 38 | + f(std::get<I>(t)); |
| 39 | + for_each<I + 1, FuncT, Tp...>(t, f); |
| 40 | + } |
| 41 | + |
| 42 | +int main() |
| 43 | +{ |
| 44 | + // Define the accelerator here. Must be one of the enabled accelerators. |
| 45 | + using TAcc = alpaka::AccCpuSerial<alpaka::DimInt<3u>, std::uint64_t>; |
| 46 | + |
| 47 | + // Types of the data that will be reduced |
| 48 | + using TRed = uint64_t; |
| 49 | + using TRedChar = char; |
| 50 | + using TRedDouble = double; |
| 51 | + |
| 52 | + // Alpaka index type |
| 53 | + using Idx = alpaka::Idx<TAcc>; |
| 54 | + // Alpaka dimension type |
| 55 | + using Dim = alpaka::Dim<TAcc>; |
| 56 | + // Type of the extent vector |
| 57 | + using Vec = alpaka::Vec<Dim, Idx>; |
| 58 | + // Find the index of the CUDA blockIdx.x component. Alpaka somehow reverses |
| 59 | + // these, i.e. the x component of cuda is always the last value in the vector |
| 60 | + constexpr Idx xIndex = Dim::value - 1u; |
| 61 | + // number of elements to reduce |
| 62 | + const Idx n = static_cast<Idx>(10); |
| 63 | + // create extent |
| 64 | + Vec extent(Vec::all(static_cast<Idx>(1))); |
| 65 | + extent[xIndex] = n; |
| 66 | + |
| 67 | + // define device, platform, and queue types. |
| 68 | + using DevAcc = alpaka::Dev<TAcc>; |
| 69 | + using PltfAcc = alpaka::Pltf<DevAcc>; |
| 70 | + // using QueueAcc = alpaka::test::queue::DefaultQueue<alpaka::Dev<TAcc>>; |
| 71 | + using PltfHost = alpaka::PltfCpu; |
| 72 | + using DevHost = alpaka::Dev<PltfHost>; |
| 73 | + using QueueAcc = alpaka::Queue<TAcc, alpaka::Blocking>; |
| 74 | + using QueueHost = alpaka::QueueCpuBlocking; |
| 75 | + |
| 76 | + // Get the host device. |
| 77 | + DevHost devHost(alpaka::getDevByIdx<PltfHost>(0u)); |
| 78 | + // Get a queue on the host device. |
| 79 | + QueueHost queueHost(devHost); |
| 80 | + // Select a device to execute on. |
| 81 | + DevAcc devAcc(alpaka::getDevByIdx<PltfAcc>(0u)); |
| 82 | + // Get a queue on the accelerator device. |
| 83 | + QueueAcc queueAcc(devAcc); |
| 84 | + |
| 85 | + // allocate memory both on host and device. |
| 86 | + auto deviceMem(alpaka::allocBuf<TRed, Idx>(devAcc, extent)); |
| 87 | + auto hostMem(alpaka::allocBuf<TRed, Idx>(devHost, extent)); |
| 88 | + // Fill memory on host with numbers from 0...n-1. |
| 89 | + TRed* hostNative = alpaka::getPtrNative(hostMem); |
| 90 | + for(Idx i = 0; i < n; ++i) |
| 91 | + hostNative[i] = static_cast<TRed>(i + 1); |
| 92 | + // Copy to accelerator. |
| 93 | + alpaka::memcpy(queueAcc, deviceMem, hostMem, extent); |
| 94 | + TRed* deviceNative = alpaka::getPtrNative(deviceMem); |
| 95 | + |
| 96 | + // allocate memory both on host and device. |
| 97 | + auto deviceMemChar(alpaka::allocBuf<TRedChar, Idx>(devAcc, extent)); |
| 98 | + auto hostMemChar(alpaka::allocBuf<TRedChar, Idx>(devHost, extent)); |
| 99 | + // Fill memory on host with char from 'a' to 'j'. |
| 100 | + TRedChar* hostNativeChar = alpaka::getPtrNative(hostMemChar); |
| 101 | + hostNativeChar[0] = 'a'; |
| 102 | + hostNativeChar[1] = 'b'; |
| 103 | + hostNativeChar[2] = 'c'; |
| 104 | + hostNativeChar[3] = 'd'; |
| 105 | + hostNativeChar[4] = 'e'; |
| 106 | + hostNativeChar[5] = 'f'; |
| 107 | + hostNativeChar[6] = 'g'; |
| 108 | + hostNativeChar[7] = 'h'; |
| 109 | + hostNativeChar[8] = 'i'; |
| 110 | + hostNativeChar[9] = 'j'; |
| 111 | + // Copy to accelerator. |
| 112 | + alpaka::memcpy(queueAcc, deviceMemChar, hostMemChar, extent); |
| 113 | + TRedChar* deviceNativeChar = alpaka::getPtrNative(deviceMemChar); |
| 114 | + |
| 115 | + // allocate memory both on host and device. |
| 116 | + auto deviceMemDouble(alpaka::allocBuf<TRedDouble, Idx>(devAcc, extent)); |
| 117 | + auto hostMemDouble(alpaka::allocBuf<TRedDouble, Idx>(devHost, extent)); |
| 118 | + // Fill memory on host with double numbers from 10.12...(n-1 + 10.12). |
| 119 | + TRedDouble* hostNativeDouble = alpaka::getPtrNative(hostMemDouble); |
| 120 | + for(Idx i = 0; i < n; ++i) |
| 121 | + hostNativeDouble[i] = static_cast<TRedDouble>(i + 10.12); |
| 122 | + // Copy to accelerator. |
| 123 | + alpaka::memcpy(queueAcc, deviceMemDouble, hostMemDouble, extent); |
| 124 | + TRedDouble* deviceNativeDouble = alpaka::getPtrNative(deviceMemDouble); |
| 125 | + |
| 126 | + std::cout << "\nTesting zip iterator in host with tuple<uint64_t, char, double>\n\n"; |
| 127 | + |
| 128 | + using IteratorTuple = std::tuple<TRed*, TRedChar*, TRedDouble*>; |
| 129 | + IteratorTuple zipTuple = std::make_tuple(hostNative, hostNativeChar, hostNativeDouble); |
| 130 | + vikunja::mem::iterator::ZipIterator<IteratorTuple> zipIter(zipTuple); |
| 131 | + |
| 132 | + std::cout << "*zipIter: "; |
| 133 | + printTuple(*zipIter); |
| 134 | + std::cout << "\n\n"; |
| 135 | + |
| 136 | + std::cout << "*++zipIter: "; |
| 137 | + printTuple(*++zipIter); |
| 138 | + std::cout << "\n*zipIter: "; |
| 139 | + printTuple(*zipIter); |
| 140 | + std::cout << "\n\n"; |
| 141 | + |
| 142 | + std::cout << "*zipIter++: "; |
| 143 | + printTuple(*zipIter++); |
| 144 | + std::cout << "\n*zipIter: "; |
| 145 | + printTuple(*zipIter); |
| 146 | + std::cout << "\n\n"; |
| 147 | + |
| 148 | + zipIter += 6; |
| 149 | + std::cout << "zipIter += 6;\n" |
| 150 | + << "*zipIter: "; |
| 151 | + printTuple(*zipIter); |
| 152 | + std::cout << "\n\n"; |
| 153 | + |
| 154 | + zipIter -= 2; |
| 155 | + std::cout << "zipIter -= 2;\n" |
| 156 | + << "*zipIter: "; |
| 157 | + printTuple(*zipIter); |
| 158 | + std::cout << "\n\n"; |
| 159 | + |
| 160 | + std::cout << "--zipIter: "; |
| 161 | + printTuple(*--zipIter); |
| 162 | + std::cout << "\n*zipIter: "; |
| 163 | + printTuple(*zipIter); |
| 164 | + std::cout << "\n\n"; |
| 165 | + |
| 166 | + std::cout << "zipIter--: "; |
| 167 | + printTuple(*zipIter--); |
| 168 | + std::cout << "\n*zipIter: "; |
| 169 | + printTuple(*zipIter); |
| 170 | + std::cout << "\n\n"; |
| 171 | + |
| 172 | + std::cout << "*(zipIter + 2): "; |
| 173 | + printTuple(*(zipIter + 2)); |
| 174 | + std::cout << "\n\n"; |
| 175 | + |
| 176 | + std::cout << "*(nzipIter - 3): "; |
| 177 | + printTuple(*(zipIter - 3)); |
| 178 | + std::cout << "\n\n"; |
| 179 | + |
| 180 | + std::cout << "*zipIter[0]: "; |
| 181 | + printTuple(zipIter[0]); |
| 182 | + std::cout << "\n"; |
| 183 | + |
| 184 | + std::cout << "*zipIter[3]: "; |
| 185 | + printTuple(zipIter[3]); |
| 186 | + std::cout << "\n"; |
| 187 | + |
| 188 | + std::cout << "*zipIter[6]: "; |
| 189 | + printTuple(zipIter[6]); |
| 190 | + std::cout << "\n"; |
| 191 | + |
| 192 | + std::cout << "*zipIter[9]: "; |
| 193 | + printTuple(zipIter[9]); |
| 194 | + std::cout << "\n\n"; |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + std::cout << "-----\n\n" |
| 199 | + << "Failed when building for accelerator testing\n\n"; |
| 200 | + |
| 201 | + // std::cout << "Testing accelerator: " << alpaka::getAccName<TAcc>() << " with size: " << n << "\n\n"; |
| 202 | + |
| 203 | + // IteratorTuple deviceZipTuple = std::make_tuple(deviceNative, deviceNativeChar, deviceNativeDouble); |
| 204 | + // vikunja::mem::iterator::ZipIterator<IteratorTuple> deviceZipIter(deviceZipTuple); |
| 205 | + |
| 206 | + // using IteratorTupleReduceResult = std::tuple<TRed, TRedChar, TRedDouble>; |
| 207 | + |
| 208 | + // // Use Lambda function for reduction |
| 209 | + // auto sum = [] ALPAKA_FN_HOST_ACC(IteratorTuple const i, IteratorTuple const j) |
| 210 | + // { |
| 211 | + // IteratorTupleReduceResult tmp = std::make_tuple(*std::get<0>(i) + *std::get<0>(j), *std::get<1>(i), *std::get<2>(i) + *std::get<2>(j)); |
| 212 | + // return tmp; |
| 213 | + // }; |
| 214 | + // auto doubleNum = [] ALPAKA_FN_HOST_ACC(IteratorTuple const i) |
| 215 | + // { |
| 216 | + // IteratorTupleReduceResult tmp = std::make_tuple(2 * *std::get<0>(i), *std::get<1>(i), 2 * *std::get<2>(i)); |
| 217 | + // return tmp; |
| 218 | + // }; |
| 219 | + |
| 220 | + // // TRANSFORM_REDUCE CALL: |
| 221 | + // // Takes the arguments: accelerator device, host device, accelerator queue, size of data, pointer-like to memory, |
| 222 | + // // transform lambda, reduce lambda. |
| 223 | + // auto transformReduceResult = vikunja::reduce::deviceTransformReduce<TAcc>( |
| 224 | + // devAcc, |
| 225 | + // devHost, |
| 226 | + // queueAcc, |
| 227 | + // n, |
| 228 | + // deviceZipIter, |
| 229 | + // doubleNum, |
| 230 | + // sum); |
| 231 | + |
| 232 | + |
| 233 | + |
| 234 | + return 0; |
| 235 | +} |
0 commit comments