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