SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / topology / symtab.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
7  * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 /*! \internal \file
39  * \brief
40  * Implements new and legacy symbol table routines.
41  *
42  * \author David van der Spoel <david.vanderspoel@icm.uu.se>
43  * \author Paul Bauer <paul.bauer.q@gmail.com>
44  * \ingroup module_topology
45  */
46 #include "gmxpre.h"
47
48 #include "symtab.h"
49
50 #include <cstdio>
51 #include <cstring>
52
53 #include <algorithm>
54
55 #include "gromacs/utility/basedefinitions.h"
56 #include "gromacs/utility/cstringutil.h"
57 #include "gromacs/utility/exceptions.h"
58 #include "gromacs/utility/fatalerror.h"
59 #include "gromacs/utility/iserializer.h"
60 #include "gromacs/utility/smalloc.h"
61 #include "gromacs/utility/stringutil.h"
62 #include "gromacs/utility/txtdump.h"
63
64 StringTableEntry StringTableBuilder::addString(const std::string& theString)
65 {
66     int         size     = map_.size();
67     std::string stripped = gmx::stripString(theString);
68
69     const auto foundEntry = map_.insert(StringTablePair(stripped, size));
70     return StringTableEntry(foundEntry.first->first, foundEntry.first->second);
71 }
72
73 int StringTableBuilder::findEntryByName(const std::string& name) const
74 {
75     auto foundEntry = map_.find(name);
76     if (foundEntry != map_.end())
77     {
78         return foundEntry->second;
79     }
80     else
81     {
82         GMX_THROW(gmx::InternalError(
83                 gmx::formatString("Could not find string \"%s\" in SymbolTable", name.c_str())));
84     }
85 }
86
87 StringTable StringTableBuilder::build()
88 {
89     std::vector<std::string> table(map_.size());
90     for (const auto& entry : map_)
91     {
92         table[entry.second] = entry.first;
93     }
94     map_.clear();
95     return StringTable(table);
96 }
97
98 void StringTable::printStringTableStorageToFile(FILE* fp, int indent, const char* title) const
99 {
100     indent = pr_title_n(fp, indent, title, table_.size());
101     int i  = 0;
102     for (const auto& entry : table_)
103     {
104         pr_indent(fp, indent);
105         fprintf(fp, "%s[%d]=\"%s\"\n", title, i++, entry.c_str());
106     }
107 }
108
109 StringTable::StringTable(gmx::ISerializer* serializer)
110 {
111     GMX_RELEASE_ASSERT(serializer->reading(), "Can not use writing serializer to read string table");
112     int nr = 0;
113     serializer->doInt(&nr);
114     table_.resize(nr);
115     for (auto& entry : table_)
116     {
117         serializer->doString(&entry);
118     }
119 }
120
121 void StringTable::serializeStringTable(gmx::ISerializer* serializer)
122 {
123     GMX_RELEASE_ASSERT(!serializer->reading(),
124                        "Can not use reading serializer to write string table");
125     int nr = table_.size();
126     serializer->doInt(&nr);
127     for (auto& entry : table_)
128     {
129         serializer->doString(&entry);
130     }
131 }
132
133 StringTableEntry StringTable::at(gmx::index index) const
134 {
135     if (index >= gmx::ssize(table_))
136     {
137         GMX_THROW(gmx::InternalError("Can't read beyond last entry"));
138     }
139     return StringTableEntry(table_[index], index);
140 }
141
142 StringTableEntry StringTable::operator[](gmx::index index) const
143 {
144     GMX_ASSERT(index < gmx::ssize(table_), "Can't read beyond last entry");
145     return StringTableEntry(table_[index], index);
146 }
147
148 void StringTableEntry::serialize(gmx::ISerializer* serializer) const
149 {
150     GMX_RELEASE_ASSERT(!serializer->reading(),
151                        "Can not use reading serializer to write string index");
152     int entry = tableIndex_;
153     serializer->doInt(&entry);
154 }
155
156 StringTableEntry readStringTableEntry(gmx::ISerializer* serializer, const StringTable& table)
157 {
158     GMX_RELEASE_ASSERT(serializer->reading(), "Can not use writing serializer to read string index");
159     int entry = 0;
160     serializer->doInt(&entry);
161     return table.at(entry);
162 }
163
164 void StringTable::copyToLegacySymtab(struct t_symtab* symtab) const
165 {
166     for (const auto& entry : table_)
167     {
168         put_symtab(symtab, entry.c_str());
169     }
170 }
171
172 // Old code for legacy data structure starts below.
173 //! Maximum size of character string in table.
174 constexpr int c_trimSize = 1024;
175 //! Maximum number of entries in each element of the linked list.
176 constexpr int c_maxBufSize = 5;
177
178 /*! \brief
179  * Remove leading and trailing whitespace from string and enforce maximum length.
180  *
181  * \param[in]    s      String to trim.
182  * \param[inout] out    String to return.
183  * \param[in]    maxlen Maximum string length to use.
184  * \returns New pruned string.
185  */
186 static char* trim_string(const char* s, char* out, int maxlen)
187 {
188     int len = 0, i = 0;
189
190     if (strlen(s) > static_cast<size_t>(maxlen - 1))
191     {
192         gmx_fatal(FARGS, "String '%s' (%zu) is longer than buffer (%d).\n", s, strlen(s), maxlen - 1);
193     }
194
195     for (; (*s) == ' '; s++) {}
196     for (len = strlen(s); (len > 0); len--)
197     {
198         if (s[len - 1] != ' ')
199         {
200             break;
201         }
202     }
203     if (len >= c_trimSize)
204     {
205         len = c_trimSize - 1;
206     }
207     for (i = 0; i < len; i++)
208     {
209         out[i] = *(s++);
210     }
211     out[i] = 0;
212     return out;
213 }
214
215 int lookup_symtab(t_symtab* symtab, char** name)
216 {
217     int       base   = 0;
218     t_symbuf* symbuf = symtab->symbuf;
219     while (symbuf != nullptr)
220     {
221         const int index = name - symbuf->buf;
222         if ((index >= 0) && (index < symbuf->bufsize))
223         {
224             return index + base;
225         }
226         else
227         {
228             base += symbuf->bufsize;
229             symbuf = symbuf->next;
230         }
231     }
232     gmx_fatal(FARGS, "symtab lookup \"%s\" not found", *name);
233 }
234
235 char** get_symtab_handle(t_symtab* symtab, int name)
236 {
237     t_symbuf* symbuf = symtab->symbuf;
238     while (symbuf != nullptr)
239     {
240         if (name < symbuf->bufsize)
241         {
242             return &(symbuf->buf[name]);
243         }
244         else
245         {
246             name -= symbuf->bufsize;
247             symbuf = symbuf->next;
248         }
249     }
250     gmx_fatal(FARGS, "symtab get_symtab_handle %d not found", name);
251 }
252
253 //! Returns a new initialized entry into the symtab linked list.
254 static t_symbuf* new_symbuf()
255 {
256     t_symbuf* symbuf = nullptr;
257
258     snew(symbuf, 1);
259     symbuf->bufsize = c_maxBufSize;
260     snew(symbuf->buf, symbuf->bufsize);
261     symbuf->next = nullptr;
262
263     return symbuf;
264 }
265
266 /*! \brief
267  * Low level function to enter new string into legacy symtab.
268  *
269  * \param[inout] symtab Symbol table to add entry to.
270  * \param[in]    name   New string to add to symtab.
271  * \returns Pointer to new entry in the legacy symbol table, or to existing entry if it already existed.
272  */
273 static char** enter_buf(t_symtab* symtab, char* name)
274 {
275     bool bCont = false;
276
277     if (symtab->symbuf == nullptr)
278     {
279         symtab->symbuf = new_symbuf();
280     }
281
282     t_symbuf* symbuf = symtab->symbuf;
283     do
284     {
285         for (int i = 0; (i < symbuf->bufsize); i++)
286         {
287             if (symbuf->buf[i] == nullptr)
288             {
289                 symtab->nr++;
290                 symbuf->buf[i] = gmx_strdup(name);
291                 return &(symbuf->buf[i]);
292             }
293             else if (strcmp(symbuf->buf[i], name) == 0)
294             {
295                 return &(symbuf->buf[i]);
296             }
297         }
298         if (symbuf->next != nullptr)
299         {
300             symbuf = symbuf->next;
301             bCont  = TRUE;
302         }
303         else
304         {
305             bCont = FALSE;
306         }
307     } while (bCont);
308
309     symbuf->next = new_symbuf();
310     symbuf       = symbuf->next;
311
312     symtab->nr++;
313     symbuf->buf[0] = gmx_strdup(name);
314     return &(symbuf->buf[0]);
315 }
316
317 char** put_symtab(t_symtab* symtab, const char* name)
318 {
319     char buf[1024];
320
321     return enter_buf(symtab, trim_string(name, buf, 1023));
322 }
323
324 void open_symtab(t_symtab* symtab)
325 {
326     symtab->nr     = 0;
327     symtab->symbuf = nullptr;
328 }
329
330 void close_symtab(t_symtab gmx_unused* symtab) {}
331
332 // TODO this will go away when we use a
333 // std::list<std::vector<std::string>>> for t_symtab.
334 t_symtab* duplicateSymtab(const t_symtab* symtab)
335 {
336     t_symtab* copySymtab = nullptr;
337     snew(copySymtab, 1);
338     open_symtab(copySymtab);
339     t_symbuf* symbuf = symtab->symbuf;
340     if (symbuf != nullptr)
341     {
342         snew(copySymtab->symbuf, 1);
343     }
344     t_symbuf* copySymbuf = copySymtab->symbuf;
345     while (symbuf != nullptr)
346     {
347         snew(copySymbuf->buf, symbuf->bufsize);
348         copySymbuf->bufsize = symbuf->bufsize;
349         for (int i = 0; (i < symbuf->bufsize) && (i < symtab->nr); i++)
350         {
351             if (symbuf->buf[i])
352             {
353                 copySymbuf->buf[i] = gmx_strdup(symbuf->buf[i]);
354             }
355         }
356         symbuf = symbuf->next;
357         if (symbuf != nullptr)
358         {
359             snew(copySymbuf->next, 1);
360             copySymbuf = copySymbuf->next;
361         }
362     }
363     copySymtab->nr = symtab->nr;
364     return copySymtab;
365 }
366
367 void done_symtab(t_symtab* symtab)
368 {
369     close_symtab(symtab);
370     t_symbuf* symbuf = symtab->symbuf;
371     while (symbuf != nullptr)
372     {
373         int i = 0;
374         for (; (i < symbuf->bufsize) && (i < symtab->nr); i++)
375         {
376             sfree(symbuf->buf[i]);
377         }
378         symtab->nr -= i;
379         sfree(symbuf->buf);
380         t_symbuf* freeptr = symbuf;
381         symbuf            = symbuf->next;
382         sfree(freeptr);
383     }
384     symtab->symbuf = nullptr;
385     if (symtab->nr != 0)
386     {
387         gmx_incons("Freeing symbol table (symtab) structure");
388     }
389 }
390
391 void free_symtab(t_symtab* symtab)
392 {
393     close_symtab(symtab);
394     t_symbuf* symbuf = symtab->symbuf;
395     while (symbuf != nullptr)
396     {
397         symtab->nr -= std::min(symbuf->bufsize, symtab->nr);
398         t_symbuf* freeptr = symbuf;
399         symbuf            = symbuf->next;
400         sfree(freeptr);
401     }
402     symtab->symbuf = nullptr;
403     if (symtab->nr != 0)
404     {
405         gmx_incons("Freeing symbol table (symtab) structure");
406     }
407 }
408
409 void pr_symtab(FILE* fp, int indent, const char* title, t_symtab* symtab)
410 {
411     if (available(fp, symtab, indent, title))
412     {
413         indent           = pr_title_n(fp, indent, title, symtab->nr);
414         int       i      = 0;
415         int       nr     = symtab->nr;
416         t_symbuf* symbuf = symtab->symbuf;
417         while (symbuf != nullptr)
418         {
419             int j = 0;
420             for (; (j < symbuf->bufsize) && (j < nr); j++)
421             {
422                 pr_indent(fp, indent);
423                 (void)fprintf(fp, "%s[%d]=\"%s\"\n", title, i++, symbuf->buf[j]);
424             }
425             nr -= j;
426             symbuf = symbuf->next;
427         }
428         if (nr != 0)
429         {
430             gmx_incons("Printing symbol table (symtab) structure");
431         }
432     }
433 }