Пример #1
0
/*--------------------------------------------------------------------*/
int main(int argc, char* argv[]) {
  long thread;
  pthread_t* thread_handles; 
  double start, finish;

  suppress_output = 0;
  // for (int i = 0; i < argc; ++i){
  //   printf("Command line args === argv[%d]: %s\n", i, argv[i]);
  // }  

  if (argc == 5) { 
  } else if (argc == 6 && (strcmp(argv[5], "n") == 0)) {
	  // printf("==== %s\n", argv[5]);
	  suppress_output = 1;
  } else {
	Usage(argv[0]);
  }
  
  thread_count = strtol(argv[1], NULL, 10);
  sample_size = strtol(argv[2], NULL, 10);
  list_size = strtol(argv[3], NULL, 10);
  input_file = argv[4];

  // Allocate memory for variables
  thread_handles = malloc(thread_count*sizeof(pthread_t));
  list = malloc(list_size * sizeof(int));
  tmp_list = malloc(list_size * sizeof(int));
  sorted_list = malloc(list_size * sizeof(int));
  sample_keys = malloc(sample_size * sizeof(int));
  sorted_keys = malloc(sample_size * sizeof(int));
  splitters = malloc(thread_count * sizeof(int));
  
  // One dimensional distribution arrays
  raw_dist = malloc(thread_count * thread_count * sizeof(int));
  col_dist = malloc(thread_count * sizeof(int));
  prefix_dist = malloc(thread_count * thread_count * sizeof(int));
  prefix_col_dist = malloc(thread_count * sizeof(int));
  
	
  // pthread_mutex_init(&barrier_mutex, NULL);
  // pthread_cond_init(&ok_to_proceed, NULL);
  pthread_barrier_init(&barrier, NULL, thread_count);
  

  // Read list content from input file
  FILE *fp = fopen(input_file, "r+");
  for (i = 0; i < list_size; i++) {
  	  if (!fscanf(fp, "%d", &list[i])) {
    	  break;
      }
  }
  Print_list(list, list_size, "original list");
  
  GET_TIME(start);
  
  for (thread = 0; thread < thread_count; thread++)
     pthread_create(&thread_handles[thread], NULL,
         Thread_work, (void*) thread);

  for (thread = 0; thread < thread_count; thread++) 
     pthread_join(thread_handles[thread], NULL);
  
  GET_TIME(finish);
  
  // Print_list(sample_keys, sample_size, "Sample keys (unsorted)");
  Print_list(sorted_keys, sample_size, "Sample keys (sorted)");
  Print_list(splitters, thread_count, "Splitters");
  Print_list(raw_dist, thread_count * thread_count, "Raw dist");
  Print_list(prefix_dist, thread_count * thread_count, "Prefix dist");
  Print_list(col_dist, thread_count, "Colsum dist");
  Print_list(prefix_col_dist, thread_count, "Prefix colsum dist");
  Print_list(tmp_list, list_size, "Temp list");
  
  // Only print list data if not suppressed
  if (suppress_output == 0) {
	  Print_list(sorted_list, list_size, "Sorted list");
  }
  
  // Print elapsed time regardless
  printf("Elapsed time = %e seconds\n", finish - start);


  pthread_barrier_destroy(&barrier);
  // pthread_mutex_destroy(&barrier_mutex);
  // pthread_cond_destroy(&ok_to_proceed);

  free(thread_handles);
  
  return 0;
}  /* main */
Пример #2
0
int main ( void )
{
  pthread_barrier_t *bar1, *bar2, *bar3, *bar4, *bar5;
  pthread_t thr1, thr2;
  int r;

  /* possibly set up a watchdog timer thread here. */











  /* initialise a barrier with a zero count */
  fprintf(stderr, "\ninitialise a barrier with zero count\n");
  bar1 = malloc(sizeof(pthread_barrier_t));
  pthread_barrier_init(bar1, NULL, 0);

  /* initialise a barrier twice */
  fprintf(stderr, "\ninitialise a barrier twice\n");
  bar2 = malloc(sizeof(pthread_barrier_t));
  pthread_barrier_init(bar2, NULL, 1);
  pthread_barrier_init(bar2, NULL, 1);

  /* initialise a barrier which has threads waiting on it.
     This isn't too simple. */
  fprintf(stderr, "\ninitialise a barrier which has threads waiting on it\n");
  bar3 = malloc(sizeof(pthread_barrier_t));
  pthread_barrier_init(bar3, NULL, 2);
  /* create a thread, whose only purpose is to block on the barrier */
  pthread_create(&thr1, NULL, child1, (void*)bar3);
  /* guarantee that it gets there first */
  sleep(1);
  /* and now reinitialise */
  pthread_barrier_init(bar3, NULL, 3);

  /* destroy a barrier that has threads waiting at it */
  fprintf(stderr, "\ndestroy a barrier that has waiting threads\n");
  /* once again, create a thread, whose only purpose is to block. */
  bar4 = malloc(sizeof(pthread_barrier_t));
  pthread_barrier_init(bar4, NULL, 2);
  /* create a thread, whose only purpose is to block on the barrier */
  pthread_create(&thr2, NULL, child1, (void*)bar4);
  /* guarantee that it gets there first */
  sleep(1);
  /* and now destroy */
  pthread_barrier_destroy(bar4);

  /* destroy a barrier that was never initialised.  This is a bit
     tricky, in that we have to fill the barrier with bytes which
     ensure that the pthread_barrier_destroy call doesn't hang for
     some reason.  Zero-fill seems to work ok on amd64-linux (glibc
     2.8). */
  fprintf(stderr, "\ndestroy a barrier that was never initialised\n");
  bar5 = malloc(sizeof(pthread_barrier_t));
  assert(bar5);
  memset(bar5, 0, sizeof(*bar5));
  pthread_barrier_destroy(bar5);

  /* now we need to clean up the mess .. */
  r= pthread_cancel(thr1); assert(!r);
  r= pthread_cancel(thr2); assert(!r);

  free(bar1); free(bar2); free(bar3); free(bar4); free(bar5);

  return 0;
}
Пример #3
0
void magma_zapplyQ_data_destroy(
    magma_zapplyQ_data *zapplyQ_data)
{
    pthread_barrier_destroy(&(zapplyQ_data->barrier));
}
Пример #4
0
int main (int argc, char *argv[])
{
	struct timeval startt, endt, result;
	int i, j, k, nt, t, n, c;
	void *status;
	pthread_attr_t attr;
	tThreadArg x[NUM_THREADS];

	int lengthA, lengthB;

	omp_set_dynamic(0);

	result.tv_sec = 0;
	result.tv_usec= 0;
	/* Initialize and set thread detached attribute */
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

/* Generate a seed input */
#ifdef BENCHMARK
	srand ( time(NULL) );

	// Generate random max-sized sorted arrays
	// Generate one use over all benchmarks sizes, since
	// we don't modify them in place.
	genRandSortedArr(A, LEN_A);
	genRandSortedArr(B, LEN_B);

	printf("|NSize|Iterations|Seq|Th01|Th02|Th04|Th08|Par16|OMP 1|OMP 2|OMP 4|OMP 8|OMP 16|\n");

	for(c = 0; c < NSIZE; c++){
		n=Ns[c];
		lengthA = lengthB = n;
		lenC = 2 * n;
		printf("| %d | %d |", n, TIMES);
#else
		lengthA = 8;
		lengthB = 32;
#endif
		gettimeofday (&startt, NULL);

		for (t=0; t<TIMES; t++){
			reinit(C_seq, lenC);
			seq_function(lengthA, lengthB);
		}

		gettimeofday (&endt, NULL);
		result.tv_usec = (endt.tv_sec*1000000+endt.tv_usec) - (startt.tv_sec*1000000+startt.tv_usec);
#ifdef BENCHMARK
		printf(" %ld.%06ld | ", result.tv_usec/1000000, result.tv_usec%1000000);
#else
		printf("Result array C seq:\t\t",lenC);
		for(int idx = 0; idx < lenC; idx++)
			printf("%d ", C_seq[idx]);
		printf("\n");
#endif
		/* Run threaded algorithm(s) */
		for(nt=1; nt<NUM_THREADS; nt=nt<<1){
			if(pthread_barrier_init(&barr, NULL, nt+1))
			{
				printf("Could not create a barrier\n");
				return -1;
			}
			if(pthread_barrier_init(&internal_barr_stage1, NULL, nt+1))
			{
				printf("Could not create a barrier\n");
				return -1;
			}
			if(pthread_barrier_init(&internal_barr_stage2, NULL, nt))
			{
				printf("Could not create a barrier\n");
				return -1;
			}

			result.tv_sec=0; result.tv_usec=0;
			for (j=0; j</*NUMTHRDS*/nt; j++){
				x[j].id = j;
				x[j].nrT = nt; // number of threads in this round
				#ifdef BENCHMARK
					x[j].lenA = n;  //input size
					x[j].lenB = n;
				#else
					x[j].lenA = LEN_A;  //input size
					x[j].lenB = LEN_B;
				#endif
				pthread_create(&callThd[j], &attr, parallel_pthreads, (void *)&x[j]);
			}

			gettimeofday (&startt, NULL);
			for (t=0; t<TIMES; t++)
			{
				reinit(C_pthreads, lenC);
				pthread_barrier_wait(&internal_barr_stage1);
				pthread_barrier_wait(&barr);
			}

#if !defined(BENCHMARK)
			printf("\nResult array C pthreads:\t");
			for(int idx = 0; idx < lenC; idx++)
				printf("%d ", C_pthreads[idx]);
			printf("\n");
#endif

			/* Wait on the other threads */
			for(j=0; j</*NUMTHRDS*/nt; j++)
			{
				pthread_join(callThd[j], &status);
			}
			gettimeofday(&endt, NULL);

			for(int idx = 0; idx < lenC; idx++)
			{
				if(C_pthreads[idx] != C_seq[idx])
					printf("Difference at idx %d, SEQ vs PAR: %d vs %d\n", idx, C_seq[idx], C_pthreads[idx]);
			}

			if (pthread_barrier_destroy(&barr)) {
				printf("Could not destroy the barrier\n");
				return -1;
			}
			if (pthread_barrier_destroy(&internal_barr_stage1)) {
				printf("Could not destroy the barrier\n");
				return -1;
			}
			if (pthread_barrier_destroy(&internal_barr_stage2)) {
				printf("Could not destroy the barrier\n");
				return -1;
			}
			result.tv_usec += (endt.tv_sec*1000000+endt.tv_usec) - (startt.tv_sec*1000000+startt.tv_usec);
			printf(" %ld.%06ld | ", result.tv_usec/1000000, result.tv_usec%1000000);
		}
		for(int nt = 1; nt < NUM_THREADS; nt=nt<<1){
			result.tv_sec=0; 
			result.tv_usec=0;
			gettimeofday (&startt, NULL);	
			for (t=0; t<TIMES; t++)
			{
				reinit(C_OMP, lenC);
				parallel_OMP(nt, lengthA, lengthB);
			}
			gettimeofday (&endt, NULL);
			result.tv_usec += (endt.tv_sec*1000000+endt.tv_usec) - (startt.tv_sec*1000000+startt.tv_usec);
			printf(" %ld.%06ld | ", result.tv_usec/1000000, result.tv_usec%1000000);
#if !defined(BENCHMARK)
			printf("\nResult array C OMP:\t\t");
			for(int idx = 0; idx < lenC; idx++)
				printf("%d ", C_OMP[idx]);
#endif
			for(int idx = 0; idx < lenC; idx++)
			{
				if(C_OMP[idx] != C_seq[idx])
					printf("Difference at idx %d, SEQ vs PAR OMP: %d vs %d\n", idx, C_seq[idx], C_OMP[idx]);
			}
		}
		printf("\n");
#ifdef BENCHMARK
	}
#endif
	// freeArrays();	
	pthread_exit(NULL);
}
Пример #5
0
int main ( void )
{
  pthread_barrier_t *bar1, *bar2, *bar3, *bar4, *bar5;
  /* int r; unused since pthread_cancel are commented out */
  pthread_t thr1, thr2, slp1, slp2, ext1;

  /* initialise a barrier with a zero count */
  fprintf(stderr, "\ninitialise a barrier with zero count\n");
  bar1 = malloc(sizeof(pthread_barrier_t));
  pthread_barrier_init(bar1, NULL, 0);

  /* initialise a barrier twice */
  fprintf(stderr, "\ninitialise a barrier twice\n");
  bar2 = malloc(sizeof(pthread_barrier_t));
  pthread_barrier_init(bar2, NULL, 1);
  pthread_barrier_init(bar2, NULL, 1);

  /* initialise a barrier which has threads waiting on it.
     This isn't too simple. */
  fprintf(stderr, "\ninitialise a barrier which has threads waiting on it\n");
  bar3 = malloc(sizeof(pthread_barrier_t));
  pthread_barrier_init(bar3, NULL, 2);
  /* create a thread, whose purpose is to "unblock" the barrier after
     some sleeping in case it keeps being blocked.  */
  pthread_create(&slp1, NULL, sleep1, (void*)bar3);
  /* create a thread, whose only purpose is to block on the barrier */
  pthread_create(&thr1, NULL, child1, (void*)bar3);
  /* guarantee that it gets there first */
  sleep(1);
  /* and now reinitialise */
  pthread_barrier_init(bar3, NULL, 3);

  /* destroy a barrier that has threads waiting at it */
  fprintf(stderr, "\ndestroy a barrier that has waiting threads\n");
  /* once again, create a thread, whose only purpose is to block. */
  bar4 = malloc(sizeof(pthread_barrier_t));
  pthread_barrier_init(bar4, NULL, 2);
  /* create a thread, whose purpose is to "unblock" the barrier after
     some sleeping in case it keeps being blocked. We hope it isn't
     needed, but if it is, because pthread_barier_destroy hangs
     and we will get an extra warning about the barrier being already
     destroyed. */
  pthread_create(&slp2, NULL, sleep1, (void*)bar4);
  /* create a thread, whose only purpose is to block on the barrier */
  pthread_create(&thr2, NULL, child1, (void*)bar4);
  /* guarantee that it gets there first */
  sleep(1);
  /* and now destroy */
  pthread_barrier_destroy(bar4);

  pthread_cancel(slp2);

  /* destroy a barrier that was never initialised.  This is a bit
     tricky, in that we have to fill the barrier with bytes which
     ensure that the pthread_barrier_destroy call doesn't crash for
     some reason.  One-fill seems to work ok on amd64-linux (glibc
     2.8). */
  fprintf(stderr, "\ndestroy a barrier that was never initialised\n");
  /* Create a thread that just exits the process after some sleep.
     We are really done at this point, even if we hang. */
  pthread_create(&ext1, NULL, exit1, NULL);
  bar5 = malloc(sizeof(pthread_barrier_t));
  assert(bar5);
  memset(bar5, 1, sizeof(*bar5));
  pthread_barrier_destroy(bar5);

  /* now we need to clean up the mess .. But skip canceling threads.  */
  /* r= pthread_cancel(thr1); assert(!r); // drd doesn't like it. Just exit.
  r= pthread_cancel(thr2); assert(!r); */

  free(bar1); free(bar2); free(bar3); free(bar4); free(bar5);

  /* Use exit, we want to kill any "sleeper threads". */
  exit (0);
}
/**
 * Appelle les thread en fonction du nombre demande
 * @author Chloe
 */
