elpa_index.c 9.93 KB
Newer Older
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
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
//    This file is part of ELPA.
//
//    The ELPA library was originally created by the ELPA consortium,
//    consisting of the following organizations:
//
//    - Max Planck Computing and Data Facility (MPCDF), formerly known as
//      Rechenzentrum Garching der Max-Planck-Gesellschaft (RZG),
//    - Bergische Universität Wuppertal, Lehrstuhl für angewandte
//      Informatik,
//    - Technische Universität München, Lehrstuhl für Informatik mit
//      Schwerpunkt Wissenschaftliches Rechnen ,
//    - Fritz-Haber-Institut, Berlin, Abt. Theorie,
//    - Max-Plack-Institut für Mathematik in den Naturwissenschaften,
//      Leipzig, Abt. Komplexe Strukutren in Biologie und Kognition,
//      and
//    - IBM Deutschland GmbH
//
//    This particular source code file contains additions, changes and
//    enhancements authored by Intel Corporation which is not part of
//    the ELPA consortium.
//
//    More information can be found here:
//    http://elpa.mpcdf.mpg.de/
//
//    ELPA is free software: you can redistribute it and/or modify
//    it under the terms of the version 3 of the license of the
//    GNU Lesser General Public License as published by the Free
//    Software Foundation.
//
//    ELPA is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with ELPA.  If not, see <http://www.gnu.org/licenses/>
//
//    ELPA reflects a substantial effort on the part of the original
//    ELPA consortium, and we ask you to respect the spirit of the
//    license that we chose: i.e., please contribute any changes you
//    may have back to the original ELPA library distribution, and keep
//    any derivatives of ELPA under the same license that we chose for
//    the original distribution, the GNU Lesser General Public License.
//
//    Authors: L. Huedepohl and A. Marek, MPCDF

#include "elpa_index.h"
48
49
50
51
52
#include "elpa_constants_private.h"

elpa_index_t elpa_index_allocate(
                int n_int_entries, elpa_index_int_entry_t int_entries[n_int_entries],
                int n_double_entries, elpa_index_double_entry_t double_entries[n_double_entries]) {
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
53
54
55
56

        elpa_index_t index = (elpa_index_t) calloc(1, sizeof(struct elpa_index_struct));

        /* Integer entries */
57
        index->n_int_entries = n_int_entries;
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
58
        index->int_values = (int*) calloc(index->n_int_entries, sizeof(int));
59
        index->int_value_is_set = (int*) calloc(index->n_int_entries, sizeof(int));
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
60
61
62
63
64
        index->int_entries = int_entries;
        for (int i = 0; i < index->n_int_entries; i++) {
                index->int_values[i] = int_entries[i].default_value;
        }

65
66
67
68
69
70
71
72
73
        /* Double entries */
        index->n_double_entries = n_double_entries;
        index->double_values = (double*) calloc(index->n_double_entries, sizeof(double));
        index->double_value_is_set = (int*) calloc(index->n_int_entries, sizeof(int));
        index->double_entries = double_entries;
        for (int i = 0; i < index->n_double_entries; i++) {
                index->double_values[i] = 0.0;
        }

Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
74
75
76
        return index;
}

77
void elpa_index_free(elpa_index_t index) {
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
78
        free(index->int_values);
79
80
81
        free(index->int_value_is_set);
        free(index->double_values);
        free(index->double_value_is_set);
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
82
83
84
85
86
        free(index);
}

static int compar(const void *key, const void *member) {
        const char *name = (const char *) key;
87
        elpa_index_int_entry_t *entry = (elpa_index_int_entry_t *) member;
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101

        int l1 = strlen(entry->name);
        int l2 = strlen(name);
        if (l1 != l2) {
                return 1;
        }
        if (strncmp(name, entry->name, l1) == 0) {
                return 0;
        } else {
                return 1;
        }
}

static int find_int_entry(elpa_index_t index, const char *name) {
102
        elpa_index_int_entry_t *entry;
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
103
        size_t nmembers = index->n_int_entries;
104
        entry = lfind((const void*) name, (const void *) index->int_entries, &nmembers, sizeof(elpa_index_int_entry_t), compar);
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
105
106
107
108
109
110
111
        if (entry) {
                return (entry - &index->int_entries[0]);
        } else {
                return -1;
        }
}

112
int elpa_index_get_int_value(elpa_index_t index, const char *name, int *success) {
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
113
114
115
116
117
        int n = find_int_entry(index, name);
        if (n >= 0) {
                if (success != NULL) {
                        *success = ELPA_OK;
                }
118
                 return index->int_values[n];
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
119
120
121
122
        } else {
                if (success != NULL) {
                        *success = ELPA_ERROR;
                } else {
123
                        fprintf(stderr, "ELPA: No such entry '%s' and you did not check for errors, returning -1!\n", name);
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
124
                }
125
                return -1;
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
126
127
128
        }
}

129
130
131
132
133
134
135
136
137
138
int* elpa_index_get_int_loc(elpa_index_t index, const char *name) {
        int n = find_int_entry(index, name);
        if (n >= 0) {
                return &index->int_values[n];
        } else {
                return NULL;
        }
}

