예제 #1
0
////////////////////////////////////////////////////////////////////////////////
//! Generate the histogram on Single Threaded CPU and Then PTHREADS and Check for Correctness
///////////////////////////////////////////////////////////my_args/////////////////////
void run_test(int num_elements, int num_threads) 
{
	float diff;
	int i; 
	int *reference_histogram = (int *)malloc(sizeof(int) * HISTOGRAM_SIZE); // Space to store histogram generated by the CPU
	int *histogram_using_pthreads = (int *)malloc(sizeof(int) * HISTOGRAM_SIZE); // Space to store histogram generated by the GPU

	// Allocate memory for the input data
	int size = sizeof(int) * num_elements;
	int *input_data = (int *)malloc(size);
	
	// Randomly generate input data. Initialize the input data to be integer values between 0 and (HISTOGRAM_SIZE - 1)
	srand(time(NULL)); // add this for real randomness
	for(i = 0; i < num_elements; i++)
		input_data[i] = floorf((HISTOGRAM_SIZE - 1) * (rand()/(float)RAND_MAX));

	printf("Creating the reference histogram. \n"); 
	// Compute the reference solution on the CPU
	struct timeval start, stop;	
	gettimeofday(&start, NULL);

	compute_gold(input_data, reference_histogram, num_elements, HISTOGRAM_SIZE);

	gettimeofday(&stop, NULL);
	printf("CPU run time = %0.10f s. \n", (float)(stop.tv_sec - start.tv_sec + (stop.tv_usec - start.tv_usec)/(float)1000000));
	// check_histogram(reference_histogram, num_elements, HISTOGRAM_SIZE);
	
	// Compute the histogram using pthreads. The result histogram should be stored on the histogram_using_pthreads array
	printf("\n");
	printf("Creating histogram using pthreads. \n");

	struct timeval pstart, pstop;	
	gettimeofday(&pstart, NULL);
	compute_using_pthreads(input_data, histogram_using_pthreads, num_elements, num_threads, HISTOGRAM_SIZE);
	gettimeofday(&pstop, NULL);
	printf("Pthreads run time = %0.10f s. \n", (float)(pstop.tv_sec - pstart.tv_sec + (pstop.tv_usec - pstart.tv_usec)/(float)1000000));
	// check_histogram(histogram_using_pthreads, num_elements, HISTOGRAM_SIZE);

	// Compute the differences between the reference and pthread results
	diff = 0.0;
	for(i = 0; i < HISTOGRAM_SIZE; i++)
		diff = diff + abs(reference_histogram[i] - histogram_using_pthreads[i]);

	printf("Difference between the reference and pthread results: %f. \n", diff);
   
	// cleanup memory
	free(input_data);
	free(reference_histogram);
	free(histogram_using_pthreads);
	pthread_mutex_destroy(&mutex);

	pthread_exit(NULL);
}
예제 #2
0
int main(void) 
{
	int n = NUM_TRAPEZOIDS;
	float a = LEFT_ENDPOINT;
	float b = RIGHT_ENDPOINT;
	float h = (b-a)/(float)n; // Height of each trapezoid  
	printf("The height of the trapezoid is %f \n", h);

	double reference = compute_gold(a, b, n, h);
   printf("Reference solution computed on the CPU = %f \n", reference);

	/* Write this function to complete the trapezoidal on the GPU. */
	double pthread_result = compute_using_pthreads(a, b, n, h);
	printf("Solution computed using pthreads = %f \n", pthread_result);
} 
예제 #3
0
void 
run_test(int num_elements) 
{
	float diff;
	int i; 

    /* Allocate memory for the histrogram structures. */
	int *reference_histogram = (int *)malloc(sizeof(int) * HISTOGRAM_SIZE);
	int *histogram_using_pthreads = (int *)malloc(sizeof(int) * HISTOGRAM_SIZE); 

	/* Generate input data---integer values between 0 and (HISTOGRAM_SIZE - 1). */
    int size = sizeof(int) * num_elements;
	int *input_data = (int *)malloc(size);

	for(i = 0; i < num_elements; i++)
		input_data[i] = floorf((HISTOGRAM_SIZE - 1) * (rand()/(float)RAND_MAX));

    /* Compute the reference solution on the CPU. */
	printf("Creating the reference histogram. \n"); 
	struct timeval start, stop;	
	gettimeofday(&start, NULL);

	compute_gold(input_data, reference_histogram, num_elements, HISTOGRAM_SIZE);

	gettimeofday(&stop, NULL);
	printf("CPU run time = %0.2f s. \n", (float)(stop.tv_sec - start.tv_sec + (stop.tv_usec - start.tv_usec)/(float)1000000));
	check_histogram(reference_histogram, num_elements, HISTOGRAM_SIZE); 
	
	/* Compute the histogram using pthreads. The result histogram should be stored in the 
     * histogram_using_pthreads array. */
	printf("Creating histogram using pthreads. \n");
	compute_using_pthreads(input_data, histogram_using_pthreads, num_elements, HISTOGRAM_SIZE);
	/* check_histogram(histogram_using_pthreads, num_elements, HISTOGRAM_SIZE); */

	/* Compute the differences between the reference and pthread results. */
	diff = 0.0;
    for(i = 0; i < HISTOGRAM_SIZE; i++)
		diff = diff + abs(reference_histogram[i] - histogram_using_pthreads[i]);

	printf("Difference between the reference and pthread results: %f. \n", diff);
   
	/* cleanup memory. */
	free(input_data);
	free(reference_histogram);
	free(histogram_using_pthreads);

	pthread_exit(NULL);
}
예제 #4
0
int main(int argc, char **argv)
{
	if(argc != 2){
		printf("Usage: vector_dot_product <num elements> \n");
		exit(1);
	}
	int num_elements = atoi(argv[1]); // Obtain the size of the vector 

	/* Create the vectors A and B and fill them with random numbers between [-.5, .5]. */
	float *vector_a = (float *)malloc(sizeof(float) * num_elements);
	float *vector_b = (float *)malloc(sizeof(float) * num_elements); 
	srand(time(NULL)); // Seed the random number generator
	for(int i = 0; i < num_elements; i++){
		vector_a[i] = ((float)rand()/(float)RAND_MAX) - 0.5;
		vector_b[i] = ((float)rand()/(float)RAND_MAX) - 0.5;
	}
	/* Compute the dot product using the reference, single-threaded solution. */
	struct timeval start, stop;	
	gettimeofday(&start, NULL);
	float reference = compute_gold(vector_a, vector_b, num_elements); 
	gettimeofday(&stop, NULL);

	printf("Reference solution = %f. \n", reference);
	printf("Execution time = %fs. \n", (float)(stop.tv_sec - start.tv_sec + (stop.tv_usec - start.tv_usec)/(float)1000000));
	printf("\n");

	/* Compute the dot product using the multi-threaded version. */
	gettimeofday(&start, NULL);
	float result = compute_using_pthreads(vector_a, vector_b, num_elements);
	gettimeofday(&stop, NULL);

	printf("Pthread solution = %f. \n", result);
	printf("Execution time = %fs. \n", (float)(stop.tv_sec - start.tv_sec + (stop.tv_usec - start.tv_usec)/(float)1000000));
	printf("\n");

	/* Free memory here. */ 
	free((void *)vector_a);
	free((void *)vector_b);

	pthread_exit(NULL);
}