int lancerThreads(MatriceInfo *matInfo, int nbThread){
    int taille=matInfo->taille;

    //Identifiants thread
    pthread_t *thread_ids;
    thread_ids=(pthread_t *)malloc(nbThread*sizeof(pthread_t));
    int i, j;
    for (i=0;i<nbThread;i++){
        pthread_t thread_id;
        thread_ids[i]= thread_id;
    }

    //Barriere
    int ret;
    if(pthread_barrier_init(&barrier,NULL,nbThread)){
        printf("Impossible de creer la barriere\n");
        return -1;
    }

    //Calcul du nombre de cases par thread
    double val= taille * taille / nbThread;
    int nbCaseParThread = sqrt((double)val);
    if (nbThread>taille){
        printf("Nombre de thread trop important par rapport a la taille.\n");
        return -1;
    }

    //Copies de matInfo pour chaque thread 
    MatriceInfo **copies;
    copies=(MatriceInfo **)malloc(nbThread*sizeof(MatriceInfo*));

    //Appel au thread en creant les copies et ajoutant les bons arguments
    int k=0;
    for(i = 0 ; i<taille ; i += nbCaseParThread){
        for(j = 0 ; j< taille ; j += nbCaseParThread){
            MatriceInfo *copie1;
            copie1=(MatriceInfo *)malloc(sizeof(MatriceInfo));
            copie1->matrice = matInfo->matrice;
            copie1->taille = matInfo->taille;
            copie1->TEMP_FROID = matInfo->TEMP_FROID ;   
            copie1->deb_i=i;
            copie1->deb_j=j;
            copie1->fin_i=i+nbCaseParThread;
            copie1->fin_j=j+nbCaseParThread;
            copies[k]=copie1;
           // printf("--> Ici je vais de : %d-%d à %d-%d \n", copie1->deb_i, copie1->deb_j, copie1->fin_i, copie1->fin_j);
            ret=pthread_create(&thread_ids[k],NULL,&uneIterationV2,(void*)copie1);
            if(ret!=0) {
                printf("Impossible de creer le thread.\n");
                return -1;
            }
            k++; 
        }
    }

    //Attentes des autres threads
    for (i=0; i<nbThread;i++){
        if(pthread_join(thread_ids[i],NULL)){
            printf("Impossible de joindre le thread.%d\n", i);
            return -1;
        }   
    }

    //destructions de la barriere et liberation memoire 
    pthread_barrier_destroy(&barrier);
    free(thread_ids);
    for (i=0; i<nbThread;i++){
        free(copies[i]);
    }
    free(copies);
    return 0;
}
Пример #7
0
void magma_dbulge_data_destroy(magma_dbulge_data *dbulge_data_S)
{
    pthread_barrier_destroy(&(dbulge_data_S->barrier));
}
Пример #8
0
int main(int argc, char *argv[])
{
	int node_id = 0;
	int arrival_lambda = 10;
	int thread_cpu_map[N_THREADS];
	int i,j,k;
	int n_threads;
	int n_left;
	int n_right;
	int next_index_left = 3;
	int next_index_right = 7;
	float local_square = 0.0, remote_square = 0.0;


	/***************** make sure #args is correct and get the n_threads, n_left and n_right */
	if(argc < 4)
	{
		printf("Usage: ./test_numa_comb n_of_threads n_of_threads_on_node0 n_of_threads_on_node1\n");
		exit(-1);
	}
	n_threads = atoi(argv[1]);
	n_left = atoi(argv[2]);
	n_right = atoi(argv[3]);
	/******************* Set the thread_cpu_map according to the n_left and n_right */
	printf("n_threads: %d, n_left: %d, n_right: %d\n",n_threads,n_left,n_right);
	for(i = 0; i < n_left; i++)
	{
		thread_cpu_map[i] = next_index_left;
		next_index_left--;
	}
	for(i = n_left; i < n_threads; i++)
	{
		thread_cpu_map[i] = next_index_right;
		next_index_right--;
	}
	for(i = 0; i < n_threads; i++)
	{
		printf("Thread %d is on cpu %d\n",i,thread_cpu_map[i]);
	}



	thread_params para[n_threads]; //The parameters to pass to the threads

	//printf("The return value of numa_get_run_node_mask(void) is %d\n",numa_get_run_node_mask());
	//printf("The return value of numa_max_node(void) is %d\n",numa_max_node());
	//numa_tonode_memory((void *)spinlock_ptr,sizeof(pthread_spinlock_t),node_id); //This doesn't work

	//initilize the spinlock pointer and put it on a specific node
	pthread_spinlock_t *spinlock_ptr = numa_alloc_onnode(sizeof(pthread_spinlock_t),node_id);

	if(spinlock_ptr == NULL) //error handling of the allocating of a spinlock pointer on a specific node
	{
		printf("alloc of spinlock on a node failed.\n");
		exit(-1);
	}

	/* initialise  syncs */
	pthread_barrier_init(&fin_barrier, NULL, n_threads);
	pthread_spin_init(spinlock_ptr,0);
	int rc;
	//create the threads
	for(i = 0; i < n_threads; i++)
	{

		para[i].thread_id = i;
		para[i].arrival_lambda = arrival_lambda;
		para[i].spinlock_ptr = spinlock_ptr;
		CPU_ZERO(&cpuset[i]);
		CPU_SET(thread_cpu_map[i],&cpuset[i]);
		rc = pthread_create(&threads[i],NULL,work,(void*)&para[i]);
		E (rc);


	}
	start_work_flag = 1; 

	/* wait here */
	for(i = 0; i < n_threads; i++)
	    pthread_join(threads[i],NULL);


	pthread_barrier_destroy(&fin_barrier);

	/*
	for(i = 0; i < n_threads; i++)
	{
		printf("The time to get one lock for thread %d is : %.9f\n",i,time_in_cs[i]/num_access_each_thread[i]);
		printf("The number of lock accesses for thread %d is : %d\n",i,num_access_each_thread[i]);
	}
	*/

	qsort((void*)g_tss,(size_t)access_count,(size_t)sizeof(timestamp),cmp_timestamp);
	/*
	for (i = 0; i < access_count; i++)
		printf("%lu with id %d\n",g_tss[i].ts,g_tss[i].id);
	*/

	/* for (i = 0; i < access_count; i++)
	 * {
	 *     printf ("%lu %d\n", g_tss[i].ts, g_tss[i].id);
	 * } */

	/* */
	
	int cs_order[access_count/2];
	for(i = 0; i < access_count/2; i++)
	{
		cs_order[i] = g_tss[i*2].id;
		//printf("%d in cs\n",cs_order[i]);
	}
	int cs_matrix[n_threads][n_threads];
	uint64_t delay_matrix[n_threads][n_threads];
	float prob_matrix[n_threads][n_threads];
	float rate_matrix[n_threads][n_threads];

	// zero out all the matrices
	memset(&cs_matrix, '\0', n_threads*n_threads*sizeof(int));
	memset(&delay_matrix, '\0', n_threads*n_threads*sizeof(uint64_t));
	memset(&prob_matrix, '\0', n_threads*n_threads*sizeof(float));


	int local_count2 = 0, remote_count2 = 0;
	uint64_t diff;
	for(i = 0; i < n_threads; i++)
	    for(j = 0; j < n_threads; j++)
		for(k = 0; k < access_count/2 -1 ; k++)
		{
		    if(cs_order[k] == i && cs_order[k+1] == j)
		    {
			cs_matrix[i][j]++;
			diff = g_tss[2*k+2].ts - g_tss[2*k+1].ts; 
			delay_matrix[i][j] += diff;
			if(is_on_same_node(i, j, n_threads, n_left, n_right))
			{
			    dprintf("local_delay: %lu\n", diff);
			    local_square += sqr(diff);
			    local_count2++;
			}
			else
			{
			    dprintf("remote_delay: %lu\n", diff);
			    remote_square += sqr(diff);
			    remote_count2++;
			}
		    }
		}

	int num_access[n_threads];
	for(i = 0; i < access_count/2 -1; i++)
	    for(j = 0; j < n_threads; j++)
	    {
		if (cs_order[i] == j) num_access[j]++;
	    }

	for(i = 0; i < n_threads; i++)
		printf("num_access[%d]:%d\n",i,num_access[i]);

	for(i = 0; i < n_threads; i++)
		for(j = 0; j < n_threads ; j++)
		{
			prob_matrix[i][j] = (float)cs_matrix[i][j]/(float)num_access[i];
			rate_matrix[i][j] = 1.0/((delay_matrix[i][j]/(float)cs_matrix[i][j])/CPU_FREQ);
		}


	printf ("\n***************** PROBS *******************\n");
	printf ("Lock is on LP, [L, R] is [%d, %d]:\n", n_left - 1, n_right);
	// tl
	printf ("L -> L\n");
	print_mtx (n_threads, n_threads, prob_matrix,
			   0, 0, n_left, n_left, 0);
    // tr
	printf ("L -> R\n");
	print_mtx (n_threads, n_threads, prob_matrix,
			   n_left, 0, n_threads, n_left, 0);

	printf ("Lock is on RP, [L, R] is [%d, %d]:\n", n_left, n_right - 1);
	// br
	printf ("R -> R\n");
	print_mtx (n_threads, n_threads, prob_matrix,
			   n_left, n_left, n_threads, n_threads, 0);
	// bl
	printf ("R -> L\n");
	print_mtx (n_threads, n_threads, prob_matrix,
			   0, n_left, n_left, n_threads, 0);
	

	printf ("\n***************** RATES *******************\n");

	printf ("Lock is on LP, [L, R] is [%d, %d]:\n", n_left - 1, n_right);
	// tl
	printf ("L -> L\n");
	print_mtx (n_threads, n_threads, rate_matrix,
			   0, 0, n_left, n_left, 1);
    // tr
	printf ("L -> R\n");
	print_mtx (n_threads, n_threads, rate_matrix,
			   n_left, 0, n_threads, n_left, 1);

	printf ("Lock is on RP, [L, R] is [%d, %d]:\n", n_left, n_right - 1);
	// br
	printf ("R -> R\n");
	print_mtx (n_threads, n_threads, rate_matrix,
			   n_left, n_left, n_threads, n_threads, 1);
	// bl
	printf ("R -> \n");
	print_mtx (n_threads, n_threads, rate_matrix,
			   0, n_left, n_left, n_threads, 1);




	//print the intra-core and inter-core delay
	//thread 0 - n_left -1 are on the left core, n_left to n_threads are on the right core
	uint64_t local_delay = 0, remote_delay = 0;
	int local_count = 0, remote_count = 0;
	float local_prob = 0.0, remote_prob = 0.0;

	for(i = 0; i < n_threads; i++)
	    for(j = 0; j < n_threads; j++)
	    {
			if (j == i)
				continue;
			if(is_on_same_node(i, j, n_threads, n_left, n_right))
			{
				//printf("%d and %d on the same node\n",i,j);
				local_delay += delay_matrix[i][j];
				local_count += cs_matrix[i][j];
				local_prob += prob_matrix[j][i];
			}
			else
			{
				//printf("%d and %d not the same node\n",i,j);
				remote_delay += delay_matrix[i][j];
				remote_count += cs_matrix[i][j];
				remote_prob += prob_matrix[j][i];
			}
	    }


	float local = (float)local_delay/(local_count);
	float remote = (float)remote_delay/(remote_count);

	printf("\n\n**************************** Aggregates ***************************\n");
	printf("local delay: %f, remote_delay: %f, local_count: %d, remote_count: %d\n",(float)local_delay/(local_count),(float)remote_delay/(remote_count),local_count,remote_count);
	printf("local prob:%f, remote prob: %f\n",local_prob/n_threads, remote_prob/n_threads);
	printf("local delay variance:%f, remote delay variance: %f\n",local_square/local_count - local*local, remote_square/remote_count - remote*remote);
	printf("local count2: %d, remote_count2:%d\n",local_count2, remote_count2);
	pthread_spin_destroy(spinlock_ptr);
	numa_free((void *)spinlock_ptr,sizeof(pthread_spinlock_t));
	pthread_exit(NULL);
	return 0;
}
Пример #9
0
/* Sets up and runs the threads
*/
void relaxationThreaded(float** inArray,
						float** outArray,
						int arraySize,
						float precision,
						int numThreads)
{
	if (numThreads > 0)
	{
		// Calculate granularity
		int currPos = 0;
		// plus 2 because we want to overlap and edges are
		// kept constant by the functions.
		int threadChunk = (arraySize / numThreads) + 2;

		// To store the data for the thread function
		LoopData	loopDataArray[numThreads];
		pthread_t 	threads[numThreads];

		// All threads must reach the barrier before we continue
		pthread_barrier_t theBarrier;
		pthread_barrier_init(&theBarrier, NULL, numThreads);

		// Shared counter so processes know when they're finished
		int finishedThreads = 0;

		// Lock for the counter
		pthread_mutex_t theLock;
		pthread_mutex_init(&theLock, NULL);

		// Loop and create/start threads
		int i;
		for ( i = 0; i < numThreads; i++)
		{
			// The last chunk is a bit of a special case
			if ( i == (numThreads - 1) )
			{
				int columnsRemaining 		= (arraySize - currPos);
				loopDataArray[i].arrayX 	= columnsRemaining;
				threadChunk 				= columnsRemaining;
			}
			else // It shouldn't be possible for any chunk but the last to go
			{   //  OOB, so keep that assumption (maybe bad practice, but
				//  protections higher up should hide it)
				loopDataArray[i].arrayX 	= threadChunk;
			}

			loopDataArray[i].inArray 		= &inArray[currPos];
			loopDataArray[i].outArray		= &outArray[currPos];

			loopDataArray[i].arrayY 		= arraySize;
			loopDataArray[i].precision 		= precision;
			loopDataArray[i].barrier		= &theBarrier;
			loopDataArray[i].numThreads		= numThreads;
			loopDataArray[i].finishedThreads= &finishedThreads;
			loopDataArray[i].finLock		= &theLock;

			if (__VERBOSE)
			{
				printf("Starting thread %d.\n", i);
			}

			pthread_create(&threads[i], NULL,
							threadLoop, (void*)&loopDataArray[i]);

			currPos += threadChunk - 2;

		}

		// join will block if the thread is going, otherwise doesn't block.
		for( i = 0; i < numThreads; i++)
		{
			pthread_join(threads[i], NULL);
		}

		// Cleanup
		pthread_barrier_destroy(&theBarrier);
		pthread_mutex_destroy(&theLock);
	}
	else
	{
		//== Serial Computation ==
		LoopData data;
		int finishedThreads = 0;

		data.inArray 	= inArray;
		data.outArray 	= outArray;
		data.arrayX 	= arraySize;
		data.arrayY		= arraySize;
		data.precision 	= precision;
		data.barrier 	= NULL;
		data.finLock	= NULL;
		data.numThreads = 1;
		data.finishedThreads = &finishedThreads;

		threadLoop((void*)&data);
	}
}
Пример #10
0
/* Do the forward Fast Fourier Transform either on two input images
   (the padded image and kernel) or on one image (the multiplication
   of the FFT of the two). In the second case, it is assumed that we
   are looking at the complex conjugate of the array so in practice
   this will be a backward transform. */
