Ignore:
Timestamp:
Jul 27, 2015, 8:40:45 PM (9 years ago)
Author:
alain
Message:

Introduce support for the "shared" argument in the giet_tty_alloc() system call,
and replace the giet_shr_printf() system call by giet_tty_printf().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • soft/giet_vm/applications/sort/main.c

    r515 r669  
    33// Date   :  November 2013
    44// 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)
    512//
    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.
    2120///////////////////////////////////////////////////////////////////////////////
    2221
    2322#include "stdio.h"
    2423#include "mapping_info.h"
    25 #include "hard_config.h"
    2624#include "user_barrier.h"
     25#include "user_lock.h"
    2726
    2827#define ARRAY_LENGTH    0x1000
    2928#define IPT             (ARRAY_LENGTH / threads) // ITEMS PER THREAD
    3029
    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
     35int              array0[ARRAY_LENGTH];
     36int              array1[ARRAY_LENGTH];
     37
     38volatile int     init_ok = 0;
     39giet_barrier_t   barrier[10];
     40user_lock_t      tty_lock;   
    5141
    5242void bubbleSort(
     
    6353        int init_pos_result);
    6454
    65 ///////////////////////////////////////////////////////
    66 // This application supports at most 1024 processors
    67 // Number of barriers = log2(threads)
    68 
    69 giet_barrier_t barrier[10];
    70 
    7155//////////////////////////////////////////
    72 __attribute__ ((constructor)) void sort()
     56__attribute__ ((constructor)) void main()
     57//////////////////////////////////////////
    7358{
    74     int thread_id = giet_thread_id();
    7559    int * src_array = NULL;
    7660    int * dst_array = NULL;
    7761    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)
    8362    unsigned int x_size;
    8463    unsigned int y_size;
     
    8665    unsigned int threads;
    8766
     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)
    8874    giet_procs_number( &x_size , &y_size , &nprocs );
    8975    threads = x_size * y_size * nprocs;
    9076
     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
    91104    if ( (threads != 1)   && (threads != 2)   && (threads != 4)   &&
    92105         (threads != 8)   && (threads != 16 ) && (threads != 32)  &&
     
    94107         (threads != 512) && (threads != 1024) )
    95108    {
    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
    125114    for (i = IPT * thread_id; i < IPT * (thread_id + 1); i++)
    126115    {
     
    128117    }
    129118
    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);
    134121
    135122    bubbleSort(array0, IPT, IPT * thread_id);
    136123
    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
    139127    for (i = 0; i < __builtin_ctz( threads ); i++)
    140128    {
    141         barrier_wait(&barrier[i]);
     129        barrier_wait( &barrier[i] );
    142130
    143131        if((thread_id % (2 << i)) != 0)
    144132        {
    145             printf("[ Thread %d ] Quit\n\r", thread_id );
     133            printf("[ SORT T%d ] Quit\n\r", thread_id );
    146134            giet_exit("Completed");
    147135        }
    148136
    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);
    150138
    151139        if((i % 2) == 0)
     
    167155                );
    168156
    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);
    170158    }
    171159
     
    173161    int failure_index;
    174162
    175     //////////////////////////////
    176163    // Verify the resulting array
    177    
    178164    if(thread_id != 0)
    179165    {
     
    186172        if(dst_array[i] > dst_array[i+1])
    187173        {
    188 
    189174            success = 0;
    190175            failure_index = i;
     
    195180    time_end = giet_proctime();
    196181
    197     printf("[ Thread 0 ] Finishing sort application at cycle %d\n"
    198            "[ Thread 0 ] Time elapsed = %d\n",
     182    printf("[ SORT T0 ] Finishing sort application at cycle %d\n"
     183           "[ SORT T0 ] Time elapsed = %d\n",
    199184            time_end, (time_end - time_start) );
    200185
     
    205190    else
    206191    {
    207         printf("[ Thread 0 ] Failure!! Incorrect element: %d\n\r",
     192        printf("[ SORT T0 ] Failure!! Incorrect element: %d\n\r",
    208193               failure_index);
    209194        for(i=0; i<ARRAY_LENGTH; i++)
Note: See TracChangeset for help on using the changeset viewer.