@HaomingJiang
2017-08-31T00:34:48.000000Z
字数 1759
阅读 1147
CSE6230
Haoming Jiang
GTID: 903324367
hw3.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
double get_walltime(void)
{
struct timeval tp;
gettimeofday(&tp, NULL);
return (double) (tp.tv_sec + tp.tv_usec/1.e6);
}
int main()
{
double x[10000][3];
double y[10000][3];
double dt = 1.e-4;
double runtime = -get_walltime();
unsigned int seed = omp_get_thread_num();
// initialize x
#pragma omp parallel for schedule(runtime)
for(int i = 0; i < 10000; i++)
{
x[i][0] = 1.0 * rand_r(&seed) / RAND_MAX;
x[i][1] = 1.0 * rand_r(&seed) / RAND_MAX;
x[i][2] = 1.0 * rand_r(&seed) / RAND_MAX;
y[i][0] = y[i][1] = y[i][2] = 0;
}
for(int iter = 0; iter < 10; iter++)
{
#pragma omp parallel for schedule(runtime)
for(int i = 0; i < 10000; i++)
{
for(int j = 0; j < 500; j++)
{
y[i][0] += sqrt(2 * dt)* (2.0 * rand_r(&seed) / RAND_MAX - 1);
y[i][1] += sqrt(2 * dt)* (2.0 * rand_r(&seed) / RAND_MAX - 1);
y[i][2] += sqrt(2 * dt)* (2.0 * rand_r(&seed) / RAND_MAX - 1);
}
}
double avgdist = 0;
#pragma omp parallel for reduction(+:avgdist) schedule(runtime)
for(int i = 0; i < 10000; i++)
{
avgdist += sqrt( y[i][0]*y[i][0] + y[i][1]*y[i][1] + y[i][2]*y[i][2]);
}
avgdist /= 10000;
printf("Average Distance: %lf, Average Distance/sqrt(Time): %lf\n", avgdist, avgdist/sqrt((iter+1)*500*dt));
}
runtime += get_walltime();
printf("runtime: %lf\n\n", runtime);
return 0;
}
Makefile
CC = cc
CFLAGS = -g -O -Wall -fopenmp
.PHONY:
clean runhw3
runhw3: hw3.c
$(CC) $(CFLAGS) -o hw3 hw3.c -lm
for nt in `seq 1 8`; do \
OMP_SCHEDULE="guided,20" OMP_NUM_THREADS=$$nt ./hw3 $N; \
done
clean:
$(RM) hw3
threads | static(/s) | dynamic,100(/s) | guided,100(/s) |
---|---|---|---|
1 | 2.219723 | 2.223385 | 2.243270 |
2 | 2.981300 | 2.558330 | 2.870840 |
3 | 2.360508 | 2.093184 | 3.142048 |
4 | 3.204859 | 3.109141 | 3.183681 |
5 | 3.231761 | 2.946350 | 2.908199 |
6 | 1.720734 | 3.193151 | 2.084254 |
7 | 2.924459 | 3.229732 | 2.114133 |
8 | 1.757644 | 3.247474 | 3.219230 |