File: array_validator.cpp

package info (click to toggle)
hipfft 6.1.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,568 kB
  • sloc: cpp: 15,967; python: 186; sh: 45; makefile: 40; xml: 15
file content (549 lines) | stat: -rw-r--r-- 15,649 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// Copyright (C) 2022 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include <iostream>
#include <numeric>
#include <unordered_set>

#include "array_validator.h"
#include "increment.h"

// Check a 2D array for collisions.
// The 2D case can be determined via a number-theoretic argument.
bool valid_length_stride_2d(const size_t l0, const size_t l1, const size_t s0, const size_t s1)
{
    if(s0 == s1)
        return false;
    const auto c = std::lcm(s0, s1);
    return !((s0 * (l0 - 1) >= c) && (s1 * (l1 - 1) >= c));
}

// Compare a 1D direction with a multi-index hyperface for collisions.
bool valid_length_stride_1d_multi(const unsigned int        idx,
                                  const std::vector<size_t> l,
                                  const std::vector<size_t> s,
                                  const int                 verbose)
{
    size_t              l0{0}, s0{0};
    std::vector<size_t> l1{}, s1{};
    for(unsigned int i = 0; i < l.size(); ++i)
    {
        if(i == idx)
        {
            l0 = l[i];
            s0 = s[i];
        }
        else
        {
            l1.push_back(l[i]);
            s1.push_back(s[i]);
        }
    }

    if(verbose > 4)
    {
        std::cout << "l0: " << l0 << "\ts0: " << s0 << std::endl;
    }

    // We only need to go to the maximum pointer offset for (l1,s1).
    const auto max_offset
        = std::accumulate(l1.begin(), l1.end(), (size_t)1, std::multiplies<size_t>())
          - std ::inner_product(l1.begin(), l1.end(), s1.begin(), (size_t)0);
    std::unordered_set<size_t> a0{};
    for(size_t i = 1; i < l0; ++i)
    {
        const auto val = i * s0;
        if(val <= max_offset)
            a0.insert(val);
        else
            break;
    }

    if(verbose > 5)
    {
        std::cout << "a0:";
        for(auto i : a0)
            std::cout << " " << i;
        std::cout << std::endl;

        std::cout << "l1:";
        for(auto i : l1)
            std::cout << " " << i;
        std::cout << std::endl;

        std::cout << "s1:";
        for(auto i : s1)
            std::cout << " " << i;
        std::cout << std::endl;
    }

    // TODO: this can be multi-threaded, since find(...) is thread-safe.
    std::vector<size_t> index(l1.size());
    std::fill(index.begin(), index.end(), 0);
    do
    {
        const int i = std::inner_product(index.begin(), index.end(), s1.begin(), (size_t)0);
        if(i > 0 && (i % s0 == 0))
        {
            // TODO: use an ordered set and binary search
            if(verbose > 6)
                std::cout << i << std::endl;
            if(a0.find(i) != a0.end())
            {
                if(verbose > 4)
                {
                    std::cout << "l0: " << l0 << "\ts0: " << s0 << std::endl;
                    std::cout << "l1:";
                    for(const auto li : l1)
                        std::cout << " " << li;
                    std::cout << " s1:";
                    for(const auto si : s1)
                        std::cout << " " << si;
                    std::cout << std::endl;
                    std::cout << "Found duplicate: " << i << std::endl;
                }
                return false;
            }
        }
    } while(increment_rowmajor(index, l1));

    return true;
}

// Compare a hyperface with another hyperface for collisions.
bool valid_length_stride_multi_multi(const std::vector<size_t> l0,
                                     const std::vector<size_t> s0,
                                     const std::vector<size_t> l1,
                                     const std::vector<size_t> s1)
{
    std::unordered_set<size_t> a0{};

    const auto max_offset
        = std::accumulate(l1.begin(), l1.end(), (size_t)1, std::multiplies<size_t>())
          - std::inner_product(l1.begin(), l1.end(), s1.begin(), (size_t)0);
    std::vector<size_t> index0(l0.size()); // TODO: check this
    std::fill(index0.begin(), index0.end(), 0);
    do
    {
        const auto i = std::inner_product(index0.begin(), index0.end(), s0.begin(), (size_t)0);
        if(i > max_offset)
            a0.insert(i);
    } while(increment_rowmajor(index0, l0));

    std::vector<size_t> index1(l1.size());
    std::fill(index1.begin(), index1.end(), 0);
    do
    {
        const auto i = std::inner_product(index1.begin(), index1.end(), s1.begin(), (size_t)0);
        if(i > 0)
        {
            // TODO: use an ordered set and binary search
            if(a0.find(i) != a0.end())
            {

                return false;
            }
        }
    } while(increment_rowmajor(index1, l1));

    return true;
}