void
twodimensionfft(struct convolveparams *p, struct fftonthreadparams *fp,
                int forward1backwardn1)
{
  int err;
  pthread_t t;          /* All thread ids saved in this, not used. */
  pthread_attr_t attr;
  pthread_barrier_t b;
  size_t i, nb, *indexs, thrdcols;
  size_t nt=p->cp.numthreads, multiple=0;

  /* First we are going to get the 1D fourier transform on the rows of
     both images. */
  if(forward1backwardn1==1)       multiple=2;
  else if(forward1backwardn1==-1) multiple=1;
  else
    error(EXIT_FAILURE, 0, "%s: a bug! The value of the variable "
          "`forward1backwardn1' is %d not 1 or 2. Please contact us at %s "
          "so we can find the cause of the problem and fix it", __func__,
          forward1backwardn1, PACKAGE_BUGREPORT);


  /* ==================== */
  /* 1D FFT on each row. */
  /* ==================== */
  gal_threads_dist_in_threads(multiple*p->ps0, nt, &indexs, &thrdcols);
  if(nt==1)
    {
      fp[0].stride=1;
      fp[0].indexs=&indexs[0];
      fp[0].forward1backwardn1=forward1backwardn1;
      onedimensionfft(&fp[0]);
    }
  else
    {
      /* Initialize the attributes. Note that this running thread
         (that spinns off the nt threads) is also a thread, so the
         number the barrier should be one more than the number of
         threads spinned off. */
      if( multiple*p->ps0 < nt ) nb=multiple*p->ps0+1;
      else nb=nt+1;
      gal_threads_attr_barrier_init(&attr, &b, nb);

      /* Spin off the threads: */
      for(i=0;i<nt;++i)
        if(indexs[i*thrdcols]!=GAL_BLANK_SIZE_T)
          {
            fp[i].id=i;
            fp[i].b=&b;
            fp[i].stride=1; /* On each row, stride=1 */
            fp[i].indexs=&indexs[i*thrdcols];
            fp[i].forward1backwardn1=forward1backwardn1;
            err=pthread_create(&t, &attr, onedimensionfft, &fp[i]);
            if(err)
              error(EXIT_FAILURE, 0, "%s: can't create thread %zu for rows",
                    __func__, i);
          }

      /* Wait for all threads to finish and free the spaces. */
      pthread_barrier_wait(&b);
      pthread_attr_destroy(&attr);
      pthread_barrier_destroy(&b);
    }
  free(indexs);



  /* ====================== */
  /* 1D FFT on each column. */
  /* ====================== */
  /* No comments, exact duplicate, except the p->ps1s! */
  gal_threads_dist_in_threads(multiple*p->ps1, nt, &indexs, &thrdcols);
  if(nt==1)
    {
      fp[0].stride=p->ps1;
      fp[0].indexs=indexs;
      fp[0].forward1backwardn1=forward1backwardn1;
      onedimensionfft(&fp[0]);
    }
  else
    {
      if( multiple*p->ps1 < nt ) nb=multiple*p->ps1+1;
      else nb=nt+1;
      gal_threads_attr_barrier_init(&attr, &b, nb);
      for(i=0;i<nt;++i)
        if(indexs[i*thrdcols]!=GAL_BLANK_SIZE_T)
          {
            fp[i].b=&b;
            fp[i].stride=p->ps1; /* On each column, stride is p->ps1 */
            fp[i].indexs=&indexs[i*thrdcols];
            fp[i].forward1backwardn1=forward1backwardn1;
            err=pthread_create(&t, &attr, onedimensionfft, &fp[i]);
            if(err)
              error(EXIT_FAILURE, 0, "%s: can't create thread %zu for columns",
                    __func__, i);
          }
      pthread_barrier_wait(&b);
      pthread_attr_destroy(&attr);
      pthread_barrier_destroy(&b);
    }
  free(indexs);
}
Пример #11
0
 ~magma_sapplyQ_m_data()
 {
     pthread_barrier_destroy(&barrier);
 }
