1 | //////////////////////////////////////////////////////////////////// |
---|
2 | version="version resources.lib 4.0.0.0 Dec_2013 "; // $Id$ |
---|
3 | category="General purpose"; |
---|
4 | info=" |
---|
5 | LIBRARY: resources.lib Tools to manage the computational resources |
---|
6 | |
---|
7 | AUTHOR: Andreas Steenpass, e-mail: steenpass@mathematik.uni-kl.de |
---|
8 | |
---|
9 | OVERVIEW: |
---|
10 | The purpose of this library is to manage the computational resources of |
---|
11 | a Singular session. The library tasks.lib and any library build upon tasks.lib |
---|
12 | respect these settings, i.e. they will not use more computational resources |
---|
13 | than provided via resources.lib. |
---|
14 | |
---|
15 | The provided procedures and their implementation are currently quite simple. |
---|
16 | The library can be extended later on to support, e.g., distributed computations |
---|
17 | on several servers. |
---|
18 | |
---|
19 | KEYWORDS: parallelization; distributed computing; semaphores |
---|
20 | |
---|
21 | SEE ALSO: tasks_lib, parallel_lib |
---|
22 | |
---|
23 | PROCEDURES: |
---|
24 | addcores(); add an integer to the number of available processor cores |
---|
25 | setcores(); set the number of available processor cores |
---|
26 | getcores(); get the number of available processor cores |
---|
27 | semaphore(); initialize a new semaphore |
---|
28 | "; |
---|
29 | |
---|
30 | /* initialize (lib-)global variables */ |
---|
31 | static proc mod_init() |
---|
32 | { |
---|
33 | int sem_cores = semaphore(system("--cpus"))-1; |
---|
34 | exportto(Resources, sem_cores); |
---|
35 | int NCORES = system("semaphore", "get_value", sem_cores)+1; |
---|
36 | exportto(Resources, NCORES); |
---|
37 | } |
---|
38 | |
---|
39 | proc addcores(int n) |
---|
40 | "USAGE: addcores(n), n int |
---|
41 | RETURN: the adjusted number of available processor cores, after n has been |
---|
42 | added to it. If n is negative, this number is reduced. |
---|
43 | NOTE: The number of available processor cores must be at least 1. Reducing |
---|
44 | this number may take some time. |
---|
45 | @* This procedure should only be called in the main process of a |
---|
46 | Singular session and not within any task defined via tasks.lib. |
---|
47 | SEE ALSO: setcores, getcores, tasks_lib, parallel_lib |
---|
48 | EXAMPLE: example addcores; shows an example" |
---|
49 | { |
---|
50 | /* check for errors */ |
---|
51 | if (NCORES+n < 1) { |
---|
52 | ERROR("The number of cores to use must be at least 1."); |
---|
53 | } |
---|
54 | |
---|
55 | /* change the value of the semaphore */ |
---|
56 | int i; |
---|
57 | int tmp; |
---|
58 | if (n >= 0) { |
---|
59 | for (i = n; i > 0; i--) { |
---|
60 | tmp = system("semaphore", "release", sem_cores); |
---|
61 | } |
---|
62 | } |
---|
63 | else { |
---|
64 | for (i = n; i < 0; i++) { |
---|
65 | tmp = system("semaphore", "acquire", sem_cores); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | /* adjust and return NCORES */ |
---|
70 | NCORES = NCORES+n; |
---|
71 | return(NCORES); |
---|
72 | } |
---|
73 | example |
---|
74 | { |
---|
75 | "EXAMPLE:"; |
---|
76 | echo = 2; |
---|
77 | setcores(4); |
---|
78 | addcores(-2); |
---|
79 | } |
---|
80 | |
---|
81 | proc setcores(int n) |
---|
82 | "USAGE: setcores(n), n int |
---|
83 | RETURN: n. The number of available processor cores is set to n and n is |
---|
84 | returned. |
---|
85 | NOTE: The number of available processor cores must be at least 1. Reducing |
---|
86 | this number may take some time. |
---|
87 | @* This procedure should only be called in the main process of a |
---|
88 | Singular session and not within any task defined via tasks.lib. |
---|
89 | SEE ALSO: addcores, getcores, tasks_lib, parallel_lib |
---|
90 | EXAMPLE: example setcores; shows an example" |
---|
91 | { |
---|
92 | return(addcores(n-NCORES)); |
---|
93 | } |
---|
94 | example |
---|
95 | { |
---|
96 | "EXAMPLE:"; |
---|
97 | echo = 2; |
---|
98 | setcores(2); |
---|
99 | setcores(4); |
---|
100 | } |
---|
101 | |
---|
102 | proc getcores() |
---|
103 | "USAGE: getcores(n), n int |
---|
104 | RETURN: the number of available processor cores. |
---|
105 | NOTE: This procedure should only be called in the main process of |
---|
106 | a Singular session and not within any task defined via tasks.lib. |
---|
107 | SEE ALSO: addcores, setcores, tasks_lib, parallel_lib |
---|
108 | EXAMPLE: example getcores; shows an example" |
---|
109 | { |
---|
110 | return(NCORES); |
---|
111 | } |
---|
112 | example |
---|
113 | { |
---|
114 | "EXAMPLE:"; |
---|
115 | echo = 2; |
---|
116 | setcores(4); |
---|
117 | getcores(); |
---|
118 | } |
---|
119 | |
---|
120 | proc semaphore(int n) |
---|
121 | "USAGE: semaphore(n), n int |
---|
122 | RETURN: the index of a new semaphore initialized with n. |
---|
123 | EXAMPLE: example semaphore; shows an example" |
---|
124 | { |
---|
125 | int i = 1; |
---|
126 | while (system("semaphore", "exists", i) == 1) { |
---|
127 | i++; |
---|
128 | } |
---|
129 | if (system("semaphore", "init", i, n) != 1) { |
---|
130 | ERROR("no more semphores"); |
---|
131 | } |
---|
132 | return(i); |
---|
133 | } |
---|
134 | example |
---|
135 | { |
---|
136 | "EXAMPLE:"; |
---|
137 | echo = 2; |
---|
138 | int sem = semaphore(1); |
---|
139 | system("semaphore", "acquire", sem); |
---|
140 | system("semaphore", "try_acquire", sem); |
---|
141 | system("semaphore", "release", sem); |
---|
142 | system("semaphore", "try_acquire", sem); |
---|
143 | } |
---|
144 | |
---|
145 | /* wrapper for the now obsolete optional parameter in parallel.lib for the |
---|
146 | * number of processor cores |
---|
147 | */ |
---|
148 | static proc setcores_subtree(int n) |
---|
149 | { |
---|
150 | list oldvalues = list(sem_cores, NCORES); |
---|
151 | sem_cores = semaphore(n-1); |
---|
152 | NCORES = n; |
---|
153 | return(oldvalues); |
---|
154 | } |
---|
155 | |
---|
156 | static proc resetcores_subtree(list oldvalues) |
---|
157 | { |
---|
158 | sem_cores = oldvalues[1]; |
---|
159 | NCORES = oldvalues[2]; |
---|
160 | } |
---|
161 | |
---|