bool valid_length_stride_3d(const std::vector<size_t>& l,
                            const std::vector<size_t>& s,
                            const int                  verbose)
{
    // Check that 2D faces are valid:
    if(!valid_length_stride_2d(l[0], l[1], s[0], s[1]))
        return false;
    if(!valid_length_stride_2d(l[0], l[2], s[0], s[2]))
        return false;
    if(!valid_length_stride_2d(l[1], l[2], s[1], s[2]))
        return false;

    // If the 2D faces are valid, check an axis vs a face for collisions:
    bool invalid = false;
#ifdef _OPENMP
#pragma omp parallel for
#endif
    for(int idx = 0; idx < 3; ++idx)
    {
        if(!valid_length_stride_1d_multi(idx, l, s, verbose))
        {
#ifdef _OPENMP
#pragma omp cancel for
#endif
            invalid = true;
        }
    }
    if(invalid)
        return false;
    return true;
}

bool valid_length_stride_4d(const std::vector<size_t>& l,
                            const std::vector<size_t>& s,
                            const int                  verbose)
{
    if(l.size() != 4)
    {
        throw std::runtime_error("Incorrect dimensions for valid_length_stride_4d");
    }

    // Check that 2D faces are valid:
    for(int idx0 = 0; idx0 < 3; ++idx0)
    {
        for(int idx1 = idx0 + 1; idx1 < 4; ++idx1)
        {
            if(!valid_length_stride_2d(l[idx0], l[idx1], s[idx0], s[idx1]))
                return false;
        }
    }

    bool invalid = false;
    // Check that 1D vs 3D faces are valid:
#ifdef _OPENMP
#pragma omp parallel for
#endif
    for(int idx0 = 0; idx0 < 4; ++idx0)
    {
        if(!valid_length_stride_1d_multi(idx0, l, s, verbose))
        {
#ifdef _OPENMP
#pragma omp cancel for
#endif
            invalid = true;
        }
    }
    if(invalid)
        return false;

    // Check that 2D vs 2D faces are valid:

    // First, get all the permutations
    std::vector<std::vector<size_t>> perms;
    std::vector<size_t>              v(l.size());
    std::fill(v.begin(), v.begin() + 2, 0);
    std::fill(v.begin() + 2, v.end(), 1);
    do
    {
        perms.push_back(v);
        if(verbose > 3)
        {
            std::cout << "v:";
            for(const auto i : v)
            {
                std::cout << " " << i;
            }
            std::cout << "\n";
        }
    } while(std::next_permutation(v.begin(), v.end()));

    // Then loop over all of the permutations.
#ifdef _OPENMP
#pragma omp parallel for
#endif
    for(size_t iperm = 0; iperm < perms.size(); ++iperm)
    {
        std::vector<size_t> l0(2);
        std::vector<size_t> s0(2);
        std::vector<size_t> l1(2);
        std::vector<size_t> s1(2);
        for(size_t i = 0; i < l.size(); ++i)
        {
            if(perms[iperm][i] == 0)
            {
                l0.push_back(l[i]);
                s0.push_back(s[i]);
            }
            else
            {
                l1.push_back(l[i]);
                s1.push_back(s[i]);
            }
        }

        if(verbose > 3)
        {
            std::cout << "\tl0:";
            for(const auto i : l0)
            {
                std::cout << " " << i;
            }
            std::cout << "\n";
            std::cout << "\ts0:";
            for(const auto i : s0)
            {
                std::cout << " " << i;
            }
            std::cout << "\n";
            std::cout << "\tl1:";
            for(const auto i : l1)
            {
                std::cout << " " << i;
            }
            std::cout << "\n";
            std::cout << "\ts1:";
            for(const auto i : s1)
            {
                std::cout << " " << i;
            }
            std::cout << "\n";
        }

        if(!valid_length_stride_multi_multi(l0, s0, l1, s1))
        {
#ifdef _OPENMP
#pragma omp cancel for
#endif
            invalid = true;
        }
    }
    if(invalid)
        return false;

    return true;
}