Пример #12
0
static void *
tf (void *arg)
{
  pthread_t th = (pthread_t) arg;

  int r = pthread_barrier_wait (&b);
  if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("parent thread: barrier_wait failed");
      exit (1);
    }

  sleep (1);

  r = pthread_kill (th, SIGHUP);
  if (r)
    {
      errno = r;
      printf ("pthread_kill failed %m\n");
      exit (1);
    }

  while (in_sh_body == 0)
    sleep (1);

  if (pthread_cancel (th) != 0)
    {
      puts ("cancel failed");
      exit (1);
    }

  /* This will cause the read in the initial thread to return.  */
  close (fd[0]);
  close (fd[1]);
  close (fd[2]);
  close (fd[3]);

  void *ret;
  if (pthread_join (th, &ret) != 0)
    {
      puts ("join failed");
      exit (1);
    }

  if (ret != PTHREAD_CANCELED)
    {
      puts ("result is wrong");
      exit (1);
    }

  if (cleanups != 0x1234L)
    {
      printf ("called cleanups %lx\n", cleanups);
      exit (1);
    }

  if (pthread_barrier_destroy (&b))
    {
      puts ("barrier destroy failed");
      exit (1);
    }

  exit (0);
}
Пример #13
0
static
uintptr_t
load_ppp_module()
{
    if (module_dl_handler) {
        // already loaded
        return 0;
    }

    // allocate auxiliary instance
    if (!aux_instance) {
        aux_instance = calloc(1, sizeof(*aux_instance));
        if (!aux_instance)
            return 1;

        aux_instance->id = tables_generate_new_pp_instance_id();
        tables_add_pp_instance(aux_instance->id, aux_instance);
    }

    // allocate message loop for browser thread
    if (ppb_message_loop_get_current() == 0) {
        PP_Resource message_loop = ppb_message_loop_create(aux_instance->id);
        ppb_message_loop_attach_to_current_thread(message_loop);
        ppb_message_loop_proclaim_this_thread_browser();
    }

    // allocate message loop for plugin thread (main thread)
    if (ppb_message_loop_get_for_main_thread() == 0) {
        pthread_barrier_init(&aux_instance->main_thread_barrier, NULL, 2);
        pthread_create(&aux_instance->main_thread, NULL, fresh_wrapper_main_thread, aux_instance);
        pthread_detach(aux_instance->main_thread);
        pthread_barrier_wait(&aux_instance->main_thread_barrier);
        pthread_barrier_destroy(&aux_instance->main_thread_barrier);
    }

    fpp_config_initialize();

    if (tried_files) {
        g_list_free_full(tried_files, g_free);
        tried_files = NULL;
    }

    if (fpp_config_get_plugin_path()) {
        const char *ptr = fpp_config_get_plugin_path();
        const char *last = strchr(ptr, ':');
        uintptr_t   ret;

        // parse ':'-separated list
        while (last != NULL) {
            // try entries one by one
            char *entry = strndup(ptr, last - ptr);
            ret = do_load_ppp_module(entry);
            free(entry);
            if (ret == 0)
                return 0;

            ptr = last + 1;
            last = strchr(ptr, ':');
        }

        // and the last entry
        ret = do_load_ppp_module(ptr);
        if (ret == 0)
            return 0;

        goto failure;
    }

    // try all paths
    const char **path_list = fpp_config_get_plugin_path_list();
    while (*path_list) {
        gchar *fname = g_strdup_printf("%s/%s", *path_list, fpp_config_get_plugin_file_name());
        uintptr_t ret = do_load_ppp_module(fname);
        g_free(fname);
        if (ret == 0)
            return 0;
        path_list ++;
    }

failure:
    config.quirks.plugin_missing = 1;
    use_fallback_version_strings();
    trace_error("%s, can't find %s\n", __func__, fpp_config_get_plugin_file_name());
    return 1;
}
int main (int argc, char *argv[])
{
	struct timeval startt, endt, result;
	int j, nt, t, n, c;
	
	int task, tasksCurrentLevel, totalTasks, id;
	int spawnedThreadsThisLevel ;
	int min_i, max_i, r;
	
	void *status;
 	pthread_attr_t attr;
	tThreadArg x[NUM_THREADS];
	
 	result.tv_sec = 0;
 	result.tv_usec= 0;

	/* Generate a seed input */
	// srand ( time(NULL) );
	// for(k=0; k<NMAX; k++){
	// 	A[k] = rand();
	// }

 	/* Initialize and set thread detached attribute */
 	pthread_attr_init(&attr);
 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

	printf("|NSize|Iterations|    Seq   |    Th01   |    Th02   |    Th04   |    Th08   |   Par16   |\n");

	// for each input size
	for(c=0; c<NSIZE; c++){
		n=Ns[c];
		printf("| %3d | %8d |",n,TIMES);

		/* Run sequential algorithm */
		result.tv_usec=0;
		gettimeofday (&startt, NULL);
		for (t=0; t<TIMES; t++) {
			init(n);
			seq_function(n);
		}
		gettimeofday (&endt, NULL);
		// printResult("Sequential", n);

		result.tv_usec = (endt.tv_sec*1000000+endt.tv_usec) - (startt.tv_sec*1000000+startt.tv_usec);
		printf(" %ld.%06ld | ", result.tv_usec/1000000, result.tv_usec%1000000);

		/* Run threaded algorithm(s) */
		for(nt=1; nt<NUM_THREADS; nt=nt<<1){

			if(pthread_barrier_init(&barr, NULL, nt+1))
			{
				printf("Could not create a barrier\n");
				return -1;
			}

			if(pthread_barrier_init(&internal_barr, NULL, nt))
			{
				printf("Could not create a barrier\n");
				return -1;
			}

			result.tv_sec=0; result.tv_usec=0;
			gettimeofday (&startt, NULL);

			for (t=0; t<TIMES; t++) //threaded execution
			{			
				init(n);
				totalTasks = n - 1;
				tasksCurrentLevel = n/2;
				task = 0;
				r = 2;
				

				while (task < totalTasks) {
					id = 1; spawnedThreadsThisLevel = 0; min_i = 0;

					while(spawnedThreadsThisLevel < tasksCurrentLevel) { // spawn threads
						task++;
						spawnedThreadsThisLevel++;
						max_i = min_i + r - 1;
						
						x[id-1].min_i = min_i;
						x[id-1].max_i = max_i;
						pthread_create(&callThd[id-1], &attr, par_function, (void *)&x[id-1]);
						// printf("Spawn thread %d/%d (index: %d -> %d). %d/%d/%d tasks assigned.\n",id,spawnedThreadsThisLevel,min_i,max_i,task,tasksCurrentLevel,totalTasks);
						id++;
										
						if(id > nt) {
							for (id = 1; id <= nt; id++) { // join all threads
								// printf("Ran out of threads! Join thread %d\n",id);
								pthread_join(callThd[id-1], &status);
							}
							id = 1;
						}
						min_i = max_i + 1;
					}

					for (j = 1; j < id; j++) { // join threads
						// printf("Finished level. Join thread %d\n",j);
						pthread_join(callThd[j-1], &status);
					}


					r *= 2;
					tasksCurrentLevel /= 2;
					// printf("\n");
				}
			}
			// printResult("Threaded", n);
			gettimeofday (&endt, NULL);

			if (pthread_barrier_destroy(&barr)) {
					printf("Could not destroy the barrier\n");
					return -1;
			}
			if (pthread_barrier_destroy(&internal_barr)) {
					printf("Could not destroy the barrier\n");
					return -1;
			}
 			result.tv_usec += (endt.tv_sec*1000000+endt.tv_usec) - (startt.tv_sec*1000000+startt.tv_usec);
			printf(" %ld.%06ld | ", result.tv_usec/1000000, result.tv_usec%1000000);
		}
		printf("\n");
	}
	pthread_exit(NULL);
}
Пример #15
0
int main()
{
	int cnt = 0;
	int rc;
	pthread_t child_thread;
	sig_rcvd = 0;
	
	printf("Initialize barrier with count = 2\n");
	if(pthread_barrier_init(&barrier, NULL, 2) != 0)
	{
		printf("main: Error at pthread_barrier_init()\n");
		return PTS_UNRESOLVED;
	}

	printf("main: create child thread\n");
	thread_state = NOT_CREATED_THREAD;
	if(pthread_create(&child_thread, NULL, fn_chld, NULL) != 0)
	{
		printf("main: Error at pthread_create()\n");
		return PTS_UNRESOLVED;
	}
	
	/* Expect the child to block*/
	cnt = 0;
	do{
		sleep(1);
	}while (thread_state !=EXITING_THREAD && cnt++ < 2); 
	
	if(thread_state == EXITING_THREAD)
	{
		/* child thread did not block */
		printf("Test FAILED: child thread did not block on "
			"pthread_barrier_wait()\n");
		exit(PTS_FAIL);
	}
	else if(thread_state != ENTERED_THREAD)
	{
		printf("Unexpected thread state: %d\n", thread_state);
		exit(PTS_UNRESOLVED);
	}

	printf("main: send SIGUSR1 to child thread\n");
	if(pthread_kill(child_thread, SIGUSR1) != 0)
	{
		printf("main: Error at pthread_kill()\n");
		exit(PTS_UNRESOLVED);
	}

	/* Expect the child to continue blocking */
	cnt = 0;
	do{
		sleep(1);
	}while (thread_state !=EXITING_THREAD && cnt++ < 2); 
	
	if(sig_rcvd != 1)
	{
		printf("child did not handle SIGUSR1\n");
		exit(PTS_UNRESOLVED);
	}
	
	if(thread_state == EXITING_THREAD)
	{
		/* child thread did not block */
		printf("Test FAILED: child thread should still block on "
			"pthread_barrier_wait() when interrupted by signal\n");
		exit(PTS_FAIL);
	}
	else if(thread_state != ENTERED_THREAD)
	{
		printf("Unexpected thread state: %d\n", thread_state);
		exit(PTS_UNRESOLVED);
	}
	printf("main: thread continued blocking after handling SIGUSR1\n");
	
	printf("main: call barrier wait\n");
	rc = pthread_barrier_wait(&barrier);
	
	if(rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD)
	{
		printf("Test FAILED: main: pthread_barrier_wait() got unexpected "
			"return code : %d\n" , rc);
		exit(PTS_FAIL);
	} 
	else if(rc == PTHREAD_BARRIER_SERIAL_THREAD)
		printf("main: get PTHREAD_BARRIER_SERIAL_THREAD\n");
	
	/* We expected the child returned from barrier wait */
	cnt = 0;	
	do{
		sleep(1);
	}while (thread_state != EXITING_THREAD && cnt++ < 3); 
	
	if(thread_state == ENTERED_THREAD)
	{
		printf("Test FAILED: child thread still blocked on "
			"barrier wait\n");
		return PTS_FAIL;
	}
	else if(thread_state != EXITING_THREAD)
	{
		printf("main: Unexpected thread state: %d\n", thread_state);
		return PTS_UNRESOLVED;
	}

	if(pthread_join(child_thread, NULL) != 0)
	{
		printf("main: Error at pthread_join()\n");
		exit(PTS_UNRESOLVED);
	}

	if(pthread_barrier_destroy(&barrier) != 0)
	{
		printf("Error at pthread_barrier_destroy()");
		return PTS_UNRESOLVED;
	}	

	printf("Test PASSED\n");
	return PTS_PASS;
}
int main (int argc, char *argv[])
{
	struct timeval startt, endt, result;
	int i, j, k, nt, t, n, c;

	result.tv_sec = 0;
	result.tv_usec= 0;

	/* Test Correctness */
	printf("Test for correctness:\n");

	read_file("test01.in");
	printf("Input array: \n");
	print_array(A, file_size);

	printf("Results for sequential algorithm:\n");
	init(file_size);
	seq_function(file_size, 1);

	printf("Results for openmp algorithm:\n");
	init(file_size);
	openmp_function(file_size, 1, 2);

	printf("Results for pthread algorithm:\n");
	init(file_size);
	par_function(file_size, 2);
	printf("suffix minima: \n");
	print_array(B, file_size);
	printf("preffix minima: \n");
	print_array(C, file_size);

	/* Generate a seed input */
	srand ( time(NULL) );
	for(k=0; k<NMAX; k++){
		A[k] = rand();
	}

	printf("OpenMP:\n");
	printf("|NSize|Iterations| Seq | Th01 | Th02 | Th04 | Th08 | Par16|\n");

	// for each input size
	for(c=0; c<NSIZE; c++){
		n=Ns[c];
		printf("| %d | %d |",n,TIMES);

		/* Run sequential algorithm */
		result.tv_usec=0;
		gettimeofday (&startt, NULL);
		for (t=0; t<TIMES; t++) {
			init(n);
			seq_function(n, 0);
		}
		gettimeofday (&endt, NULL);
		result.tv_usec = (endt.tv_sec*1000000+endt.tv_usec) - (startt.tv_sec*1000000+startt.tv_usec);
		printf(" %ld.%06ld | ", result.tv_usec/1000000, result.tv_usec%1000000);

		/* Run OpenMP algorithm */
		for(nt=1; nt<NUM_THREADS; nt=nt<<1){
			result.tv_sec=0; 
			result.tv_usec=0;
			gettimeofday (&startt, NULL);
			for (t=0; t<TIMES; t++) 
			{
				init(n);
				openmp_function(n, 0, nt);
			}
			gettimeofday (&endt, NULL);
			result.tv_usec += (endt.tv_sec*1000000+endt.tv_usec) - (startt.tv_sec*1000000+startt.tv_usec);
			printf(" %ld.%06ld | ", result.tv_usec/1000000, result.tv_usec%1000000);
		}
		printf("\n");
	}

	/* Initialize and set thread detached attribute */
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

	printf("Pthread:\n");
	printf("|NSize|Iterations| Seq | Th01 | Th02 | Th04 | Th08 | Par16|\n");

	// for each input size
	for(c=0; c<NSIZE; c++){
		n=Ns[c];
		printf("| %d | %d |",n,TIMES);

		/* Run sequential algorithm */
		result.tv_usec=0;
		gettimeofday (&startt, NULL);
		for (t=0; t<TIMES; t++) {
			init(n);
			seq_function(n, 0);
		}
		gettimeofday (&endt, NULL);
		result.tv_usec = (endt.tv_sec*1000000+endt.tv_usec) - (startt.tv_sec*1000000+startt.tv_usec);
		printf(" %ld.%06ld | ", result.tv_usec/1000000, result.tv_usec%1000000);

		/* Run pthread algorithm(s) */
		for(nt=1; nt<NUM_THREADS; nt=nt<<1){
			if(pthread_barrier_init(&barr, NULL, nt+1))
			{
				printf("Could not create a barrier\n");
				return -1;
			}
			if(pthread_barrier_init(&internal_barr, NULL, nt))
			{
				printf("Could not create a barrier\n");
				return -1;
			}

			result.tv_sec=0; 
			result.tv_usec=0;

			gettimeofday (&startt, NULL);
			for (t=0; t<TIMES; t++) 
			{
				init(n);
				par_function(n, nt);
				// pthread_barrier_wait(&barr);
			}
			gettimeofday (&endt, NULL);

			if (pthread_barrier_destroy(&barr)) {
				printf("Could not destroy the barrier\n");
				return -1;
			}
			if (pthread_barrier_destroy(&internal_barr)) {
				printf("Could not destroy the barrier\n");
				return -1;
			}
			result.tv_usec += (endt.tv_sec*1000000+endt.tv_usec) - (startt.tv_sec*1000000+startt.tv_usec);
			printf(" %ld.%06ld | ", result.tv_usec/1000000, result.tv_usec%1000000);
		}
		printf("\n");
	}
	pthread_exit(NULL);
}
Пример #17
0
int main (int argc, char **argv) 
{
    int i;
    unsigned int total_len = 0;
    struct timeval begtime,endtime;
    FILE *sfd,*pfd;
    char sfilename[20] = "string";
    char pfilename[20] = "pattern";

	
//=============================================== 
    if (argc < 4)
    {
        fprintf (stderr,"Usage: acsmx stringfile patternfile ...  -nocase\n");
        exit (0);
    }
    strcpy (sfilename, argv[1]);
    sfd = fopen(sfilename,"r");
    if(sfd == NULL)
    {
        fprintf(stderr,"Open file error!\n");
        exit(1);
    }

    strcpy(pfilename,argv[2]);
    pfd = fopen(pfilename,"r");
    if(sfd == NULL)
    {
        fprintf(stderr,"Open file error!\n");
        exit(1);
    }
    thread_num = atoi(argv[3]);
   	acsm = acsmNew (thread_num); 
   
//read patterns    
	i = 0;
    while(fgets(pattern,MAXPATTERNLEN,pfd))
    {
    	int len = strlen(pattern);
    	acsmAddPattern (acsm, pattern, len-1);
		i++;
    }
    fclose(pfd);
    printf("\n\nread %d patterns\n\n===============================",i);
    /* Generate GtoTo Table and Fail Table */
    acsmCompile (acsm);
//========================================================= 

    /*read string*/
    for(i = 0;i < MAXLINE;i++)
    {
    	if(!fgets(text[i],MAXLEN,sfd))
    		break;
   		total_len += strlen(text[i]) - 1; //ignore the last char '\n'
    }
    line = i;
    fclose(sfd);
    printf("\n\nreading finished...\n=============================\n\n");
    printf("%d lines\t%d bytes",line,total_len);
    printf("\n\n=============================\n");
    
    gettimeofday(&begtime,0);
    //create multi_thread
    thread_array = calloc(thread_num,sizeof(pthread_t));
	valid_len_array = calloc(thread_num,sizeof(unsigned int));
    pthread_barrier_init(&barrier_thread,NULL,thread_num);
    pthread_barrier_init(&barrier_validation,NULL,thread_num);
 
    for(i = 0;i < thread_num; i++)
	{
		pthread_create(&thread_array[i], NULL, SearchThread, (void*)i);
    }
//=========================================================== 
    int err;
    for(i = 0;i < thread_num;i++)
    {
        err = pthread_join(thread_array[i],NULL);
        if(err != 0)
        {
            printf("can not join with thread %d:%s\n", i,strerror(err));
        }
    }
    gettimeofday(&endtime,0);

    PrintSummary(acsm);
    acsmFree (acsm);

    printf ("\n### AC Match Finished ###\n");
    printf("\nTime Cost: %lu us\n\n",(endtime.tv_sec - begtime.tv_sec)*1000000 + (endtime.tv_usec - begtime.tv_usec));
    printf ("\n====================================\n\n");
    printf ("Validation Stage Len:\n\n");
    for(i = 0;i < thread_num;i++)
        printf("rank%d\t%u\n",i,valid_len_array[i]);
    printf ("\n====================================\n\n");
   
    free(thread_array);
    free(valid_len_array);
    pthread_barrier_destroy(&barrier_thread);
    pthread_barrier_destroy(&barrier_validation);
    return 0;
}
Пример #18
0
/**
 * @brief destroys a created barrier
 *
 * @param bar     pointer to the barrier

 * @returns SMELT_SUCCESS or error value
 */
