-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgen.c
181 lines (137 loc) · 3.68 KB
/
pgen.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// pgen.c -- support and interface to primegen library with multi-threaded applications
//
// Usage:
// These functions perform the actual call to primegen_next, primegen_skipto,
// and primegen_init appropriately.
//
// Secondarily, as a caller 'posts' it's completion of the 'P' it previous
// retrieved, these routines perform the necessary housekeeping to properly
// update 'LastP' if appropriate and 'fold' any pending 'posts' which arrived before
// it into LastP as well. This eliminates the need to have a seperate rendezvous
// thread, which is itself a problem in async multi-threading.
//
// $RCSfile: pgen.c,v $
//
// $Date: 2013/12/02 19:16:57 $
//
// $Author: chuck $
//
// $Id: pgen.c,v 1.3 2013/12/02 19:16:57 chuck Exp $
//
//
// ChangeLog:
//
// $Log: pgen.c,v $
// Revision 1.3 2013/12/02 19:16:57 chuck
// added rcs_file_version identification string
//
// Revision 1.2 2013/11/30 17:55:50 chuck
// Wrapper for primegen library for use with pthreads. Primegen is single-threaded only
//
// Revision 1.1 2013/11/29 21:46:08 chuck
// Initial revision
//
//
static char *rcs_file_verson = "$Id: pgen.c,v 1.3 2013/12/02 19:16:57 chuck Exp $";
//
// Special 'internal' flag for pgen.h
#define _am_pgen_internals_h_
// global includes
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <pthread.h>
#include <inttypes.h>
// local includes
#include "limits.h"
#include "primegen.h"
#include "pgen.h"
// include Sieve.h for 'LastP' and 'DeltaP' ntegration only
// #include "sieve.h"
uint64_t LastP;
int DeltaP;
// pgeninit() - initialize the Pgen buffers for keeping track of 'NumThread' threads of P
//
PgenInit( Threads )
int Threads;
{
int i,j;
uint64_t *P;
// call primegen and init PG
primegen_init(&PG);
// validate the Number of threads requested (these are 'worker threads')
if (Threads < 1 || Threads > (MAXTHREADS-1)) return (-1);
// initialize the Pgen tide markers
for (i=0; i<MAXTHREADS; i++) Lowtide[i] = Hightide[i] = 0;
// Remember how many threads were initialized / activated
PgenNumThreads = Threads;
// return now many we created
return (Threads);
}
// pgenskipto() - Skip to the P specified
//
int PgenSkipto(uint64_t Skipto)
{
// Acquire the lock
pthread_mutex_lock(&PgenLock);
// Skip ahead
primegen_skipto(&PG, Skipto);
// release the lock
pthread_mutex_unlock(&PgenLock);
}
//
// PgenPause - Simply Pause all processing by pausing Pgen
//
int PgenPause()
{
// Acquire the lock
pthread_mutex_lock(&PgenLock);
}
//
// PgenResume() - Unlock Pgen so processing can resume
//
int PgenResume()
{
// Release the lock
pthread_mutex_unlock(&PgenLock);
}
// PgenDone() - Report this thread's P as done but do not ask for another
//
//
int PgenDone(int Tid)
{
// Move Hightide -> Lowtide
Lowtide[Tid] = Hightide[Tid];
// and return
return;
}
uint64_t PgenNext(int Tid)
{
// The P value we will return
register uint64_t P;
// Move HighTide -> LowTide
Lowtide[Tid] = Hightide[Tid];
// Acquire the mutex
pthread_mutex_lock(&PgenLock);
// Get and account for P;
Hightide[Tid] = P = primegen_next(&PG);
// Increment P-acquired counter
DeltaP++;
// Release the mutex and set pointers
pthread_mutex_unlock(&PgenLock);
//printf("Tid %d gets %d\n",Tid,P);
// and return
return (P);
}
//
// PgenLowtide -- Return the lowtide of all lowtides currently active
// (called by statsworker and savedata only)
uint64_t PgenLowtide()
{
uint64_t tide = SIEVELIMIT;
int i;
for (i=0; i<MAXTHREADS; i++) if (Lowtide[i] && (Lowtide[i] < tide)) tide = Lowtide[i];
LastP = tide;
return tide;
}