| [6dc7011a] | 1 | //
 | 
|---|
 | 2 | // Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo
 | 
|---|
 | 3 | //
 | 
|---|
 | 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
 | 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
 | 6 | //
 | 
|---|
 | 7 | // multiSort.c -- compute sort single array multiple times
 | 
|---|
 | 8 | //                      make sure sorting works, thread don't screw up
 | 
|---|
 | 9 | //                      and trampolines work on thread stacks
 | 
|---|
 | 10 | //
 | 
|---|
 | 11 | // Author           : Peter A. Buhr
 | 
|---|
 | 12 | // Created On       : Tue 05 24 11:34:23 2022
 | 
|---|
 | 13 | // Last Modified By :
 | 
|---|
 | 14 | // Last Modified On :
 | 
|---|
 | 15 | // Update Count     :
 | 
|---|
 | 16 | //
 | 
|---|
 | 17 | 
 | 
|---|
 | 18 | #include <barrier.hfa>
 | 
|---|
 | 19 | #include <fstream.hfa>
 | 
|---|
 | 20 | #include <math.trait.hfa>
 | 
|---|
 | 21 | #include <stdlib.hfa>
 | 
|---|
 | 22 | #include <thread.hfa>
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | forall(T) {
 | 
|---|
 | 25 |         struct MyVec2 {
 | 
|---|
 | 26 |                 T val1;
 | 
|---|
 | 27 |                 T val2;
 | 
|---|
 | 28 |         };
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 |         forall(| Equality( T ))
 | 
|---|
 | 31 |         int ?!=?( MyVec2(T) lhs, MyVec2(T) rhs ) { return lhs.val1 != rhs.val1 || lhs.val2 != rhs.val2; }
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 |         forall(| Relational(T)) {
 | 
|---|
 | 34 |                 static inline int ?<?( MyVec2(T) lhs, MyVec2(T) rhs ) {
 | 
|---|
 | 35 |                         if(lhs.val1 < rhs.val1) return true;
 | 
|---|
 | 36 |                         if(lhs.val1 > rhs.val1) return false;
 | 
|---|
 | 37 |                         if(lhs.val2 < rhs.val2) return true;
 | 
|---|
 | 38 |                         if(lhs.val2 > rhs.val2) return false;
 | 
|---|
 | 39 |                         return false;
 | 
|---|
 | 40 |                 }
 | 
|---|
 | 41 |         }
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 |         forall(| { T random( void ); })
 | 
|---|
 | 44 |         MyVec2(T) random( void ) {
 | 
|---|
 | 45 |                 MyVec2(T) r;
 | 
|---|
 | 46 |                 r.val1 = random();
 | 
|---|
 | 47 |                 r.val2 = random();
 | 
|---|
 | 48 |                 return r;
 | 
|---|
 | 49 |         }
 | 
|---|
 | 50 | }
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | const unsigned nthreads = 7;
 | 
|---|
 | 55 | const unsigned nvecs = 313;
 | 
|---|
 | 56 | barrier bar = { nthreads + 1 };
 | 
|---|
 | 57 | const MyVec2(long int) * original;
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | thread Sorter {
 | 
|---|
 | 60 |         MyVec2(long int) * copy;
 | 
|---|
 | 61 | };
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 | void ^?{}( Sorter & mutex this ) {
 | 
|---|
 | 64 |         free(this.copy);
 | 
|---|
 | 65 | }
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 | // Make this a polymorphic call to prevent thunks from being hosted
 | 
|---|
 | 68 | forall( T | Relational(T) | sized(MyVec2(T)) )
 | 
|---|
 | 69 | void block_sort( MyVec2(T) * vals, size_t dim ) __attribute__((noinline)) {
 | 
|---|
 | 70 |         MyVec2(T) dummy = vals[0];
 | 
|---|
 | 71 |         block( bar );
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 |         qsort(vals, dim);
 | 
|---|
 | 74 | }
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 | void main( Sorter & this ) {
 | 
|---|
 | 78 |         this.copy = aalloc(nvecs);
 | 
|---|
 | 79 |         for(i; nvecs) {
 | 
|---|
 | 80 |                 this.copy[i] = original[i];
 | 
|---|
 | 81 |         }
 | 
|---|
 | 82 | 
 | 
|---|
 | 83 |         block_sort(this.copy, nvecs);
 | 
|---|
 | 84 | }
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 | int main() {
 | 
|---|
 | 87 |         sout | "Generating";
 | 
|---|
 | 88 |         MyVec2(long int) * local = aalloc( nvecs );
 | 
|---|
 | 89 |         for(i; nvecs) {
 | 
|---|
 | 90 |                 local[i] = random();
 | 
|---|
 | 91 |         }
 | 
|---|
 | 92 | 
 | 
|---|
 | 93 |         original = local;
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 |         sout | "Launching";
 | 
|---|
 | 96 | 
 | 
|---|
 | 97 |         processor p; {
 | 
|---|
 | 98 |                 Sorter sorters[nthreads];
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 |                 block( bar );
 | 
|---|
 | 101 | 
 | 
|---|
 | 102 |                 sout | "Sorting";
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 |                 qsort(local, nvecs);
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 |                 sout | "Checking";
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 |                 for(i; nthreads) {
 | 
|---|
 | 109 |                         const MyVec2(long int) * copy = join( sorters[i] ).copy;
 | 
|---|
 | 110 |                         for(j; nvecs) {
 | 
|---|
 | 111 |                                 if(copy[j] != original[j]) {
 | 
|---|
 | 112 |                                         sout | "Error at thread" | i | ", index" | j | ": data doesn't match!";
 | 
|---|
 | 113 |                                 }
 | 
|---|
 | 114 |                         }
 | 
|---|
 | 115 |                 }
 | 
|---|
 | 116 |         }
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 |         free(local);
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 |         sout | "Done";
 | 
|---|
 | 121 | }
 | 
|---|