errval_t smlt_platform_barrier_destroy(smlt_platform_barrier_t *barrier)
{
    return pthread_barrier_destroy(barrier);
}
Пример #19
0
static void *
tf (void *arg)
{
  pthread_t th = (pthread_t) arg;

  int r = pthread_barrier_wait (&b);
  if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("parent thread: barrier_wait failed");
      exit (1);
    }

  sleep (1);

  r = pthread_kill (th, SIGHUP);
  if (r)
    {
      errno = r;
      printf ("pthread_kill failed %m\n");
      exit (1);
    }

  while (in_sh_body == 0)
    sleep (1);

  if (pthread_cancel (th) != 0)
    {
      puts ("cancel failed");
      exit (1);
    }

  void *ret;
  if (pthread_join (th, &ret) != 0)
    {
      puts ("join failed");
      exit (1);
    }

  if (ret != PTHREAD_CANCELED)
    {
      puts ("result is wrong");
      exit (1);
    }

  if (cleanups != 0x1234L)
    {
      printf ("called cleanups %lx\n", cleanups);
      exit (1);
    }

  if (pthread_barrier_destroy (&b))
    {
      puts ("barrier destroy failed");
      exit (1);
    }

  /* The pipe closing must be issued after the cancellation handling to avoid
     a race condition where the cancellation runs after both pipe ends are
     closed.  In this case the read syscall returns EOF and the cancellation
     must not act.  */
  close (fd[0]);
  close (fd[1]);
  close (fd[2]);
  close (fd[3]);

  exit (0);
}
Пример #20
0
int main(int argc, char *argv[])
{
	if (argc != 5) {
		printf("Usage: %s nx ny steps threads.\n", argv[0]);
		return 1;
	}
	int i, ti;
	le_point2 n = {atoi(argv[1]), atoi(argv[2])};
	int steps = atoi(argv[3]);
	int max_threads = atoi(argv[4]);
	le_task task;
	le_material mat;
	le_vec2 h = {1.0, 1.0};
	real dt = 0.3;
	le_vec2 center = {n.x / 2, n.y / 2};
	char name[1000];

	double t;
	unsigned long cc;
	
	/* pthread variables */
	pthread_t threads[NUM_THREADS];
	st_pthread data[NUM_THREADS];
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
	/* end of pthread variables */

	le_init_material(2.0, 1.0, 1.5, &mat);
	task.max_threads = max_threads;
	task.steps = steps;
	le_init_task(&task, dt, h, mat, n);
	le_set_ball(&task, center, 10.0, 1.0);

	pthread_barrier_init(&b, NULL, max_threads);
	
	cc = getCC();
	t = timer();
	
	for(ti = 0; ti < max_threads; ti++) {		
		data[ti].task = &task;
		data[ti].thread_num = ti;
		int rc = pthread_create(&(threads[ti]), &attr, le_step, (void *)(&(data[ti])));
		assert(!rc);
	}
	for(ti = 0; ti < max_threads; ti++) {
		void *st;
		int rc = pthread_join(threads[ti], &st);
		assert(!rc);;
	}
	
	t = timer() - t;
	cc = getCC() - cc;
	
	pthread_barrier_destroy(&b);
	
	printf("%d %d %d %f ", n.x, n.y, steps, t);
	
	le_save_task(&task, "result.vtk");

	le_free_task(&task);
	pthread_exit(NULL);
}
// Calculer la prochaine etape de la diffusion (methode avec barrier POSIX)
void nextStepBarrier(float* mat1, float* mat2, int size, int nbThreads){
	int indThread;
	pthread_t *thread_id;
	if ( (thread_id = malloc(sizeof(pthread_t)*(nbThreads+1))) == NULL ){
		fprintf(stderr,"Allocation impossible \n");
		exit(EXIT_FAILURE);
 	}

 	switch(etapeEnCours){
		case 1:
			if(pthread_barrier_init(&barrierHorizontal, NULL, nbThreads+1)!=0){
				printf("Barrier fail to init\n");
			}
		break;
		case 2:
			if(myBarrier_init(&myBarrierHorizontal, nbThreads+1)!=0){
				printf("MyBarrier fail to init\n");	
			}
		break;
		case 3:
			//printf("Init semaBarrier...\n");
			if(semaBarrier_init(&semaBarrierHorizontal, nbThreads+1)!=0){
				printf("semaBarrier fail to init\n");		
			}
		break;

		default:
		break;
	}

	for(indThread=0;indThread<nbThreads;indThread++){
		argument_barriere* args;
		if( (args = malloc(sizeof(argument_barriere))) == NULL){
			fprintf(stderr,"Allocation impossible \n");
			exit(EXIT_FAILURE);
		}
		args->mat1=mat1;
		args->mat2=mat2;
		args->size=size;
		args->minI=tab[indThread].iMin;
		args->maxI=tab[indThread].iMax;
		args->minJ=tab[indThread].jMin;
		args->maxJ=tab[indThread].jMax;

		if(pthread_create (&thread_id[indThread], NULL, &calculSubMatrixHorizontal, (void *)args) !=0){
			printf("[ERREUR] Creation de la thread fail\n");
		}
	}

	switch(etapeEnCours){
		case 1:
			pthread_barrier_wait (&barrierHorizontal);
		break;
		case 2:
			myBarrier_wait(&myBarrierHorizontal);
		break;
		case 3:
			semaBarrier_wait(&semaBarrierHorizontal);
		break;
		default:
		break;
	}

	for(indThread=0;indThread<nbThreads;indThread++)
		pthread_join(thread_id[indThread],NULL);//On fait un join de toutes les thread.

	switch(etapeEnCours){
		case 1:
			pthread_barrier_destroy(&barrierHorizontal);
			if(pthread_barrier_init(&barrierVertical, NULL, nbThreads+1)!=0){
				printf("Barrier fail to init\n");
			}
		break;
		case 2:
			myBarrier_destroy(&myBarrierHorizontal);
			if(myBarrier_init(&myBarrierVertical,nbThreads+1)!=0){
				printf("MyBarrier fail to init\n");	
			}
		break;
		case 3:
			//printf("destroyBarrier...\n");
			semaBarrier_destroy(&semaBarrierHorizontal);
			if(semaBarrier_init(&semaBarrierVertical,nbThreads+1)!=0){
				printf("semaBarrier fail to init\n");		
			}
		break;
		default:
		break;
	}

	for(indThread=0;indThread<nbThreads;indThread++){
		argument_barriere* args;
		if( (args = malloc(sizeof(argument_barriere))) == NULL){
			fprintf(stderr,"Allocation impossible \n");
			exit(EXIT_FAILURE);
		}
		args->mat1=mat1;
		args->mat2=mat2;
		args->size=size;
		args->minI=tab[indThread].iMin;
		args->maxI=tab[indThread].iMax;
		args->minJ=tab[indThread].jMin;
		args->maxJ=tab[indThread].jMax;

		int errorPthread;
		if(errorPthread = pthread_create (&thread_id[indThread], NULL, &calculSubMatrixVertical, (void *)args) != 0){
			fprintf(stderr, "error: pthread_create, rc: %d\n", errorPthread);
		}
	}

	switch(etapeEnCours){
		case 1:
			pthread_barrier_wait (&barrierVertical);
		break;
		case 2:
			myBarrier_wait(&myBarrierVertical);
		break;
		case 3:
			//TODO barrier wait....semaphore
			semaBarrier_wait(&semaBarrierVertical);
		break;
		default:
		break;
	}

	for(indThread=0;indThread<nbThreads;indThread++)
		pthread_join(thread_id[indThread],NULL);//On fait un join de toutes les thread.
	
	switch(etapeEnCours){
		case 1:
			pthread_barrier_destroy(&barrierVertical);
			pthread_barrier_init(&barrierFillCenter, NULL, 1);
		break;
		case 2:
			myBarrier_destroy(&myBarrierVertical);
			myBarrier_init(&myBarrierFillCenter,1);
		break;
		case 3:
			//TODO  destroy barrier semaphore.
			semaBarrier_destroy(&semaBarrierVertical);
			semaBarrier_init(&semaBarrierFillCenter,1);
		break;
		default:
		break;
	}

	fillCenter(mat1, size);

	switch(etapeEnCours){
		case 1:
			pthread_barrier_wait(&barrierFillCenter);
			pthread_barrier_destroy(&barrierFillCenter);
		break;
		case 2:
			myBarrier_wait(&myBarrierFillCenter);
			myBarrier_destroy(&myBarrierFillCenter);
		break;
		case 3:
			//todo semaphore.
			semaBarrier_wait(&semaBarrierFillCenter);
			semaBarrier_destroy(&semaBarrierFillCenter);
		break;
		default:
		break;
	}
}
Пример #22
0
int
main()
{
  int i;
  int fail = 0;
  pthread_t thread[NUM_THREADS];

  assert(pthread_barrier_init(&startBarrier, NULL, NUM_THREADS/2) == 0);

  for (i = 1; i < NUM_THREADS/2; i++)
    {
      accesscount[i] = thread_set[i] = thread_destroyed[i] = 0;
      assert(pthread_create(&thread[i], NULL, mythread, (void *)&accesscount[i]) == 0);
    }

  /*
   * Here we test that existing threads will get a key created
   * for them.
   */
  assert(pthread_key_create(&key, destroy_key) == 0);

  (void) pthread_barrier_wait(&startBarrier);

  /*
   * Test main thread key.
   */
  accesscount[0] = 0;
  setkey((void *) &accesscount[0]);

  /*
   * Here we test that new threads will get a key created
   * for them.
   */
  for (i = NUM_THREADS/2; i < NUM_THREADS; i++)
    {
      accesscount[i] = thread_set[i] = thread_destroyed[i] = 0;
      assert(pthread_create(&thread[i], NULL, mythread, (void *)&accesscount[i]) == 0);
    }

  /*
   * Wait for all threads to complete.
   */
  for (i = 1; i < NUM_THREADS; i++)
    {
	assert(pthread_join(thread[i], NULL) == 0);
    }

  assert(pthread_key_delete(key) == 0);

  assert(pthread_barrier_destroy(&startBarrier) == 0);

  for (i = 1; i < NUM_THREADS; i++)
    {
	/*
	 * The counter is incremented once when the key is set to
	 * a value, and again when the key is destroyed. If the key
	 * doesn't get set for some reason then it will still be
	 * NULL and the destroy function will not be called, and
	 * hence accesscount will not equal 2.
	 */
	if (accesscount[i] != 2)
	  {
	    fail++;
	    fprintf(stderr, "Thread %d key, set = %d, destroyed = %d\n",
			i, thread_set[i], thread_destroyed[i]);
	  }
    }

  fflush(stderr);

  return (fail);
}
Пример #23
0
/**
 * \internal Rozwiązuje nazwę serwera w osobnym wątku.
 *
 * Funkcja działa analogicznie do \c gg_resolver_fork_start(), z tą różnicą,
 * że działa na wątkach, nie procesach. Jest dostępna wyłącznie gdy podczas
 * kompilacji włączono odpowiednią opcję.
 *
 * \param fd Wskaźnik na zmienną, gdzie zostanie umieszczony deskryptor gniazda
 * \param priv_data Wskaźnik na zmienną, gdzie zostanie umieszczony wskaźnik
 *                  do prywatnych danych wątku rozwiązującego nazwę
 * \param hostname Nazwa serwera do rozwiązania
 *
 * \return 0 jeśli się powiodło, -1 w przypadku błędu
 */
