int main (int argc, char *argv[]) { pthread_t thread_id; engine_t *engine; int count = 0, calls = 0; int status; status = pthread_key_create (&engine_key, destructor); if (status != 0) dbg_printf("Create key \n"); status = workq_init (&workq, 4, engine_routine); if (status != 0) dbg_printf ( "Init work queue \n"); status = pthread_create (&thread_id, NULL, thread_routine, NULL); if (status != 0) dbg_printf ("Create thread\n"); (void)thread_routine (NULL); status = pthread_join (thread_id, NULL); if (status != 0) dbg_printf ( "Join thread \n"); status = workq_destroy (&workq); /*死等模式将无法退出*/ if (status != 0) dbg_printf ("Destroy work queue \n"); return 0; }
int main (int argc, char *argv[]) { pthread_t thread_id; engine_t *engine; int count = 0, calls = 0; int status; status = pthread_key_create (&engine_key, destructor); if (status != 0) err_abort (status, "Create key"); status = workq_init (&workq, 4, engine_routine); if (status != 0) err_abort (status, "Init work queue"); status = pthread_create (&thread_id, NULL, thread_routine, NULL); if (status != 0) err_abort (status, "Create thread"); (void)thread_routine (NULL); status = pthread_join (thread_id, NULL); if (status != 0) err_abort (status, "Join thread"); status = workq_destroy (&workq); if (status != 0) err_abort (status, "Destroy work queue"); /* * By now, all of the engine_t structures have been placed * on the list (by the engine thread destructors), so we * can count and summarize them. */ engine = engine_list_head; while (engine != NULL) { count++; calls += engine->calls; printf ("engine %d: %d calls\n", count, engine->calls); engine = engine->link; } printf ("%d engine threads processed %d calls\n", count, calls); return 0; }
int main(int argc, char** argv) { pthread_t thread_id; engine_t* engine; int count = 0; int calls = 0; int status; status = pthread_key_create(&engine_key, destructor); assert(status == 0); status = workq_init(&workq, 4, engine_routine); assert(status == 0); status = pthread_create(&thread_id, NULL, thread_routine, NULL); assert(status == 0); (void)thread_routine(NULL); status = pthread_join(thread_id, NULL); assert(status == 0); status = workq_destroy(&workq); assert(status == 0); engine = engine_list_head; while (engine != NULL) { count++; calls += engine->calls; printf("engine %d: %d calls\n", count, engine->calls); engine = engine->link; } printf("%d engine threads processed %d calls\n", count, calls); return 0; }
int main(int argc, char* argv[]) { int c; u_int8_t payload[10] = {0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a}; u_int8_t srcmac1[6] = {0x10,0x11,0x12,0x13,0x14,0x15}; u_int8_t srcmac2[6] = {0x16,0x17,0x18,0x19,0x1a,0x1b}; u_int8_t (*p)[6]; p=&srcmac2; struct config* pointer_config = NULL; struct thread_input input; while((c=getopt(argc, argv, "c:")) != -1) { if(c == 'c') { pointer_config = load_config(optarg); if(pointer_config == NULL) { return 1; } } } input.device = pointer_config->device; input.interval = pointer_config->interval; input.count = pointer_config->count; input.srcmac_qty = 2; input.size = pointer_config->size; input.srcip = pointer_config->srcip; input.dstip = pointer_config->dstip; memcpy(input.dstmac, pointer_config->dstmac, 6*sizeof(u_int8_t)); input.payload = (u_int8_t*)malloc(10*sizeof(u_int8_t)); input.size = 10; input.srcmac = malloc(2*sizeof(u_int8_t*)); input.srcmac[0] = malloc(6*sizeof(u_int8_t)); input.srcmac[1] = malloc(6*sizeof(u_int8_t)); memcpy(input.srcmac[0], srcmac1, 6*sizeof(u_int8_t)); memcpy(input.srcmac[1], srcmac2, 6*sizeof(u_int8_t)); thread_routine(&input); return 0; }
// // benchmarking program // int main( int argc, char **argv ) { // // process command line // if( find_option( argc, argv, "-h" ) >= 0 ) { printf( "Options:\n" ); printf( "-h to see this help\n" ); printf( "-n <int> to set the number of particles\n" ); printf( "-p <int> to set the number of threads\n" ); printf( "-o <filename> to specify the output file name\n" ); return 0; } n = read_int( argc, argv, "-n", 1000 ); n_threads = read_int( argc, argv, "-p", 2 ); char *savename = read_string( argc, argv, "-o", "pthreads.txt" ); // // allocate resources // fsave = savename ? fopen( savename, "w" ) : NULL; particles = (particle_t*) malloc( n * sizeof(particle_t) ); set_size( n ); init_particles( n, particles ); blks_num = max(2 * size / sqrt((MAX_PART_THREADS)*(3.14159)*cutoff*cutoff), 1); blks_size = size / ((double) blks_num); subblks_num = max(2 * blks_size / sqrt((MAX_PART_SUBB)*(3.14159)*cutoff*cutoff), 1); subblks_size = blks_size / ((double) subblks_num); totblks_num = blks_num * subblks_num; part_int = n/( blks_num * blks_num ) + 1; printf("blks_num %d, blks_size %f, subblks_num %d, subblks_size %f, totblks_num %d \n", blks_num, blks_size, subblks_num, subblks_size, totblks_num); printf("size %f, totblks_num * subblks_size %f \n", size, totblks_num * subblks_size); //printf("n %d, part_int %d, blks_num %d, cutoff %f \n", n, part_int, blks_num, cutoff); bins = (particle_t **) malloc( MAX_PART_SUBB * totblks_num * totblks_num * sizeof(particle_t*)); bin_part_num = (int *) malloc( totblks_num * totblks_num * sizeof(int)); bin_mutexes = (pthread_mutex_t*) malloc( totblks_num * totblks_num * sizeof(pthread_mutex_t)); memset(bin_part_num, 0, totblks_num * totblks_num * sizeof(int)); pthread_attr_t attr; P( pthread_attr_init( &attr ) ); P( pthread_barrier_init( &barrier, NULL, blks_num * blks_num ) ); pthread_mutex_init( &set_count_zero, NULL ); pthread_cond_init( &count_zero_cond, NULL ); for(int i=0; i<totblks_num*totblks_num; ++i) { if(pthread_mutex_init(&bin_mutexes[i], NULL)) { printf("\nsetupBins(): Unable to initialize the %i th mutex\n", i); } } int *thread_ids = (int *) malloc( blks_num * blks_num * sizeof( int ) ); for( int i = 0; i < blks_num*blks_num; i++ ) thread_ids[i] = i; pthread_t *threads = (pthread_t *) malloc( blks_num * blks_num * sizeof( pthread_t ) ); printf("shit allocated \n"); // // do the parallel work // simulation_time = read_timer( ); for( int i = 1; i < blks_num * blks_num; i++ ) P( pthread_create( &threads[i], &attr, thread_routine, &thread_ids[i] ) ); printf("pthreads created \n"); thread_routine( &thread_ids[0] ); printf("first thread runs \n"); for( int i = 1; i < blks_num * blks_num; i++ ) P( pthread_join( threads[i], NULL ) ); simulation_time = read_timer( ) - simulation_time; printf( "n = %d, n_threads = %d, simulation time = %g seconds\n", n, blks_num*blks_num, simulation_time ); // // release resources // P( pthread_barrier_destroy( &barrier ) ); P( pthread_attr_destroy( &attr ) ); pthread_mutex_destroy(&set_count_zero); pthread_cond_destroy(&count_zero_cond); for(int i=0; i<totblks_num*totblks_num; i++) { pthread_mutex_destroy(&bin_mutexes[i]); } free( thread_ids ); free( threads ); free( bin_mutexes ); free( bin_part_num ); free( bins ); free( particles ); if( fsave ) fclose( fsave ); return 0; }
// // benchmarking program // int main( int argc, char **argv ) { // // process command line // if( find_option( argc, argv, "-h" ) >= 0 ) { printf( "Options:\n" ); printf( "-h to see this help\n" ); printf( "-n <int> to set the number of particles\n" ); printf( "-p <int> to set the number of threads\n" ); printf( "-o <filename> to specify the output file name\n" ); return 0; } n = read_int( argc, argv, "-n", 1000 ); n_threads = read_int( argc, argv, "-p", 2 ); char *savename = read_string( argc, argv, "-o", NULL ); // // allocate resources // fsave = savename ? fopen( savename, "w" ) : NULL; particles = (particle_t*) malloc( n * sizeof(particle_t) ); set_size( n ); init_particles( n, particles ); pthread_attr_t attr; P( pthread_attr_init( &attr ) ); P( pthread_barrier_init( &barrier, NULL, n_threads ) ); // VIRAJ doBinning(); // !VIRAJ // create threads int *thread_ids = (int *) malloc( n_threads * sizeof( int ) ); for( int i = 0; i < n_threads; i++ ) thread_ids[i] = i; pthread_t *threads = (pthread_t *) malloc( n_threads * sizeof( pthread_t ) ); // // do the parallel work // double simulation_time = read_timer( ); for( int i = 1; i < n_threads; i++ ) P( pthread_create( &threads[i], &attr, thread_routine, &thread_ids[i] ) ); thread_routine( &thread_ids[0] ); for( int i = 1; i < n_threads; i++ ) P( pthread_join( threads[i], NULL ) ); simulation_time = read_timer( ) - simulation_time; printf( "n = %d, n_threads = %d, simulation time = %g seconds\n", n, n_threads, simulation_time ); // // release resources // P( pthread_barrier_destroy( &barrier ) ); P( pthread_attr_destroy( &attr ) ); free( thread_ids ); free( threads ); free( particles ); if( fsave ) fclose( fsave ); return 0; }
// // benchmarking program // int main( int argc, char **argv ) { // // process command line // if( find_option( argc, argv, "-h" ) >= 0 ) { printf( "Options:\n" ); printf( "-h to see this help\n" ); printf( "-n <int> to set the number of particles\n" ); printf( "-p <int> to set the number of threads\n" ); printf( "-o <filename> to specify the output file name\n" ); printf( "-s <filename> to specify a summary file name\n" ); printf( "-no turns off all correctness checks and particle output\n"); return 0; } n = read_int( argc, argv, "-n", 1000 ); n_threads = read_int( argc, argv, "-p", 2 ); char *savename = read_string( argc, argv, "-o", NULL ); char *sumname = read_string( argc, argv, "-s", NULL ); fsave = savename ? fopen( savename, "w" ) : NULL; fsum = sumname ? fopen ( sumname, "a" ) : NULL; if( find_option( argc, argv, "-no" ) != -1 ) no_output = 1; // // allocate resources // particles = (particle_t*) malloc( n * sizeof(particle_t) ); set_size( n ); init_particles( n, particles ); pthread_attr_t attr; P( pthread_attr_init( &attr ) ); P( pthread_barrier_init( &barrier, NULL, n_threads ) ); int *thread_ids = (int *) malloc( n_threads * sizeof( int ) ); for( int i = 0; i < n_threads; i++ ) thread_ids[i] = i; pthread_t *threads = (pthread_t *) malloc( n_threads * sizeof( pthread_t ) ); // // do the parallel work // double simulation_time = read_timer( ); for( int i = 1; i < n_threads; i++ ) P( pthread_create( &threads[i], &attr, thread_routine, &thread_ids[i] ) ); thread_routine( &thread_ids[0] ); for( int i = 1; i < n_threads; i++ ) P( pthread_join( threads[i], NULL ) ); simulation_time = read_timer( ) - simulation_time; printf( "n = %d, simulation time = %g seconds", n, simulation_time); if( find_option( argc, argv, "-no" ) == -1 ) { gabsavg /= (n_threads*1.0); // // -The minimum distance absmin between 2 particles during the run of the simulation // -A Correct simulation will have particles stay at greater than 0.4 (of cutoff) with typical values between .7-.8 // -A simulation where particles don't interact correctly will be less than 0.4 (of cutoff) with typical values between .01-.05 // // -The average distance absavg is ~.95 when most particles are interacting correctly and ~.66 when no particles are interacting // printf( ", absmin = %lf, absavg = %lf", gabsmin, gabsavg); if (gabsmin < 0.4) printf ("\nThe minimum distance is below 0.4 meaning that some particle is not interacting "); if (gabsavg < 0.8) printf ("\nThe average distance is below 0.8 meaning that most particles are not interacting "); } printf("\n"); // // Printing summary data // if( fsum) fprintf(fsum,"%d %d %g\n",n,n_threads,simulation_time); // // release resources // P( pthread_barrier_destroy( &barrier ) ); P( pthread_attr_destroy( &attr ) ); free( thread_ids ); free( threads ); free( particles ); if( fsave ) fclose( fsave ); if( fsum ) fclose ( fsum ); return 0; }
extern "C" void* thread_routine (void *arg) { thr_args_base* const args = (thr_args_base*)arg; printf ("thread %lu starting\n", args->threadno_); switch (args->type_tag_) { case thr_args_base::Char: return thread_routine ((thr_args<char>*)(arg)); case thr_args_base::SChar: return thread_routine ((thr_args<signed char>*)(arg)); case thr_args_base::UChar: return thread_routine ((thr_args<unsigned char>*)(arg)); case thr_args_base::Short: return thread_routine ((thr_args<short>*)(arg)); case thr_args_base::UShort: return thread_routine ((thr_args<unsigned short>*)(arg)); case thr_args_base::Int: return thread_routine ((thr_args<int>*)(arg)); case thr_args_base::UInt: return thread_routine ((thr_args<unsigned int>*)(arg)); case thr_args_base::Long: return thread_routine ((thr_args<long>*)(arg)); case thr_args_base::ULong: return thread_routine ((thr_args<unsigned long>*)(arg)); #ifdef _RWSTD_LONG_LONG case thr_args_base::LLong: return thread_routine ((thr_args<_RWSTD_LONG_LONG>*)(arg)); case thr_args_base::ULLong: return thread_routine ((thr_args<unsigned _RWSTD_LONG_LONG>*)(arg)); #endif // _RWSTD_LONG_LONG }; return 0; }