All pastes #2133427 Raw Edit

Anonymous

public text v1 · immutable
#2133427 ·published 2012-03-28 22:08 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <gsl/gsl_rng.h>
#include <omp.h>

double d_t (struct timespec t1, struct timespec t2){

	return ((t2.tv_sec - t1.tv_sec) + (double) (t2.tv_nsec - t1.tv_nsec) / 1000000000.0);
}

int main (int argc, char *argv[]){

	double a, b;

	int i,j,k;

	int n=atoi(argv[1]), seed=atoi(argv[2]), nt=atoi(argv[3]);

	printf("\nn\t= %d", n);
	printf("\nseed\t= %d", seed);
	printf("\nnt\t= %d", nt);

	struct timespec t1, t2, t3, t4;

	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);

	//initialize gsl random number generator
	const gsl_rng_type *rng_t;
	gsl_rng *rng;
	gsl_rng_env_setup();
	rng_t = gsl_rng_default;
	rng = gsl_rng_alloc (rng_t);
	
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);

	for (i=0;i<n;i++){
		a = gsl_rng_uniform(rng);
	}

	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t3);

	omp_set_num_threads(nt);
	#pragma omp parallel
	{
		nt = omp_get_num_threads();
		gsl_rng_set(rng,seed*omp_get_thread_num());
		#pragma omp parallel for
		for(i=0;i<n;i++){
			a = gsl_rng_uniform(rng);
		}
	}

	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t4);

	printf("\n\ninitializing:\t\tt1 = %f seconds",			d_t(t1,t2));
	printf("\nsequencial for loop:\tt2 = %f seconds",		d_t(t2,t3));
	printf("\nparalel for loop:\tt3 = %f seconds (%f * t2)",	d_t(t3,t4), (double) d_t(t3,t4) / (double) d_t(t2,t3));
	printf("\nnumber of threads:\tnt = %d\n",			nt);

	//free random number generator
	gsl_rng_free(rng);

	return 0;

}