static int gg_resolver_pthread_start(int *fd, void **priv_data, const char *hostname)
{
	struct gg_resolver_pthread_data *data = NULL;
	struct gg_resolver_pthread_params *params = NULL;
	int pipes[2], pipe_ready = 0, new_errno;
	pthread_barrier_t init_barrier;

	gg_debug(GG_DEBUG_FUNCTION, "** gg_resolver_pthread_start(%p, %p, \"%s\");\n", fd, priv_data, hostname);

	if (fd == NULL || priv_data == NULL || hostname == NULL) {
		gg_debug(GG_DEBUG_MISC, "// gg_resolver_pthread_start() invalid arguments\n");
		errno = EFAULT;
		return -1;
	}

	data = malloc(sizeof(struct gg_resolver_pthread_data));
	params = malloc(sizeof(struct gg_resolver_pthread_params));

	if (data == NULL || params == NULL) {
		gg_debug(GG_DEBUG_MISC, "// gg_resolver_pthread_start() out of memory for resolver data\n");
		goto cleanup;
	}

	if (socketpair(AF_LOCAL, SOCK_STREAM, 0, pipes) == -1) {
		gg_debug(GG_DEBUG_MISC, "// gg_resolver_pthread_start() unable "
			"to create pipes (errno=%d, %s)\n",
			errno, strerror(errno));
		goto cleanup;
	}
	pipe_ready = 1;

	params->wfd = pipes[1];
	params->hostname = strdup(hostname);

	if (params->hostname == NULL) {
		gg_debug(GG_DEBUG_MISC, "// gg_resolver_pthread_start() out of memory\n");
		goto cleanup;
	}

	if (pthread_barrier_init(&init_barrier, NULL, 2) != 0) {
		gg_debug(GG_DEBUG_MISC, "// gg_resolver_pthread_start() "
			"can't create barrier\n");
		goto cleanup;
	}
	params->init_barrier = &init_barrier;

	if (pthread_create(&data->thread, NULL, gg_resolver_pthread_thread, params)) {
		gg_debug(GG_DEBUG_MISC, "// gg_resolver_pthread_start() unable to create thread\n");
		pthread_barrier_destroy(&init_barrier);
		goto cleanup;
	}

	gg_debug(GG_DEBUG_MISC, "// gg_resolver_pthread_start() %p\n", data);

	/* Poczekaj, aż wątek rozwiązywania nazw potwierdzi odebranie
	 * parametrów, aby mieć pewność, że go nie zabijemy zanim je zwolni.
	 */
	pthread_barrier_wait(&init_barrier);
	pthread_barrier_destroy(&init_barrier);

	*fd = pipes[0];
	*priv_data = data;

	return 0;

cleanup:
	new_errno = errno;

	free(data);

	if (params != NULL)
		free(params->hostname);
	free(params);

	if (pipe_ready) {
		close(pipes[0]);
		close(pipes[1]);
	}

	errno = new_errno;

	return -1;
}
Пример #24
0
/* The main test function. */
int main( int argc, char *argv[] )
{
	int ret = 0;
	pthread_t tcontrol, tchange;
	pthread_barrier_t bar;

	/* Initialize output routine */
	output_init();

	ret = pthread_barrier_init( &bar, NULL, 2 );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "Failed to init barrier" );
	}

	/* Create the controler thread */
	ret = pthread_create( &tcontrol, NULL, controler, &bar );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "thread creation failed" );
	}

	/* Now create the changer thread */
	ret = pthread_create( &tchange, NULL, changer, &tcontrol );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "thread creation failed" );
	}

	/* wait until the changer finishes */
	ret = pthread_join( tchange, NULL );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "Failed to join the thread" );
	}


	/* let the controler control */
	ret = pthread_barrier_wait( &bar );

	if ( ( ret != 0 ) && ( ret != PTHREAD_BARRIER_SERIAL_THREAD ) )
	{
		UNRESOLVED( ret, "barrier wait failed" );
	}


	ret = pthread_join( tcontrol, NULL );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "Failed to join the thread" );
	}

	ret = pthread_barrier_destroy( &bar );

	if ( ret != 0 )
	{
		UNRESOLVED( ret, "barrier destroy failed" );
	}

	PASSED;
}
Пример #25
0
 ~magma_cbulge_data()
 {
     pthread_barrier_destroy(&barrier);
 }
