Changeset 440 for trunk/user/sort


Ignore:
Timestamp:
May 3, 2018, 5:51:22 PM (4 years ago)
Author:
alain
Message:

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

Location:
trunk/user/sort
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/user/sort/Makefile

    r439 r440  
    44
    55-include ../../params-soft.mk
     6
    67ifeq ($(ARCH_NAME),)
    78$(error Please define in ARCH_NAME parameter in params-soft.mk!)
     
    1314
    1415compile: dirs build/sort.elf
     16
    1517build/sort.elf : $(OBJS) sort.ld
    1618        $(LD) -o $@ -T sort.ld $(OBJS) -nostdlib -L$(LIBC) -L$(LIBPTHREAD) -lc -lpthread
     
    1921build/sort.o : sort.c
    2022        $(CC) $(INCLUDES) $(CFLAGS) -c -o  $@ $<
    21         $(DU) -D $@ > $@.txt
    2223
    2324dirs:
     
    2526
    2627clean:
    27         rm -rf build/*.o build/*.elf build/*.o.txt
     28        rm -rf build/*.o  build/*.elf  build/*.txt
    2829
    2930.PHONY: dirs clean
  • trunk/user/sort/sort.c

    r436 r440  
    2727#include <pthread.h>
    2828
    29 #define ARRAY_LENGTH    0x100    // 256 values
    30 #define VERBOSE         0
     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
    3135
    3236///////////////////////////////////////////////////////
    3337// macros for fixed format cxy <=> (x,y) translation
     38// TODO these macros are only for TSAR architecture...
    3439///////////////////////////////////////////////////////
    3540
     
    6166pthread_barrier_t   barrier;                 // synchronisation variables
    6267
     68pthread_attr_t      attr[MAX_THREADS];       // thread attributes (one per thread)
     69args_t              arg[MAX_THREADS];        // sort function arguments (one per thread)
    6370
    6471////////////////////////////////////
     
    135142    unsigned int       i;
    136143    unsigned long long cycle;
     144    unsigned int       cxy;
     145    unsigned int       lid;
    137146
    138147    int         * src_array  = NULL;
    139148    int         * dst_array  = NULL;
     149
     150    // get core coordinates an date
     151    get_core( &cxy , &lid );
     152    get_cycle( &cycle );
    140153
    141154    unsigned int  thread_uid = ptr->thread_uid;
     
    143156    unsigned int  main_uid   = ptr->main_uid;
    144157
     158printf("\n### core[%x,%d] enter sort : threads %d / thread_uid %x / main_uid %x / cycle %d\n",
     159cxy, lid, threads, thread_uid, main_uid, (int)cycle );
     160
     161    while( 1 ) { asm volatile("nop"); }
     162
    145163    unsigned int  items      = ARRAY_LENGTH / threads;
    146164    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 );
    152165
    153166    bubbleSort( array0, items, items * thread_uid );
     
    157170    /////////////////////////////////
    158171    pthread_barrier_wait( &barrier );
     172
     173    printf("\n[SORT] thread[%d] exit barrier\n", thread_uid );
    159174
    160175    // the number of threads contributing to sort
     
    220235    pthread_t              trdid;              // kernel allocated thread index (unused)
    221236    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)
    224237
    225238    // compute number of threads (one thread per proc)
     
    251264
    252265    get_cycle( &cycle );
    253     printf("\n[SORT] starts : %d threads / %d values / cycle %d\n",
    254     threads, ARRAY_LENGTH , (unsigned int)cycle );
     266    printf("\n\n[SORT] main starts on core[%x,%d] / %d threads / %d values / cycle %d\n",
     267    main_cxy, main_lid, threads, ARRAY_LENGTH, (unsigned int)cycle );
    255268
    256269    // Barrier initialization
     
    265278
    266279    get_cycle( &cycle );
    267     printf("\n[SORT] completes barrier init at cycle %d\n", (unsigned int)cycle );
     280    printf("\n[SORT] main completes barrier init at cycle %d\n", (unsigned int)cycle );
    268281
    269282    // Array to sort initialization
     
    273286    }
    274287
    275 #if VERBOSE
     288#if DISPLAY_ARRAY
    276289printf("\n*** array before sort\n");
    277290for( n=0; n<ARRAY_LENGTH; n++) printf("array[%d] = %d\n", n , array0[n] );
     
    279292
    280293    get_cycle( &cycle );
    281     printf("\n[SORT] completes array init at cycle %d\n", (unsigned int)cycle );
     294    printf("\n[SORT] main completes array init at cycle %d\n", (unsigned int)cycle );
    282295
    283296    // launch other threads to execute sort() function
     
    303316                if( thread_uid != main_uid )
    304317                {
     318
     319get_cycle( &cycle );
     320printf("\n### main creates thread_uid %d / &sort_arg %x / cycle %d\n",
     321thread_uid, &arg[thread_uid], (unsigned int)cycle );
     322
    305323                    if ( pthread_create( &trdid,              // not used because no join
    306324                                         &attr[thread_uid],   // thread attributes
     
    308326                                         &arg[thread_uid] ) ) // sort arguments
    309327                    {
    310                         printf("\n[SORT ERROR] creating thread %x\n", thread_uid );
     328                        printf("\n[SORT ERROR] main created thread %x \n", thread_uid );
    311329                        exit( 0 );
    312330                    }
     331                }
    313332         
    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
     333#if DISPLAY_THREADS
     334display_sched( CXY_FROM_XY(x,y) , lid );
     335#endif
     336            }
     337        }
     338    }
     339
     340    get_cycle( &cycle );
     341    printf("\n[SORT] main completes threads create at cycle %d\n", (unsigned int)cycle );
     342
     343    // main run also the sort() function
    323344    sort( &arg[main_uid] );
    324345
     
    335356        if ( res_array[n] > res_array[n+1] )
    336357        {
     358            printf("\n[SORT] array[%d] = %d > array[%d] = %d\n",
     359            n , res_array[n] , n+1 , res_array[n+1] );
    337360            success = 0;
    338361            break;
     
    340363    }
    341364
    342 #if VERBOSE
     365#if DISPLAY_ARRAY
    343366printf("\n*** array after sort\n");
    344367for( n=0; n<ARRAY_LENGTH; n++) printf("array[%d] = %d\n", n , res_array[n] );
Note: See TracChangeset for help on using the changeset viewer.