source: trunk/user/sort/sort.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

  • Property svn:executable set to *
File size: 12.1 KB
Line 
1///////////////////////////////////////////////////////////////////////////////
2// File   :  sort.c
3// Date   :  November 2013
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 cores.
9// Computation is organised as a binary tree:
10// - All threads execute in parallel a buble sort on a sub-array during the
11//   the first stage of parallel sort,
12// - The number of participating threads is divided by 2 at each next stage,
13//   to make a merge sort, on two subsets of previous stage.
14//
15//       Number_of_stages = number of barriers = log2(Number_of_threads)
16//
17// Constraints :
18// - It supports up to 1024 cores: x_size, y_size, and ncores must be
19//   power of 2 (max 16*16 clusters / max 4 cores per cluster)
20// _ The array of values to be sorted (ARRAY_LENGTH) must be power of 2
21//   larger than the number of cores.
22///////////////////////////////////////////////////////////////////////////////
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <pthread.h>
27#include <almos-mkh.h>
28
29#define ARRAY_LENGTH        0x400    // 1024 values
30
31#define MAX_THREADS         1024     // 16 * 16 * 4
32
33#define DISPLAY_ARRAY       0
34#define INTERACTIVE_MODE    1
35
36///////////////////////////////////////////////////////
37// macros for fixed format cxy <=> (x,y) translation
38// TODO these macros are only for TSAR architecture...
39///////////////////////////////////////////////////////
40
41#define CXY_FROM_XY( x , y )  ((x<<4) + y)
42
43#define X_FROM_CXY( cxy )     ((cxy>>4) & 0xF)
44
45#define Y_FROM_CXY( cxy )     (cxy & 0xF)
46
47/////////////////////////////////////////////////////////////
48// argument for the sort() function (one thread per core)
49/////////////////////////////////////////////////////////////
50
51typedef struct
52{
53    unsigned int threads;      // total number of threads
54    unsigned int thread_uid;    // thread user index (0 to threads -1)
55    unsigned int main_uid;      // main thread user index
56}
57args_t;
58
59//////////////////////////////////////////
60//      Global variables
61//////////////////////////////////////////
62
63int                 array0[ARRAY_LENGTH];    // values to sort
64int                 array1[ARRAY_LENGTH];   
65
66pthread_barrier_t   barrier;                 // synchronisation variables
67
68pthread_attr_t      attr[MAX_THREADS];       // thread attributes (one per thread)
69args_t              arg[MAX_THREADS];        // sort function arguments (one per thread)
70
71////////////////////////////////////
72void bubbleSort( int *        array,
73                 unsigned int length,
74                 unsigned int init_pos )
75{
76    int i;
77    int j;
78    int aux;
79
80    for(i = 0; i < length; i++)
81    {
82        for(j = init_pos; j < (init_pos + length - i - 1); j++)
83        {
84            if(array[j] > array[j + 1])
85            {
86                aux          = array[j + 1];
87                array[j + 1] = array[j];
88                array[j]     = aux;
89            }
90        }
91    }
92}  // end bubbleSort()
93
94
95/////////////////////////
96void merge( int * src,
97            int * dst,
98            int length,
99            int init_pos_src_a,
100            int init_pos_src_b,
101            int init_pos_dst )
102{
103    int i;
104    int j;
105    int k;
106
107    i = 0;
108    j = 0;
109    k = init_pos_dst;
110
111    while((i < length) || (j < length))
112    {
113        if((i < length) && (j < length))
114        {
115            if(src[init_pos_src_a + i] < src[init_pos_src_b + j])
116            {
117                dst[k++] = src[init_pos_src_a + i];
118                i++;
119            }
120            else
121            {
122                dst[k++] = src[init_pos_src_b + j];
123                j++;
124            }
125        }
126        else if(i < length)
127        {
128            dst[k++] = src[init_pos_src_a + i];
129            i++;
130        }
131        else
132        {
133            dst[k++] = src[init_pos_src_b + j];
134            j++;
135        }
136    }
137}  // end merge()
138
139/////////////////////////
140void sort( args_t * ptr )
141{
142    unsigned int       i;
143    unsigned long long cycle;
144    unsigned int       cxy;
145    unsigned int       lid;
146
147    int         * src_array  = NULL;
148    int         * dst_array  = NULL;
149
150    // get core coordinates an date
151    get_core( &cxy , &lid );
152    get_cycle( &cycle );
153
154    unsigned int  thread_uid = ptr->thread_uid;
155    unsigned int  threads    = ptr->threads;
156    unsigned int  main_uid   = ptr->main_uid;
157
158    unsigned int  items      = ARRAY_LENGTH / threads;
159    unsigned int  stages     = __builtin_ctz( threads ) + 1;
160
161    printf("\n[SORT] thread[%d] : start\n", thread_uid );
162
163    bubbleSort( array0, items, items * thread_uid );
164
165    printf("\n[SORT] thread[%d] / stage 0 completed\n", thread_uid );
166
167    /////////////////////////////////
168    pthread_barrier_wait( &barrier ); 
169    printf("\n[SORT] thread[%d] exit barrier\n", thread_uid );
170
171    // the number of threads contributing to sort
172    // is divided by 2 at each next stage
173    for ( i = 1 ; i < stages ; i++ )
174    {
175        pthread_barrier_wait( &barrier );
176
177        if( (thread_uid & ((1<<i)-1)) == 0 )
178        {
179            printf("\n[SORT] thread[%d] / stage %d start\n", thread_uid , i );
180
181            if((i % 2) == 1)               // odd stage
182            {
183                src_array = array0;
184                dst_array = array1;
185            }
186            else                           // even stage
187            {
188                src_array = array1;
189                dst_array = array0;
190            }
191
192            merge( src_array, 
193                   dst_array,
194                   items << i,
195                   items * thread_uid,
196                   items * (thread_uid + (1 << (i-1))),
197                   items * thread_uid );
198
199            printf("\n[SORT] thread[%d] / stage %d completed\n", thread_uid , i );
200        }
201
202        /////////////////////////////////
203        pthread_barrier_wait( &barrier );
204        printf("\n[SORT] thread[%d] exit barrier\n", thread_uid );
205
206    }
207
208    // all threads but the main thread exit
209    if( thread_uid != main_uid ) pthread_exit( NULL );
210
211} // end sort()
212
213
214///////////
215void main()
216{
217    unsigned int           x_size;             // number of rows
218    unsigned int           y_size;             // number of columns
219    unsigned int           ncores;             // number of cores per cluster
220    unsigned int           threads;            // total number of threads
221    unsigned int           thread_uid;         // user defined thread index
222    unsigned int           main_cxy;           // cluster identifier for main
223    unsigned int           main_x;             // X coordinate for main thread
224    unsigned int           main_y;             // Y coordinate for main thread
225    unsigned int           main_lid;           // core local index for main thread
226    unsigned int           main_uid;           // thread user index for main thread
227    unsigned int           x;                  // X coordinate for a thread
228    unsigned int           y;                  // Y coordinate for a thread
229    unsigned int           lid;                // core local index for a thread
230    unsigned int           n;                  // index in array to sort
231    unsigned long long     cycle;              // current date for log
232    pthread_t              trdid;              // kernel allocated thread index (unused)
233    pthread_barrierattr_t  barrier_attr;       // barrier attributes
234
235    // compute number of threads (one thread per proc)
236    get_config( &x_size , &y_size , &ncores );
237    threads = x_size * y_size * ncores;
238
239    // get core coordinates and user index for the main thread
240    get_core( &main_cxy , & main_lid );
241    main_x   = X_FROM_CXY( main_cxy );
242    main_y   = Y_FROM_CXY( main_cxy );
243    main_uid = (((main_x * y_size) + main_y) * ncores) + main_lid; 
244
245    // checks number of threads
246    if ( (threads != 1)   && (threads != 2)   && (threads != 4)   && 
247         (threads != 8)   && (threads != 16 ) && (threads != 32)  && 
248         (threads != 64)  && (threads != 128) && (threads != 256) && 
249         (threads != 512) && (threads != 1024) )
250    {
251        printf("\n[SORT ERROR] number of cores must be power of 2\n");
252        exit( 0 );
253    }
254
255    // check array size
256    if ( ARRAY_LENGTH % threads) 
257    {
258        printf("\n[SORT ERROR] array size must be multiple of number of threads\n");
259        exit( 0 );
260    }
261
262    get_cycle( &cycle );
263    printf("\n\n[SORT] main starts on core[%x,%d] / %d threads / %d values / cycle %d\n",
264    main_cxy, main_lid, threads, ARRAY_LENGTH, (unsigned int)cycle );
265
266    // Barrier initialization
267    barrier_attr.x_size   = x_size; 
268    barrier_attr.y_size   = y_size;
269    barrier_attr.nthreads = ncores;
270    if( pthread_barrier_init( &barrier, &barrier_attr , threads ) )
271    {
272        printf("\n[SORT ERROR] cannot initialise barrier\n" );
273        exit( 0 );
274    }
275
276    get_cycle( &cycle );
277    printf("\n[SORT] main completes barrier init at cycle %d\n", (unsigned int)cycle );
278
279    // Array to sort initialization
280    for ( n = 0 ; n < ARRAY_LENGTH ; n++ )
281    {
282        array0[n] = rand();
283    }
284
285#if DISPLAY_ARRAY
286printf("\n*** array before sort\n");
287for( n=0; n<ARRAY_LENGTH; n++) printf("array[%d] = %d\n", n , array0[n] );
288#endif
289
290    get_cycle( &cycle );
291    printf("\n[SORT] main completes array init at cycle %d\n", (unsigned int)cycle );
292
293    // launch other threads to execute sort() function
294    // on cores other than the core running the main thread
295    for ( x=0 ; x<x_size ; x++ )
296    {
297        for ( y=0 ; y<y_size ; y++ )
298        {
299            for ( lid=0 ; lid<ncores ; lid++ )
300            {
301                thread_uid = (((x * y_size) + y) * ncores) + lid;
302
303                // set sort arguments for all threads
304                arg[thread_uid].threads      = threads;
305                arg[thread_uid].thread_uid   = thread_uid;
306                arg[thread_uid].main_uid     = main_uid;
307
308                // set thread attributes for all threads
309                attr[thread_uid].attributes = PT_ATTR_CLUSTER_DEFINED | PT_ATTR_CORE_DEFINED;
310                attr[thread_uid].cxy        = CXY_FROM_XY( x , y );
311                attr[thread_uid].lid        = lid;
312
313                if( thread_uid != main_uid )
314                {
315                    if ( pthread_create( &trdid,              // not used because no join
316                                         &attr[thread_uid],   // thread attributes
317                                         &sort,               // entry function
318                                         &arg[thread_uid] ) ) // sort arguments
319                    {
320                        printf("\n[SORT ERROR] main cannot create thread %x \n", thread_uid );
321                        exit( 0 );
322                    }
323                    else
324                    {
325                        printf("\n[SORT] main created thread %x \n", thread_uid );
326                    }
327                }
328
329#if INTERACTIVE_MODE
330idbg();
331#endif
332            }
333        }
334    }
335   
336    get_cycle( &cycle );
337    printf("\n[SORT] main completes threads create at cycle %d\n", (unsigned int)cycle );
338
339    // main run also the sort() function
340    sort( &arg[main_uid] );
341
342#if INTERACTIVE_MODE
343idbg();
344#endif
345   
346    // Check result
347    int    success = 1;
348    int*   res_array = ( (threads==  2) ||
349                         (threads==  8) || 
350                         (threads== 32) || 
351                         (threads==128) || 
352                         (threads==512) ) ? array1 : array0;
353   
354    for( n=0 ; n<(ARRAY_LENGTH-2) ; n++ )
355    {
356        if ( res_array[n] > res_array[n+1] )
357        {
358            printf("\n[SORT] array[%d] = %d > array[%d] = %d\n",
359            n , res_array[n] , n+1 , res_array[n+1] );
360            success = 0;
361            break;
362        }
363    }
364
365#if DISPLAY_ARRAY
366printf("\n*** array after sort\n");
367for( n=0; n<ARRAY_LENGTH; n++) printf("array[%d] = %d\n", n , res_array[n] );
368#endif
369
370    get_cycle( &cycle );
371
372    if ( success )
373    {
374        printf("\n[SORT] success at cycle %d\n", (unsigned int)cycle );
375        exit( 0 );
376    }
377    else
378    {
379        printf("\n[SORT] failure at cycle %d\n", (unsigned int)cycle );
380        exit( 1 );
381    }
382
383}  // end main()
384
385
386/*
387vim: tabstop=4 : shiftwidth=4 : expandtab
388*/
Note: See TracBrowser for help on using the repository browser.