[257] | 1 | /////////////////////////////////////////////////////////////////////////////// |
---|
[295] | 2 | // File : main.c |
---|
| 3 | // Date : November 2013 |
---|
| 4 | // Author : Cesar Fuguet Tortolero <cesar.fuguet-tortolero@lip6.fr> |
---|
[256] | 5 | // |
---|
[257] | 6 | // Description : |
---|
| 7 | // |
---|
| 8 | // Sort application using the GIET-VM OS. This application uses the |
---|
| 9 | // barrier routines to apply a sort algorithm in several stages. |
---|
| 10 | // |
---|
| 11 | // Considerations : |
---|
| 12 | // |
---|
| 13 | // - It supports up to 256 processors and the number of processors |
---|
| 14 | // must be a power of 2. |
---|
| 15 | // |
---|
| 16 | // - If there is only one TTY available, this application uses a spin |
---|
| 17 | // lock to avoid several threads writting at the same time. |
---|
| 18 | // |
---|
| 19 | // - This application must be executed on a cache coherent |
---|
| 20 | // architecture. Otherwise some modifications must be applied |
---|
| 21 | // |
---|
| 22 | // - The processors executing this application must have a contiguous |
---|
| 23 | // processor id and the first processor must have id 0. |
---|
| 24 | // |
---|
| 25 | /////////////////////////////////////////////////////////////////////////////// |
---|
[256] | 26 | |
---|
| 27 | #include "stdio.h" |
---|
[292] | 28 | #include "mapping_info.h" |
---|
[256] | 29 | #include "hard_config.h" |
---|
| 30 | #include "barrier.h" |
---|
| 31 | |
---|
[295] | 32 | #define ARRAY_LENGTH 512 |
---|
[292] | 33 | #define IPT (ARRAY_LENGTH / *nb_thread) // ITEMS PER THREAD |
---|
[257] | 34 | |
---|
| 35 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 36 | // Processors other than 0 display algorithm state |
---|
| 37 | // The processor 0 always displays some information so this does not affect him |
---|
| 38 | |
---|
[256] | 39 | #define VERBOSE 1 |
---|
| 40 | |
---|
[295] | 41 | //////////////////////////////////////////////////////////////////////////////// |
---|
[257] | 42 | // Define printf according to verbosity option and number of available |
---|
| 43 | // TTY |
---|
| 44 | |
---|
[256] | 45 | #if (VERBOSE == 1) |
---|
[295] | 46 | # define printf(...) giet_shr_printf(__VA_ARGS__) |
---|
[318] | 47 | #else |
---|
[257] | 48 | # define printf(...) |
---|
[256] | 49 | #endif |
---|
| 50 | |
---|
[295] | 51 | #define task0_printf(...) if(thread_id == 0) giet_shr_printf(__VA_ARGS__) |
---|
[257] | 52 | |
---|
[256] | 53 | #define exit giet_exit |
---|
| 54 | #define procid giet_procid |
---|
| 55 | #define rand giet_rand |
---|
| 56 | |
---|
| 57 | int array0[ARRAY_LENGTH]; |
---|
| 58 | int array1[ARRAY_LENGTH]; |
---|
| 59 | |
---|
[345] | 60 | volatile int init_ok = 0; |
---|
[256] | 61 | |
---|
| 62 | void bubbleSort( |
---|
| 63 | int * array, |
---|
| 64 | unsigned int length, |
---|
| 65 | unsigned int init_pos); |
---|
| 66 | |
---|
| 67 | void merge( |
---|
| 68 | int * array, |
---|
| 69 | int * result, |
---|
| 70 | int length, |
---|
| 71 | int init_pos_a, |
---|
| 72 | int init_pos_b, |
---|
| 73 | int init_pos_result); |
---|
| 74 | |
---|
[257] | 75 | /////////////////////////////////////////////////// |
---|
| 76 | // This application support at most 256 processors |
---|
[292] | 77 | // Number of barriers = log2(nb_thread) |
---|
[257] | 78 | |
---|
[256] | 79 | giet_barrier_t barrier[8]; |
---|
| 80 | |
---|
[295] | 81 | ////////////////////////////////////////// |
---|
[256] | 82 | __attribute__ ((constructor)) void sort() |
---|
| 83 | { |
---|
[269] | 84 | int thread_id = giet_thread_id(); |
---|
[292] | 85 | unsigned int* nb_thread; |
---|
[381] | 86 | int * src_array = NULL; |
---|
| 87 | int * dst_array = NULL; |
---|
[256] | 88 | int i; |
---|
[292] | 89 | |
---|
| 90 | unsigned int time_start = giet_proctime(); |
---|
| 91 | unsigned int time_end; |
---|
[256] | 92 | |
---|
[295] | 93 | giet_vobj_get_vbase( "sort" , |
---|
[318] | 94 | "sort_args", |
---|
[295] | 95 | (unsigned int*)&nb_thread ); |
---|
[292] | 96 | |
---|
[345] | 97 | task0_printf("\n[ Thread 0 ] Starting sort application with %u threads " |
---|
| 98 | "at cycle %u\n", *nb_thread, time_start); |
---|
[256] | 99 | |
---|
[269] | 100 | /////////////////////////// |
---|
| 101 | // Barriers Initialization |
---|
[256] | 102 | |
---|
[292] | 103 | if (thread_id == 0) |
---|
[256] | 104 | { |
---|
[292] | 105 | for (i = 0; i < __builtin_ctz(*nb_thread); i++) |
---|
| 106 | { |
---|
| 107 | barrier_init(&barrier[i], *nb_thread >> i); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | init_ok = 1; |
---|
[256] | 111 | } |
---|
[292] | 112 | else |
---|
| 113 | { |
---|
| 114 | while(!init_ok); |
---|
| 115 | } |
---|
[256] | 116 | |
---|
[269] | 117 | //////////////////////// |
---|
| 118 | // Array Initialization |
---|
[256] | 119 | |
---|
[271] | 120 | for (i = IPT * thread_id; i < IPT * (thread_id + 1); i++) |
---|
[256] | 121 | { |
---|
[269] | 122 | array0[i] = rand(); |
---|
[256] | 123 | } |
---|
| 124 | |
---|
[257] | 125 | /////////////////////////////////// |
---|
| 126 | // Parallel sort of array elements |
---|
[256] | 127 | |
---|
[295] | 128 | printf("[ Thread %d ] Stage 0: Sorting...\n\r", thread_id); |
---|
[257] | 129 | |
---|
[271] | 130 | bubbleSort(array0, IPT, IPT * thread_id); |
---|
[256] | 131 | |
---|
[269] | 132 | printf("[ Thread %d ] Finishing Stage 0\n\r", thread_id); |
---|
[257] | 133 | |
---|
[292] | 134 | for (i = 0; i < __builtin_ctz(*nb_thread); i++) |
---|
[256] | 135 | { |
---|
| 136 | barrier_wait(&barrier[i]); |
---|
| 137 | |
---|
[269] | 138 | if((thread_id % (2 << i)) != 0) |
---|
| 139 | { |
---|
[295] | 140 | printf("[ Thread %d ] Quit\n\r", thread_id ); |
---|
| 141 | exit("Completed"); |
---|
[269] | 142 | } |
---|
[257] | 143 | |
---|
[295] | 144 | printf("[ Thread %d ] Stage %d: Sorting...\n\r", thread_id, i+1); |
---|
[256] | 145 | |
---|
| 146 | if((i % 2) == 0) |
---|
| 147 | { |
---|
| 148 | src_array = &array0[0]; |
---|
| 149 | dst_array = &array1[0]; |
---|
| 150 | } |
---|
| 151 | else |
---|
| 152 | { |
---|
| 153 | src_array = &array1[0]; |
---|
| 154 | dst_array = &array0[0]; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | merge(src_array, dst_array |
---|
[271] | 158 | , IPT << i |
---|
| 159 | , IPT * thread_id |
---|
| 160 | , IPT * (thread_id + (1 << i)) |
---|
| 161 | , IPT * thread_id |
---|
[256] | 162 | ); |
---|
| 163 | |
---|
[269] | 164 | printf("[ Thread %d ] Finishing Stage %d\n\r", thread_id, i + 1); |
---|
[256] | 165 | } |
---|
| 166 | |
---|
| 167 | int success; |
---|
| 168 | int failure_index; |
---|
| 169 | |
---|
[257] | 170 | ////////////////////////////// |
---|
| 171 | // Verify the resulting array |
---|
[345] | 172 | |
---|
| 173 | if(thread_id != 0) |
---|
| 174 | { |
---|
| 175 | exit("error: only thread 0 should get here"); |
---|
| 176 | } |
---|
[257] | 177 | |
---|
[345] | 178 | success = 1; |
---|
| 179 | for(i=0; i<(ARRAY_LENGTH-1); i++) |
---|
[256] | 180 | { |
---|
[345] | 181 | if(dst_array[i] > dst_array[i+1]) |
---|
[256] | 182 | { |
---|
| 183 | |
---|
[345] | 184 | success = 0; |
---|
| 185 | failure_index = i; |
---|
| 186 | break; |
---|
[256] | 187 | } |
---|
[345] | 188 | } |
---|
[256] | 189 | |
---|
[345] | 190 | time_end = giet_proctime(); |
---|
[295] | 191 | |
---|
[345] | 192 | printf("[ Thread 0 ] Finishing sort application at cycle %d\n" |
---|
| 193 | "[ Thread 0 ] Time elapsed = %d\n", |
---|
| 194 | time_end, (time_end - time_start) ); |
---|
[292] | 195 | |
---|
[345] | 196 | if (success) |
---|
| 197 | { |
---|
| 198 | exit("!!! Success !!!"); |
---|
| 199 | } |
---|
| 200 | else |
---|
| 201 | { |
---|
| 202 | printf("[ Thread 0 ] Failure!! Incorrect element: %d\n\r", |
---|
| 203 | failure_index); |
---|
| 204 | for(i=0; i<ARRAY_LENGTH; i++) |
---|
[256] | 205 | { |
---|
[345] | 206 | printf("array[%d] = %d\n", i, dst_array[i]); |
---|
[256] | 207 | } |
---|
[345] | 208 | exit("!!! Failure !!!"); |
---|
[256] | 209 | } |
---|
[345] | 210 | |
---|
[295] | 211 | exit("Completed"); |
---|
[256] | 212 | } |
---|
| 213 | |
---|
[295] | 214 | //////////////////////////////////// |
---|
| 215 | void bubbleSort( int * array, |
---|
| 216 | unsigned int length, |
---|
| 217 | unsigned int init_pos ) |
---|
[256] | 218 | { |
---|
| 219 | int i; |
---|
| 220 | int j; |
---|
| 221 | int aux; |
---|
| 222 | |
---|
| 223 | for(i = 0; i < length; i++) |
---|
| 224 | { |
---|
| 225 | for(j = init_pos; j < (init_pos + length - i - 1); j++) |
---|
| 226 | { |
---|
| 227 | if(array[j] > array[j + 1]) |
---|
| 228 | { |
---|
| 229 | aux = array[j + 1]; |
---|
| 230 | array[j + 1] = array[j]; |
---|
| 231 | array[j] = aux; |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
[295] | 237 | ///////////// |
---|
[256] | 238 | void merge( |
---|
| 239 | int * array, |
---|
| 240 | int * result, |
---|
| 241 | int length, |
---|
| 242 | int init_pos_a, |
---|
| 243 | int init_pos_b, |
---|
| 244 | int init_pos_result) |
---|
| 245 | { |
---|
| 246 | int i; |
---|
| 247 | int j; |
---|
| 248 | int k; |
---|
| 249 | |
---|
| 250 | i = 0; |
---|
| 251 | j = 0; |
---|
| 252 | k = init_pos_result; |
---|
| 253 | |
---|
| 254 | while((i < length) || (j < length)) |
---|
| 255 | { |
---|
| 256 | if((i < length) && (j < length)) |
---|
| 257 | { |
---|
| 258 | if(array[init_pos_a + i] < array[init_pos_b + j]) |
---|
| 259 | { |
---|
| 260 | result[k++] = array[init_pos_a + i]; |
---|
| 261 | i++; |
---|
| 262 | } |
---|
| 263 | else |
---|
| 264 | { |
---|
| 265 | result[k++] = array[init_pos_b + j]; |
---|
| 266 | j++; |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | else if(i < length) |
---|
| 270 | { |
---|
| 271 | result[k++] = array[init_pos_a + i]; |
---|
| 272 | i++; |
---|
| 273 | } |
---|
| 274 | else |
---|
| 275 | { |
---|
| 276 | result[k++] = array[init_pos_b + j]; |
---|
| 277 | j++; |
---|
| 278 | } |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | /* vim: tabstop=4 : shiftwidth=4 : expandtab |
---|
| 283 | */ |
---|