Пример #26
0
void barrier_test(void)
{
	pthread_t barrier_thread[CONFIG_EXAMPLES_KERNEL_SAMPLE_NBARRIER_THREADS];
	pthread_addr_t result;
	pthread_attr_t attr;
	pthread_barrierattr_t barrierattr;
	int status;
	int i;

	printf("barrier_test: Initializing barrier\n");

	status = pthread_barrierattr_init(&barrierattr);
	if (status != OK) {
		printf("barrier_test: pthread_barrierattr_init failed, status=%d\n", status);
	}

	status = pthread_barrier_init(&barrier, &barrierattr, CONFIG_EXAMPLES_KERNEL_SAMPLE_NBARRIER_THREADS);
	if (status != OK) {
		printf("barrier_test: pthread_barrierattr_init failed, status=%d\n", status);
	}

	/* Create the barrier */

	(void)pthread_barrierattr_init(&barrierattr);

	/* Start CONFIG_EXAMPLES_KERNEL_SAMPLE_NBARRIER_THREADS thread instances */

	status = pthread_attr_init(&attr);
	if (status != OK) {
		printf("barrier_test: pthread_attr_init failed, status=%d\n", status);
	}

	for (i = 0; i < CONFIG_EXAMPLES_KERNEL_SAMPLE_NBARRIER_THREADS; i++) {
		status = pthread_create(&barrier_thread[i], &attr, barrier_func, (pthread_addr_t)i);
		if (status != 0) {
			printf("barrier_test: Error in thread %d create, status=%d\n", i, status);
			printf("barrier_test: Test aborted with waiting threads\n");
			goto abort_test;
		} else {
			printf("barrier_test: Thread %d created\n", i);
		}
	}
	FFLUSH();

	/* Wait for all thread instances to complete */

	for (i = 0; i < CONFIG_EXAMPLES_KERNEL_SAMPLE_NBARRIER_THREADS; i++) {
		status = pthread_join(barrier_thread[i], &result);
		if (status != 0) {
			printf("barrier_test: Error in thread %d join, status=%d\n", i, status);
		} else {
			printf("barrier_test: Thread %d completed with result=%p\n", i, result);
		}
	}

	/* Destroy the barrier */

abort_test:
	status = pthread_barrier_destroy(&barrier);
	if (status != OK) {
		printf("barrier_test: pthread_barrier_destroy failed, status=%d\n", status);
	}

	status = pthread_barrierattr_destroy(&barrierattr);
	if (status != OK) {
		printf("barrier_test: pthread_barrierattr_destroy failed, status=%d\n", status);
	}
	FFLUSH();
}
Пример #27
0
void uv_barrier_destroy(uv_barrier_t* barrier) {
  if (pthread_barrier_destroy(barrier))
    abort();
}
Пример #28
0
TEST (PthreadTest, All) {
    // At one point we had a bug in that tatas lock relied on
    // the impelemtation's sizeof pthread_t
    tatas_lock lock; 
    cout << "Sizes: " << endl;
    cout << "pthread_t: " << sizeof(pthread_t) << endl;
    cout << "lock._holder: " << sizeof(lock._holder) << endl;
    cout << "lock._holder.bits: " << sizeof(lock._holder.bits) << endl;
    EXPECT_EQ(sizeof(lock._holder.handle), sizeof(lock._holder.bits));
    cout << endl;

    int threads = 2;

        /* Determine how many threads we can create in this process
         * First, see if there is a limit in <limits.h>
         *   This doesn't have to be defined if the value is determined by
         *   available memeory.
         * Second, see if there is a limit available with sysconf.
         *   This does have to be define by a POSIX 1003.1c-1995-compliant system.
         * */

        long m;
    cout << "_POSIX_VERSION/_SC_VERSION ----------------------------- " <<endl;
#ifdef _POSIX_VERSION
    cout << "POSIX_VERSION " << _POSIX_VERSION << endl;
#endif
#ifdef _SC_VERSION
        m = sysconf(_SC_VERSION);
    cout << "sysconf: _SC_VERSION " << 
                        _SC_VERSION << endl;
#endif

    cout << "_POSIX2_VERSION/_SC_2_VERSION ----------------------------- " <<endl;
#ifdef _POSIX2_VERSION
    cout << "POSIX2_VERSION " << _POSIX2_VERSION << endl;
#endif
#ifdef _SC_2_VERSION
        m = sysconf(_SC_2_VERSION);
    cout << "sysconf: _SC_2_VERSION " << 
                        _SC_2_VERSION << endl;
#endif

    cout << "_XOPEN_VERSION/_SC_XOPEN_VERSION ----------------------------- " <<endl;
#ifdef _XOPEN_VERSION
    cout << "XOPEN_VERSION " << _XOPEN_VERSION << endl;
#endif
#ifdef _SC_XOPEN_VERSION
        m = sysconf(_SC_XOPEN_VERSION);
    cout << "sysconf: _SC_XOPEN_VERSION " << 
                        _SC_XOPEN_VERSION << endl;
#endif

    cout << "_POSIX_THREADS/_SC_THREADS ----------------------------- " <<endl;
#ifdef _POSIX_THREADS
    cout << "_POSIX_THREADS " << _POSIX_THREADS << endl;
#endif
#ifdef _SC_THREADS
        m = sysconf(_SC_THREADS);
    cout << "sysconf: _SC_THREADS " << 
                        _SC_THREADS << endl;
#endif

    cout << "_POSIX_THREAD_ATTR_STACKSIZE/_SC_THREAD_ATTR_STACKSIZE ----------------------------- " <<endl;
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
    cout << "POSIX_THREAD_ATTR_STACKSIZE " << _POSIX_THREAD_ATTR_STACKSIZE << endl;
#endif
#ifdef _SC_THREAD_ATTR_STACKSIZE
        m = sysconf(_SC_THREAD_ATTR_STACKSIZE);
    cout << "sysconf: _SC_THREAD_ATTR_STACKSIZE " << 
                        _SC_THREAD_ATTR_STACKSIZE << endl;
#endif

    cout << "_POSIX_THREAD_ATTR_STACKADDR/_SC_THREAD_ATTR_STACKADDR ----------------------------- " <<endl;
#ifdef _POSIX_THREAD_ATTR_STACKADDR
    cout << "POSIX_THREAD_ATTR_STACKADDR " << _POSIX_THREAD_ATTR_STACKADDR << endl;
#endif
#ifdef _SC_THREAD_ATTR_STACKADDR
        m = sysconf(_SC_THREAD_ATTR_STACKADDR);
    cout << "sysconf: _SC_THREAD_ATTR_STACKADDR " << 
                        _SC_THREAD_ATTR_STACKADDR << endl;
#endif

#ifdef _POSIX_BARRIERS
    cout << "POSIX_BARRIERS " << _POSIX_BARRIERS << endl;
#endif
#ifdef _POSIX_READER_WRITER_LOCKS
    cout << "POSIX_READER_WRITER_LOCKS " << _POSIX_READER_WRITER_LOCKS << endl;
#endif

#ifdef _POSIX_THREAD_THREADS_MAX
    cout << "POSIX_THREAD_THREADS_MAX " << _POSIX_THREAD_THREADS_MAX << endl;
#endif

#ifdef PTHREAD_THREADS_MAX
        threads = PTHREADS_MAX;
    cout << "limits.h: maximum pthreads per process is " << 
                        threads << endl;
#else
    cout 
        << "limits.h: maximum pthreads per process is not defined." 
        << endl;
#endif
#ifdef _SC_THREAD_THREADS_MAX
        m = sysconf(_SC_THREAD_THREADS_MAX);
    cout << "limits.h: maximum pthreads per process is " << 
                        m << endl;
        threads = int(m);
#else
    cout 
        << "NOT COMPLIIANT: sysconf: maximum _SC_PTHREAD_THREADS_MAX is not defined." 
        << endl;
#endif

        // create and fork as many threads as the system will allow,

#ifdef TEST_FIND_PTHREAD_RUNTIMEMAX
        threads=0;
    while(true) {
                threads++;
                // pthread_attr_t attr;
                pthread_t thr;
                int e= pthread_create(&thr, NULL, dummy, NULL);
                if(e!=0) {
                        if(e==EAGAIN) {
                                cout << "runtime: maximum pthreads " << threads << endl;
                                break;
                        }
                        perror("perror ");
                        cout << "ERROR " << e << endl;
                        cout << "threads " << threads << endl;
                        break;
                }
    }
        // On RHEL 5 I got 32748 with the runtime test despite having
        // the stated limit of POSIX_THREAD_THREADS_MAX of 64.
#endif

        threads=2;
        // They all wait on this barrier.
    pthread_barrier_init(&b, NULL, threads+1);
    sthread_t **t = new sthread_t *[threads];

    for (int i = 0; i < threads; i++)
        t[i] = new simple_thread_t();

    for (int i = 0; i < threads; i++)
        EXPECT_TRUE(t[i] != NULL);

    for (int i = 0; i < threads; i++)
        EXPECT_FALSE(t[i]->fork().is_error());

    pthread_barrier_wait(&b);

    for (int i = 0; i < threads; i++)
        EXPECT_FALSE(t[i]->join().is_error());

    pthread_barrier_destroy(&b);
    
    delete[] t;
}
Пример #29
0
/****************************************************************
*
*	Function: main
*	Input:	int argc 	number of command line arguements
*		char **arg	pointer to those arguements
*
*	Output: int 0 for success and 1 for error
*
*	Description: Runs the prefix and suffix algorithm multiple
*	times averaging the results and printing them to terminal. 
*
*
*****************************************************************/
int main(int argc, char **argv)
{
	struct timeval startt, endt, result;

	char name[16] = "posix";
	
	int status;
	int n;
	int* A;
	int* P;
	int* S;

	//Check if app was given enough input
	if(argc < 7){
		printf("Missing Arguement Parameters\n");
		printf("Format ./seq file_path array_size P_ans_path S_ans_Path RUNS THREADS\n");
		return 1;
	}

	//Save args to memory and allocate memory for arrays
	n = atoi(argv[2]);
	RUNS = atoi(argv[5]);
	MAX_THREADS = atoi(argv[6]);
	A = malloc(n*sizeof(int));
	P = malloc(n*sizeof(int));
	S = malloc(n*sizeof(int));

	if(A==NULL){
		printf("Failed to Allocate Memory for Input Array");	
	}

	//Read the input array from file and save to memory
	status = read_input(A, n, argv[1]);

	if(status){	
		printf("Failed to Read Input \n");
		return 1;
	}

	//Start algorithm testing
	int j;
	double average;
	for(j=0; j<RUNS; j++){

		/*Start Timer*/
		result.tv_sec=0;
		result.tv_usec=0;
		gettimeofday (&startt, NULL);

		//setup the threads and args for threads
		pthread_t thread_id[MAX_THREADS];
		int k;
		data thread_args[MAX_THREADS];
		
		//figure out the amount of work for each thread
		int m = n/MAX_THREADS;
		int rem = n%MAX_THREADS;
		int allocated=0;

		//Set the threads to Detached state so they release resources immediately
		//when terminated
		pthread_attr_t attribute;
		pthread_attr_init(&attribute);
		pthread_attr_setdetachstate(&attribute, PTHREAD_CREATE_DETACHED);
		
		//Make sure the number of barriers is correct in case of work is less than
		//amount of threads available.
		int numbarriers = MAX_THREADS;
		if(m==0){
			numbarriers = rem;
		}
		
		//Initiate the barrier variable for threads
		pthread_barrier_init (&barrier, NULL, numbarriers);

		//Setup all the thread arguements with their respective workloads
		for(k=0; k<MAX_THREADS; k++){
			thread_args[k].A = A;
			thread_args[k].P = P;
			thread_args[k].S = S;
			thread_args[k].n = n;
			if(rem){
				thread_args[k].start = k*m+allocated;
				thread_args[k].end = (k+1)*m+1+allocated;
				rem--;
				allocated++;
			}
			else{
				thread_args[k].start = k*m+allocated;
				thread_args[k].end = (k+1)*m+allocated;
				
			}
			thread_args[k].id = k;
		}

		//Create all the threads
		for(k=1; k<MAX_THREADS; k++){
			//Check to see if they have work
			if(thread_args[k].start != thread_args[k].end )
			pthread_create(&thread_id[k], &attribute, &psMin, &thread_args[k]);
		}
		
		//Run main thread with arguements
		psMin(&thread_args[0]);

		//Release barrier resources
		pthread_barrier_destroy(&barrier);

		/*Stop Timer*/
		gettimeofday (&endt, NULL);
		result.tv_usec = (endt.tv_sec*1000000+endt.tv_usec) - (startt.tv_sec*1000000+startt.tv_usec);
		average += result.tv_usec;
	}
	average = average/RUNS;	//Find average execution time
	
	//Print the results	
	printf("%d 	%f	us \n",n,average);

	//Check to see if answer is correct if told to do so in the input args
	if((atoi(argv[3])!=1) && (atoi(argv[4])!=1))
	{
		status = outputCheck(P, S, argv[3], argv[4], n);
		if(status){
			printf("Incorrect Answer\n");
		}
		else{
			printf("Correct Answer\n");
		}

	}
	/*Output Results if N is small*/
	if(n<=50){
		status = write_output(P, S, n, name);
		if(status){	
			printf("Failed to Write Output \n");
			return 1;
		}
	}
	
	//free the memory
	free(P);
	free(S);
	free(A);

    	return 0;
}
Пример #30
0
static void *
tf (void *arg)
{
  int old;
  int r = pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &old);
  if (r != 0)
    {
      puts ("setcancelstate failed");
      exit (1);
    }

  r = pthread_barrier_wait (&b);
  if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("barrier_wait failed");
      exit (1);
    }

  for (int i = 0; i < 10; ++i)
    {
      struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
      TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
    }

  seen = 1;
  pthread_setcancelstate (old, NULL);

  struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
  TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));

  exit (1);
}


static int
do_test (void)
{
  if (pthread_barrier_init (&b, NULL, 2) != 0)
   {
     puts ("barrier init failed");
     return 1;
   }

  pthread_t th;
  if (pthread_create (&th, NULL, tf, NULL) != 0)
    {
      puts ("thread creation failed");
      return 1;
    }

  int r = pthread_barrier_wait (&b);
  if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      puts ("barrier_wait failed");
      return 1;
    }

  if (pthread_cancel (th) != 0)
    {
      puts ("cancel failed");
      return 1;
    }

  void *status;
  if (pthread_join (th, &status) != 0)
    {
      puts ("join failed");
      return 1;
    }
  if (status != PTHREAD_CANCELED)
    {
      puts ("thread not canceled");
      return 1;
    }

  if (pthread_barrier_destroy (&b) != 0)
    {
      puts ("barrier_destroy failed");
      return 1;
    }

  if (seen != 1)
    {
      puts ("thread cancelled when PTHREAD_CANCEL_DISABLED");
      return 1;
    }

  return 0;
}