int elpa_index_set_int_value(elpa_index_t index, const char *name, int value) {
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
139
140
141
        int n = find_int_entry(index, name);
        int res = ELPA_ERROR;
        if (n >= 0) {
142
                res = index->int_entries[n].valid(index, n, value);
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
143
144
                if (res == ELPA_OK) {
                        index->int_values[n] = value;
145
                        index->int_value_is_set[n] = 1;
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
146
147
148
149
                }
        }
        return res;
}
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

int elpa_index_int_value_is_set(elpa_index_t index, const char *name) {
        int n = find_int_entry(index, name);
        if (n >= 0) {
                if (index->int_value_is_set[n]) {
                        return ELPA_OK;
                } else {
                        return ELPA_NO;
                }
        } else {
                return ELPA_ERROR;
        }
}


static const char *invalid_name = "(INVALID NAME)";
static const char *invalid_value = "(INVALID VALUE)";

const char* elpa_index_get_int_repr(elpa_index_t index, const char *name) {
        int n = find_int_entry(index, name);
        if (n >= 0) {
                if (index->int_entries[n].fixed_range_reprs != NULL) {
                        return index->int_entries[n].repr(index, n);
                } else {
                        return NULL;
                }
        } else {
                return invalid_name;
        }
}

/* Helper functions for simple int entries */

static int is_in_fixed_range(elpa_index_t index, int n, int new_value) {
        if (new_value >= 0 && new_value < index->int_entries[n].cardinality(index, n)) {
                return ELPA_OK;
        } else {
                return ELPA_ERROR;
        }
}

static int enumerate_fixed_range(elpa_index_t index, int n, unsigned int i) {
        return i;
}

static int is_in_fixed_range_plus_one(elpa_index_t index, int n, int new_value) {
        if (new_value >= 1 && new_value <= index->int_entries[n].cardinality(index, n)) {
                return ELPA_OK;
        } else {
                return ELPA_ERROR;
        }
}

static int enumerate_fixed_range_plus_one(elpa_index_t index, int n, unsigned int i) {
        return i + 1;
}

static int cardinality_fixed_range(elpa_index_t index, int n) {
        return index->int_entries[n].fixed_range;
}


static const char* repr_fixed_range(elpa_index_t index, int n) {
        int value = index->int_values[n];
        return index->int_entries[n].fixed_range_reprs[value];
}


/* The single instance */

#define BOOL_ENTRY(option_name, default) \
        (elpa_index_int_entry_t) { \
                .name = option_name, \
                .default_value = default, \
                .cardinality = cardinality_fixed_range, \
                .enumerate_option = enumerate_fixed_range, \
                .valid = is_in_fixed_range, \
                .fixed_range = 2, \
        }

#define INT_LIST_ENTRY(option_name, default, range, reprs) \
        (elpa_index_int_entry_t) { \
                .name = option_name, \
                .default_value = default, \
                .cardinality = cardinality_fixed_range, \
                .enumerate_option = enumerate_fixed_range, \
                .valid = is_in_fixed_range, \
                .repr = repr_fixed_range, \
                .fixed_range = range, \
                .fixed_range_reprs = reprs, \
        }

#define INT_ANY_ENTRY(option_name) \
        (elpa_index_int_entry_t) { \
                .name = option_name, \
        }

static elpa_index_int_entry_t elpa_index_int_entries[] = {
        BOOL_ENTRY("summary_timings", 0),
        BOOL_ENTRY("debug", 0),
        BOOL_ENTRY("qr", 0),
        BOOL_ENTRY("gpu", 0),
        INT_LIST_ENTRY("solver", ELPA_SOLVER_1STAGE, ELPA_NUMBER_OF_SOLVERS, ELPA_SOLVER_NAMES),
        INT_LIST_ENTRY("real_kernel", ELPA_2STAGE_REAL_DEFAULT, ELPA_2STAGE_NUMBER_OF_REAL_KERNELS, ELPA_2STAGE_REAL_KERNEL_NAMES),
        INT_LIST_ENTRY("complex_kernel", ELPA_2STAGE_COMPLEX_DEFAULT, ELPA_2STAGE_NUMBER_OF_COMPLEX_KERNELS, ELPA_2STAGE_COMPLEX_KERNEL_NAMES),
        INT_ANY_ENTRY("mpi_comm_rows"),
        INT_ANY_ENTRY("mpi_comm_cols"),
        INT_ANY_ENTRY("mpi_comm_parent"),
};

#define DOUBLE_ENTRY(option_name) \
        (elpa_index_double_entry_t) { \
                .name = option_name, \
        }

static elpa_index_double_entry_t elpa_index_double_entries[] = {
        DOUBLE_ENTRY("time_evp_fwd"),
        DOUBLE_ENTRY("time_evp_solve"),
        DOUBLE_ENTRY("time_evp_back"),
};

/*
 !f> interface
 !f>   function elpa_index_instance() result(index) bind(C, name="elpa_index_instance")
 !f>     import c_ptr
 !f>     type(c_ptr) :: index
 !f>   end function
 !f> end interface
 */
elpa_index_t elpa_index_instance() {
        elpa_index_t index = elpa_index_allocate(
                nelements(elpa_index_int_entries), elpa_index_int_entries,
                nelements(elpa_index_double_entries), elpa_index_double_entries
        );
}