source: soft/giet_vm/sort/main.c @ 312

Last change on this file since 312 was 312, checked in by alain, 10 years ago

Reducing the number of log messages.

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