-
Notifications
You must be signed in to change notification settings - Fork 0
/
prime.c
77 lines (68 loc) · 2.37 KB
/
prime.c
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
#include <time.h>
#include "shared.h"
#include "prime.h"
/*
* result_t is_prime(uint32_t from, uint32_t to) (FOR DEMO PURPOSES)
* Takes two inputs: from and to
* uint32_t 'from' represents the lower limits of the numbers to be checked
* uint32_t 'to' represents the upper limits of the numbers to be checked
*
* When all numbers from variable 'from' to variable 'to has been checked, an int value of the
* counted primes is returned.
*/
result_t is_prime(uint32_t from, uint32_t to) {
int count = 0;
uint32_t number_to_check;
for (number_to_check = from; number_to_check <= to; number_to_check++)
{
uint32_t division;
int isprime = 1;
for (division = 1; division < number_to_check; division++)
{
if (division != 1 && division != number_to_check && number_to_check % division == 0)
isprime = 0;
}
if (isprime && number_to_check != 1){
/* printf("Prime: %d\n", number_to_check); */
count++;
}}
return count;
}
/*
* result_t weighted_prime_worker(weight_t weight, struct task new_task)
* Takes 2 inputs: weight and new_task
* weight_t weight represent the worker-nodes weight (How quick it is), higher is better. (Max 100)
* struct task new_task represents the task struct containing the lower and upper limit of the task to be calculated.
*
* Returns the prime count from the lower to upper limit.
*/
result_t weighted_prime_worker(weight_t weight, struct task new_task) {
struct timespec start_time;
struct timespec end_time;
clock_gettime(CLOCK_MONOTONIC, &start_time);
result_t prime_count = is_prime(new_task.from, new_task.to);
clock_gettime(CLOCK_MONOTONIC, &end_time);
double time_elapsed = timespec_to_double(end_time) - timespec_to_double(start_time);
if (weight < MAX_WEIGHT) {
double new_time = (MAX_WEIGHT/(double)weight) * time_elapsed;
double sleeptime = new_time - time_elapsed;
sleep(sleeptime);
}
return prime_count;
}
/* Custom sleep function for more varity
* void sleep(double msec)
* Takes 1 input: sec
* double msec represents the milliseconds that needs to elapse before the program returns
*/
void sleep(double sec) {
struct timespec start_time;
struct timespec current_time;
clock_gettime(CLOCK_MONOTONIC, &start_time);
double time_elapsed = 0.00;
do
{
clock_gettime(CLOCK_MONOTONIC, ¤t_time);
time_elapsed = timespec_to_double(current_time) - timespec_to_double(start_time);
} while ( time_elapsed < sec);
}