Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <process.h>
  4. #include <vector>
  5. #include <ctime>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. typedef unsigned long long element_t;
  11. typedef element_t count_t;
  12.  
  13. count_t count;
  14.  
  15. element_t N;
  16.  
  17. const int BATCH_SIZE = (1 << 20) - 1;
  18.  
  19. bool prime(element_t n) {
  20. if (n < 2) {
  21. return false;
  22. }
  23. for (element_t i = 2, end = (element_t) sqrt(n) + 1; i < end; i++) {
  24. if (n % i == 0) {
  25. return false;
  26. }
  27. }
  28. return true;
  29. }
  30.  
  31. struct arg_list_t {
  32. element_t from;
  33. element_t to;
  34.  
  35. arg_list_t(element_t f, element_t t)
  36. : from(f), to(t) { }
  37. };
  38.  
  39. unsigned int WINAPI thread_func(void *params) {
  40. arg_list_t *arg_list = static_cast<arg_list_t *>(params);
  41. element_t from = arg_list->from, to = arg_list->to;
  42. for (element_t i = from; i < to; i++) {
  43. if (prime(i)) {
  44. InterlockedIncrement(&count);
  45. }
  46. }
  47. _endthreadex(0);
  48. return 0;
  49. }
  50.  
  51. int main(void) {
  52. cout << "Enter the value of N: ";
  53. cin >> N;
  54.  
  55. vector<arg_list_t> arg_lists;
  56. vector<HANDLE> handles;
  57. vector<unsigned int> thread_ids;
  58.  
  59. cout << "Windows API" << endl;
  60. cout << "[INFO] Calculating ..." << endl;
  61.  
  62. clock_t start = clock();
  63. element_t from = 2, to = min(N, from + BATCH_SIZE) + 1;
  64. while (from < N) {
  65. unsigned int thread_id;
  66. arg_lists.push_back({from, to});
  67. handles.push_back((HANDLE) _beginthreadex(
  68. NULL, 0, thread_func, (void *) &arg_lists.back(), 0, &thread_id));
  69. thread_ids.push_back(thread_id);
  70. from = to;
  71. to = min(N, from + BATCH_SIZE) + 1;
  72. }
  73. WaitForMultipleObjects(handles.size(), &handles[0], TRUE, INFINITE);
  74. clock_t end = clock();
  75.  
  76. cout << "[INFO] Done! Total time: "
  77. << static_cast<double>(end - start) / CLOCKS_PER_SEC
  78. << " second(s)" << endl;
  79. cout << "There are " << count
  80. << " prime(s) in the range [1, " << N << "]" << endl;
  81.  
  82. return 0;
  83. }
  84.  
  85. // ...
  86. vector<arg_list_t *> arg_lists;
  87. while (from < N) {
  88. unsigned int thread_id;
  89. arg_list_t *arg_list = new arg_list_t(from, to);
  90. arg_lists.push_back(arg_list);
  91. handles.push_back((HANDLE) _beginthreadex(
  92. NULL, 0, thread_func, (void *) arg_list, 0, &thread_id));
  93. // ...
  94.  
  95. // POSIX edition
  96.  
  97. #include <stdio.h>
  98. #include <pthread.h>
  99. #include <math.h>
  100. #include <stdlib.h>
  101.  
  102. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  103.  
  104. typedef int bool;
  105. #define TRUE 1
  106. #define FALSE 0
  107.  
  108. typedef unsigned long long element_t;
  109. typedef element_t count_t;
  110.  
  111. const int BATCH_SIZE = (1 << 20) - 1;
  112.  
  113. count_t count = 0;
  114. element_t N;
  115.  
  116. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  117.  
  118. bool prime(element_t n) {
  119. if (n < 2) {
  120. return FALSE;
  121. }
  122. for (element_t i = 2, end = (element_t) sqrt(n) + 1; i < end; i++) {
  123. if (n % i == 0) {
  124. return FALSE;
  125. }
  126. }
  127. return TRUE;
  128. }
  129.  
  130. struct arg_list_t {
  131. element_t from;
  132. element_t to;
  133. };
  134.  
  135. void *thread_start(void *args) {
  136. struct arg_list_t *arg_list = (struct arg_list_t *) args;
  137. element_t from = arg_list->from, to = arg_list->to;
  138. for (element_t i = from; i < to; i++) {
  139. if (prime(i)) {
  140. pthread_mutex_lock(&mutex);
  141. count++;
  142. pthread_mutex_unlock(&mutex);
  143. }
  144. }
  145. return NULL;
  146. }
  147.  
  148. int main(void) {
  149. printf("Enter the value of N: ");
  150. scanf("%llu", &N);
  151.  
  152. size_t capacity = N / BATCH_SIZE + 1;
  153. struct arg_list_t *arg_lists = (struct arg_list_t *) malloc(
  154. capacity * sizeof(struct arg_list_t));
  155. pthread_t *thread_ids = (pthread_t *) malloc(capacity * sizeof(pthread_t));
  156.  
  157. printf("POSIXn");
  158. printf("[INFO] Calculating ...n");
  159.  
  160. pthread_t tid;
  161. size_t len = 0;
  162. clock_t start = clock();
  163. element_t from = 2, to = MIN(N, from + BATCH_SIZE) + 1;
  164. while (from <= N) {
  165. arg_lists[len].from = from;
  166. arg_lists[len].to = to;
  167. if (pthread_create(
  168. &tid, NULL, thread_start, (void *) &arg_lists[len])) {
  169. perror("Thread creation failedn");
  170. goto EXIT;
  171. }
  172. thread_ids[len++] = tid;
  173. from = to;
  174. to = MIN(N, from + BATCH_SIZE) + 1;
  175. }
  176. for (size_t i = 0; i < len; i++) {
  177. pthread_join(thread_ids[i], NULL);
  178. }
  179. clock_t end = clock();
  180.  
  181. printf("[INFO] Done! Total time: %lf secondsn",
  182. (double) (end - start) / CLOCKS_PER_SEC);
  183. printf("There are %llu prime(s) in range [1, %llu]n", count, N);
  184.  
  185. EXIT:
  186. free(thread_ids);
  187. free(arg_lists);
  188. pthread_mutex_destroy(&mutex);
  189.  
  190. return 0;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement