Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <process.h>
- #include <vector>
- #include <ctime>
- #include <cmath>
- using namespace std;
- typedef unsigned long long element_t;
- typedef element_t count_t;
- count_t count;
- element_t N;
- const int BATCH_SIZE = (1 << 20) - 1;
- bool prime(element_t n) {
- if (n < 2) {
- return false;
- }
- for (element_t i = 2, end = (element_t) sqrt(n) + 1; i < end; i++) {
- if (n % i == 0) {
- return false;
- }
- }
- return true;
- }
- struct arg_list_t {
- element_t from;
- element_t to;
- arg_list_t(element_t f, element_t t)
- : from(f), to(t) { }
- };
- unsigned int WINAPI thread_func(void *params) {
- arg_list_t *arg_list = static_cast<arg_list_t *>(params);
- element_t from = arg_list->from, to = arg_list->to;
- for (element_t i = from; i < to; i++) {
- if (prime(i)) {
- InterlockedIncrement(&count);
- }
- }
- _endthreadex(0);
- return 0;
- }
- int main(void) {
- cout << "Enter the value of N: ";
- cin >> N;
- vector<arg_list_t> arg_lists;
- vector<HANDLE> handles;
- vector<unsigned int> thread_ids;
- cout << "Windows API" << endl;
- cout << "[INFO] Calculating ..." << endl;
- clock_t start = clock();
- element_t from = 2, to = min(N, from + BATCH_SIZE) + 1;
- while (from < N) {
- unsigned int thread_id;
- arg_lists.push_back({from, to});
- handles.push_back((HANDLE) _beginthreadex(
- NULL, 0, thread_func, (void *) &arg_lists.back(), 0, &thread_id));
- thread_ids.push_back(thread_id);
- from = to;
- to = min(N, from + BATCH_SIZE) + 1;
- }
- WaitForMultipleObjects(handles.size(), &handles[0], TRUE, INFINITE);
- clock_t end = clock();
- cout << "[INFO] Done! Total time: "
- << static_cast<double>(end - start) / CLOCKS_PER_SEC
- << " second(s)" << endl;
- cout << "There are " << count
- << " prime(s) in the range [1, " << N << "]" << endl;
- return 0;
- }
- // ...
- vector<arg_list_t *> arg_lists;
- while (from < N) {
- unsigned int thread_id;
- arg_list_t *arg_list = new arg_list_t(from, to);
- arg_lists.push_back(arg_list);
- handles.push_back((HANDLE) _beginthreadex(
- NULL, 0, thread_func, (void *) arg_list, 0, &thread_id));
- // ...
- // POSIX edition
- #include <stdio.h>
- #include <pthread.h>
- #include <math.h>
- #include <stdlib.h>
- #define MIN(a, b) ((a) < (b) ? (a) : (b))
- typedef int bool;
- #define TRUE 1
- #define FALSE 0
- typedef unsigned long long element_t;
- typedef element_t count_t;
- const int BATCH_SIZE = (1 << 20) - 1;
- count_t count = 0;
- element_t N;
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- bool prime(element_t n) {
- if (n < 2) {
- return FALSE;
- }
- for (element_t i = 2, end = (element_t) sqrt(n) + 1; i < end; i++) {
- if (n % i == 0) {
- return FALSE;
- }
- }
- return TRUE;
- }
- struct arg_list_t {
- element_t from;
- element_t to;
- };
- void *thread_start(void *args) {
- struct arg_list_t *arg_list = (struct arg_list_t *) args;
- element_t from = arg_list->from, to = arg_list->to;
- for (element_t i = from; i < to; i++) {
- if (prime(i)) {
- pthread_mutex_lock(&mutex);
- count++;
- pthread_mutex_unlock(&mutex);
- }
- }
- return NULL;
- }
- int main(void) {
- printf("Enter the value of N: ");
- scanf("%llu", &N);
- size_t capacity = N / BATCH_SIZE + 1;
- struct arg_list_t *arg_lists = (struct arg_list_t *) malloc(
- capacity * sizeof(struct arg_list_t));
- pthread_t *thread_ids = (pthread_t *) malloc(capacity * sizeof(pthread_t));
- printf("POSIXn");
- printf("[INFO] Calculating ...n");
- pthread_t tid;
- size_t len = 0;
- clock_t start = clock();
- element_t from = 2, to = MIN(N, from + BATCH_SIZE) + 1;
- while (from <= N) {
- arg_lists[len].from = from;
- arg_lists[len].to = to;
- if (pthread_create(
- &tid, NULL, thread_start, (void *) &arg_lists[len])) {
- perror("Thread creation failedn");
- goto EXIT;
- }
- thread_ids[len++] = tid;
- from = to;
- to = MIN(N, from + BATCH_SIZE) + 1;
- }
- for (size_t i = 0; i < len; i++) {
- pthread_join(thread_ids[i], NULL);
- }
- clock_t end = clock();
- printf("[INFO] Done! Total time: %lf secondsn",
- (double) (end - start) / CLOCKS_PER_SEC);
- printf("There are %llu prime(s) in range [1, %llu]n", count, N);
- EXIT:
- free(thread_ids);
- free(arg_lists);
- pthread_mutex_destroy(&mutex);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement