[关闭]
@HaomingJiang 2017-08-31T00:34:48.000000Z 字数 1759 阅读 1147

CSE6230 HW#3

CSE6230

Haoming Jiang
GTID: 903324367


Program:

hw3.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <sys/time.h>
  5. double get_walltime(void)
  6. {
  7. struct timeval tp;
  8. gettimeofday(&tp, NULL);
  9. return (double) (tp.tv_sec + tp.tv_usec/1.e6);
  10. }
  11. int main()
  12. {
  13. double x[10000][3];
  14. double y[10000][3];
  15. double dt = 1.e-4;
  16. double runtime = -get_walltime();
  17. unsigned int seed = omp_get_thread_num();
  18. // initialize x
  19. #pragma omp parallel for schedule(runtime)
  20. for(int i = 0; i < 10000; i++)
  21. {
  22. x[i][0] = 1.0 * rand_r(&seed) / RAND_MAX;
  23. x[i][1] = 1.0 * rand_r(&seed) / RAND_MAX;
  24. x[i][2] = 1.0 * rand_r(&seed) / RAND_MAX;
  25. y[i][0] = y[i][1] = y[i][2] = 0;
  26. }
  27. for(int iter = 0; iter < 10; iter++)
  28. {
  29. #pragma omp parallel for schedule(runtime)
  30. for(int i = 0; i < 10000; i++)
  31. {
  32. for(int j = 0; j < 500; j++)
  33. {
  34. y[i][0] += sqrt(2 * dt)* (2.0 * rand_r(&seed) / RAND_MAX - 1);
  35. y[i][1] += sqrt(2 * dt)* (2.0 * rand_r(&seed) / RAND_MAX - 1);
  36. y[i][2] += sqrt(2 * dt)* (2.0 * rand_r(&seed) / RAND_MAX - 1);
  37. }
  38. }
  39. double avgdist = 0;
  40. #pragma omp parallel for reduction(+:avgdist) schedule(runtime)
  41. for(int i = 0; i < 10000; i++)
  42. {
  43. avgdist += sqrt( y[i][0]*y[i][0] + y[i][1]*y[i][1] + y[i][2]*y[i][2]);
  44. }
  45. avgdist /= 10000;
  46. printf("Average Distance: %lf, Average Distance/sqrt(Time): %lf\n", avgdist, avgdist/sqrt((iter+1)*500*dt));
  47. }
  48. runtime += get_walltime();
  49. printf("runtime: %lf\n\n", runtime);
  50. return 0;
  51. }

Makefile

  1. CC = cc
  2. CFLAGS = -g -O -Wall -fopenmp
  3. .PHONY:
  4. clean runhw3
  5. runhw3: hw3.c
  6. $(CC) $(CFLAGS) -o hw3 hw3.c -lm
  7. for nt in `seq 1 8`; do \
  8. OMP_SCHEDULE="guided,20" OMP_NUM_THREADS=$$nt ./hw3 $N; \
  9. done
  10. clean:
  11. $(RM) hw3

Graph of average distance moved from 0 to 5000 time steps

1.PNG-27.7kB


Table of the execution time for a few different loop scheduling options, and using an appropriate number of threads.

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
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注