Пример #1
0
void calculate_merge()
{
	int eq;

	seq_function(dim_x, log_x);

	int Cp[dim_2x] = {1, 2, 2, 3, 4, 5, 6, 8, 8, 10, 12, 13, 14, 16, 18, 20, 21, 22, 24, 26, 28, 30, 32, 34, 55, 89, 144, 233, 377, 610, 987, 1597};

	eq = array_cmp(C, Cp);
	assert(eq == 1);
}
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);
}
Пример #3
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);
}
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);
}