Advertisement
Guest User

Untitled

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