-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment_2.cpp
More file actions
140 lines (98 loc) · 3.1 KB
/
Assignment_2.cpp
File metadata and controls
140 lines (98 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Assignment_2.cpp : Defines the entry point for the console application.
//
#include<iostream>
#include<atomic>
#include<chrono>
#include <vector>
#include <thread>
#include"prime.h"
#include<random>
#include<algorithm>
std::mutex mutex;
prime task;
void print_(int task, std::thread::id this_id);
int random_();
void worker_farm(prime& tq,int delay);
int main(int argc, char * argv[])
{
if (argc == 1) // means sequential version
{
std::cout << "Usage is: " << argv[0] << " tasks workers delay " << std::endl;
return(0);
}
int n = atoi(argv[1]);
int nw = atoi(argv[2]);
int delay = atoi(argv[3]);
/********************* EMITTER STAGE START ****************************/
/*Creates 2 arrays . One for holding the random tasks calculated and one holds the number of primes from 1-N . Where N is a random task*/
task.create(n);
/*Generate random integers and push them into a queue of integers*/
for (size_t i = 0; i < n; i++)
{
task.add_task(random_(),i);
}
/********************* EMITTER STAGE ENDS ****************************/
/* Get the time before setting the parallel environment */
std::vector<std::thread> tid;
auto t1 = std::chrono::high_resolution_clock::now();
/********************* WORKER STAGE STARTS ****************************/
/* Start the threads where each thread works on a task */
for (int i = 0; i<nw; i++)
{
tid.push_back(std::thread(worker_farm, std::ref(task),delay));
}
/* wait for the threads to sychronize */
for (int i = 0; i<nw; i++)
tid[i].join();
/********************* WORKER STAGE ENDS ****************************/
/********************* COLLECTOR STAGE STARTS ****************************/
/*output the results*/
//task.get_result();
/********************* COLLECTOR STAGE ENDS ****************************/
/*get the time after the excecution of the parallel tasks*/
auto t2 = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = t2 - t1;
std::cout << "\nTOTAL TIME ELASPED : " << elapsed.count() << " Seconds" <<std::endl;
return 0;
}
void worker_farm(prime& tq,int delay)
{
std::atomic<int> Reduce = { 0 };
/* each worker computes the number of tasks given*/
std::atomic<int> tasks = { 0 };
std::thread::id this_id = std::this_thread::get_id();
/*passive waiting for tasks to be taken*/
while (true)
{
/* pop a task form the queue */
int task = tq.assign_task();
/* if the worker gets a valid task */
if (task > 0)
{
/* increment the number of tasks recieved */
tasks++;
Reduce = tq.getPrime(task,delay);
}
else
{
/* queue is empty . No more tasks*/
break;
}
}
print_(tasks, this_id);
}
void print_(int task, std::thread::id this_id)
{
std::unique_lock<std::mutex> lck(mutex);
std::cout << "TOTAL TASKS RECIEVED BY THREAD : " << this_id << " : " << task << std::endl;
}
int random_()
{
std::random_device rd;
// Initialize Mersenne Twister pseudo-random number generator
std::mt19937 gen(rd());
// Generate pseudo-random numbers
// uniformly distributed in range (1, 1000)
std::uniform_int_distribution<> dis(1, 1000);
return dis(gen);
}