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

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

Fix a bug in rpc_vmm_get_pte_client() function (bad RPC index).

  • Property svn:executable set to *
File size: 11.9 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
31#define MAX_THREADS         1024     // 16 * 16 * 4
32
33#define DISPLAY_ARRAY       0
34#define DISPLAY_THREADS     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    bubbleSort( array0, items, items * thread_uid );
162
163    printf("\n[SORT] thread[%d] / stage 0 completed\n", thread_uid );
164
165    /////////////////////////////////
166    pthread_barrier_wait( &barrier ); 
167
168    printf("\n[SORT] thread[%d] exit barrier\n", thread_uid );
169
170    // the number of threads contributing to sort
171    // is divided by 2 at each next stage
172    for ( i = 1 ; i < stages ; i++ )
173    {
174        pthread_barrier_wait( &barrier );
175
176        if( (thread_uid & ((1<<i)-1)) == 0 )
177        {
178            printf("\n[SORT] thread[%d] / stage %d start\n", thread_uid , i );
179
180            if((i % 2) == 1)               // odd stage
181            {
182                src_array = array0;
183                dst_array = array1;
184            }
185            else                           // even stage
186            {
187                src_array = array1;
188                dst_array = array0;
189            }
190
191            merge( src_array, 
192                   dst_array,
193                   items << i,
194                   items * thread_uid,
195                   items * (thread_uid + (1 << (i-1))),
196                   items * thread_uid );
197
198            printf("\n[SORT] thread[%d] / stage %d completed\n", thread_uid , i );
199        }
200
201        /////////////////////////////////
202        pthread_barrier_wait( &barrier );
203
204    }
205
206    // all threads but the main thread exit
207    if( thread_uid != main_uid ) pthread_exit( NULL );
208
209} // end sort()
210
211
212///////////
213void main()
214{
215    unsigned int           x_size;             // number of rows
216    unsigned int           y_size;             // number of columns
217    unsigned int           ncores;             // number of cores per cluster
218    unsigned int           threads;            // total number of threads
219    unsigned int           thread_uid;         // user defined thread index
220    unsigned int           main_cxy;           // cluster identifier for main
221    unsigned int           main_x;             // X coordinate for main thread
222    unsigned int           main_y;             // Y coordinate for main thread
223    unsigned int           main_lid;           // core local index for main thread
224    unsigned int           main_uid;           // thread user index for main thread
225    unsigned int           x;                  // X coordinate for a thread
226    unsigned int           y;                  // Y coordinate for a thread
227    unsigned int           lid;                // core local index for a thread
228    unsigned int           n;                  // index in array to sort
229    unsigned long long     cycle;              // current date for log
230    pthread_t              trdid;              // kernel allocated thread index (unused)
231    pthread_barrierattr_t  barrier_attr;       // barrier attributes
232
233    // compute number of threads (one thread per proc)
234    get_config( &x_size , &y_size , &ncores );
235    threads = x_size * y_size * ncores;
236
237    // get core coordinates and user index for the main thread
238    get_core( &main_cxy , & main_lid );
239    main_x   = X_FROM_CXY( main_cxy );
240    main_y   = Y_FROM_CXY( main_cxy );
241    main_uid = (((main_x * y_size) + main_y) * ncores) + main_lid; 
242
243    // checks number of threads
244    if ( (threads != 1)   && (threads != 2)   && (threads != 4)   && 
245         (threads != 8)   && (threads != 16 ) && (threads != 32)  && 
246         (threads != 64)  && (threads != 128) && (threads != 256) && 
247         (threads != 512) && (threads != 1024) )
248    {
249        printf("\n[SORT ERROR] number of cores must be power of 2\n");
250        exit( 0 );
251    }
252
253    // check array size
254    if ( ARRAY_LENGTH % threads) 
255    {
256        printf("\n[SORT ERROR] array size must be multiple of number of threads\n");
257        exit( 0 );
258    }
259
260    get_cycle( &cycle );
261    printf("\n\n[SORT] main starts on core[%x,%d] / %d threads / %d values / cycle %d\n",
262    main_cxy, main_lid, threads, ARRAY_LENGTH, (unsigned int)cycle );
263
264    // Barrier initialization
265    barrier_attr.x_size   = x_size; 
266    barrier_attr.y_size   = y_size;
267    barrier_attr.nthreads = ncores;
268    if( pthread_barrier_init( &barrier, &barrier_attr , threads ) )
269    {
270        printf("\n[SORT ERROR] cannot initialise barrier\n" );
271        exit( 0 );
272    }
273
274    get_cycle( &cycle );
275    printf("\n[SORT] main completes barrier init at cycle %d\n", (unsigned int)cycle );
276
277    // Array to sort initialization
278    for ( n = 0 ; n < ARRAY_LENGTH ; n++ )
279    {
280        array0[n] = rand();
281    }
282
283#if DISPLAY_ARRAY
284printf("\n*** array before sort\n");
285for( n=0; n<ARRAY_LENGTH; n++) printf("array[%d] = %d\n", n , array0[n] );
286#endif
287
288    get_cycle( &cycle );
289    printf("\n[SORT] main completes array init at cycle %d\n", (unsigned int)cycle );
290
291    // launch other threads to execute sort() function
292    // on cores other than the core running the main thread
293    for ( x=0 ; x<x_size ; x++ )
294    {
295        for ( y=0 ; y<y_size ; y++ )
296        {
297            for ( lid=0 ; lid<ncores ; lid++ )
298            {
299                thread_uid = (((x * y_size) + y) * ncores) + lid;
300
301                // set sort arguments for all threads
302                arg[thread_uid].threads      = threads;
303                arg[thread_uid].thread_uid   = thread_uid;
304                arg[thread_uid].main_uid     = main_uid;
305
306                // set thread attributes for all threads
307                attr[thread_uid].attributes = PT_ATTR_CLUSTER_DEFINED | PT_ATTR_CORE_DEFINED;
308                attr[thread_uid].cxy        = CXY_FROM_XY( x , y );
309                attr[thread_uid].lid        = lid;
310
311                if( thread_uid != main_uid )
312                {
313                    if ( pthread_create( &trdid,              // not used because no join
314                                         &attr[thread_uid],   // thread attributes
315                                         &sort,               // entry function
316                                         &arg[thread_uid] ) ) // sort arguments
317                    {
318                        printf("\n[SORT ERROR] main created thread %x \n", thread_uid );
319                        exit( 0 );
320                    }
321                }
322         
323#if DISPLAY_THREADS
324display_sched( CXY_FROM_XY(x,y) , lid );
325#endif
326            }
327        }
328    }
329
330    get_cycle( &cycle );
331    printf("\n[SORT] main completes threads create at cycle %d\n", (unsigned int)cycle );
332
333    // main run also the sort() function
334    sort( &arg[main_uid] );
335
336while( 1 ) asm volatile( "nop" );
337   
338    // Check result
339    int    success = 1;
340    int*   res_array = ( (threads==  2) ||
341                         (threads==  8) || 
342                         (threads== 32) || 
343                         (threads==128) || 
344                         (threads==512) ) ? array1 : array0;
345   
346    for( n=0 ; n<(ARRAY_LENGTH-2) ; n++ )
347    {
348        if ( res_array[n] > res_array[n+1] )
349        {
350            printf("\n[SORT] array[%d] = %d > array[%d] = %d\n",
351            n , res_array[n] , n+1 , res_array[n+1] );
352            success = 0;
353            break;
354        }
355    }
356
357#if DISPLAY_ARRAY
358printf("\n*** array after sort\n");
359for( n=0; n<ARRAY_LENGTH; n++) printf("array[%d] = %d\n", n , res_array[n] );
360#endif
361
362    get_cycle( &cycle );
363
364    if ( success )
365    {
366        printf("\n[SORT] success at cycle %d\n", (unsigned int)cycle );
367        exit( 0 );
368    }
369    else
370    {
371        printf("\n[SORT] failure at cycle %d\n", (unsigned int)cycle );
372        exit( 1 );
373    }
374
375}  // end main()
376
377
378/*
379vim: tabstop=4 : shiftwidth=4 : expandtab
380*/
Note: See TracBrowser for help on using the repository browser.