Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.64 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <omp.h>
  6. #include <sys/time.h>
  7.  
  8. #define PACKET_COUNT 1000
  9. #define RESOLUTION 10000000
  10.  
  11. typedef struct {
  12. void *data;} t_data;
  13.  
  14. typedef struct {
  15. void *result;
  16. } t_result;
  17.  
  18. double dtime()
  19. {
  20. double tseconds = 0.0;
  21. struct timeval mytime;
  22. gettimeofday(&mytime,(struct timezone*)0);
  23. tseconds = (double)(mytime.tv_sec +mytime.tv_usec*1.0e-6);
  24. return( tseconds );
  25. }
  26.  
  27. double f(double x) { // the function to be integrated
  28. return sin(x)*sin(x)/x;
  29. }
  30.  
  31. void process(t_data data,t_result result) {
  32. double integrate=0;
  33. double r_length=(r_b-r_a)/RESOLUTION;
  34. double x=r_a;
  35. int i;
  36.  
  37. // a simple implementation of the integrate
  38. for(i=0;i<RESOLUTION;i++) {
  39. integrate+=r_length*f(x);
  40. x+=r_length;
  41. }
  42.  
  43. *((double *)result.result)=integrate; // store the result
  44. }
  45.  
  46. t_result *allocate_results(int *packet_count) {
  47. int i;
  48. // prepare space for results
  49. t_result *r_results=(t_result *)malloc(sizeof(t_result)*PACKET_COUNT);
  50. if (r_results==NULL) {
  51. perror("Not enough memory");
  52. exit(-1);
  53. }
  54. for(i=0;i<PACKET_COUNT;i++) {
  55. r_results[i].result=malloc(sizeof(double));
  56. if (r_results[i].result==NULL) {
  57. perror("Not enough memory");
  58. exit(-1);
  59. }
  60. }
  61.  
  62. *packet_count=PACKET_COUNT;
  63. return r_results;
  64. }
  65.  
  66. t_data *generate_data(int *packet_count) {
  67. // prepare the input data
  68. // i.e. the given range is to be divided into packets
  69. int i;
  70. double a=1,b=100000000;
  71. double packet_size=(b-a)/PACKET_COUNT;
  72. t_data *p_packets;
  73. double *p_data;
  74. double r_a,r_b;
  75.  
  76. // prepare PACKET_COUNT number of packets
  77. t_data *packets=(t_data *)malloc(sizeof(t_data)*PACKET_COUNT);
  78. if (packets==NULL) {
  79. perror("Not enough memory");
  80. exit(-1);
  81. }
  82. }
  83.  
  84. *((double *)result.result)=integrate; // store the result
  85. }
  86.  
  87. t_result *allocate_results(int *packet_count) {
  88. int i;
  89. // prepare space for results
  90. t_result *r_results=(t_result *)malloc(sizeof(t_result)*PACKET_COUNT);
  91. if (r_results==NULL) {
  92. perror("Not enough memory");
  93. exit(-1);
  94. }
  95. for(i=0;i<PACKET_COUNT;i++) {
  96. r_results[i].result=malloc(sizeof(double));
  97. if (r_results[i].result==NULL) {
  98. perror("Not enough memory");
  99. exit(-1);
  100. }
  101. }
  102.  
  103. *packet_count=PACKET_COUNT;
  104. return r_results;
  105. }
  106.  
  107. t_data *generate_data(int *packet_count) {
  108. // prepare the input data
  109. // i.e. the given range is to be divided into packets
  110. int i;
  111. double a=1,b=100000000;
  112. double packet_size=(b-a)/PACKET_COUNT;
  113. t_data *p_packets;
  114. double *p_data;
  115. double r_a,r_b;
  116.  
  117. // prepare PACKET_COUNT number of packets
  118. t_data *packets=(t_data *)malloc(sizeof(t_data)*PACKET_COUNT);
  119. if (packets==NULL) {
  120. perror("Not enough memory");
  121. exit(-1);
  122. }
  123.  
  124. r_a=a;
  125. r_b=a+packet_size;
  126. p_packets=packets; // pointer to the beginning of the packets
  127.  
  128. for(i=0;i<PACKET_COUNT;i++) {
  129. packets[i].data=malloc(2*sizeof(double));
  130. if (packets[i].data==NULL) {
  131. perror("Not enough memory");
  132. exit(-1);
  133. }
  134.  
  135. // populate the packet with the data
  136. p_data=(double *)packets[i].data;
  137. *p_data=r_a;
  138. *(p_data+1)=r_b;r_a+=packet_size;
  139. r_b+=packet_size;
  140. }
  141.  
  142. *packet_count=PACKET_COUNT;
  143. return packets;
  144. }
  145.  
  146. t_data *data;
  147. t_result *results;
  148.  
  149. main(int argc,char **argv) {
  150. // prepare the input data
  151. // i.e. the given range is to be divided into packets
  152. // now the main processing loop using OpenMP
  153. int counter;
  154. int my_data;
  155. double result;
  156. int i;
  157. int packet_count;
  158. int results_count;
  159. double t_start,t_stop,t_total,t_current,t_min;
  160. int t_counter=0;
  161. t_min=100000000;
  162.  
  163. do {
  164. // generate input data
  165.  
  166. r_a=a;
  167. r_b=a+packet_size;
  168. p_packets=packets; // pointer to the beginning of the packets
  169.  
  170. for(i=0;i<PACKET_COUNT;i++) {
  171. packets[i].data=malloc(2*sizeof(double));
  172. if (packets[i].data==NULL) {
  173. perror("Not enough memory");
  174. exit(-1);
  175. }
  176.  
  177. // populate the packet with the data
  178. p_data=(double *)packets[i].data;
  179. *p_data=r_a;
  180. *(p_data+1)=r_b;r_a+=packet_size;
  181. r_b+=packet_size;
  182. }
  183.  
  184. *packet_count=PACKET_COUNT;
  185. return packets;
  186. }
  187.  
  188. t_data *data;
  189. t_result *results;
  190.  
  191. main(int argc,char **argv) {
  192. // prepare the input data
  193. // i.e. the given range is to be divided into packets
  194. // now the main processing loop using OpenMP
  195. int counter;
  196. int my_data;
  197. double result;
  198. int i;
  199. int packet_count;
  200. int results_count;
  201. double t_start,t_stop,t_total,t_current,t_min;
  202. int t_counter=0;
  203. t_min=100000000;
  204.  
  205. do {
  206. // generate input data
  207. data=generate_data(&packet_count);
  208. // allocate memory for results
  209. results=allocate_results(&results_count);
  210. counter=0;
  211. t_start=dtime();
  212. // launch threads in parallel – the number will be set using an environment variable
  213. #pragma omp parallel private(my_data) shared(counter){
  214.  
  215. do {
  216. // each thread will try to get its data from the available list - synchronization is needed
  217. #pragma omp critical
  218. {
  219. my_data=counter;
  220. counter++; // also write result to the counter
  221. }
  222.  
  223. // process and store result -- this can be done without synchronization
  224. if (my_data<PACKET_COUNT)
  225. process(data[my_data],results[my_data]); // note that processing
  226.  
  227. // may take various times for various data packets
  228. } while (counter<PACKET_COUNT); // otherwise simply exit because
  229.  
  230. // there are no more data packets to process
  231. }
  232.  
  233. t_stop=dtime(); // stop benchmarking
  234. #pragma omp barrier
  235. // now just add results
  236. result=0;
  237.  
  238. for(i=0;i<PACKET_COUNT;i++) {
  239. result+=*((double *)results[i].result);
  240. }
  241. t_counter++;
  242. t_current=t_stop-t_start;
  243.  
  244. if (t_current<t_min)
  245. t_min=t_current;
  246.  
  247. // repeat a few times if the total execution time is short!
  248.  
  249. // generate input data
  250. data=generate_data(&packet_count);
  251. // allocate memory for results
  252. results=allocate_results(&results_count);
  253. counter=0;
  254. t_start=dtime();
  255. // launch threads in parallel – the number will be set using an environment variable
  256. #pragma omp parallel private(my_data) shared(counter){
  257.  
  258. do {
  259. // each thread will try to get its data from the available list - synchronization is needed
  260. #pragma omp critical
  261. {
  262. my_data=counter;
  263. counter++; // also write result to the counter
  264. }
  265.  
  266. // process and store result -- this can be done without synchronization
  267. if (my_data<PACKET_COUNT)
  268. process(data[my_data],results[my_data]); // note that processing
  269.  
  270. // may take various times for various data packets
  271. } while (counter<PACKET_COUNT); // otherwise simply exit because
  272.  
  273. // there are no more data packets to process
  274. }
  275.  
  276. t_stop=dtime(); // stop benchmarking
  277. #pragma omp barrier
  278. // now just add results
  279. result=0;
  280.  
  281. for(i=0;i<PACKET_COUNT;i++) {
  282. result+=*((double *)results[i].result);
  283. }
  284. t_counter++;
  285. t_current=t_stop-t_start;
  286.  
  287. if (t_current<t_min)
  288. t_min=t_current;
  289.  
  290. // repeat a few times if the total execution time is short!
  291. } while ((t_counter<4) && (t_current<100));
  292.  
  293. printf("\nFinished");
  294. printf("\nThe total value of the integrate is %.5f\n",result);
  295. printf("\nTotal time elapsed=%.8f\n",t_min);
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement