source: git/Singular/LIB/resources.lib @ fbc7cb

fieker-DuValspielwiese
Last change on this file since fbc7cb was 808192, checked in by Andreas Steenpass <steenpass@…>, 10 years ago
fix: initialize sem_cores correctly (cherry picked from commit fc412a66edca885f4769640fcfb4639995b20785) Signed-off-by: Andreas Steenpass <steenpass@mathematik.uni-kl.de>
  • Property mode set to 100644
File size: 4.4 KB
Line 
1////////////////////////////////////////////////////////////////////
2version="version resources.lib 4.0.0.0 Dec_2013 "; // $Id$
3category="General purpose";
4info="
5LIBRARY:   resources.lib  Tools to manage the computational resources
6
7AUTHOR:    Andreas Steenpass, e-mail: steenpass@mathematik.uni-kl.de
8
9OVERVIEW:
10The purpose of this library is to manage the computational resources of
11a Singular session. The library tasks.lib and any library build upon tasks.lib
12respect these settings, i.e. they will not use more computational resources
13than provided via resources.lib.
14
15The provided procedures and their implementation are currently quite simple.
16The library can be extended later on to support, e.g., distributed computations
17on several servers.
18
19KEYWORDS:  parallelization; distributed computing; semaphores
20
21SEE ALSO:  tasks_lib, parallel_lib
22
23PROCEDURES:
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 */
31static 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
39proc addcores(int n)
40"USAGE:   addcores(n), n int
41RETURN:   the adjusted number of available processor cores, after n has been
42          added to it. If n is negative, this number is reduced.
43NOTE:     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.
47SEE ALSO: setcores, getcores, tasks_lib, parallel_lib
48EXAMPLE:  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}
73example
74{
75    "EXAMPLE:";
76    echo = 2;
77    setcores(4);
78    addcores(-2);
79}
80
81proc setcores(int n)
82"USAGE:   setcores(n), n int
83RETURN:   n. The number of available processor cores is set to n and n is
84          returned.
85NOTE:     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.
89SEE ALSO: addcores, getcores, tasks_lib, parallel_lib
90EXAMPLE:  example setcores; shows an example"
91{
92    return(addcores(n-NCORES));
93}
94example
95{
96    "EXAMPLE:";
97    echo = 2;
98    setcores(2);
99    setcores(4);
100}
101
102proc getcores()
103"USAGE:   getcores(n), n int
104RETURN:   the number of available processor cores.
105NOTE:     This procedure should only be called in the main process of
106          a Singular session and not within any task defined via tasks.lib.
107SEE ALSO: addcores, setcores, tasks_lib, parallel_lib
108EXAMPLE:  example getcores; shows an example"
109{
110    return(NCORES);
111}
112example
113{
114    "EXAMPLE:";
115    echo = 2;
116    setcores(4);
117    getcores();
118}
119
120proc semaphore(int n)
121"USAGE:   semaphore(n), n int
122RETURN:   the index of a new semaphore initialized with n.
123EXAMPLE:  example semaphore; shows an example"
124{
125    int i = 0;
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}
134example
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 */
148static 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
156static proc resetcores_subtree(list oldvalues)
157{
158    sem_cores = oldvalues[1];
159    NCORES = oldvalues[2];
160}
161
Note: See TracBrowser for help on using the repository browser.