bool valid_length_stride_generald(const std::vector<size_t> l,
                                  const std::vector<size_t> s,
                                  const int                 verbose)
{
    if(verbose > 2)
    {
        std::cout << "checking dimension " << l.size() << std::endl;
    }

    // Recurse on d-1 hyper-faces:
    for(unsigned int idx = 0; idx < l.size(); ++idx)
    {
        std::vector<size_t> l0{};
        std::vector<size_t> s0{};
        for(size_t i = 0; i < l.size(); ++i)
        {
            if(i != idx)
            {
                l0.push_back(l[i]);
                s0.push_back(s[i]);
            }
        }
        if(!array_valid(l0, s0, verbose))
            return false;
    }

    // Handle the 1D vs (N-1) case:
    for(unsigned int idx = 0; idx < l.size(); ++idx)
    {
        if(!valid_length_stride_1d_multi(idx, l, s, verbose))
            return false;
    }

    for(size_t dim0 = 2; dim0 <= l.size() / 2; ++dim0)
    {
        const size_t dim1 = l.size() - dim0;
        if(verbose > 2)
            std::cout << "dims: " << dim0 << " " << dim1 << std::endl;

        // We iterate over all permutations of an array of length l.size() which contains dim0 zeros
        // and dim1 ones.  We start with {0, ..., 0, 1, ... 1} to guarantee that we hit all the
        // possibilities.

        // First, get all the permutations
        std::vector<std::vector<size_t>> perms;
        std::vector<size_t>              v(l.size());
        std::fill(v.begin(), v.begin() + dim1, 0);
        std::fill(v.begin() + dim1, v.end(), 1);
        do
        {
            perms.push_back(v);
            if(verbose > 3)
            {
                std::cout << "v:";
                for(const auto i : v)
                {
                    std::cout << " " << i;
                }
                std::cout << "\n";
            }

        } while(std::next_permutation(v.begin(), v.end()));

        bool invalid = false;
        // Then loop over all of the permutations.
#ifdef _OPENMP
#pragma omp parallel for
#endif
        for(size_t iperm = 0; iperm < perms.size(); ++iperm)
        {
            std::vector<size_t> l0(dim0);
            std::vector<size_t> s0(dim0);
            std::vector<size_t> l1(dim1);
            std::vector<size_t> s1(dim1);

            for(size_t i = 0; i < l.size(); ++i)
            {
                if(v[i] == 0)
                {
                    l0.push_back(l[i]);
                    s0.push_back(s[i]);
                }
                else
                {
                    l1.push_back(l[i]);
                    s1.push_back(s[i]);
                }
            }

            if(verbose > 3)
            {
                std::cout << "\tl0:";
                for(const auto i : l0)
                {
                    std::cout << " " << i;
                }
                std::cout << "\n";
                std::cout << "\ts0:";
                for(const auto i : s0)
                {
                    std::cout << " " << i;
                }
                std::cout << "\n";
                std::cout << "\tl1:";
                for(const auto i : l1)
                {
                    std::cout << " " << i;
                }
                std::cout << "\n";
                std::cout << "\ts1:";
                for(const auto i : s1)
                {
                    std::cout << " " << i;
                }
                std::cout << "\n";
            }

            if(!valid_length_stride_multi_multi(l0, s0, l1, s1))
            {
#ifdef _OPENMP
#pragma omp cancel for
#endif
                invalid = true;
            }
        }
        if(invalid)
            return false;
    }

    return true;
}

bool sort_by_stride(const std::pair<size_t, size_t>& ls0, const std::pair<size_t, size_t>& ls1)
{
    return ls0.second < ls1.second;
}

bool array_valid(const std::vector<size_t>& length,
                 const std::vector<size_t>& stride,
                 const int                  verbose)
{
    if(length.size() != stride.size())
        return false;

    // If a length is 1, then the stride is irrelevant.
    // If a length is > 1, then the corresponding stride must be > 1.
    std::vector<size_t> l{}, s{};
    for(unsigned int i = 0; i < length.size(); ++i)
    {
        if(length[i] > 1)
        {
            if(stride[i] == 0)
                return false;
            l.push_back(length[i]);
            s.push_back(stride[i]);
        }
    }

    if(length.size() > 1)
    {
        // Check happy path.
        bool                                   happy_path = true;
        std::vector<std::pair<size_t, size_t>> ls;
        for(size_t idx = 0; idx < length.size(); ++idx)
        {
            ls.push_back(std::pair(length[idx], stride[idx]));
        }
        std::sort(ls.begin(), ls.end(), sort_by_stride);

        if(verbose > 2)
        {
            for(size_t idx = 0; idx < ls.size(); ++idx)
            {
                std::cout << ls[idx].first << "\t" << ls[idx].second << "\n";
            }
        }

        for(size_t idx = 1; idx < ls.size(); ++idx)
        {
            if(ls[idx].second < ls[idx - 1].first * ls[idx - 1].second)
            {
                happy_path = false;
                break;
            }
        }
        if(happy_path)
        {
            if(verbose > 2)
            {
                std::cout << "happy path\n";
            }
            return true;
        }
    }

    switch(l.size())
    {
    case 0:
        return true;
        break;
    case 1:
        return s[0] != 0;
        break;
    case 2:
    {
        return valid_length_stride_2d(l[0], l[1], s[0], s[1]);
        break;
    }
    case 3:
    {
        return valid_length_stride_3d(l, s, verbose);
        break;
    }
    case 4:
    {
        return valid_length_stride_4d(l, s, verbose);
        break;
    }
    default:
        return valid_length_stride_generald(l, s, verbose);
        return true;
    }

    return true;
}