Changeset 669 for soft/giet_vm/applications/sort/main.c
- Timestamp:
- Jul 27, 2015, 8:40:45 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/applications/sort/main.c
r515 r669 3 3 // Date : November 2013 4 4 // Author : Cesar Fuguet Tortolero <cesar.fuguet-tortolero@lip6.fr> 5 /////////////////////////////////////////////////////////////////////////////// 6 // This multi-threaded application implement a multi-stage sort application. 7 // The various stages are separated by synchronisation barriers. 8 // There is one thread per physical processors. Computation is organised as 9 // a binary tree: All threads contribute to the first stage of parallel sort 10 // but, the number of participating threads is divided by 2 at each next stage. 11 // Number_of_stages = number of barriers = log2(Number_of_threads) 5 12 // 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 // Constraints : 12 // 13 // - It supports up to 1024 processors and the number of processors 14 // must be a power of 2. 15 // 16 // _ The array of values to be sorted (ARRAY_LENGTH) must be power of 2 17 // larger than the number of processors. 18 // 19 // - This application must be executed on a cache coherent architecture. 20 // 13 // Constraints : 14 // - It supports up to 1024 processors and the number of processors 15 // must be a power of 2. 16 // _ The array of values to be sorted (ARRAY_LENGTH) must be power of 2 17 // larger than the number of processors. 18 // - This application uses a private TTY terminal, shared by all threads, 19 // that is protectted by an user-level SQT lock. 21 20 /////////////////////////////////////////////////////////////////////////////// 22 21 23 22 #include "stdio.h" 24 23 #include "mapping_info.h" 25 #include "hard_config.h"26 24 #include "user_barrier.h" 25 #include "user_lock.h" 27 26 28 27 #define ARRAY_LENGTH 0x1000 29 28 #define IPT (ARRAY_LENGTH / threads) // ITEMS PER THREAD 30 29 31 //////////////////////////////////////////////////////////////////////////////// 32 // Processors other than 0 display algorithm state if VERBOSE non zero 33 34 #define VERBOSE 1 35 36 //////////////////////////////////////////////////////////////////////////////// 37 // Define printf according to verbosity option and number of available TTY 38 39 #if (VERBOSE == 1) 40 # define printf(...) giet_shr_printf(__VA_ARGS__) 41 #else 42 # define printf(...) 43 #endif 44 45 #define task0_printf(...) if(thread_id == 0) giet_shr_printf(__VA_ARGS__) 46 47 int array0[ARRAY_LENGTH]; 48 int array1[ARRAY_LENGTH]; 49 50 volatile int init_ok = 0; 30 // macro to use a shared TTY 31 #define printf(...) lock_acquire( &tty_lock ); \ 32 giet_tty_printf(__VA_ARGS__); \ 33 lock_release( &tty_lock ) 34 35 int array0[ARRAY_LENGTH]; 36 int array1[ARRAY_LENGTH]; 37 38 volatile int init_ok = 0; 39 giet_barrier_t barrier[10]; 40 user_lock_t tty_lock; 51 41 52 42 void bubbleSort( … … 63 53 int init_pos_result); 64 54 65 ///////////////////////////////////////////////////////66 // This application supports at most 1024 processors67 // Number of barriers = log2(threads)68 69 giet_barrier_t barrier[10];70 71 55 ////////////////////////////////////////// 72 __attribute__ ((constructor)) void sort() 56 __attribute__ ((constructor)) void main() 57 ////////////////////////////////////////// 73 58 { 74 int thread_id = giet_thread_id();75 59 int * src_array = NULL; 76 60 int * dst_array = NULL; 77 61 int i; 78 79 unsigned int time_start = giet_proctime();80 unsigned int time_end;81 82 // compute number of threads (one thread per proc)83 62 unsigned int x_size; 84 63 unsigned int y_size; … … 86 65 unsigned int threads; 87 66 67 // each thread gets its thread_id 68 int thread_id = giet_thread_id(); 69 70 unsigned int time_start = giet_proctime(); 71 unsigned int time_end; 72 73 // each thread compute number of threads (one thread per proc) 88 74 giet_procs_number( &x_size , &y_size , &nprocs ); 89 75 threads = x_size * y_size * nprocs; 90 76 77 // thread 0 makes TTY and barrier initialisations 78 // other threads wait initialisation completion. 79 if ( thread_id == 0 ) 80 { 81 // request a shared TTY used by all threads 82 giet_tty_alloc(1); 83 84 // TTY lock initialisation 85 lock_init( &tty_lock ); 86 87 printf("\n[ SORT T0 ] Starting sort application with %d threads " 88 "at cycle %d\n", threads, time_start); 89 90 // Barriers Initialization 91 for (i = 0; i < __builtin_ctz( threads ); i++) 92 { 93 barrier_init( &barrier[i], threads >> i ); 94 } 95 96 init_ok = 1; 97 } 98 else 99 { 100 while( !init_ok ); 101 } 102 103 // each thread checks number of tasks 91 104 if ( (threads != 1) && (threads != 2) && (threads != 4) && 92 105 (threads != 8) && (threads != 16 ) && (threads != 32) && … … 94 107 (threads != 512) && (threads != 1024) ) 95 108 { 96 task0_printf("[SORT ERROR] Number of processors must be power of 2\n" 97 " x_size = %d / y_size = %d / nprocs = %d\n", 98 x_size , y_size , nprocs ); 99 giet_exit("error"); 100 } 101 102 task0_printf("\n[ Thread 0 ] Starting sort application with %d threads " 103 "at cycle %d\n", threads, time_start); 104 105 /////////////////////////// 106 // Barriers Initialization 107 108 if (thread_id == 0) 109 { 110 for (i = 0; i < __builtin_ctz( threads ); i++) 111 { 112 barrier_init(&barrier[i], threads >> i); 113 } 114 115 init_ok = 1; 116 } 117 else 118 { 119 while(!init_ok); 120 } 121 122 //////////////////////// 123 // Array Initialization 124 109 giet_exit("error : number of processors must be power of 2"); 110 } 111 112 113 // Each thread contribute to Array Initialization 125 114 for (i = IPT * thread_id; i < IPT * (thread_id + 1); i++) 126 115 { … … 128 117 } 129 118 130 /////////////////////////////////// 131 // Parallel sort of array elements 132 133 printf("[ Thread %d ] Stage 0: Sorting...\n\r", thread_id); 119 // all threads contribute to the first stage of parallel sort 120 printf("[ SORT T%d ] Stage 0: Sorting...\n\r", thread_id); 134 121 135 122 bubbleSort(array0, IPT, IPT * thread_id); 136 123 137 printf("[ Thread %d ] Finishing Stage 0\n\r", thread_id); 138 124 printf("[ SORT T%d ] Finishing Stage 0\n\r", thread_id); 125 126 // the number of threads is divided by 2 at each next stage 139 127 for (i = 0; i < __builtin_ctz( threads ); i++) 140 128 { 141 barrier_wait( &barrier[i]);129 barrier_wait( &barrier[i] ); 142 130 143 131 if((thread_id % (2 << i)) != 0) 144 132 { 145 printf("[ Thread%d ] Quit\n\r", thread_id );133 printf("[ SORT T%d ] Quit\n\r", thread_id ); 146 134 giet_exit("Completed"); 147 135 } 148 136 149 printf("[ Thread%d ] Stage %d: Sorting...\n\r", thread_id, i+1);137 printf("[ SORT T%d ] Stage %d: Sorting...\n\r", thread_id, i+1); 150 138 151 139 if((i % 2) == 0) … … 167 155 ); 168 156 169 printf("[ Thread%d ] Finishing Stage %d\n\r", thread_id, i + 1);157 printf("[ SORT T%d ] Finishing Stage %d\n\r", thread_id, i + 1); 170 158 } 171 159 … … 173 161 int failure_index; 174 162 175 //////////////////////////////176 163 // Verify the resulting array 177 178 164 if(thread_id != 0) 179 165 { … … 186 172 if(dst_array[i] > dst_array[i+1]) 187 173 { 188 189 174 success = 0; 190 175 failure_index = i; … … 195 180 time_end = giet_proctime(); 196 181 197 printf("[ Thread0 ] Finishing sort application at cycle %d\n"198 "[ Thread0 ] Time elapsed = %d\n",182 printf("[ SORT T0 ] Finishing sort application at cycle %d\n" 183 "[ SORT T0 ] Time elapsed = %d\n", 199 184 time_end, (time_end - time_start) ); 200 185 … … 205 190 else 206 191 { 207 printf("[ Thread0 ] Failure!! Incorrect element: %d\n\r",192 printf("[ SORT T0 ] Failure!! Incorrect element: %d\n\r", 208 193 failure_index); 209 194 for(i=0; i<ARRAY_LENGTH; i++)
Note: See TracChangeset
for help on using the changeset viewer.