-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.lib
161 lines (145 loc) · 4.44 KB
/
resources.lib
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
////////////////////////////////////////////////////////////////////
version="version resources.lib 3-1-7-0 Dec_2013 ";
category="General purpose";
info="
LIBRARY: resources.lib Tools to manage the computational resources
AUTHOR: Andreas Steenpass, e-mail: [email protected]
OVERVIEW:
The purpose of this library is to manage the computational resources of
a Singular session. The library tasks.lib and any library build upon tasks.lib
respect these settings, i.e. they will not use more computational resources
than provided via resources.lib.
The provided procedures and their implementation are currently quite simple.
The library can be extended later on to support, e.g., distributed computations
on several servers.
KEYWORDS: parallelization; distributed computing; semaphores
SEE ALSO: tasks_lib, parallel_lib
PROCEDURES:
addcores(); add an integer to the number of available processor cores
setcores(); set the number of available processor cores
getcores(); get the number of available processor cores
semaphore(); initialize a new semaphore
";
/* initialize (lib-)global variables */
static proc mod_init()
{
int sem_cores = semaphore(system("--cpus")-1);
exportto(Resources, sem_cores);
int NCORES = system("semaphore", "get_value", sem_cores)+1;
exportto(Resources, NCORES);
}
proc addcores(int n)
"USAGE: addcores(n), n int
RETURN: the adjusted number of available processor cores, after n has been
added to it. If n is negative, this number is reduced.
NOTE: The number of available processor cores must be at least 1. Reducing
this number may take some time.
@* This procedure should only be called in the main process of a
Singular session and not within any task defined via tasks.lib.
SEE ALSO: setcores, getcores, tasks_lib, parallel_lib
EXAMPLE: example addcores; shows an example"
{
/* check for errors */
if (NCORES+n < 1) {
ERROR("The number of cores to use must be at least 1.");
}
/* change the value of the semaphore */
int i;
int tmp;
if (n >= 0) {
for (i = n; i > 0; i--) {
tmp = system("semaphore", "release", sem_cores);
}
}
else {
for (i = n; i < 0; i++) {
tmp = system("semaphore", "acquire", sem_cores);
}
}
/* adjust and return NCORES */
NCORES = NCORES+n;
return(NCORES);
}
example
{
"EXAMPLE:";
echo = 2;
setcores(4);
addcores(-2);
}
proc setcores(int n)
"USAGE: setcores(n), n int
RETURN: n. The number of available processor cores is set to n and n is
returned.
NOTE: The number of available processor cores must be at least 1. Reducing
this number may take some time.
@* This procedure should only be called in the main process of a
Singular session and not within any task defined via tasks.lib.
SEE ALSO: addcores, getcores, tasks_lib, parallel_lib
EXAMPLE: example setcores; shows an example"
{
return(addcores(n-NCORES));
}
example
{
"EXAMPLE:";
echo = 2;
setcores(2);
setcores(4);
}
proc getcores()
"USAGE: getcores(n), n int
RETURN: the number of available processor cores.
NOTE: This procedure should only be called in the main process of
a Singular session and not within any task defined via tasks.lib.
SEE ALSO: addcores, setcores, tasks_lib, parallel_lib
EXAMPLE: example getcores; shows an example"
{
return(NCORES);
}
example
{
"EXAMPLE:";
echo = 2;
setcores(4);
getcores();
}
proc semaphore(int n)
"USAGE: semaphore(n), n int
RETURN: the index of a new semaphore initialized with n.
EXAMPLE: example semaphore; shows an example"
{
int i = 0;
while (system("semaphore", "exists", i) == 1) {
i++;
}
if (system("semaphore", "init", i, n) != 1) {
ERROR("no more semphores");
}
return(i);
}
example
{
"EXAMPLE:";
echo = 2;
int sem = semaphore(1);
system("semaphore", "acquire", sem);
system("semaphore", "try_acquire", sem);
system("semaphore", "release", sem);
system("semaphore", "try_acquire", sem);
}
/* wrapper for the now obsolete optional parameter in parallel.lib for the
* number of processor cores
*/
static proc setcores_subtree(int n)
{
list oldvalues = list(sem_cores, NCORES);
sem_cores = semaphore(n-1);
NCORES = n;
return(oldvalues);
}
static proc resetcores_subtree(list oldvalues)
{
sem_cores = oldvalues[1];
NCORES = oldvalues[2];
}