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

Last change on this file since 438 was 436, checked in by alain, 6 years ago

1) improve the threads and process destruction mechanism.
2) introduce FIFOs in the soclib_tty driver.

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